Anatomy of a UUID
A UUID is a 128-bit number, conventionally written as 32 hexadecimal digits split into five groups by hyphens: 8-4-4-4-12, for example 3fa85f64-5717-4562-b3fc-2c963f66afa6. The third group's leading digit marks the version (4 here means version 4 — fully random), and it's designed so that IDs generated independently, on different machines, with no coordination between them, still won't collide.
What version 4 means
UUIDs come in several versions with different generation strategies — version 1 encodes a timestamp and network identifier, version 5 is a deterministic hash of a name, and version 4 is simply 122 bits of cryptographically random data (6 bits are reserved to mark the version and variant). Version 4 is the default choice for most software because it needs no coordination, no clock, and no input — just randomness.
Common uses
- Database primary keys that need to be generated client-side, before an insert
- Idempotency keys for API requests, so a retried request isn't processed twice
- Session tokens, request IDs for logging/tracing, and test fixture data