Most developers pick v4 by default because it's what they've always seen. It's random and fine — until you need sortable IDs, or you're inserting millions of rows into an index.
UUID version 4 is the identifier equivalent of a default font. It's everywhere, it's fine, and almost nobody chose it on purpose — it's just what the tutorial used. For a lot of code, fine is genuinely enough. But "fine" hides real costs in three situations: when you need IDs that sort by creation time, when an ID leaks information about the machine that made it, and when you're jamming millions of them into a database index. The version you pick actually matters, and the May 2024 update to the spec gave us a better default worth knowing about.
The versions, cleanly
There are several, and you can hold them in your head with one line each:
- v1 — a timestamp plus a node ID, traditionally the machine's MAC address.
- v3 — an MD5 hash of a namespace and a name (deterministic).
- v4 — random. The default you've seen a thousand times.
- v5 — a SHA-1 hash of a namespace and a name (deterministic, the better-hashed sibling of v3).
- v6 — v1's fields reordered so the value sorts by time. Mostly for systems already built on v1.
- v7 — a Unix-epoch millisecond timestamp plus random bits. Time-sortable and modern.
- v8 — a free-form layout for experimental or vendor-specific schemes; only the version and variant bits are fixed.
(v2, the DCE security version, exists but you will almost never meet it.) These were formalised in RFC 9562, published in May 2024, which obsoletes the original RFC 4122 from 2005 and standardised the newer time-ordered versions:
"A UUID is 128 bits long and is intended to guarantee uniqueness across space and time."
— IETF RFC 9562, "Universally Unique IDentifiers (UUIDs)"
How unique is "unique"?
A v4 UUID has 122 random bits; the other six are fixed for the version and variant. Using the standard birthday approximation — probability ≈ n² ÷ (2 × 2¹²²) — the chance of any collision among a billion v4 IDs is around 9 × 10⁻²⁰. You'd need roughly 2.7 × 10¹⁸ of them before a collision became a coin flip. In practice, collisions come from broken random number generators or copied seed state, not from the maths, which is why the RFC's guidance on random sources matters more than the bit count.
Uniqueness is not secrecy, though. RFC 9562's security section says implementations should not assume UUIDs are hard to guess and that they "MUST NOT be used as security capabilities (identifiers whose mere possession grants access)". A v4 in a password-reset link is not a substitute for a proper single-use token with an expiry.
Why v4 hurts databases at scale
Here's the failure mode that surprises people. Database primary keys usually live in a B-tree index, which stays efficient when new keys arrive in roughly increasing order — each insert lands near the last one. A v4 UUID is fully random, so every insert lands at a random spot in the index. RFC 9562 names this directly: UUID versions that are not time-ordered, such as v4, "have poor database-index locality", so inserts happen "at random locations". On a small table nobody notices. On a table with tens of millions of rows, that randomness tends to mean more page splits, a less cache-friendly working set and fragmentation, and write throughput can suffer for it. Teams hit this, blame the ORM or the disk, and never suspect the ID format that was "just the default."
A simple demonstration of the root cause: generate 1,000 v4 values in a row and sort them. The sorted list does not match creation order — of course it doesn't, they're random — and that mismatch is exactly what the index has to absorb.
v7: the right default for new projects
UUID v7 fixes exactly that problem. Its first 48 bits are a big-endian Unix timestamp in milliseconds, and the remaining bits (after version and variant) are random or an optional counter. New IDs are therefore naturally time-ordered while still being practically impossible to collide. RFC 9562 itself says implementations "SHOULD utilize UUIDv7 instead of UUIDv1 and UUIDv6 if possible".
You can read the time straight out of one. In the example value 01a1c478-9800-7004-8203-040506070809 (built for this article with a fixed timestamp and filler bits), the first 12 hex digits, 01a1c4789800, are 1,794,297,600,000 milliseconds after 1 January 1970 — that is 10 November 2026, 08:00 UTC. The 48-bit field runs out somewhere around the year 10,890, so that isn't a planning concern.
Two caveats. First, the embedded timestamp is information: anyone holding a v7 ID knows roughly when the record was created. For most internal keys that's harmless; for IDs shown to customers it can reveal signup dates or order volumes. Second, millisecond precision means several IDs created in the same millisecond rely on the random bits (or a counter) for their order, so v7 gives you approximately sorted IDs across machines, not a strict global sequence.
v1 and the privacy problem
v1 combines a timestamp with a node field. RFC 9562 says that field "consists of an IEEE 802 MAC address, usually the host address or a randomly derived value". When it is the real MAC, every v1 UUID a machine produces is traceable back to that network card, which is why v1 fell out of favour for anything client-generated or externally visible. If you see v1 in a system that hands IDs to the outside world, check whether its library uses a random node; if not, treat it as a small information disclosure and move to v7. The UUID generator here produces v4 and v7, which is enough to compare the random layout with the timestamp-prefixed one side by side.
v3 and v5: when you want the same ID every time
Randomness isn't always what you want. v3 and v5 are deterministic: feed the same namespace and the same name, and you always get the same UUID back. Hashing this article's own URL in the standard URL namespace with v5 gives c63dd098-3082-59f7-bc65-8557358bb8a7 every single time, on any machine. That's perfect for stable IDs of canonical things — a URL, a product slug, a well-known entity — where two systems need to agree on an identifier without coordinating. Prefer v5 over v3, because v5 uses SHA-1 rather than the weaker MD5. Don't use either for secrets: if the name is guessable, so is the UUID.
Storage and format mistakes
- Storing UUIDs as 36-character text. The canonical string is 36 characters; the value itself is 16 bytes. Many databases have a native UUID type, and using it saves space in every index that contains the key.
- Comparing as strings with mixed case.
01A1…and01a1…are the same UUID; normalise to lowercase. - Parsing the version and trusting it. Any client can send any bits. If you need to know who created an ID, record that separately.
- Switching v4 to v7 mid-life without thinking about sort order. Old and new IDs will interleave oddly in ordered listings for a while. Decide whether "sorted by ID" is ever used to mean "sorted by time".
FAQ
Is v7 safe to expose in URLs? Collision-wise, yes. Just remember it reveals creation time to the millisecond.
Does switching to v7 need a database migration? Usually not; it's the same 128-bit type. Only new rows get time-ordered keys.
Is a UUID better than an auto-increment integer? It depends. Integers are smaller and strictly ordered, but they leak record counts and need a central sequence. UUIDs can be generated anywhere without coordination.
The short version
New project, general-purpose IDs: use v7. Need the same ID derived from the same input every time: v5. Debugging or generating one-off IDs by hand: any UUID generator will do, and it's worth generating a few of each version to see the structure — the timestamp prefix on v7, the pure randomness of v4. To watch why name-based IDs are reproducible, run the same input twice through a hash generator and compare the digests. The one habit to break is reaching for v4 reflexively on a table you expect to grow. It won't break anything today. It'll just quietly cost you at a million rows, and by then it's load-bearing.
← All articles