A UUID (Universally Unique Identifier) is a 128-bit label standardised in RFC 4122. It is represented as 32 hexadecimal digits grouped by hyphens: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx, where M encodes the version and N encodes the variant. The probability of two randomly generated v4 UUIDs colliding is astronomically small โ roughly 1 in 5.3 ร 10ยณโถ.
| Version | Algorithm | Pros | Cons |
|---|---|---|---|
| v1 | Timestamp + MAC address | Sortable by creation time | Exposes MAC address; not truly random; can clash if generated in rapid bursts |
| v3 | MD5 hash of namespace + name | Deterministic โ same input always gives same UUID | MD5 is cryptographically weak; use v5 instead |
| v4 | Cryptographically random | Simple, globally unique, no coordination needed | Not sortable โ random order can hurt B-tree index performance at scale |
| v5 | SHA-1 hash of namespace + name | Deterministic; better than v3 | Still not random; requires agreeing on a namespace |
| v7 (draft) | Unix timestamp + random bits | Sortable and random โ best of both worlds | Not yet in all UUID libraries (gaining adoption fast) |
| Scheme | Example | Best for |
|---|---|---|
| UUID v4 | 550e8400-e29b-41d4-... | Most distributed systems, REST APIs, when no sort order is needed |
| UUID v7 | 018f5e3a-6a82-7... | Databases where index locality matters (Postgres, MySQL) |
| ULID | 01ARZ3NDEKTSV4RRFFQ69G5FAV | Sortable, URL-safe, timestamp-prefixed โ great for event logs |
| NanoID | V1StGXR8_Z5jdHi6B-myT | Short URL-safe IDs; configurable alphabet and length |
| Auto-increment | 1, 2, 3โฆ | Simple single-database setups where enumeration is not a security risk |