What Is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit identifier formatted like this:
550e8400-e29b-41d4-a716-446655440000
It consists of 32 hexadecimal characters separated by hyphens. UUIDs are designed to be unique across space and time without requiring a central authority. The odds of generating a duplicate UUID at random are astronomically low.
Common Uses for UUIDs
- Database primary keys: Replace auto-increment IDs to avoid conflicts in distributed systems
- API resource identifiers: Identify users, orders, files, etc. in REST APIs
- Session tokens: Generate unpredictable session IDs for improved security
- File naming: Assign unique names to uploaded files to prevent collisions
- Distributed tracing: Track requests across microservices
UUID v4 vs UUID v7: What's the Difference?
| Feature | UUID v4 | UUID v7 |
|---|---|---|
| Generation | Fully random | Timestamp + random |
| Sortable | No | Yes (by creation time) |
| DB performance | Poor (random inserts fragment indexes) | Good (ordered inserts, less B-tree fragmentation) |
| Embeds timestamp | No | Yes (creation time extractable) |
| Best for | Simple unique IDs | Database primary keys needing order |
Recommendation: Prefer UUID v7 for new projects — better database write performance. UUID v4 is fine for simple unique identifiers where ordering doesn't matter.