Guide

Choosing a UUID Version: v4 versus v7

When to use random UUID v4 and when a time-ordered UUID v7 is the better identifier.

A UUID is a 128-bit identifier. Version 4 fills nearly all of those bits with random data; version 7 starts with a Unix millisecond timestamp and fills the rest with random data. The choice is about ordering and index locality, not secrecy.

01How to choose

  • Need an opaque identifier with no meaningful order: version 4.
  • Need roughly time-sortable keys that index well: version 7.
  • Need a deterministic identifier derived from a name: version 5 (SHA-1) or version 3 (MD5) with a namespace.
  • Never use a UUID as a secret or as a password.

02Limitations

Uniqueness is probabilistic. A collision is extraordinarily unlikely at ordinary volumes, but the assurance comes from the generator's entropy, not from the shape of the identifier.

Frequently asked questions

Should primary keys be UUID v4 or v7?
v7 if you want inserts to stay near the end of a B-tree index; v4 if ordering is irrelevant and you want maximum opacity.

Sources

  1. RFC 9562: Universally Unique IDentifiers (UUIDs) — IETF (accessed 2026-09-27)