UUIDs Explained: The Art of Generating Unique Identifiers

What makes a UUID unique? A deep dive into UUID versions, generation, and when (not) to use them as database keys.

UUIDs Explained: The Art of Generating Unique Identifiers

What is a UUID?

A Universally Unique Identifier (UUID) is a 128-bit label, typically written as a 36-character hexadecimal string with hyphens: 550e8400-e29b-41d4-a716-446655440000. The probability of generating two identical UUIDs is vanishingly small — hence "universally unique."

UUID Versions

There are several UUID versions, each with different generation strategies:

UUID v1 — Timestamp + MAC Address

550e8400-e29b-11d4-a716-446655440000
              └─ timestamp + MAC here

Contains the generating machine's MAC address — a privacy concern. Can be reordered for time-sortability.

UUID v4 — Random (Most Common)

6ba7b810-9dad-41d4-80b4-00c04fd430c8
└── random bits, 122 bits of entropy

Purely random. No information leakage. Good for most use cases. Use this unless you have a specific reason not to.

UUID v7 — Time-Ordered

0191a7c4-5e3f-7000-9673-a1b2c3d4e5f6
└── embedded timestamp, sortable

Newer (RFC 9562). Embeds a Unix timestamp, making it lexicographically sortable while remaining random enough for uniqueness.

Database Performance

UUIDs as primary keys have a significant drawback: random insertion performance. B-tree indexes store entries in sorted order. Random UUIDs cause constant page splits and index bloat.

  • Don't use UUID v4 as primary keys in high-write workloads
  • Consider UUID v7 for time-sortable IDs with good write performance
  • Use sequential IDs (auto-increment) for simple cases
  • CUID2 / ULID are modern alternatives designed for database performance

Generating UUIDs

// JavaScript (browser or Node)
crypto.randomUUID()
// "550e8400-e29b-41d4-a716-446655440000"

// Python
import uuid
uuid.uuid4()   # v4 random
uuid.uuid7()   # v7 time-ordered

When to Use UUIDs

  • Distributed systems where multiple services generate IDs independently
  • URL-safe identifiers (no need for collision checking)
  • When the ID should not reveal information about order or count
  • External/publicfacing IDs (not guessable)

Try Our UUID Generator

Generate v4 UUIDs instantly with our UUID Generator. One click to generate one or批量 UUIDs with copy-to-clipboard support.

← Back to Blog
Copied!