UUID Internals, How It Works
Structure of a UUID
A UUID is 128 bits, conventionally formatted as xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx where M identifies the version (1–8) and the top bits of N identify the variant (RFC 9562 uses binary 10).
UUID v1
60-bit 100-ns timestamp since 1582-10-15 + 14-bit clock sequence + 48-bit node ID (often MAC). Leaks host and time.
UUID v3
MD5 of (namespace UUID || name). Deterministic, same inputs always give the same UUID.
UUID v4
122 bits of cryptographic randomness. Fully unordered. The default opaque identifier.
UUID v5
SHA-1 of (namespace UUID || name). Same deterministic behaviour as v3 but stronger hash.
UUID v6
Reorders v1 so the timestamp is in big-endian order, making the UUID sortable.
UUID v7
48-bit Unix ms timestamp + 74 bits random. Time-ordered, RFC 9562 native, ideal for primary keys.
UUID v8
122 bits of user-defined data. Use when you need a custom layout that still claims a UUID slot.
Best practices
- Consider v7 for primary keys: time ordering can improve index locality, but it does not eliminate page splits.
- Use v4 for public IDs, no information leakage.
- Store as native UUID / BINARY(16), never as VARCHAR(36).
- Always use a cryptographically secure RNG (
crypto.getRandomValues,SecureRandom). - Pair UUIDs with a monotonically increasing sequence only if you need strict ordering.
Common mistakes
- Using UUID v1 in public APIs, it leaks the host MAC address.
- Storing UUIDs as VARCHAR(36) instead of
uuid/BINARY(16), 2.25× the storage. - Picking UUID v4 as a clustered primary key on a write-heavy table: random order scatters inserts across the B-tree, which can increase page splits and cache misses.
- Generating UUIDs with
Math.random(), not cryptographically secure. - Exposing v1 / v6 / v7 IDs externally without considering the embedded timestamp.
Why a UUID fails validation
Most "invalid UUID" errors come from formatting, not from a bad value. This tool's validator accepts the canonical form, UPPERCASE, {braced} GUIDs from .NET and SQL Server, urn:uuid: prefixes and 32-character hex without dashes, then reports the canonical lowercase form. A value is rejected when it has the wrong length, non-hex characters, or a variant nibble (the first character of the fourth group) outside 8, 9, a or b. The variant check catches strings that merely look like UUIDs.
| Input | Result |
|---|---|
{01890B3D-4F6F-7C34-A8D1-2B3C4D5E6F70} | Valid v7, normalised to lowercase without braces |
urn:uuid:550e8400-e29b-41d4-a716-446655440000 | Valid v4 |
00000000-0000-0000-0000-000000000000 | Nil UUID (RFC 9562 section 5.9), a placeholder rather than an ID |
550e8400-e29b-41d4-c716-446655440000 | Invalid: variant nibble c is not the RFC 9562 variant |
Keeping UUID v7 in order inside one millisecond
A v7 UUID only stores milliseconds, so a naive generator that fills the rest with random bits can emit IDs out of order when several are created in the same millisecond. RFC 9562 section 6.2 describes fixes. This tool uses method 1: the 12-bit rand_a field acts as a counter that increases for each ID in the same millisecond, starting from a random low value. Bulk-generated v7 lists therefore come out already sorted. If the counter would overflow, the generator moves to the next millisecond. Ordering holds within one generator; IDs from different machines still interleave by clock.
Key takeaways
- UUID v7 is a time-ordered option for database primary keys, defined in RFC 9562.
- UUID v4 remains correct for opaque external identifiers.
- ULID and v7 are functionally equivalent in ordering guarantees; choose ULID for shorter string form, v7 for native DB UUID type.
- Snowflake wins on raw size (64-bit) but requires worker-id coordination.
- NanoID and CUID2 are for URLs and anti-enumeration, not for high-throughput databases.
- For independent uniform UUID v4 generation, collision probability reaches about 50% at 2.71 × 10¹⁸ IDs. Enforce uniqueness and handle collisions.