All posts

The Monotonicity Pitfalls of UUIDv7 in High-Throughput Databases

Blurred lights and yellow text at night
Photo by Maksim Samuilionak (https://unsplash.com/@maksim_taawy) on Unsplash (https://unsplash.com/photos/blurred-lights-and-yellow-text-at-night-aQiK1QufbdY)

Many engineering teams adopt UUIDv7 as a modern alternative to UUIDv4 and traditional auto-incrementing integers. UUIDv7 promises the benefits of 128-bit globally unique identifiers alongside time-ordered sequentiality, which prevents the severe index page fragmentation associated with UUIDv4 in B-tree structures. However, assuming that UUIDv7 provides strict, end-to-end monotonic ordering overlooks the details of the specification and runtime realities under heavy write throughput.

As outlined in an analysis by Alex Sci, the UUIDv7 structure relies on a 48-bit timestamp representing Unix time in milliseconds. Monotonicity is only naturally guaranteed at millisecond boundaries. When multiple identifiers are generated within the same millisecond, ordering guarantees depend entirely on the implementation strategy chosen for the remaining bits.

The Sub-Millisecond Ordering Gap

The UUIDv7 specification allows implementations to populate the sub-millisecond portion of the identifier using either pseudo-random bits or an optional sub-millisecond monotonic counter.

When a generator populates the sub-millisecond fields with random data, any two IDs created within the same millisecond are randomly ordered relative to each other. In high-throughput architectures where thousands of rows are inserted per second, a single millisecond regularly sees dozens or hundreds of generation requests. In systems using purely random sub-millisecond bits, these rows arrive at the database engine out of order.

Even when implementations include an explicit sub-millisecond counter, monotonicity frequently breaks down under edge conditions, as examined in discussions on Hacker News. If a process generates more identifiers within a single millisecond than the designated counter bit-width allows, the counter will overflow. Implementations deal with this rollover in different ways: some freeze the counter, others roll over to zero, and some increment the timestamp artificially into the future. Rolling over to zero immediately breaks monotonic ordering within that millisecond, causing newer keys to evaluate as smaller than earlier keys generated during the same tick.

Concurrency and Distributed Ingestion

The challenge compounds when identifiers are generated across multiple application threads or distributed service instances.

A central guarantee of auto-incrementing database sequences is centralized serialization: the storage engine assigns values strictly sequentially. In contrast, UUIDv7 generation is usually decentralized across API workers. Even if each individual thread maintains an internal monotonic counter, two distinct threads will not synchronize their sub-millisecond state.

Clock skew between servers, non-monotonic adjustments via NTP, and thread scheduling variations mean that identifiers arriving at the primary database node will not form a strictly increasing sequence. While the macroscopic ordering remains sorted by millisecond, microscopic ordering inside any active millisecond window is effectively chaotic.

B-Tree Degradation Under Micro-Churn

Database storage engines such as PostgreSQL (B-Tree) and MySQL InnoDB (Clustered Index) optimize for sequential right-edge insertions. When primary keys arrive strictly in ascending order, the engine appends rows to the end of the right-most leaf page, maintaining high page fill factors (often near 90 to 100 percent) and minimizing I/O overhead.

When sub-millisecond counter rollover or random bits cause keys to arrive out of order within active leaf pages, the engine must perform middle insertions rather than clean appends. The consequences include:

  1. Page Splits: When a target B-tree leaf page lacks capacity for an out-of-order key, the engine splits the page into two, allocating a new page and moving roughly half the keys to the new location.
  2. Reduced Page Fill Factor: Frequent page splits leave index leaf pages half-empty, bloating the index size on disk and consuming unnecessary buffer pool memory.
  3. Write Amplification: Splitting leaf pages increases write amplification at the storage layer, generating additional WAL (Write-Ahead Log) records and dirtied pages that must be flushed to disk.

While UUIDv7 limits this churn to the boundaries of the current millisecond window, the degradation is real for services operating at thousands of writes per millisecond. If counter overflows occur regularly during write spikes, the B-tree leaf nodes under construction experience continuous splits.

Engineering Mitigations

Architects relying on UUIDv7 for high-concurrency storage systems must evaluate whether sub-millisecond guarantees matter for their operational model: