Storage decisions

Best UUID Version for Databases

Choose between UUID v4, UUID v7, database sequences and public IDs for real application tables.

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.

ChoiceStrengthTradeoff
SequenceFast and compactNeeds central database coordination
UUID v4Private timing and easy generationRandom index pattern
UUID v7Better sort order for new rowsApproximate creation time is visible
Public UUID plus internal keyFlexible API and storage designMore columns and lookup rules

FAQ

Best UUID Version for Databases questions

Is UUID v7 the best primary key?

It is a strong choice for many new distributed systems, especially write-heavy tables. It is not automatically better for every small or single-database app.

Should URLs expose database IDs?

They can expose UUIDs when authorization is enforced. Do not expose sequential numeric IDs if enumeration would reveal sensitive business information.

Do UUIDs make joins slower?

UUID keys are larger than integers, so they can use more memory and index space. Whether that matters depends on table size, workload and database design.

Best UUID Version for Databases - UUID v4 vs v7