UUID v4 vs UUID v7: Which Should You Use?
By SimpleTools24Published 4 min read
Use UUID v7 for database primary keys and anything stored in a sorted index, because its leading timestamp makes new IDs land near the end of the index, avoiding the fragmentation random keys cause. Use UUID v4 when IDs are exposed publicly and you don't want them to reveal when they were created, or when you simply need a random identifier and ordering doesn't matter. Both are 128-bit standard UUIDs, fit the same column types and are effectively collision-free.
In this guide
The Core Difference
v4: 9b2c1f7e-5d3a-4c8b-a1e2-7f6d0c9b3a54 random everywhere except version/variant
v7: 0192f0c1-6a4b-7c3d-9e2f-0a1b2c3d4e5f first 48 bits = Unix time in msWhat is UUID v4 and what is UUID v7 explain each layout.
| UUID v4 | UUID v7 | |
|---|---|---|
| Content | 122 random bits | 48-bit timestamp + 74 random/counter bits |
| Sort order | Random | Roughly by creation time |
| Index inserts | Scattered across the index | Near the end of the index |
| Reveals creation time | No | Yes, to the millisecond |
| Collision risk | Negligible | Negligible |
| Standard | RFC 4122, now RFC 9562 | RFC 9562 (2024) |
| Native support | Almost everywhere | Growing quickly |
Database Performance
Most relational databases store primary keys in B-tree indexes. With v4:
- Each insert goes to a random page, so many pages must stay in memory.
- Pages split frequently, leaving them partly empty.
- On large, write-heavy tables, this shows up as more I/O and bigger indexes.
With v7, inserts behave much like an auto-increment integer: they append near the end, keeping the hot part of the index small. Range queries on recent records can also benefit.
For small tables, the difference is negligible. It grows with table size and write rate. SQL Server has a special consideration: its uniqueidentifier sort order compares the last bytes first, so text-ordered v7 values don't automatically sort chronologically there; see GUID as a primary key.
Privacy and Information Leakage
A v7 ID reveals roughly when it was created. Consequences to consider:
- A public order or user ID can show when an account was created or how activity is distributed.
- IDs created close together share a prefix, which makes the time relationship between records visible.
v4 reveals nothing. A common pattern uses v7 for internal primary keys and v4 (or a separate random token) for anything in public URLs.
Uniqueness
Both are unique for practical purposes. v4 has 122 random bits. v7 has 74 random or counter bits per millisecond, separated by the timestamp, which is still far beyond what any realistic system needs. Implementation quality matters more than the version: use your platform's generator. UUID collision probability covers the maths.
Ordering Caveats
v7 is sortable, but not a perfect sequence. Different machines have slightly different clocks, values from the same millisecond can interleave across generators, and clocks can move backwards. Use a timestamp column or a sequence when exact order matters.
Migration and Compatibility
Switching new IDs from v4 to v7 is usually safe: both are valid UUIDs, so existing columns, validators and APIs accept them. Existing v4 rows stay random; only new rows gain the ordering benefit. Check that any code which parses UUIDs doesn't reject versions other than 4; a strict v4-only regex is a common snag. UUID regex patterns includes version-agnostic options.
Recommendations
- New database primary keys: v7.
- Public, shareable IDs where timing is sensitive: v4.
- Correlation and request IDs: either; v7 makes logs easier to scan by time.
- Existing systems on v4: switch new IDs to v7 if index performance is a problem; otherwise no urgency.
Generating Both
The UUID v7 generator and the UUID v4 generator run in your browser and create up to 1,000 values at a time. Generating a batch of each is a quick way to see the difference: the v7 batch shares an increasing prefix, while the v4 batch is scattered. Paste any v7 value into the UUID decoder to read its timestamp.
A Practical Rollout Plan
If you decide to move from v4 to v7:
- Audit validation for v4-only regexes or checks and relax them to accept any standard version.
- Switch generation in one place, ideally a shared ID helper, so every service changes together.
- Monitor index size and insert latency on your largest tables before and after the change.
- Document that IDs now encode creation time, so nobody exposes them where that matters.
- Leave existing data alone; mixed v4 and v7 values coexist without problems.
When to Stay on v4
Staying on v4 is perfectly reasonable when tables are small, writes are infrequent, IDs are mostly public, or your database's clustered key is something else anyway. The benefits of v7 are real but situational; measure before migrating a large system.
Frequently Asked Questions
Is UUID v7 better than v4?
For database keys, usually yes. For public IDs where creation time should stay private, v4 is better.
Can I mix v4 and v7 in the same column?
Yes. Both are standard UUIDs.
Is UUID v7 slower to generate?
No meaningful difference for application use.
Does PostgreSQL support UUID v7?
PostgreSQL 18 added uuidv7(). Earlier versions can store v7 values generated by the application. See UUID v7 in PostgreSQL.
Summary
v7 for index-friendly keys, v4 for private, random identifiers. Both are standard, unique and interchangeable in storage, so the choice is about ordering and privacy.
Related tools
Sources
- RFC 9562 – Universally Unique IDentifiers (UUIDs) – IETF (opens in a new tab)
Related articles
- What Is UUID v7?The layout of a version 7 UUID, how its timestamp makes IDs sortable, and the limits of that ordering.
- What Is UUID v4?The structure of a version 4 UUID, its 122 random bits, how to spot one and when it's the right choice.
- GUID as a Primary KeyThe real trade-offs of GUID keys: fragmentation, size, merging and security, and how sequential GUIDs and UUID v7 help.