UUID v4 vs v6: What's the Difference?
Understand the differences between UUID version 4 and version 6. Learn when to use each version for your application.
Introduction
UUIDs (Universally Unique Identifiers) are 128-bit numbers used to uniquely identify information in distributed systems. UUID v4 and v6 are two popular versions with different characteristics and use cases.
This guide explains the key differences to help you choose the right UUID version.
UUID v4 (Random)
UUID v4 generates completely random identifiers:
550e8400-e29b-41d4-a716-446655440000
f47ac10b-58cc-4372-a567-0e02b2c3d479
3d6f4e2a-9b7c-4a1f-8e5d-2c3b4a5f6e7d
- Generation: Uses cryptographically secure random numbers
- Pros: Simple, no coordination needed, highly secure
- Cons: Poor database performance, not sortable
- Uniqueness: Practically guaranteed (collision chance: 1 in 2^122)
UUID v6 (Time-Ordered)
UUID v6 includes timestamp for better sorting and database performance:
1ef3c8f0-8b2a-6000-8000-00005ab8e4c0
1ef3c8f1-2d4e-6000-8000-00005ab8e4c1
1ef3c8f2-7f9a-6000-8000-00005ab8e4c2
- Generation: Timestamp + random bits
- Pros: Sortable, better DB performance, maintains chronological order
- Cons: Slightly more complex generation
- Structure: Time-high + time-mid + time-low + clock-seq + node
Performance Comparison
| Metric | UUID v4 | UUID v6 |
|---|---|---|
| Database Index Performance | Poor (random insertions) | Excellent (sequential) |
| Sortability | Not sortable | Chronologically sortable |
| Generation Speed | Very fast | Fast |
| Collision Risk | Negligible | Negligible |
| Storage Size | 128 bits | 128 bits |
When to Use UUID v4
- Maximum privacy: No time information revealed
- Simple requirements: Don't need sorting
- Distributed systems: Multiple servers generating IDs
- Security tokens: Session IDs, API keys
- Small scale: Database performance not critical
When to Use UUID v6
- Database primary keys: Better index performance
- High-volume inserts: Millions of records
- Time-series data: Events, logs, transactions
- Need sorting: Display by creation time
- Distributed databases: CockroachDB, YugabyteDB
Database Impact
UUID v4 causes random B-tree insertions, leading to:
- Frequent page splits
- Poor cache locality
- Slower writes (2-3x slower than v6)
- Index fragmentation
UUID v6 provides sequential insertions:
- Minimal page splits
- Better cache usage
- Faster writes
- Compact indexes
Code Examples
// UUID v4 (JavaScript)
const uuid = crypto.randomUUID();
// Output: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
// UUID v6 (using library)
import { v6 as uuidv6 } from 'uuid';
const uuid = uuidv6();
// Output: '1ef3c8f0-8b2a-6000-8000-00005ab8e4c0'
// PostgreSQL
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMP DEFAULT NOW()
);
Try Our Tools
Explore our free online developer tools: