Anatomy of a v4 UUID

A UUID is written as 32 hexadecimal digits in five groups: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx. The 4 marks version 4; the y in the fourth group is one of 8, 9, a or b, encoding the RFC 4122 variant. The remaining 122 bits are random.

text
f47ac10b-58cc-4372-a567-0e02b2c3d479
     ^    ^
     |    variant nibble (8/9/a/b)
     version nibble (4)

When v4 is the right choice

  • Public identifiers where unpredictability matters more than sortability.
  • Distributed systems that cannot coordinate a timestamp or node ID.
  • Test fixtures that need a valid identifier quickly.
  • Anywhere a database index will not be the bottleneck.

When v4 is a poor choice

Random primary keys fragment B-tree indexes: each insert lands on a random page, causing more page splits and larger indexes than a time-ordered key. For high-volume inserts, v7 or ULID is usually better. For a small lookup table, the difference is negligible.

Version 4 has no embedded timestamp, so you cannot recover creation time from the ID. Store a separate created_at column.