UUID v4 vs v5
Both are 128-bit unique identifiers, but they are generated differently: v4 is random (collision is practically impossible) and v5 is deterministic (derived from the hash of a namespace + a name, so the same input always yields the same UUID). One prioritizes unpredictability; the other, reproducibility.
| Aspect | UUID v4 | UUID v5 |
|---|---|---|
| How it is generated | Random (CSPRNG) | Hash of namespace+name |
| Same input, same ID | No | Yes |
| Predictable | No | Yes (if you know the input) |
| Typical case | Primary keys, tokens | Dedup, idempotency |
When to use UUID v4
Use v4 for record IDs, primary keys, session tokens: you want them unpredictable and revealing nothing about the content.
When to use UUID v5
Use v5 when you need the same ID from the same data without storing a map (dedup, deriving a resource ID from its URL, idempotency).
In short: v4 for unpredictable uniqueness (the common case); v5 when the ID must be stable and derivable from a known input.