Developer Tools

UUID v1 vs v4 vs v5: Which Version Should You Actually Use?

6 min
By
UUID v1 vs v4 vs v5: Which Version Should You Actually Use?

Photo by Pexels

A junior engineer asked me last week which UUID version to use for their new microservice. I gave the wrong answer on instinct, then corrected myself. The version you pick actually matters.

UUIDs look like one thing, but they are not. The string 550e8400-e29b-41d4-a716-446655440000 follows a format defined in RFC 4122, and the digit after the second hyphen tells you which algorithm produced it. That single digit changes everything about how the identifier behaves.

What Each Version Actually Does

**Version 1** is time-based. It combines the current timestamp (measured in 100-nanosecond intervals since October 15, 1582) with the MAC address of the generating machine. The result is a UUID that sorts chronologically if you store enough of them. Example: 63b4c2a0-1f3e-11ec-9f3a-3c8e2d5a1b7f — that 1 after the second hyphen marks it as v1.

**Version 4** is pure randomness. 122 bits of the 128 come from a cryptographically secure random source. Example: f47ac10b-58cc-4372-a567-0e02b2c3d479. This is what most online generators produce by default, including our UUID generator.

**Version 5** is namespace-plus-name, hashed with SHA-1. You feed it a namespace UUID and a name string, and it always returns the same UUID for the same inputs. Example: 74738ff5-5367-5dab-9e9f-3a2b1c8e4d6a (namespace dns + name example.com). The v5 generator is deterministic by design.

When v1 Makes Sense

Use v1 when you need chronological ordering and you control the generating machines. Database primary keys in time-series workloads. Event logs. Audit trails where you want rows to cluster by insertion time on disk.

The MAC address leak is the tradeoff. A v1 UUID exposes the network card that made it. That is fine inside a data center. It is not fine in a public-facing token. Also, two machines generating v1 UUIDs at the same instant will collide unless their clock sequences and node IDs differ — the spec handles this, but implementations occasionally get it wrong.

When v4 Is The Default Choice

Most of the time. v4 is what you want for: - API keys and session tokens - Public-facing identifiers in URLs - Anywhere you do not need sort order - Anywhere you do not need determinism

The randomness makes v4 UUIDs unguessable and evenly distributed across the keyspace. The downside: they do not sort, so they fragment indexes if you use them as a clustered primary key in something like SQL Server or InnoDB.

When v5 Is The Right Tool

v5 shines when you need the same name to produce the same UUID across systems, without coordination. Think: - Generating a stable ID for a DNS name, a URL, or an email address - Deduplicating entities across disconnected databases - Content-addressed storage where the ID must be reproducible

A namespace uuid generator workflow looks like this: pick a namespace UUID (often a well-known one like 6ba7b810-9dad-11d1-80b4-00c04fd430c8 for DNS), append your name, hash. Every system that does the same gets the same UUID. No central authority needed.

A Quick Comparison Table

| Version | Source | Deterministic? | Sortable? | Collision Risk | |---------|--------|----------------|-----------|----------------| | v1 | Time + MAC | No | Yes (mostly) | Low with correct clock sync | | v4 | Random | No | No | Negligible (2^-122 per pair) | | v5 | SHA-1(namespace+name) | Yes | No | None for same inputs |

What About v2 and v3?

v2 is a DCE Security variant rarely seen outside legacy systems. v3 is like v5 but uses MD5 instead of SHA-1. Use v5 instead of v3. MD5 is dead for new code.

The Decision In One Sentence

If you need ordering, use v1. If you need determinism across systems, use v5. Otherwise use v4 — and generate it with a tool you trust, like the one at /uuid-generator.

For a deeper look at how v4 UUIDs perform as database keys, see our post on UUIDs as database primary keys. For bulk generation patterns, read generate UUIDs in bulk for database seeds.

Frequently Asked Questions

Which UUID version is best for a database primary key?

v4 for most cases. v1 if you need chronological clustering and control the generating machines. Avoid v5 for primary keys — it is deterministic, which means duplicate names produce duplicate keys.

Is UUID v5 the same as v3?

No. Both are namespace-plus-name UUIDs, but v3 uses MD5 and v5 uses SHA-1. Use v5 for new code. MD5 is considered broken for collision resistance.

Can I convert between UUID versions?

No. Each version uses a different algorithm. You cannot turn a v4 UUID into a v1 UUID. You generate a new one of the version you need.

Try NovelCrypt Tools

Experience military-grade encryption for your sensitive data. Create self-destructing messages, encrypt files, or explore our experimental lab tools.

Explore NovelCrypt