UUID v4 vs v7: Choosing Between Random and Time-Ordered IDs

A UUID is a 128-bit identifier, but not all UUIDs behave the same. Here's how the fully random v4 stacks up against the time-ordered v7 — and which one belongs in your database.

What is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit value used to label something without coordinating with a central authority. Because the space of possible values is astronomically large — roughly 3.4 × 1038 combinations — two independently generated UUIDs are effectively guaranteed never to collide. That makes them ideal for primary keys, request IDs, file names, and distributed systems where many machines mint identifiers at once.

UUIDs are written as 32 hexadecimal digits in five hyphen-separated groups, like f81d4fae-7dec-4d2a-8a1b-9c3f2e6b7a01. A few bits are reserved to encode the version (how the UUID was generated) and the variant. The two versions most relevant to modern applications are v4 and v7.

UUID v4: Fully random

Version 4 is the workhorse most developers reach for. Of its 128 bits, 6 are fixed for version and variant metadata, leaving 122 bits of pure randomness. There is no embedded timestamp, MAC address, or counter — just entropy from a cryptographically secure random source.

The strengths are obvious: excellent uniqueness, no information leakage, and trivial generation that needs no shared state. The values are completely opaque, so an outsider cannot infer when a record was created or guess neighboring IDs.

The weakness shows up in databases. Because v4 values are random, consecutive inserts land in scattered positions across a B-tree index. New rows do not append to the "end" of the index; they wedge into random pages. This hurts index locality, causes page splits and fragmentation, bloats the working set the database must keep in memory, and slows down bulk inserts. For a high-write table keyed on a v4 UUID, this overhead is real and measurable.

UUID v7: Time-ordered

Version 7 was designed specifically to fix the database problem while keeping the convenience of a random-looking identifier. Its layout puts a 48-bit Unix-millisecond timestamp at the front, followed by version/variant bits and roughly 74 bits of randomness.

Because the timestamp leads, v7 UUIDs generated over time are naturally sortable by creation order. Lexicographic string ordering matches chronological ordering. New inserts cluster at the high end of the index, so the B-tree grows by appending rather than fragmenting — dramatically better index locality and far fewer page splits.

Here is roughly how the bytes break down:

v7:  | 48-bit ms timestamp | ver | rand-a | var | rand-b |
     |<------ sortable ------>|<----- random tail ---->|

v4:  | 122 random bits + 6 fixed version/variant bits     |
     |<------------- entirely unordered ----------------->|

The random tail keeps v7 collision-resistant even when many IDs are created within the same millisecond. You get sortable, index-friendly keys without sacrificing practical uniqueness.

How to choose

Pick based on what the identifier needs to do, not on which version is "newer."

  • Use v7 for database primary keys, event logs, message IDs, or anything you insert in order or sort by time. The time-ordering pays off directly in write throughput and index health.
  • Use v4 when you need maximum opacity and must not leak timing information — public-facing tokens, password-reset links, API keys, or any ID an attacker could mine. A v7's embedded timestamp reveals when the record was created, which is sometimes a privacy or security concern.

A common pattern is to use v7 for internal primary keys and a separate v4 (or another opaque token) for anything exposed externally.

The other versions, briefly

Two older versions still appear in the wild:

  • v1 — time-based, combining a timestamp with the host's MAC address and a clock sequence. It sorts by time but can leak the generating machine's hardware identity. v7 is the modern, privacy-safer replacement.
  • v5name-based, deriving the UUID by hashing a namespace and a name with SHA-1. The same input always yields the same UUID, which is useful for deterministic, reproducible IDs. (v3 is the older MD5-based equivalent.)

Generate both with Formatly

Formatly's UUID Generator creates v4 and v7 identifiers instantly so you can grab exactly the kind you need — opaque randomness or sortable time-ordering — and copy them straight into your code or database seed. Generation happens entirely in your browser: nothing is uploaded and no values touch a server, so the IDs you create stay private to you. That client-side approach matters when the UUIDs are destined to become tokens, keys, or anything else you would not want logged by a third party.

FAQ

Is UUID v7 better than v4?

Neither is universally better — they solve different problems. v7 is better for database primary keys and anything you sort or insert in order, because its leading 48-bit timestamp gives excellent B-tree index locality. v4 is better when you need full opacity and must not leak when a record was created, since it embeds no timestamp at all.

Does UUID v7 leak the creation time?

Yes. A v7 UUID begins with a 48-bit Unix-millisecond timestamp, so anyone holding the ID can read off roughly when it was generated. That is intentional and useful for sorting, but it makes v7 a poor choice for public tokens or anything where timing is sensitive. Use v4 for opaque, non-time-revealing identifiers.

Why does UUID v4 slow down database inserts?

v4 values are fully random, so consecutive inserts scatter across an index rather than appending in order. This triggers frequent page splits and fragmentation in a B-tree, forces the database to keep more pages hot in memory, and reduces write throughput. v7's time-ordered prefix keeps new rows clustered at the end of the index, avoiding most of that overhead.

Are UUIDs generated by Formatly safe to use as secrets?

Formatly generates UUIDs entirely in your browser — nothing is uploaded to any server, so the values stay private to you. v4 UUIDs use a cryptographically random source and are suitable for many token uses. That said, for high-stakes secrets like session tokens, follow your framework's dedicated security guidance rather than relying on a UUID alone.

What happened to UUID v1, v3, and v5?

They still exist and remain valid. v1 is time-based but embeds the host MAC address, which can leak hardware identity — v7 is the modern replacement. v3 and v5 are name-based: they hash a namespace plus a name (MD5 for v3, SHA-1 for v5) to produce a deterministic, repeatable UUID, handy when the same input must always map to the same ID.