Skip to content
SimpleTools24

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.

The Core Difference

text
v4: 9b2c1f7e-5d3a-4c8b-a1e2-7f6d0c9b3a54   random everywhere except version/variant
v7: 0192f0c1-6a4b-7c3d-9e2f-0a1b2c3d4e5f   first 48 bits = Unix time in ms

What is UUID v4 and what is UUID v7 explain each layout.

UUID v4 and UUID v7 compared
UUID v4UUID v7
Content122 random bits48-bit timestamp + 74 random/counter bits
Sort orderRandomRoughly by creation time
Index insertsScattered across the indexNear the end of the index
Reveals creation timeNoYes, to the millisecond
Collision riskNegligibleNegligible
StandardRFC 4122, now RFC 9562RFC 9562 (2024)
Native supportAlmost everywhereGrowing 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:

  1. Audit validation for v4-only regexes or checks and relax them to accept any standard version.
  2. Switch generation in one place, ideally a shared ID helper, so every service changes together.
  3. Monitor index size and insert latency on your largest tables before and after the change.
  4. Document that IDs now encode creation time, so nobody exposes them where that matters.
  5. 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.

Sources