← Guides

UUID Versions Compared: v1, v4, v7 and Which to Use

What a UUID actually is

A UUID is a 128-bit value written as 32 hexadecimal digits in five groups: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. The version number sits in the first digit of the third group, so you can tell how a UUID was generated just by reading it. Version 4 is the random one most people meet first, and version 7 is the newer, time-ordered variant.

Version 4: randomness

UUID v4 uses 122 random bits from a cryptographically secure generator. There is no timestamp, no machine identifier and no ordering: two values generated one millisecond apart are as unrelated as two values from different machines. Collisions are effectively impossible at any realistic scale — you would need to generate about 2.7×1018 values before a 50% chance of one collision.

The trade-off is that v4 values sort badly. Inserting random keys into a B-tree index scatters writes across the whole index, which matters for write-heavy databases.

Version 1: time and machine identity

UUID v1 combines a timestamp with a node identifier derived from the machine’s MAC address. It is sequential, which makes it index-friendly, but the embedded MAC address and creation time are both visible to anyone who receives the value. For public identifiers that is an information leak, so v1 is mostly a legacy choice.

Version 7: time-ordered and random

UUID v7, standardised in RFC 9562 (2024), starts with a Unix timestamp in milliseconds followed by random bits. The result sorts chronologically, so new rows append near the end of an index instead of landing in random pages, while the random suffix keeps values unguessable. For database primary keys it is usually the best of both worlds.

Practical guidance

  • Database keys: v7 if your database benefits from ordered inserts; v4 otherwise.
  • Public API identifiers: v4 or v7. Avoid v1 because it leaks host information.
  • Secrets and tokens: none of them. A UUID is an identifier, not a password; use a dedicated random-token generator with enough entropy.
  • Test fixtures: v4 is fine, and fixed strings are better when tests need to be reproducible.

Reading a UUID by eye

The version digit is the first character of the third group, and the variant bits make the first character of the fourth group 8, 9, a or b in every standard UUID. So 9f1c2d3e-4b5a-7c6d-8e9f-0a1b2c3d4e5f is a v7 value, while the same layout with a 4 in that position would be v4. For v7 the leading 48 bits are a millisecond Unix timestamp, which means you can recover the creation time without storing a separate column — useful when debugging ordering, but also a reminder that a v7 identifier reveals roughly when a record was created.

Mistakes that reach production

  • Generating v4 with Math.random() — it is not a cryptographic generator and some engines seed it predictably. Use crypto.randomUUID() or the platform’s CSPRNG.
  • Truncating to eight characters for friendlier IDs — 32 bits of randomness collide at roughly 77,000 values by the birthday bound, which is well within the size of a busy table.
  • Storing 36-character text — a UUID fits in 16 bytes. Text columns inflate the index, and comparisons become case-sensitive unless you normalise the hex digits first.
  • Assuming a UUID is unguessable — v1 and v7 leak timestamps, and even v4 is an identifier, not an authorisation token.

UUID, ULID and the database’s own counter

A UUID is not the only option. Auto-increment integers are compact, perfectly ordered and fast to index, but they are guessable and awkward when several writers generate IDs offline. ULIDs use a similar time-first layout with a Crockford base32 alphabet, which sorts correctly as text and is shorter to read. Snowflake-style IDs pack a timestamp, a machine number and a sequence into 64 bits. UUID v7 is the safest default when you need a standard format that clients can generate without coordination; a plain integer remains the better choice for a single-database table that never leaves the server.

Sorting, sharding and index behaviour

Ordered identifiers are not only a storage detail. Pagination by id > cursor is cheaper than OFFSET, and time-ordered keys let a range scan answer “everything created since Monday”. With random v4 keys that query becomes a full scan plus a sort, and with a sharded database the write pattern becomes uneven: one shard receives a hot page while others idle. Choosing v7 does not remove the need for an index on the timestamp column, but it does keep inserts clustered at the end of the B-tree, which is the difference between appending a page and rewriting several.

Need a batch of identifiers right now? Generate them with our UUID generator, which runs entirely in the browser and supports uppercase, hyphen-free, braced and URN formats.

Try it yourself: open the UUID Generator →