How UUIDs Work: A Developer's Guide to Unique Identifiers
UUIDs power databases, APIs, and distributed systems. This guide explains UUID versions (v1, v4, v7), their structure, and when to use each type.
UUIDs (Universally Unique Identifiers) are the invisible glue of distributed systems. Every database row, every API resource, every event in an event-driven architecture might have one. Understanding UUID versions helps you choose the right one.
UUID Structure
A UUID is a 128-bit value displayed as 32 hex characters in 5 groups: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
- M = Version digit (1, 4, or 7)
- N = Variant bits (8, 9, a, or b)
UUID Version 1: Time-Based
v1 encodes a 60-bit timestamp and the MAC address of the generating machine. This makes v1 UUIDs sortable by generation time, but it's considered a privacy risk since the MAC address is embedded in the value.
UUID Version 4: Random
v4 UUIDs are generated from 122 bits of cryptographically random data. This is the most widely used version โ maximum randomness, no privacy concerns, no ordering. The probability of collision is astronomically low (you'd need to generate 1 billion UUIDs per second for 85 years to have a 50% chance of a collision).
UUID Version 7: Time-Ordered Random (New)
v7, introduced in the 2022 RFC update, combines the best of v1 and v4: the first 48 bits are a Unix millisecond timestamp, making v7 UUIDs naturally sortable, while the rest is random. This is ideal for database primary keys where you want unique IDs that can be indexed efficiently (sorted = better B-tree performance).
Which Version to Use
- API resource IDs shared publicly: UUID v4
- Database primary keys: UUID v7 (or ULID)
- Distributed event IDs needing ordering: UUID v7
Generate UUIDs instantly with our UUID Generator.