What is a UUID?
A UUID (Universally Unique Identifier), also known as GUID (Globally Unique Identifier) in Microsoft systems, is a 128-bit identifier designed to be unique across all computers and time. UUIDs are represented as 32 hexadecimal digits, displayed in five groups separated by hyphens: 8-4-4-4-12.
Example: 550e8400-e29b-41d4-a716-446655440000
UUID Versions Explained
UUID v1 - Time-based
Generated from a timestamp and MAC address. Useful when you need time-ordered identifiers, but reveals information about when and where the UUID was created.
- Time-ordered (can be sorted chronologically)
- Contains timestamp information
- Not ideal for security-sensitive applications
UUID v4 - Random (Most Common)
Generated entirely from random numbers (122 bits of randomness). This is the most commonly used version because it's simple, secure, and doesn't reveal any information.
- Completely random
- No timestamp or location information
- Used by this generator
UUID v5 - Name-based (SHA-1)
Generated by hashing a namespace identifier and a name. The same namespace + name always produces the same UUID, making it useful for deterministic UUID generation.
UUID v7 - Time-ordered Random (Newest)
A newer standard that combines timestamp ordering with random data. Better for database performance than v4 while still being secure. Gaining adoption in modern systems.
UUID Structure
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
│ │ │ │ │
│ │ │ │ └─ 12 random hex digits (48 bits)
│ │ │ └────── N = variant (8, 9, a, or b)
│ │ └─────────── M = version (1-5, 7)
│ └──────────────── 4 random hex digits
└───────────────────────── 8 random hex digits
Total: 32 hex digits + 4 hyphens = 36 charactersCommon Use Cases
Database Primary Keys
UUIDs are often used as primary keys in databases, especially in distributed systems where multiple nodes need to generate IDs without coordination.
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);API Resource Identifiers
Using UUIDs for API resources makes URLs unpredictable, preventing enumeration attacks:
// Predictable (bad for security)
GET /api/users/123
// Unpredictable (better)
GET /api/users/550e8400-e29b-41d4-a716-446655440000File and Object Naming
UUIDs are useful for naming uploaded files or objects to avoid conflicts:
// Instead of: photo.jpg (could conflict)
// Use: 550e8400-e29b-41d4-a716-446655440000.jpgUUID vs Auto-Increment
UUID Advantages
- Can be generated anywhere without coordination
- Unpredictable (better security)
- Works across distributed systems
- No central authority needed
UUID Disadvantages
- Larger storage (16 bytes vs 4-8 bytes)
- Slower to index than integers
- Harder to read and debug
- Not human-friendly for support tickets
UUID in Different Languages
// JavaScript
crypto.randomUUID();
// Python
import uuid
uuid.uuid4()
// Java
UUID.randomUUID()
// C# (.NET)
Guid.NewGuid()
// PHP
Str::uuid() // Laravel
bin2hex(random_bytes(16)) // ManualCollision Probability
UUID v4 has 122 random bits, giving about 5.3 × 10^36 possible values. The probability of generating two identical UUIDs is astronomically low:
- After 1 billion UUIDs: 0.00000000000000000000002% chance of collision
- To have 50% collision chance: need ~2.71 quintillion UUIDs
- For practical purposes, UUIDs are unique