The best database ID is not always the cleverest one. It is the one your team can generate reliably, index efficiently, expose safely and debug at 2 a.m. without inventing folklore.
Key takeaways
- Use UUID v7 when ordered inserts matter and timestamp visibility is acceptable.
- Use UUID v4 when privacy and simplicity matter more than natural ordering.
- Use sequences when IDs never need to be generated outside one database.
Start with the creation pattern
If one database creates every row, a sequence or identity column is still hard to beat. It is compact, fast and easy to reason about. UUIDs become more attractive when records can be created in several services, offline clients, import jobs or edge systems.
The moment you need IDs before the central database sees the row, UUIDs remove a coordination step.
UUID v4 for simple public identifiers
UUID v4 works well for public IDs that should not reveal the creation time. It is common in APIs because clients can treat it as opaque and developers can generate it in almost any language.
The tradeoff is random insert order. On a small table that may not matter. On a heavy write path with a clustered index, it deserves a real benchmark.
UUID v7 for ordered writes
UUID v7 is often the best modern default for database rows that are created continuously. It keeps UUID-style uniqueness while making new values sort near each other.
That helps pagination, log inspection and some index patterns. The cost is that the ID reveals approximate creation time.
Separate public ID from primary key when needed
Some systems use an internal numeric primary key and a separate public UUID. That is a reasonable design when joins need compact keys but APIs need unguessable-looking references.
The extra column is not free, but it can keep database performance and product security needs from fighting each other.
| Choice | Strength | Tradeoff |
|---|---|---|
| Sequence | Fast and compact | Needs central database coordination |
| UUID v4 | Private timing and easy generation | Random index pattern |
| UUID v7 | Better sort order for new rows | Approximate creation time is visible |
| Public UUID plus internal key | Flexible API and storage design | More columns and lookup rules |
