Reading a UUID
Take 3f8a1b2c-7d4e-4f5a-9b8c-1d2e3f4a5b6c. The first digit of the third group is the version (here: 7), and the first digit of the fourth group is the variant. Versions you will actually meet:
| Version | Built from | Typical use |
|---|---|---|
| v1 | Timestamp + MAC address | Legacy; leaks hardware identity and has real privacy problems |
| v4 | Random bits | The workhorse — 122 random bits, used by nearly everything for 20 years |
| v5 | SHA-1 of a name + namespace | Reproducible IDs: same input, same UUID, forever |
| v7 | Millisecond timestamp + randomness | Time-sortable; the modern default for database keys |
Why v7 exists: the index problem
v4's pure randomness is a gift for privacy and a curse for B-tree indexes. Database primary keys are usually stored in a sorted tree; inserting a completely random key means writing to a random position in that tree every time. With data in RAM this is fine. Once the tree exceeds memory, every insert can drag in a different page from disk — and write throughput collapses. Benchmarks consistently show random keys several times slower than ordered ones at scale, with heavier write amplification in storage engines that must rewrite pages.
v7 solves this by putting a Unix-epoch-millisecond timestamp in the most significant bits. New UUIDs sort after older ones, so inserts always land on the right edge of the index — the same locality a plain auto-increment integer enjoys — while the low bits still carry enough randomness to make collisions negligible between machines generating IDs in the same millisecond.
What v7 buys and what it costs
The timestamp prefix makes UUIDs time-sortable: sorting by the ID alone approximates creation order, which is handy for feeds and pagination. It also means IDs leak "this was created around that time" — for most applications that is acceptable, but if creation time is a secret, stay on v4.
v4 remains completely fine for everything that is not a hot write-heavy database index: session tokens, API keys, one-time links, file names. The performance argument matters when a UUID is the clustered primary key of a large table.
Practical rules
- General default today: v7 for database keys, v4 everywhere else. Postgres 18 and the newest language libraries ship native v7 support.
- Never generate v1 anymore unless a legacy system demands it — the embedded MAC address leaks.
- Reproducibility? v5 with a fixed namespace. Two systems independently derive the same ID for the same entity.
- Security tokens: the UUID's randomness (v4/v7) is generally adequate, but a dedicated 256-bit random key is the cleaner choice for bearer secrets — a UUID was designed for uniqueness, not as a password.
- Never store UUIDs as strings in the database if you can avoid it: a native 16-byte column is half the size of a 36-char string and indexes faster.
Try it yourself
Generate a batch of v4 and v7 IDs with the UUID tool and look at where they differ: in v7 the first 12 hex digits increment with the clock, so a fresh batch shows a common prefix that ticks forward — while v4 gives you noise everywhere. Pair it with the hash tool if you want to explore how v5-style name-based derivation differs from pure randomness.