uuid

versionv4

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. If you want the same randomness in a shorter string, Nano ID and CUID2 pack more entropy into fewer characters.

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. ULID applies the same idea outside the UUID spec, encoding it in 26 characters instead of 36.

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.

UUID or something else?

UUID is the safe, standardized choice, but its 36 characters are verbose for ids that travel in URLs. If you need something shorter with the same collision safety, Nano ID carries slightly more randomness in 21 characters. For sortable keys in a compact, case-insensitive encoding, ULID is the closest alternative to v7. And if the id must be safe as an HTML id or CSS selector out of the box, CUID2 always starts with a letter.

© 2026 Unique ID Generator by meluiz