UUID v7 outperforms v4 as a database primary key due to time-ordered sorting
A developer exploring UUID internals found that version 7 UUIDs, standardised in RFC 9562 in 2024, offer a meaningful advantage over the widely used v4 format for database primary keys. Both types are 128-bit identifiers that allow distributed systems to generate unique IDs locally without any central coordination or risk of collision. While v4 UUIDs fill 122 bits with cryptographic randomness, v7 embeds a 48-bit Unix millisecond timestamp at the most significant end, making IDs sort chronologically as plain strings. This time-ordered structure prevents the random B-tree index scattering caused by v4 keys, which triggers page splits and cache inefficiency at scale. No browser built-in currently generates v7, but the logic can be hand-rolled by writing the timestamp byte-by-byte to avoid JavaScript's 32-bit bitwise operator limitation.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in