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.
After removing the bits used for version and variant, a v4 UUID still has 122 randomly chosen bits.
Hyphens are only formatting, so you may store the value without them or as a 16-byte binary value and it is still the same identifier.
Hex digits are case-insensitive, so normalize to one case before comparing two values as text.
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.
Exposing it in a web address leaks no creation time or server detail, so it suits identifiers shown to the public.
It needs a good source of randomness. Weak random numbers can make collisions far more likely than the theory suggests.
For security tokens, do not rely on the UUID format; use a sufficiently long random value designed for that purpose.
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.
Many implementations add an internal counter so several values created within the same millisecond stay ordered.
If a computer clock moves backwards, order can be broken, so keep a separate sequence number where strict ordering matters.
Check whether revealing the creation moment is acceptable, for example whether a sign-up time may become public.
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.
A common split is v7 for internal primary keys and v4 for values placed in public addresses.
Storing a UUID as a 36-character string takes more than double the space of 16 binary bytes, so use a binary type if your storage supports it.
Generate a few values with the UUID generator on this site and look at the first digit of the third group; spotting versions becomes second nature.
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.
Version 5 always returns the same value for the same namespace and name, so it can serve as a deduplication key.
The collision chance is negligibly small, but adding a unique constraint in the database is still the safe habit.
The all-zero value 00000000-0000-0000-0000-000000000000 is a special value meaning nothing or empty.