What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit label used for information in computer systems. Unlike incremental IDs, UUIDs are designed to be unique across space and time without requiring a central registration authority. This makes them ideal for distributed systems, databases, and microservices.
UUID v4 vs. UUID v7
While both versions provide uniqueness, they serve different architectural needs:
- UUID v4 (Random): Generated using purely random numbers. It is the most common version and is perfect for temporary identifiers where sortability doesn't matter.
- UUID v7 (Time-Ordered): The new standard that includes a Unix timestamp. Because they are chronologically sortable, they significantly improve database indexing performance.
UUID Format Reference
| Feature | UUID v4 | UUID v7 |
|---|---|---|
| Generation | Fully Random | Timestamp + Random |
| Sortable | No | Yes (By Creation Time) |
| Best For | Privacy/Randomness | Database Primary Keys |
Why Developers Use UUIDs
- Distributed Systems: Generate IDs on different servers simultaneously without the risk of collisions or needing a central database lock.
- Security: Unlike sequential IDs (1, 2, 3...), UUIDs are unpredictable, preventing users from guessing other records or total resource counts.
- Offline Support: Mobile or edge applications can generate valid IDs while offline and sync them to the cloud later without conflicts.
Frequently Asked Questions
Can UUIDs collide?
The probability of a collision in UUID v4 is effectively zero. You would need to generate billions of UUIDs per second for many years to have a 50% chance of a single duplicate.
Why use v7 instead of v4 for databases?
UUID v7 is B-tree friendly. Because the IDs are sequential, databases don't have to re-order the index for every new insert, leading to much faster write speeds.