Home › Guides › UUID v4 vs. v7
UUID v4 vs. UUID v7: Which Should You Use?
A UUID is a 128-bit identifier you can generate anywhere, without asking a central database for the next number. Version 4 is pure randomness and has been the default for years. Version 7, standardized in RFC 9562 in 2024, starts with a timestamp, so new IDs sort in creation order. That one change makes v7 much friendlier to database indexes. This guide shows how both are built, when to pick each, and what to watch out for. You can generate either kind in bulk with UUID Forge.
Reading a UUID
2a740d41-d233-4c27-b459-f79f2bcfe9e8
^ ^
| variant (8, 9, a or b)
version
A UUID is 32 hex digits in groups of 8-4-4-4-12. The first digit of the third group is the version (4 here). The first digit of the fourth group holds the variant, which is 8, 9, a or b for standard UUIDs. Everything else depends on the version.
UUID v4: random
Apart from the 6 version and variant bits, all 122 bits are random. That's enough that collisions are not a practical concern: you'd need to generate about 2.7 × 1018 v4 UUIDs before the chance of even one duplicate reached 50%, as long as the generator uses a cryptographically secure random source (crypto.randomUUID() in browsers and Node.js, uuid.uuid4() in Python). Math.random()-based generators are not safe for this.
UUID v7: timestamp first
019b76da-a800-7f2f-8e08-c68156c03158
└───────────┘ └ version 7
48-bit Unix time in milliseconds
The first 48 bits are the Unix time in milliseconds. In this example, 019b76daa800 in hex is 1767225600000, which is January 1, 2026 at 00:00:00 UTC. After the version and variant bits, the remaining 74 bits are random, or partly a counter that keeps IDs made in the same millisecond in order. Because the timestamp comes first, v7 UUIDs sort by creation time as plain strings or bytes.
Why v7 is better for database keys
Most databases store primary keys in a B-tree index, which works best when new keys arrive at the end. Random v4 keys land at random places in the index, so inserts touch pages all over it, cause page splits, and keep more of the index in memory. With v7, each new key is larger than the last, so inserts append at the end, like an auto-increment integer. On large tables this means faster inserts and smaller indexes. You also get a rough "created at" for free, and sorting by ID gives creation order.
Which one to use
| Situation | Pick |
|---|---|
| Primary keys in PostgreSQL, MySQL, SQL Server and similar | v7 |
| Event, log or message IDs you want to sort by time | v7 |
| Public IDs where revealing the creation time is a problem | v4 |
| Tokens, secrets, password-reset links | Neither: use a dedicated random token (for example 32 random bytes) |
| An existing system that already uses v4 | Keep v4; both fit the same 36-character format and column type |
Things to watch out for
- v7 reveals when it was made. Anyone with the ID can read the timestamp to the millisecond. That's usually fine for internal keys, less so for something like a user ID shown publicly.
- UUIDs aren't secrets. Even v4 shouldn't be the only thing protecting a private URL or a password reset. Use a token designed for that, with an expiry.
- Store them compactly. As text a UUID takes 36 bytes; as binary it takes 16. PostgreSQL has a native
uuidtype; in MySQL,BINARY(16)is common. - Clock problems. v7 depends on the system clock. Good libraries handle a clock that jumps backwards by keeping IDs increasing, but check what yours does if strict ordering matters.
- Older versions. v1 includes the machine's MAC address and a timestamp in a byte order that doesn't sort well; v3 and v5 are hashes of a name, so the same input always gives the same UUID. RFC 9562 recommends v7 over v1 for new systems.
Related tools
- UUID Forge: bulk v4 and v7 UUIDs, ready to copy or export.
- UnixTime: turn the millisecond timestamp inside a v7 UUID into a date.
- Base Shift: convert the hex timestamp to decimal.