Short answer: use v4 for everyday unique IDs and v7 for database primary keys. A v4 UUID is 122 bits of randomness with no order at all; a v7 UUID starts with a 48-bit Unix timestamp in milliseconds, so IDs sort by creation time. Both are standardized in RFC 9562 (2024), and you can tell them apart by the 13th hex digit — a 4 means v4, a 7 means v7.
What is inside each version
A v4 UUID fills 122 of its 128 bits with random data from a cryptographic generator. There is no timestamp, no machine identifier, nothing to read — that is the point. A v7 UUID spends its first 48 bits on the current Unix time in milliseconds, then fills the remaining 74 bits with randomness. Collisions are a non-issue for both: v4 has about 5.3 × 10³⁶ possible values, and v7 has about 1.9 × 10²² unique values per millisecond on top of the timestamp separating every millisecond.
The anatomy is worth one look, because it explains the version digit everyone quotes. A UUID is written as five groups — 8-4-4-4-12 hex digits. The version lives in the first digit of the third group, and the variant in the first digit of the fourth. In a v7, the entire first group and half of the second are the timestamp, which is why sorting v7 strings alphabetically sorts them chronologically: the time is literally the prefix. In a v4, those same positions are random, which is why v4 strings sort into noise. Same container, opposite philosophies — one spends its most significant bits on when, the other refuses to say anything at all.
The database problem v7 solves
PostgreSQL, MySQL, and most relational databases index primary keys with B-trees, which love sequential inserts: each new row appends to the end of the index. A v4 primary key lands at a random position on every insert, causing page splits and a fragmented index that degrades write throughput and cache locality at scale. A v7 primary key starts with a timestamp, so new IDs are monotonically increasing and inserts append cleanly — the write pattern databases want, with no central coordinator the way auto-increment needs.
Two practical corollaries follow. First, storage: a UUID stored as a 36-character string costs 36+ bytes per row and per index entry, while stored as a native UUID or binary(16) it costs 16 — at a hundred million rows, that difference is gigabytes, and it also makes comparisons faster. Every major database has a native UUID type or an accepted binary pattern; use it instead of varchar. Second, v7's ordering only helps if the database actually compares the raw bytes in order — store it as text in a case-mixed or non-standard format and you can quietly throw the benefit away.
Generate one in JavaScript
v4 is built into every modern browser and Node.js — no library needed. v7 is newer, so it still needs a library:
// v4 — built in, no install
crypto.randomUUID(); // "3b241101-e2bb-4d53-af18-1c9a2d80a910"
// v7 — needs the uuid package (v11+)
import { v7 as uuidv7 } from "uuid";
uuidv7(); // "018f3f5e-1c2d-7a9b-8f10-3c4d5e6f7a8b"What about v1, v5, and ULIDs
The family is bigger than the headline pair. v1 also embeds a timestamp — but pairs it with the generating machine's MAC address, which leaks hardware identity; v7 exists largely to give v1's time-ordering without that leak. v5 is deterministic: the same namespace and name always hash to the same UUID, which is the right tool for deriving stable IDs from natural keys. And outside the UUID standard entirely, ULIDs and similar formats offer the same time-ordered idea in a more compact, case-insensitive encoding — worth knowing about, though v7's standardisation means databases and libraries increasingly speak it natively. For completeness: if you are generating identifiers people will type or read aloud, none of these are friendly, and a shorter random code is the better tool — the same generator family that makes strong passphrases is the right neighbourhood to look in.
Common mistakes and how to fix them
- Storing UUIDs as varchar(36) in a busy table. Cause: the string form is what code prints, so it becomes the column type. Fix: native UUID or binary(16) storage — smaller, faster to compare, and it preserves v7's byte ordering.
- Using a UUID as an unguessable secret. Cause: 'nobody can guess 122 random bits' feels airtight. Fix: uniqueness is not authorisation — tokens need expiry, revocation, and storage as hashes. A leaked UUID in a URL is a capability you cannot take back; design accordingly.
- Sorting v7 as formatted text in the application. Cause: string sort mostly works — until a library emits uppercase or a different grouping. Fix: sort on the stored binary value, or normalise to lowercase canonical form before comparing; the guarantee lives in the bytes.
- Generating v4 with a non-cryptographic random source. Cause: Math.random()-based UUID snippets copied from old tutorials. Fix: use crypto.randomUUID() or a maintained library — predictable 'random' UUIDs collide and leak in ways that only surface at scale.
The traps
- Leaking creation time: a v7 ID embeds a millisecond timestamp, so anyone can read roughly when the resource was created. If that reveals something sensitive, stick with v4.
- Expecting exact ordering: IDs generated in the same millisecond share a timestamp, so v7 gives time grouping, not a strict sequence. Need exact order? Keep an explicit timestamp or sequence column.
- Using UUIDs as secrets: UUIDs are unique, not unguessable-by-design. For access tokens, use a random byte string from a CSPRNG with an expiry and a way to revoke it.
- Migrating existing v4 keys: the v7 win is for new tables. Rewriting the primary keys of a live table is rarely worth the trouble.
The rule of thumb
v4 by default — ad-hoc identifiers, idempotency keys, filenames, anything where order does not matter. v7 for database primary keys and records you want roughly time-ordered without a separate timestamp column. And if you need the same input to always produce the same ID, that is v5, not v4 or v7.
Frequently asked questions
Can two v4 UUIDs ever collide? In the same sense that a shuffled deck can deal the same hand twice: the space is 2^122, and the birthday bound says you would need roughly 2.7 × 10^18 UUIDs — billions per second for a century — before a collision becomes likely. Systems fail from many things; this will not be one of them. Generate freely and do not add uniqueness checks against a central registry 'just in case' — that reintroduces the coordination UUIDs exist to avoid.
Does v7 reveal how many records I create? Indirectly, yes: anyone holding two of your IDs can see the time between them, and a public sequence of IDs reveals your creation rate and total volume over time. Competitors have inferred business metrics from exactly this. It is the same trade as auto-increment IDs, softened — but if record timing or volume is sensitive, that is a real argument for v4 on anything public-facing.
Should API resources use v4 or v7 in URLs? v4, usually. The database benefit of v7 is internal; the URL is a public surface where the timestamp leak lives. A common pattern is both: v7 as the internal primary key, and the same value exposed — or a v4 public identifier alongside — depending on how much the timing matters. Decide per resource, not per habit.
My framework only generates v4 — is that fine? For almost every application, yes. The v7 advantage is a write-scaling optimisation that matters at high insert rates into indexed tables. A small or medium application will never feel v4's index fragmentation; switch when you have the scale to measure it, or when you are starting a table fresh and v7 costs you nothing.
Try it
Generate UUID v4s and secure passwords with the Password & UUID Generator — everything runs locally in your browser.