Back to Blog
System Design

UUID v4 in Distributed Systems: Why Randomness Matters

Learn how UUID v4 identifiers work, why collisions are statistically impossible, and how to choose between UUID v4, v7, and alternatives like ULID.

By Birhrt Team

The Problem of Identity in Distributed Systems

In a single-database application, generating unique identifiers is simple: use an auto-incrementing integer column. The database guarantees uniqueness because it controls the sequence. But in distributed systems — microservices, multi-region databases, offline-capable apps — there is no single authority to manage a sequence. Each service needs to generate identifiers independently, without coordination, and without collisions.

This is the problem that UUIDs solve.

What Is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit identifier standardized in RFC 4122. It is represented as 32 hexadecimal characters in five groups: 550e8400-e29b-41d4-a716-446655440000

The standard defines several versions, each with a different generation strategy:

  • v1: Timestamp + MAC address (privacy concerns, sequential)
  • v3: MD5 hash of a namespace + name (deterministic)
  • v4: Random (most commonly used)
  • v5: SHA-1 hash of a namespace + name (deterministic)
  • v7: Timestamp + random (sortable, new in RFC 9562)

UUID v4: The Random Approach

UUID v4 uses 122 bits of cryptographically secure random data (the remaining 6 bits encode the version and variant). The format is:

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
                ^    ^
                |    variant (8, 9, a, or b)
                version (always 4)

How it is generated:

  1. Generate 128 bits of random data using a cryptographic random number generator
  2. Set the version bits (4 bits) to 0100 (version 4)
  3. Set the variant bits (2 bits) to 10 (RFC 4122 variant)
  4. Format as 32 hex characters with hyphens

Why Collisions Will Never Happen

The probability of a UUID v4 collision is:

P(collision) ≈ 1 - e^(-n² / (2 × 2^122))

To put this in perspective:

  • Generating 1 billion UUIDs per second for 100 years gives a collision probability of about 50%.
  • Generating 1 million UUIDs: collision probability is roughly 1 in 10^30 (one in a nonillion).
  • You are more likely to be struck by a meteorite while being struck by lightning while winning the lottery.

The key requirement is that the random number generator must be cryptographically secure. JavaScript's crypto.getRandomValues() and most operating systems' /dev/urandom meet this requirement. Do NOT use Math.random() — it is a pseudo-random number generator that can produce predictable sequences.

UUID v4 vs. Auto-Increment IDs

PropertyAuto-IncrementUUID v4
Uniqueness scopeSingle databaseGlobal
GenerationRequires DB roundtripClient-side, no coordination
PredictabilitySequential (security risk)Random (no enumeration)
Storage size4-8 bytes16 bytes
Index performanceExcellent (sequential)Poor (random inserts)
URL safetyShort (/user/42)Long (/user/550e8400-...)

The B-Tree Index Problem

UUID v4's biggest drawback is database index performance. B-tree indexes (used by PostgreSQL, MySQL, and most databases) are optimized for sequential inserts. Auto-incrementing IDs always insert at the end of the index, which is efficient. Random UUIDs insert at arbitrary positions, causing:

  • Page splits: The index tree needs to be rebalanced frequently
  • Cache misses: Random inserts access different memory pages, reducing cache efficiency
  • Write amplification: More disk I/O for each insert operation

For high-write-throughput databases (thousands of inserts per second), this can become a measurable performance issue.

UUID v7: The Best of Both Worlds

UUID v7 (defined in RFC 9562, published 2024) combines a millisecond-precision timestamp with random data:

0190dc63-1a20-7ba0-8000-a3e8f7c9b2d1
|            |    ^
|            |    version 7
|            random
Unix timestamp (ms)

Advantages over v4:

  • Sortable: UUIDs generated later sort after earlier ones
  • Index-friendly: Sequential timestamps mean sequential B-tree inserts
  • Roughly time-ordered: You can extract the approximate creation time from the UUID

Disadvantages vs. v4:

  • Less random: The timestamp portion is predictable (you can estimate when an ID was created)
  • Newer standard: Less library support (as of 2025, adoption is growing)

Alternatives to UUID

ULID (Universally Unique Lexicographically Sortable Identifier):

01ARZ3NDEKTSV4RRFFQ69G5FAV
|          |
timestamp  random
  • 128-bit like UUID, but encoded as 26 Crockford Base32 characters
  • Sortable by creation time
  • More compact string representation than UUID

Snowflake IDs (Twitter):

  • 64-bit integers: timestamp (41 bits) + machine ID (10 bits) + sequence (12 bits)
  • Compact and sortable, but require machine ID coordination
  • Used by Twitter, Discord, and Instagram

NanoID:

import { nanoid } from 'nanoid';
const id = nanoid(); // "V1StGXR8_Z5jdHi6B-myT"
  • Configurable length and alphabet
  • URL-safe by default
  • Smaller than UUID but same collision resistance for equivalent length

Choosing the Right ID Strategy

Use CaseRecommended
Distributed systems, no sorting neededUUID v4
Distributed systems, sorting neededUUID v7 or ULID
High-write databasesUUID v7 or auto-increment
Public-facing URLsNanoID or short UUID
Single database, no distributionAuto-increment
Cross-system deterministic IDsUUID v5

Practical Tips

  1. Store UUIDs as binary, not strings: In PostgreSQL, use the uuid column type (16 bytes). In MySQL, use BINARY(16). Storing as VARCHAR(36) wastes space and slows comparisons.
  2. Generate on the client: One of UUID v4's key advantages is that you can generate IDs before the database insert. This enables optimistic UI updates, batch inserts, and offline-first architectures.
  3. Use crypto.randomUUID(): Modern browsers and Node.js 19+ provide a native crypto.randomUUID() function that is faster and more correct than library implementations.
  4. Do not use UUIDs as secrets: UUIDs are unique but not necessarily unguessable. v1 UUIDs leak the MAC address, and even v4 UUIDs should not be used as API keys or session tokens without additional security measures.

Summary

UUID v4 provides globally unique identifiers using 122 bits of cryptographic randomness. Collisions are statistically impossible when using a proper random number generator. The tradeoff is database index performance, which UUID v7 addresses by adding a time-ordered prefix. For most applications, UUID v4 remains the simplest and most portable choice. Use UUID v7 or ULID when sort order and index performance matter.

Try the Related Tool

Put this knowledge into practice with our free, privacy-first tool.

Open Uuid Tool →