Should the primary key be an auto-increment integer or a UUID? It's one of the longest-running schema-design debates, and the answer used to be "it depends". Now that time-ordered UUIDs (UUIDv7, standardized in RFC 9562) are available, the trade-offs are much clearer.
What was wrong with UUIDs: randomness, not size
The classic complaint about UUID keys was performance, and the cause was UUIDv4, which is 122 bits of pure randomness.
B-tree indexes are sorted. With an increasing key (1, 2, 3… or a timestamp), every insert lands on the rightmost leaf page, which is always in memory, and pages fill up neatly. With random keys, each insert lands on a random page somewhere in the index:
- the working set is the entire index, so once it no longer fits in RAM, inserts turn into random disk reads;
- pages split in the middle and end up half-full, making the index bigger;
- in PostgreSQL, the first change to each page after a checkpoint writes a full-page image to the WAL, so scattered writes produce many times more WAL;
- in MySQL/InnoDB and SQL Server, the table is the clustered index, so the table data itself gets fragmented.
Benchmarks show a clear pattern: random-v4 insert throughput degrades as the table grows, while sequential keys stay flat.
UUIDv7 fixes the random part
UUIDv7 puts a 48-bit millisecond timestamp at the front, followed by random bits:
0192 3f4a - 8b1c - 7d2e - 9f10 - 4a5b6c7d8e9f
└──── timestamp ───┘ ver └──── random ──────┘
New values sort after old ones, so inserts append at the right edge of the index like an auto-increment would. You keep the benefits that made people want UUIDs in the first place:
- generate IDs anywhere: in the app, offline, on a phone, in five services, with no round trip to the database;
- merge data from shards or tenants without collisions;
- don't leak business data:
/orders/10442tells competitors how many orders you have; a UUID doesn't.
Watch out for one thing: v7 contains its creation time. If the time an ID was created is itself sensitive, don't expose v7 IDs publicly.
