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.

Frequently Asked Questions

Should I use UUIDs as database primary keys?

It depends. UUIDs (especially v4) are great for distributed systems and exposed IDs (security through obscurity). But they're 16 bytes vs 4-8 for an integer, and random UUIDs fragment indexes badly. UUID v7 fixes the index fragmentation by being time-sortable. For most apps: use auto-increment internally, UUID externally for API exposure.

What's the difference between UUID and GUID?

Functionally the same. GUID (Globally Unique Identifier) is Microsoft's term from COM/DCOM; UUID is the IETF standard. The format is identical (128 bits, hex with dashes). Most modern platforms accept either interchangeably. Microsoft's GUIDs use a different byte ordering in the first 3 groups but libraries normalize this transparently.

Can I generate UUIDs without a library?

For UUID v4 you can use crypto.randomUUID() in modern browsers and Node.js, or crypto.getRandomValues() for older environments. For v7 you'll need a library (uuid npm package) since v7 includes a millisecond timestamp. For v1 (don't use it), you'd need access to the MAC address via OS-specific APIs.

← Back to Blog
Copied!