UUID v4 vs v7 vs v1: Which Version to Use
When to use UUID v4, v7, or v1, and why v7 is the right default for new database tables. Bit layouts, practical recipes, and common pitfalls.
UUIDs are everywhere - request ids, primary keys, distributed traces, idempotency tokens, file names. Most people learn v4 once, paste crypto.randomUUID() into every codebase, and never think about it again. That worked fine until v7 was standardised, and now there is a genuine reason to look at the alphabet you have been using. This guide walks through what each version actually contains, where each one shines, and the small mistakes that quietly turn a "unique" id into a duplicate.
What is a UUID and Why Use One
A UUID (or GUID, same thing) is a 128-bit identifier formatted as xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Two big properties make it useful: it can be generated independently in many places without a coordination server, and the chance of collision is astronomically low when generated correctly. That combination is why your database, your message queue, your CDN, and your favourite feature flag system all use them.
The trade-off is that 36 ASCII characters per id is more than a 64-bit integer would be, and the bytes are not friendly for humans or for indexes that prefer sequential keys. Most of the version trade-offs come back to that second point.
v4 vs v7 vs v1
All three versions share the 128-bit length and the version + variant nibbles in fixed positions. What changes is what fills the rest of the bits.
| Version | Time bits | Random bits | Other | Sortable? |
|---|---|---|---|---|
| v4 | 0 | 122 | - | No |
| v7 | 48 (Unix ms) | 74 | 6 (version + variant) | Yes (by time) |
| v1 | 60 (100ns since 1582) | up to 62 (with random node) | 14 (clock seq) + 48 (node) | Yes, but byte-shuffled |
v4 is "all random." That makes it bias-free, simple to reason about, and the most common default on the web. The downside is that consecutive v4 ids are scattered across the keyspace, which is bad for B-tree indexes that prefer locality.
v7 fixes exactly that. The first 48 bits are the Unix millisecond timestamp, big-endian, which means lexicographic ordering matches chronological ordering for free. The remaining 74 bits are random, which is enough entropy that two ids generated in the same millisecond on the same machine are still essentially impossible to collide. This is the version you want for new database tables in 2026.
v1 was the original time-ordered scheme, but the time field is split awkwardly across time-low / time-mid / time-hi, so byte-wise sorting does not give chronological order. v1 also famously embedded the device MAC address, which is a privacy footgun. Modern v1 generators (ours included) replace the node id with a random multicast value, which keeps the format valid without leaking hardware.
In Practice
Postgres primary keys: pick v7. A monotonically increasing key keeps the index hot at the rightmost leaf, which means inserts touch a small set of pages and the buffer cache stays warm. On large tables this is the difference between a fast OLTP workload and a slow one. Postgres added native UUIDv7 functions in recent versions, but generating them client-side is just as good as long as you keep clocks roughly in sync.
Event streams and idempotency keys: v4 is fine. When a request id only needs to be unique and unguessable, v4's 122 bits of pure entropy is the simplest and safest choice. There is no benefit to time-ordering for a key that gets looked up by hash anyway.
Migrating from v1: keep the column, generate v7 going forward. UUID columns hold any version, so there is no schema change. New rows pick up the time-ordered benefit, and old rows continue to work. If you index for time-range queries, consider adding an explicit created_at column - it is cheaper than parsing UUID bytes back into a timestamp.
File names and object storage keys: v7. Sortable UUIDs are great prefixes for S3-style buckets where listings are ordered lexicographically. You can list "everything from this hour" by the first few hex characters.
Public URLs: v4. Time-ordered ids leak creation time, which is sometimes a privacy issue (e.g. if "user 17" was created before "user 18"). Use v4 for ids that travel out of your system.
Common Pitfalls
Using Math.random() to roll your own
Math.random() is fast but not cryptographic. Two browsers seeded from a similar state can produce overlapping output, and the per-call entropy is far below 122 bits. Always use crypto.randomUUID() or crypto.getRandomValues() for the random portion. Our generator uses both, with manual byte-twiddling only where the spec requires it.
Treating v7 as collision-proof at extreme rates
v7 has 74 random bits per millisecond per process. That means roughly 2^37 (~137 billion) ids per millisecond before the birthday bound bites. You will not hit that on any normal workload, but if you are sharding a single timestamp across many threads, give each thread its own generator state rather than racing on a global one. The spec also defines a "monotonic random" submode that increments within the same millisecond - a nice extra step if your application generates many ids per millisecond on a single host.
Using a UUID as a secret token
UUIDs are unique, not secret. v1 and v7 are partly predictable (the timestamp is right there), and even v4 gives only 122 bits versus the 256 you would expect from a session token. If you need a bearer credential, generate crypto.getRandomValues(new Uint8Array(32)) and base64-url encode it. Keep UUIDs for "name a thing", not "prove who you are."
Leaking the MAC address with old v1 libraries
The original v1 spec used the network card's MAC as the node id. That tied every id back to a specific device, which is exactly the kind of fingerprint privacy regulations now hate. Our v1 generator (and any modern library) sets the multicast bit and uses random bytes instead - the format stays valid, but no real hardware identifier ever appears.
Dashes vs no dashes
Some systems strip the dashes ("compact" form, 32 hex chars) to save bytes. That is fine if it stays consistent. The risk is mixing the two: a key inserted as c0a8... and looked up as c0a8-... will not match. Pick one canonical form per system, store it that way, and only format on display.
FAQ
Is crypto.randomUUID() cryptographically secure? Yes. It is required to use a CSPRNG and to set the version + variant bits correctly. Our v4 path delegates to it whenever the runtime supports it, falling back to manual generation only on older browsers.
Can I sort v7 ids as strings? Yes. Lexicographic order on the 36-character form matches chronological order, because the timestamp is at the front and is big-endian.
Why are some bits "wasted" on the version and variant? They are markers that let any consumer recognise the format and check it is not a v1 leaking a MAC, or a malformed string. Six lost bits out of 128 is a good trade for self-describing identifiers.
What about v6 and v8? v6 is a re-shuffled v1 (timestamp first, sortable) - useful for retrofits but largely superseded by v7. v8 is "vendor-defined," meaning the application gets to choose the layout. Stick with v4 and v7 unless you have a specific reason.
Open the UUID generator to try v4, v7, and v1 side by side, batch-generate up to 100, or download as .txt. If you need other random tools, our password generator covers strong secrets and the draw lots tool handles fair selection.