UUID v4 vs v7 — which should you use?

A UUID (Universally Unique Identifier) is a 128-bit value written as 36 characters: 8-4-4-4-12 hex digits. Two properly generated UUIDs will, for all practical purposes, never collide — which is why they're everywhere: primary keys, request IDs, file names, device IDs. Windows and .NET developers know the same thing as a GUID.

v4 — pure randomness

UUID v4 fills 122 of its 128 bits with random data. That's about 5.3 × 1036 possible values; you could generate a billion per second for a century and the odds of one duplicate would still be negligible. It's the default choice and what crypto.randomUUID() produces.

v7 — random, but sortable

UUID v7 (standardized in RFC 9562, 2024) starts with a 48-bit Unix millisecond timestamp, then random bits. IDs created later sort after IDs created earlier — as text or as bytes.

v4v7
Structure122 random bits48-bit timestamp + 74 random bits
Sortable by creation timeNoYes
B-tree index behaviourRandom inserts → page splits, cache missesAppend-mostly → compact, fast
Leaks creation time?NoYes (millisecond precision)
Best forOpaque public IDs, tokensDatabase primary keys, event logs

Rule of thumb: v7 for primary keys in Postgres/MySQL (your inserts stop shredding the index), v4 when the ID is public and you'd rather not reveal when a record was created.

Generate one now

The UUID Now generator produces both versions using your browser's secure random number generator — instantly, offline-capable, and without the ID ever touching a server.