What Is a UUID? Version 4 Explained for Developers
UUIDs let systems create unique IDs without asking a central database. They power distributed databases, APIs and file names everywhere. Here’s what’s inside one and when to use it.
Anatomy of a UUID
A UUID is 128 bits written as 32 hex characters in five groups: 8-4-4-4-12. In version 4, 122 bits are random; the third group always starts with 4 (version) and the fourth with 8, 9, a or b (variant).
Will two UUIDs ever collide?
There are 5.3 × 1036 possible v4 UUIDs. You would need to generate roughly 2.7 quintillion to reach a 50% chance of one duplicate — provided your random source is cryptographically secure, as RandNow’s is.
UUIDs as database keys
- Pros: generate IDs anywhere, merge datasets safely, no guessable sequences.
- Cons: 16 bytes vs 4–8 for integers; random order can fragment B-tree indexes.
- Tip: for heavy insert workloads, consider time-ordered UUID v7.
Pro tips
- Store UUIDs in a native UUID/binary column, not as text.
- Use uppercase + braces for Windows registry and COM GUIDs.
- Generate up to 500 at once for fixtures.