UUID v4 vs v7 — Which UUID Version Should You Use? | LangStop Blog
UUID v4 vs v7 — Which UUID Version Should You Use?
Universally Unique Identifiers (UUIDs) are everywhere in modern software: database primary keys, distributed system identifiers, session tokens, and API resource identifiers. But not all UUIDs are created equal.
With the introduction of UUID v7 (time-ordered UUIDs), developers now have a compelling alternative to the ubiquitous UUID v4 (random UUIDs). This guide compares both versions to help you decide which one belongs in your next project.
Tip: Generate UUID v4 or v7 instantly using the UUID Generator. Fully client-side—no server requests, complete privacy.
crypto.randomUUID({ version: 7 }) gives you a v4
On Node 22.22.0, passing a version option to crypto.randomUUID() is ignored with no error, and you get a v4:
const id = crypto.randomUUID({ version: 7 });
console.log(id[14]); // prints "4"Character 14 of the canonical text is the version digit, so 4 means a v4 UUID. To get a real v7, use v7() from the uuid npm package:
import { v7 } from "uuid";
const id = v7();
console.log(id[14]); // prints "7"Check the version digit whenever you rely on a specific UUID version.
Security note: RFC 9562 says implementations SHOULD NOT assume UUIDs are hard to guess and that they MUST NOT be used as security capabilities. Session tokens, reset tokens and API keys should use dedicated random tokens from a CSPRNG, whichever UUID version you use for primary keys.
What Are UUIDs?
A UUID is a 128-bit identifier standardized by RFC 9562, which obsoletes RFC 4122. The canonical textual representation looks like:
550e8400-e29b-41d4-a716-446655440000
UUIDs are designed to be unique across space and time without requiring a central authority. There are several versions, each using a different strategy to generate the 128 bits.
UUID v4: Random UUIDs
UUID v4 generates identifiers using random or pseudo-random numbers. Of the 128 bits, 122 are randomly generated (6 bits are reserved for version and variant markers).
How UUID v4 Works
Version 4 UUID Structure:
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
Where:
- x = random hex digit (122 random bits overall)
- 4 = version indicator (4 bits)
- y = variant (2 bits, one of 8, 9, a, or b)
Example UUID v4
f47ac10b-58cc-4372-a567-0e02b2c3d479
Pros of UUID v4
- Maximum randomness — 122 random bits means ~5.3 × 10³⁶ possible values
- No coordination needed — Any system can generate IDs independently
- Simple implementation — Just need a good source of random numbers
Cons of UUID v4
- Random B-tree index fragmentation — Random inserts cause page splits in database indexes
- Poor data locality — Related rows aren't stored together
- No inherent sorting — Can't sort by creation time
- Cache miss rate — Higher in clustered indexes
UUID v7: Time-Ordered UUIDs
UUID v7 (defined in RFC 9562) generates identifiers with a time-ordered prefix, followed by random bits. This makes them sortable by creation time while maintaining uniqueness.
How UUID v7 Works
Version 7 UUID Structure:
tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx
Where:
- t = Unix timestamp in milliseconds (48 bits)
- 7 = version indicator (4 bits)
- x = random or counter bits
- y = variant (2 bits)
- 74 random or counter bits in total
Example UUID v7
018f3b6a-7e4c-7a3c-b123-2c3d479e0a1b
Pros of UUID v7
- Roughly time-ordered — Generators aim for new UUIDs to sort after older ones, but ordering across machines depends on their clocks
- B-tree friendly — Sequential inserts reduce page splits
- Sortable by time — Can order by UUID alone
- Good data locality — Related time-range data stored together
- Still unique — 74 random or counter bits provide collision resistance
- Timestamp embedded — The creation time is readable from the ID, although many teams keep a separate
created_atcolumn anyway
Cons of UUID v7
- Time dependency — Ordering is only as good as the generating clocks
- Exposes creation time — Anyone holding the ID can read when it was created
- Slightly less randomness — Fewer random bits than v4
- Newer standard — Not all libraries support it yet (but adoption is growing fast)
Database Index Performance: The Key Difference
The most important practical difference between UUID v4 and v7 is database index performance.
B-Tree Index Structure
Relational databases use B-tree indexes for primary keys. When inserting random values:
UUID v4 (Random):
- New IDs land anywhere in the tree
- Causes frequent page splits
- Index fragmentation increases over time
- Cache hit rate decreases
UUID v7 (Time-ordered):
- New IDs mostly land at the end of the index
- Page splits are rare
- Index stays compact and efficient
- Excellent cache locality
When to Use UUID v4
Non-Database Use Cases
- Client-generated correlation IDs
- Distributed tracing spans
- Message queue identifiers
- Temporary identifiers that are not security capabilities
When You Need No Coordination
- Offline systems generating IDs independently
- Event sourcing where any node produces IDs
When to Use UUID v7
Database Primary Keys
- PostgreSQL — Primary key with B-tree index
- MySQL / MariaDB — Clustered primary key
Time-Sorted Data
- Event logs — Where temporal ordering is useful
- Audit trails — Sort by ID to get chronological order
- Time-series data — Partitioned by time range
Large-Scale Systems
- High-write-volume tables — Reduce index contention
- Multi-region databases — IDs can be generated anywhere; cross-region ordering is approximate and depends on clock skew
- CQRS/Event Sourcing — Approximate event ordering
Ecosystem Support
| Database | UUID v4 | UUID v7 |
|---|---|---|
| PostgreSQL | ✅ Built-in (uuidv4(), gen_random_uuid()) |
✅ Built-in uuidv7() in PostgreSQL 18; earlier versions need an extension such as pg_uuidv7 |
| MySQL 8+ | UUID() produces version 1 |
No built-in v7; generate it in the application |
| SQLite | ✅ | ✅ With extension |
| CockroachDB | ✅ gen_random_uuid() is v4 |
Generate v7 in the application |
Programming Language Support
| Language | UUID v4 | UUID v7 |
|---|---|---|
| JavaScript | ✅ crypto.randomUUID() |
✅ v7() from the uuid npm package |
| Python | ✅ uuid.uuid4() |
✅ uuid.uuid7() (Python 3.14+) |
| Go | ✅ google/uuid |
✅ uuid.NewV7() from google/uuid |
| Rust | ✅ uuid::Uuid::new_v4() |
✅ Uuid::now_v7() |
| Java | ✅ UUID.randomUUID() |
No JDK v7; use a library |
Recommendation
| Scenario | Recommended UUID Version |
|---|---|
| Database primary key (high write volume) | v7 |
| Database primary key (low write volume) | Either |
| Security token / API key | Neither: use a dedicated random token from a CSPRNG |
| Distributed event sourcing | v7 |
| Client-generated identifiers | v4 (simpler, well-supported) |
| Time-sorted data | v7 |
| Session identifier | Neither: use a dedicated random token from a CSPRNG |
| Cross-system reference ID | Either (agree on version) |
The trend: For database primary keys, UUID v7 is rapidly becoming the default choice. PostgreSQL 18's built-in uuidv7() (or the pg_uuidv7 extension on earlier versions) and library support in most languages make it practical today. MySQL has no built-in v7, so generate it in the application.
Generating UUIDs Online
Use LangStop's free UUID generators — both run 100% client-side with no server uploads:
- UUID v4 Generator — Generate random UUIDs
- UUID v7 Generator — Generate time-ordered UUIDs
Need UUIDs?
Generate any UUID version with LangStop's free tool:
Client-side generation. No tracking. No limits.