What is a UUID
A UUID (Universally Unique Identifier) is a 128-bit identifier, usually written as 32 hexadecimal digits grouped into five blocks (8-4-4-4-12). Its goal is to generate unique identifiers without central coordination: two systems can create UUIDs independently with a negligible chance of collision. A few bits are reserved to mark the variant (the bit layout) and the version, which says how the value was generated.
Time-based ids
The earliest versions derive the id from when and where it was created. Version 1 mixes a timestamp with a node identifier — usually the machine's MAC address — while version 2 is a rare DCE Security variant that swaps part of the timestamp for a POSIX UID/GID. They guarantee strong uniqueness, but can leak the MAC address and creation time.
Name-based ids
Versions 3 and 5 are deterministic: they hash a namespace together with a name, so the same input always produces the same UUID. Version 3 uses MD5 and version 5 uses SHA-1 — prefer v5, since it relies on a stronger hash. These are ideal when you need a stable id derived from existing data rather than a fresh random one.
Random ids
Version 4 is the most common one today: 122 bits are filled with random data. It is simple, reveals nothing about the machine or time, and its collision probability is practically zero. The trade-off is that it is not sortable, which can hurt database index locality.
Sortable, modern ids
The newer versions bring back time ordering without the privacy issues of v1. Version 6 reorders the v1 fields so the most significant timestamp bits come first, and version 7 — the recommended choice for new systems — uses a Unix timestamp in milliseconds followed by randomness. Both are sortable, making them great as database keys.
Custom ids
Version 8 is a free/experimental format: the spec only fixes the version and variant bits and leaves the rest to the application. It lets you build your own layout while still producing something recognized as a valid UUID.
Which version to use
For most cases, reach for v4 when you just need a random id and ordering does not matter, or v7 when you want sortable database keys. Use v5 for deterministic, name-derived ids. The time-based v1/v2 are mostly legacy, and v8 is reserved for specialized, custom needs. Versions 6, 7, and 8 were standardized by RFC 9562, which replaced RFC 4122.