🌐 English

Home › Guides

UUID v4 vs v7 and how to choose

A UUID is a 128-bit identifier that lets independent systems create values that practically never collide. Different versions build the value differently, which changes sorting, storage and what the value reveals. This guide explains how to decide.

What a UUID looks like

A UUID is 128 bits written as 32 hexadecimal digits with hyphens in an 8-4-4-4-12 pattern, such as 123e4567-e89b-42d3-a456-426614174000.

The first digit of the third group is the version, and the first digit of the fourth group marks the variant. In the example, the 4 means version 4. Only those few bits are fixed by the standard; everything else is filled in according to the version.

v4: the random UUID

A v4 UUID fills almost every bit with random data. It needs no central server and no clock, and it is easy to implement, which is why it is the most common choice. The random space is so large that at realistic scales you do not need to worry about two identical values.

The downside is that values have no order. You cannot tell when one was created, and sorting spreads them around regardless of creation order. That unpredictability is a strength for privacy, but it can be a weakness for storage.

v7: the time-ordered UUID

A v7 UUID puts a 48-bit Unix time in milliseconds at the front and fills the rest with random bits. Sorting by string or bytes therefore keeps values roughly in creation order.

Database indexes work best when new values land near the end, so v7 tends to give better insert speed and cache use than random v4 as a primary key. The trade-off is that the first part contains a timestamp that anyone can read.

How to choose

The rule of thumb is simple. If you store many rows and often sort or query by ranges, v7 is the better fit. If the identifier is visible outside and revealing time is a concern, v4 is the better fit.

There is no need to convert existing v4 values. Start considering v7 for new designs; the two share the same format, so mixing them in one column causes no storage problem.

Other versions and common misconceptions

Version 1 mixes a timestamp with a device-specific value, which can reveal when and where it was made, so it is usually avoided today. Versions 3 and 5 hash a name, so the same input always yields the same UUID, which is useful when you need a reproducible identifier.

A UUID is not a secret. Being hard to guess is no reason to skip permission checks; if anyone who knows the address can open the item, you still need a real access check.