Guide

UUIDs in practice: v4, v7 and how to choose an identifier

Published · 7 minute read

A UUID is 128 bits written as 32 hexadecimal digits. That much is common to every version. What differs is where those bits come from, and that choice affects database performance, privacy and whether two systems can generate the same ID for the same thing.

Reading a UUID

f47ac10b-58cc-4372-a567-0e02b2c3d479
              ↑    ↑
        version    variant

The digit at the start of the third group is the version. The first digit of the fourth group encodes the variant and is 8, 9, a or b for every UUID you are likely to meet. So you can tell how an ID was made just by looking at it: a 4 in that position means random, a 7 means time-ordered.

The versions that matter

VersionBuilt fromUse it when
v1Timestamp plus the machine's network addressRarely now; it reveals where and when it was made
v3 / v5A hash (MD5 / SHA-1) of a namespace and a nameThe same input must always give the same ID
v4122 random bitsYou need an unguessable ID with no meaning
v748-bit Unix time in milliseconds, then random bitsThe ID is a database key or needs to sort by creation time

Versions 6, 7 and 8 were standardised in 2024 in RFC 9562, which replaced the original specification. Version 7 is the one that changes everyday practice.

Why random UUIDs hurt database indexes

Most databases store a table's primary key in a B-tree, which keeps keys in sorted order. With an auto-incrementing number, every new row lands at the right-hand edge of the tree. The pages being written are few, they stay in memory, and they fill up completely.

A version 4 UUID has no order. Each insert lands at a random place in the index, so over time:

None of this is visible on a small table. It appears as insert throughput falling away once a table reaches tens of millions of rows.

What version 7 changes

A v7 UUID begins with the current time in milliseconds, followed by random bits. IDs generated later sort after IDs generated earlier, so inserts go to the end of the index just as they would with a sequence, while the random part keeps IDs unique across machines without coordination.

018f3c5e-7a10-7b3c-9f21-4d6a8e0c1b52
└─ 48-bit time ─┘ 7 └──── random ────┘

You also get creation order for free: sorting by ID is sorting by time, to the millisecond.

The cost is that the timestamp is readable by anyone who sees the ID. If an ID appears in a URL, it reveals when the record was created, and a sequence of IDs reveals how fast records are being created. For internal keys that is harmless. For identifiers handed to outsiders, such as invitation links, password reset tokens or anything that must not be guessable or informative, use v4, or better, a purpose-made random token.

How likely is a collision?

A v4 UUID has 122 random bits, about 5.3 × 1036 possible values. The relevant question is how many you must generate before two are likely to match. By the birthday calculation, you need roughly 2.7 × 1018 UUIDs for a 50% chance of a single collision. Generating a billion per second, that takes about 86 years.

In practice, collisions that have been reported come from broken random number generators, not from bad luck: virtual machines cloned with identical generator state, or code using a non-cryptographic source. Use your platform's built-in UUID function, which draws from the operating system's secure generator. In a browser that is crypto.randomUUID(), which is what the UUID Generator here uses.

Name-based UUIDs: the same input, the same ID

Versions 3 and 5 are not random at all. They hash a namespace UUID together with a name and format the result as a UUID. The same pair always produces the same ID, on any machine, with no shared state:

uuid5(NAMESPACE_URL, "https://example.com/users/42")
→ always the same value

This is useful for importing data repeatedly without creating duplicates, or for deriving a stable ID from a natural key owned by another system. Prefer v5 (SHA-1) over v3 (MD5). Do not treat these as secret: anyone who knows the namespace and can guess the name can compute the ID.

Storage: 16 bytes, not 36 characters

The familiar text form is 36 characters. Stored as a string, that is more than twice the 16 bytes the value actually needs, and the cost is paid again in every index and foreign key that references it. Use the native type where there is one (uuid in PostgreSQL, uniqueidentifier in SQL Server) or a 16-byte binary column elsewhere. Convert to text only at the edges of the system.

If you store text anyway, pick one case. UUIDs are case-insensitive by definition, but string comparison is not, and F47AC10B… will not match f47ac10b… in a case-sensitive column. Lowercase is the convention.

Do you need a UUID at all?

A common and sensible design uses both: an internal v7 or integer primary key for joins and indexes, and a separate random public identifier for URLs and APIs.

Tools used in this guide

More guides