InfraHub
Back to Blog
Developer
DevOps Engineer

UUID Guide for Developers: v4, v7, Collision Probability, and Database Use

Understand UUID versions, the differences between UUID v4 and v7, collision probability, and best practices for using UUIDs as database primary keys.

UUID Guide for Developers: v4, v7, Collision Probability, and Database Use

UUIDs (Universally Unique Identifiers) are 128-bit values used as identifiers in databases, APIs, distributed systems, and URLs. They're generated without central coordination, making them ideal for distributed architectures. But not all UUID versions are created equal — choosing the wrong one has real performance implications.

UUID Format

A UUID is 128 bits represented as 32 hexadecimal characters in five groups:

550e8400-e29b-41d4-a716-446655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
  • M (position 13): Version number
  • N (position 17): Variant bits (RFC 4122 uses 8, 9, a, or b)

UUID Versions

UUID v1: Time-Based

Embeds the current timestamp (100-nanosecond intervals since October 15, 1582) and the MAC address of the generating machine:

Time component: Provides monotonic ordering
MAC address:    Unique across machines (but reveals hardware identity)

Problem: The MAC address exposes network interface information, enabling potential correlation attacks. Rare in new systems.

UUID v3 and v5: Name-Based

Deterministic UUIDs generated from a namespace and a name:

import uuid

# v3 uses MD5
v3 = uuid.uuid3(uuid.NAMESPACE_DNS, 'example.com')

# v5 uses SHA-1 (preferred)
v5 = uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')
# Always produces: 2ed6657d-e927-568b-95e3-af9a819d37ea

Use v5 when you need the same UUID to be generated from the same input across systems — deduplication, content-addressed storage, idempotent operations.

UUID v4: Random

128 bits of random data with 6 bits used for version and variant flags, leaving 122 bits of randomness:

crypto.randomUUID()
// "9b7c3f8a-2e5d-4a1b-8f6c-0d3e9a7c2b1f"

This is the most widely used version. Most UUID libraries default to v4.

UUID v7: Time-Ordered Random (RFC 9562, 2024)

UUID v7 encodes a Unix timestamp in the most significant bits followed by random data:

0192d80d-a5b2-7b3a-8c4d-1e5f6a7b8c9d
└──────────────┘
  Unix timestamp ms
  (first 48 bits)

Why this matters: The timestamp prefix makes v7 UUIDs monotonically increasing within a millisecond. This is critical for database performance.

Collision Probability

UUID v4 has 122 bits of randomness. The birthday paradox formula gives the probability of at least one collision after generating N UUIDs:

P ≈ 1 - e^(-N²/(2×2¹²²))
UUIDs Generated Collision Probability
1 billion 1 in 10²⁷
1 trillion 1 in 10²¹
1 quadrillion 1 in 10¹⁵

In practice, you would need to generate approximately 2.7 × 10¹⁸ UUIDs before reaching a 50% collision probability. For any real-world application, UUID v4 collision probability is negligible.

UUID v4 vs v7: Which to Use?

The Database Index Problem

Database indexes perform best when values are inserted in order. UUID v4 is completely random, causing index fragmentation:

Sequential inserts with UUID v4:
Index page: [3a1f..., 9b2c..., 2e7a..., f4d1...]
             Random positions scattered throughout B-tree

Sequential inserts with UUID v7:
Index page: [01928a..., 01928a..., 01928b..., 01928b...]
             Monotonically increasing — appended to end of B-tree

For high-insert-rate tables in PostgreSQL or MySQL with UUID primary keys, UUID v7 provides significantly better write performance than v4 due to reduced page splits and better index locality.

Benchmark results from PostgreSQL tests typically show 20–50% improvement in insert throughput with v7 over v4 at scale.

UUID v7 in Practice

// Using the 'uuid' library v10+ which supports v7
import { v7 as uuidv7 } from 'uuid';

const id = uuidv7();
// "01928a3b-c4d5-7e6f-8a9b-0c1d2e3f4a5b"

// The timestamp can be extracted
const timestamp = parseInt(id.replace(/-/g, '').substring(0, 12), 16);
// Unix timestamp in milliseconds

Decision Guide

Use Case Recommended Version
General-purpose IDs v4 or v7
Database primary keys (high insert rate) v7
Deterministic IDs from known inputs v5
Time-sortable IDs (logs, events, messages) v7
Legacy system compatibility v4

UUIDs vs Other ID Strategies

Strategy Pros Cons
UUID v4 No coordination, universal Random order, 36 chars as string
UUID v7 No coordination, time-ordered Newer, less library support
Auto-increment integer Compact, ordered Reveals row count, not safe for distributed systems
ULID Sortable, URL-safe, 26 chars Less standard than UUID
Snowflake ID Sortable, compact 64-bit Requires coordination (machine ID)

Generate UUIDs in Your Browser

The UUID Generator on InfraHub generates UUID v4 and v7 values instantly in your browser using the Web Crypto API. Generate single values or bulk lists, copy to clipboard, and switch between versions. No server calls, no logging — the IDs are generated entirely client-side.

Useful for populating test fixtures, generating IDs for API testing, or seeding database migrations.

Share Feedback

We read every message