UUID v4 vs v7 — Which Should You Use?
v4 and v7 are the two UUIDs you will reach for most, but they solve different problems. v4 is 122 bits of cryptographic randomness: opaque, unpredictable, and private. v7 puts a 48-bit Unix millisecond timestamp first, then fills the rest with randomness, so IDs sort chronologically. Random where ordering must stay hidden, time-ordered where insertion order and locality matter. Both generate locally in your browser.
Use v4 when
- You need an opaque identifier where creation time must not leak: tokens, session IDs, and public-facing keys.
- Every caller can generate independently and you do not need sortable order in the database.
- You want the widest tool support available everywhere with zero debate.
- Privacy over locality: the bits reveal nothing about when the ID was made.
Use v7 when
- The ID is a primary key and you want sequential inserts: B-tree indexes stay dense and recent rows stay adjacent.
- You want natural chronological order by sorting the string itself, with no separate created_at column.
- You generate IDs at high volume and monotonic ordering helps downstream sorting, partitioning, or pagination.
- You are starting a new system on RFC 9562 and want the modern time-ordered recommendation.
Side by side
| UUID v4 | UUID v7 | |
|---|---|---|
| Core property | 122 bits random | 48 bits time + 74 bits random |
| String sort | Random order | Chronological order |
| Time visible? | No | Yes, first 12 hex chars |
| DB insert pattern | Random throughout index | Sequential at the end |
| Spec | RFC 9562 v4 | RFC 9562 v7 (2024) |
| Collision risk | Negligible, pure random | Negligible, time plus random |
| Leaks MAC? | No | No |
| Where it runs | Browser, no upload | Browser, no upload |
Neither tool uploads what you generate. v4 uses crypto.getRandomValues, v7 uses the same RNG plus Date.now() for the time prefix. Both produce RFC 9562 compliant IDs entirely in the browser.
Pick v7 for database keys and any ID where order matters. Its timestamp prefix makes inserts sequential and keeps sorting free. Stick to v4 for opaque public IDs where you do not want the creation time visible. They coexist fine in the same system: v7 inside, v4 at the boundary. Use What Is UUID v7 to see the layout and UUID Format and Examples to identify each version at a glance.