UUIDs (Universally Unique Identifiers) are 128-bit numbers used to uniquely identify information in computer systems. They're essential in distributed systems, microservices, and modern databases where generating unique IDs without central coordination is critical.
A UUID is a 128-bit (16-byte) number, typically displayed as 36 characters in this format:
550e8400-e29b-41d4-a716-446655440000
The format is: 8 characters - 4 - 4 - 4 - 12 characters, all hexadecimal (0-9, a-f).
Generate IDs anywhere without checking a central database. Perfect for distributed systems where multiple servers create records simultaneously.
The probability of generating duplicate UUIDs is astronomically low. With 2^122 possible UUIDs (version 4), you can generate a billion UUIDs per second for 100 years before having a 50% chance of collision.
Unlike auto-incrementing integers, UUIDs don't reveal information about record count or creation order. This improves security by making IDs unpredictable.
UUIDs work across any database system. Move data between PostgreSQL, MySQL, MongoDB, or DynamoDB without ID conflicts.
Generated from current timestamp and machine MAC address.
Generated using random or pseudo-random numbers.
// JavaScript
const uuid = crypto.randomUUID();
// Output: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d
// Python
import uuid
uuid_str = str(uuid.uuid4())
// Java
import java.util.UUID;
UUID uuid = UUID.randomUUID();
🆔 Generate UUIDs Instantly →
Generated from namespace and name using SHA-1 hash. Same inputs always produce same UUID.
UUIDs are 128 bits (16 bytes) vs 32-64 bits for integers:
INTEGER : 4 bytes
BIGINT : 8 bytes
UUID : 16 bytes (4x larger than INTEGER)
Impact on 1M records:
- INTEGER: 4 MB
- UUID: 16 MB
Random UUIDs cause B-tree fragmentation in databases, slowing inserts. Solutions:
-- Native UUID type
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255)
);
-- Insert
INSERT INTO users (username) VALUES ('john');
-- Query
SELECT * FROM users WHERE id = '550e8400-e29b-41d4-a716-446655440000';
-- Binary storage for efficiency
CREATE TABLE users (
id BINARY(16) PRIMARY KEY,
username VARCHAR(255)
);
-- Insert (requires function to convert)
INSERT INTO users (id, username)
VALUES (UUID_TO_BIN(UUID()), 'john');
-- Query
SELECT BIN_TO_UUID(id), username FROM users;
// MongoDB uses ObjectId by default (12 bytes)
// But you can use UUIDs
const { v4: uuidv4 } = require('uuid');
db.users.insertOne({
_id: uuidv4(),
username: 'john'
});
Don't implement UUID generation yourself. Use battle-tested libraries:
crypto.randomUUID() or uuid npm packageuuid standard libraryjava.util.UUIDgithub.com/google/uuidVARCHAR(36) wastes space. Binary(16) saves 44% storage and improves performance.
CREATE INDEX idx_user_id ON orders(user_id);
Version 4 is simple, secure, and widely supported. Use v1 only if you need time-based sorting, v5 for deterministic generation.
Always validate UUID format in API inputs:
// JavaScript validation
function isValidUUID(str) {
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return regex.test(str);
}
36 characters is too long for users to type. Use short codes for customer-facing IDs.
Random UUIDs fragment indexes. Monitor database performance and consider UUIDv7 or ULID.
Store and compare as binary for better performance.
Need UUIDs right now? Our UUID Generator creates RFC-compliant version 4 UUIDs instantly. Generate one or thousands with a single click.
UUIDs are essential for modern distributed systems. They eliminate coordination overhead, prevent ID collisions, and work seamlessly across databases and services. While they have trade-offs in storage size and performance, the benefits usually outweigh the costs in scalable applications.
Choose UUIDv4 for most use cases, store them efficiently, and enjoy the freedom of decentralized ID generation.
← Back to Blog