What Is a UUID?
A UUID (Universally Unique Identifier) is a standardized 128-bit label formatted as 32 hexadecimal digits in five groups separated by hyphens — xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Defined by RFC 9562 (formerly RFC 4122), UUIDs allow systems to create globally unique identifiers without coordinating with a central registry. The mathematical probability of generating a duplicate UUID v4 is approximately 1 in 2122 — making collisions virtually impossible even across billions of records.
UUIDs are the backbone of modern software architecture. From relational databases like PostgreSQL and MySQL to NoSQL stores like MongoDB and DynamoDB, developers rely on UUIDs as primary keys to ensure data integrity across distributed nodes. Unlike auto-incrementing integers, UUIDs can be generated on any client or server independently, which makes them ideal for offline-first apps, event-driven architectures, and multi-tenant SaaS platforms.
UUID Version Comparison
| Version | Generation Method | Sortable | Best For |
|---|---|---|---|
| v1 | Timestamp + MAC address | Partially | Legacy systems, audit trails |
| v4 | Cryptographically random | No | API keys, session tokens, general-purpose IDs |
| v7 | Unix timestamp (ms) + random | Yes | Database primary keys, time-series data |
| Nil | All zeros (constant) | N/A | Placeholder, default “empty” value |
| GUID | v4 wrapped in curly braces | No | Windows, .NET, COM interoperability |
When Should You Use UUID v4 vs v7?
UUID v4 remains the most widely adopted version because of its simplicity — every bit (aside from version and variant fields) is randomly generated. If your application does not require chronological ordering of identifiers, v4 is the safest default. It is the go-to choice for REST API idempotency keys, OAuth token identifiers, anonymous analytics tracking IDs, and any scenario where unpredictability is more important than order.
UUID v7, introduced in the latest RFC 9562 specification, embeds a millisecond-precision Unix timestamp in the most significant bits. This makes v7 UUIDs naturally sortable by creation time — a significant advantage for B-tree indexed database columns. In benchmarks, inserting v7 UUIDs into PostgreSQL or MySQL indexes is up to 40% faster than v4, because sequential keys reduce page splits and index fragmentation. Choose v7 when you need database primary keys, event logs with time-based ordering, or distributed message queues where arrival order matters.
Common UUID Use Cases
Database Primary Keys
Replace auto-increment integers with UUIDs to safely merge tables across shards, replicas, or microservices without ID conflicts.
API Request Tracing
Attach a UUID to every HTTP request as a correlation ID, enabling end-to-end distributed tracing across services and log aggregators.
File & Asset Naming
Generate collision-free filenames for cloud storage uploads (S3, GCS, Azure Blob) — eliminating overwrite risks in concurrent environments.
Session & Token Management
Create opaque, unguessable session identifiers and CSRF tokens that cannot be enumerated or predicted by attackers.
Event Sourcing & CQRS
Assign unique event IDs in event-driven architectures to guarantee exactly-once processing and reliable replay.
IoT Device Registration
Provision unique device identifiers at manufacturing time — no central registry needed, even for millions of devices.