UUID v4 vs v7: Random or Time-Ordered, Which to Pick
UUID v4 is 122 random bits; UUID v7 starts with a millisecond timestamp so IDs sort by creation time. See how each affects database indexes and which to use.
Updated
UUID v4 and UUID v7 are both 128-bit identifiers in the same 36-character format. Version 4 is entirely random, so consecutive IDs have no relationship to each other. Version 7 puts a millisecond timestamp in the first 48 bits and fills the rest with random data, so IDs sort in the order they were created. Use v7 for database keys in large tables, and v4 when an ID must reveal nothing about when it was made.
How each is laid out
A version 4 UUID such as f47ac10b-58cc-4372-a567-0e02b2c3d479 is random apart from two markers: the 4 that opens the third group, and the first digit of the fourth group, which is always 8, 9, a or b. That leaves 122 random bits.
A version 7 UUID such as 01a1e960-d600-7c3a-9f2b-4d5e6f708192 has three parts:
- The first 48 bits, which are the first 12 hex digits
01a1e960d600, hold the Unix time in milliseconds. - The version digit
7and the variant bits sit in the same positions as in every UUID. - The remaining 74 bits are random.
In the example, 01a1e960d600 in decimal is 1794916800000, which is 12:00:00 UTC on November 17, 2026. Forty-eight bits of milliseconds will not run out until roughly the year 10889.
Both versions are defined in RFC 9562, published in May 2024, which replaced RFC 4122. Version 4 dates back to the older document; versions 6, 7 and 8 are new in RFC 9562. For the general structure and the other versions, see what a UUID is.
Side by side
| Property | UUID v4 | UUID v7 |
|---|---|---|
| Random bits | 122 | 74 |
| Timestamp | None | 48-bit Unix milliseconds |
| Sorts by creation time | No | Yes, to the millisecond |
| Reveals creation time | No | Yes |
| Index insert pattern | Random positions | Appended at the end |
| Standardized in | RFC 4122, kept in RFC 9562 | RFC 9562 |
| Built-in support | Nearly universal | Growing |
Why ordering matters to a database
Most relational databases index primary keys with a B-tree, which keeps keys in sorted order across fixed-size pages. Where a new key lands depends on its value.
With an auto-increment integer, every new key is larger than the last, so inserts go to the rightmost page. That page stays in memory, fills up, and a new one is started. It is the cheapest possible pattern.
With version 4, each new key belongs at a random spot in the index. On a large table that has three effects:
- Page splits. The target page is often full, so the database splits it in two, leaving both partly empty. The index grows larger than the data requires.
- Cache misses. Any page is equally likely to be needed next, so once the index outgrows memory, inserts start waiting on disk reads.
- More write volume. Scattered changes touch more pages, which increases logging and replication work.
In engines that store the table itself in primary key order, such as MySQL's InnoDB and SQL Server's default clustered index, the cost is higher because the row data is shuffled along with the key.
Version 7 restores the append pattern. New keys share a recent timestamp prefix and land at the right-hand edge of the index, like integers, while remaining globally unique without a central counter. The size of the gain depends on table size, hardware and workload, so measure rather than assume a figure, but the direction is consistent: the larger the table relative to memory, the more it matters.
Ordering has a second benefit. Sorting by ID approximates sorting by creation time, so "latest 50 rows" can use the primary key index, and a range of IDs corresponds to a range of time.
What v7 costs you
- The creation time is public. Anyone holding the ID can decode when the record was made. That is harmless for an internal order row and less so for a public user ID if signup time is sensitive, or where it lets a competitor estimate activity.
- Less randomness. 74 bits is still a vast space: a collision needs two IDs in the same millisecond that also match on all 74 random bits. But it is fewer than 122, and a v7 value is partly predictable. It is not a substitute for a secret token.
- Order within a millisecond is not guaranteed by default. Two IDs created in the same millisecond may sort either way unless the generator uses one of the RFC's optional monotonic methods, such as a counter in the bits after the timestamp. Many libraries do; check yours if strict ordering matters.
- Hot spots in some distributed databases. Systems that split data by key range can funnel every insert to the same node when keys are sequential. Those systems often recommend random keys, which is a case where v4 wins.
Which to pick
Pick v7 when:
- The ID is a primary key or indexed column in a table that will grow large.
- You want rows to sort by creation without a separate indexed timestamp.
- IDs are generated across many services and still need to interleave in time order.
Pick v4 when:
- The ID appears in public URLs or is handed to third parties and should carry no information.
- You are using a range-sharded store that suffers from sequential keys.
- You need the widest support with no extra library.
- The table is small, in which case index locality will not be noticeable.
Do not mix the decision up with security. Neither version should be the only thing protecting a resource; use proper authorization, and generate real secrets with a cryptographic random source.
Generating each version
Version 4 is built in almost everywhere: crypto.randomUUID() in browsers and Node.js, uuid.uuid4() in Python, gen_random_uuid() in PostgreSQL, UUID.randomUUID() in Java, Guid.NewGuid() in .NET.
Version 7 support is newer. PostgreSQL 18 added a uuidv7() function, Python 3.14 added uuid.uuid7(), and .NET 9 added Guid.CreateVersion7(). On older runtimes, and in JavaScript, use a maintained library such as the uuid package on npm. Since both versions are ordinary UUIDs, they fit in the same column type, so a table can switch from v4 to v7 for new rows without a migration.
The UUID Generator creates version 4 UUIDs, up to 100 at a time, with uppercase and no-hyphen options. It does not produce version 7, so generate those in code with one of the functions above.
To read the time out of a v7 value by hand, strip the hyphens, convert the first 12 hex digits to decimal, and paste the result into the Unix timestamp converter, which reads a 13-digit value as milliseconds. Unix timestamps explained covers the seconds versus milliseconds distinction.
Try it
Generate a batch in the UUID Generator and sort the list: the order is arbitrary, which is exactly the property that scatters v4 keys across an index. Then compare with a few v7 values from your own runtime, where the first 12 digits barely change from one ID to the next.