A UUID (Universally Unique Identifier) is a 128-bit identifier standardized in RFC 9562 (2024, replacing RFC 4122). UUIDv4, the most common version, generates 122 random bits — producing 5.3 x 10^36 possible values, making collision probability negligible even at billions of generated IDs. UUIDv7 (2024) embeds a Unix timestamp in the first 48 bits, making UUIDs sortable by creation time — ideal for database primary keys where insertion order matters for index performance.
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit label used to identify information across distributed systems without requiring a central coordinating authority. The standard text representation is 32 hexadecimal digits in five dash-separated groups: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. UUIDs are defined by RFC 4122 (original) and RFC 9562 (2024 update that introduced v7).
The version digit (the 13th hex character) tells you how the UUID was generated. Version 4 means random, version 7 means timestamp-based. The variant bits (positions 17-18) indicate the UUID conforms to the RFC standard.
What is the difference between UUID v4 and v7?
UUID v4 fills 122 of its 128 bits with cryptographically secure random data (the remaining 6 bits encode the version and variant). This gives 5.3 × 10³⁶ possible values, making collisions astronomically unlikely. V4 is the most widely used version today.
UUID v7, defined in RFC 9562 (2024), embeds a Unix millisecond timestamp in the first 48 bits. The remaining bits are random (with version and variant markers). This means v7 UUIDs are naturally sortable by creation time — a huge benefit for database primary keys, since B-tree indexes perform best with sequential inserts. PostgreSQL, MySQL, CockroachDB, and most modern databases benefit significantly from ordered IDs.
In short: use v4 for maximum randomness and zero information leakage. Use v7 when you want time-sortable, index-friendly identifiers.
When should you use UUIDs?
UUIDs are the standard choice whenever you need globally unique identifiers without a central ID authority:
- Database primary keys — avoid auto-increment conflicts in distributed or sharded databases
- Distributed systems — each node generates IDs independently with no coordination
- API resource identifiers — expose UUIDs in URLs instead of sequential integers to prevent enumeration attacks
- Session and correlation IDs — trace requests across microservices with unique tokens
- File and object naming — generate unique filenames for uploads without collision risk
Need to encode your UUIDs for transport? Try our Base64 encoder.