The Programmer's Guide to Universal Unique Identifiers

By The Coding Gopher

Published: 2026-09-15

URL: https://thecodinggopher.substack.com/p/uuids-explained


Why distributed systems abandon sequential integers, and the database penalties of pure randomness.

In a traditional, monolithic application, assigning an ID to a new piece of data is trivial. The relational database acts as the central authority, using an auto-incrementing integer. The first user is ID: 1, the second is ID: 2, and so on.

However, as systems scale horizontally, this centralized model collapses. If five independent microservices are processing thousands of orders per second across different servers, they cannot share a single auto-incrementing counter. Doing so requires a distributed lock—forcing every server to wait in a single-file line to ask the central database for the “next” number. This creates a massive bottleneck that destroys system throughput.glossary - What is a UUID? - Stack OverflowRFC 4122 is the foundational Internet standard that defines the 128-bit format and generation methods for Universally Unique Identifiers (UUIDs). It ensures identifiers remain globally unique across space and time without requiring a centralized registration authority.

To scale, engineers abandon sequential integers and rely on the mathematical probability of RFC 4122: the UUID.

UUIDs (Universally Unique Identifiers) are 128-bit random strings used to uniquely identify records in distributed systems. They allow multiple independent servers to generate database IDs simultaneously without consulting a central authority, completely eliminating the risk of ID collisions.

Subscribe now

The Math of Infinite Scale

Instead of relying on a central authority to track the next sequence, a system using UUIDs simply generates a 128-bit random number. The goal is not to strictly guarantee uniqueness through coordination, but to make the probability of a collision so astronomically low that engineers can safely treat it as zero.

The most common implementation is UUIDv4, which is entirely random. While a 128-bit string offers 2^128 possibilities, UUIDv4 reserves 6 bits for version and variant metadata. This leaves 122 bits of pure cryptographic randomness, resulting in exactly 2^122 (or 5.3 times 10^36) possible IDs.

To understand the sheer scale of this number: you could generate 1 billion UUIDs per second for 85 years straight, and the probability of a single collision occurring would still only be about 50%.Julian's Blog - Working With UUIDsFor standard 128-bit Version 4 (random) UUIDs, the probability of a collision is mathematically negligible. Due to the Birthday Paradox, you only need to generate about 2.71 × 10^18 UUIDs to reach a 50% chance of a single collision.

The Anatomy of the String

A standard UUID is represented as a 32-character hexadecimal string, broken into five groups separated by hyphens (an 8-4-4-4-12 format).

Despite being “random,” a UUIDv4 has a strict internal structure. You can always identify a v4 string by looking at two specific characters that act as metadata flags.

𝐋𝐞𝐚𝐫𝐧 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐆𝐢𝐭, 𝐃𝐨𝐜𝐤𝐞𝐫, 𝐑𝐞𝐝𝐢𝐬, 𝐇𝐓𝐓𝐏 𝐬𝐞𝐫𝐯𝐞𝐫𝐬, 𝐚𝐧𝐝 𝐜𝐨𝐦𝐩𝐢𝐥𝐞𝐫𝐬, 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡. Get 40% OFF CodeCrafters: https://app.codecrafters.io/join?via=the-coding-gopher

Subscribe now

The Database Penalty (Page Fragmentation)

While UUIDv4 perfectly solves the distributed generation problem, it introduces a severe physical penalty at the database storage layer.

Relational databases (like PostgreSQL and MySQL) use B-Trees to index their Primary Keys. A B-Tree is a hierarchical data structure highly optimized for sequential, ordered data. When you insert ID: 4, the database simply appends it to the far right edge of the tree, right next to ID: 3. This operation is lightning-fast and keeps the index perfectly compact.Introduction of B Tree - GeeksforGeeksA B-tree is a self-balancing tree data structure that keeps data sorted and allows for searches, insertions, and deletions in logarithmic time. Unlike binary search trees, B-tree nodes can have more than two children. This broad, bushy structure reduces tree height, optimizing the retrieval of large blocks of data.

Because UUIDv4 is completely random, an incoming insert like f47ac10b... forces the database engine to scan the B-Tree, find the exact middle page where this random string belongs lexicographically, and forcefully squeeze it in.What are SQL Server Index Fragmentation and Index Fill Factor? - Purple  Frog SystemsIndex fragmentation occurs when the logical ordering of database records no longer matches their physical storage sequence. As rows are inserted, updated, or deleted, database pages can split or empty, forcing the engine to perform more random disk I/O and slowing down query performance.

If that specific memory page is already full, the database must split the page in half, rewrite the data, and update the parent nodes. This process is known as Index Fragmentation. Under heavy write loads, pure random UUIDs force the disk to constantly shift and split existing data, destroying write throughput and severely bloating the physical size of the index on the hard drive.

The Evolution: UUIDv7Goodbye to sequential integers, hello UUIDv7! | BuildkiteUUID v4 is a fully random identifier that ensures maximum privacy and uniqueness. In contrast, UUID v7 features an embedded Unix timestamp, making the identifiers sequential and time-sortable.

Engineers needed the distributed, lock-free generation of UUIDv4, but they desperately needed the sequential database performance of auto-incrementing integers.

This friction led to the creation of UUIDv7, the modern standard for database identifiers. It solves the fragmentation problem by splitting the 128 bits into two distinct architectural parts:

-

The Timestamp Prefix: The first 48 bits encode the current Unix epoch time in milliseconds.

-

The Random Suffix: The remaining 74 bits are cryptographically random.

Because the leading characters of the string are generated from the current time, UUIDv7 strings naturally sort chronologically. When a database receives thousands of UUIDv7 inserts per second, the strings append sequentially to the right edge of the B-Tree.UUIDv7 structure with microsecond-precision timestampBecause it starts with a timestamp, newly generated IDs are sequentially larger. This allows databases (like PostgreSQL and MySQL) to append inserts to the end of an index rather than scattering them randomly across the table, eliminating costly index fragmentation.

This completely eliminates index fragmentation, restoring peak write performance while still guaranteeing global uniqueness without a centralized server.

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

Thanks for reading! This post is public so feel free to share it.

Share