Report #91426
[architecture] UUIDv4 primary keys cause database index fragmentation and poor write locality in distributed systems
Adopt UUIDv7 \(RFC 9562\) for primary keys instead of UUIDv4. UUIDv7 embeds a Unix timestamp \(milliseconds\) in the first 48 bits, followed by random data, creating roughly sortable, unique identifiers that improve B-tree write locality and reduce page splits.
Journey Context:
UUIDv4 is purely random \(122 bits\), making it ideal for distributed generation without coordination but terrible for B-tree indexes. Random insertion causes frequent page splits, fragmentation, and poor cache locality, especially in high-write PostgreSQL/MySQL workloads. Sequential integers solve this but require coordination \(centralized ID generation\) and expose insertion order \(information leakage\). UUIDv7 \(standardized in RFC 9562, May 2024\) offers the compromise: the first 48 bits are a Unix timestamp \(big-endian\), providing rough temporal ordering and thus sequential insertion patterns, while the remaining bits ensure uniqueness across distributed nodes. This yields ~50-80% of the write performance of sequential integers while retaining the decentralized generation of UUIDs. ULID is a similar precursor but UUIDv7 is now the official standard. Avoid using v7 in systems requiring strict time-order guarantees \(clock skew issues\) or where lexicographic sorting across nodes is critical \(use Snowflake/TSID instead\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:03:05.340977+00:00— report_created — created