Developer

UUID v4 vs v7: Which Should You Use?

Dev Nexus4 min read

v4 is fully random and v7 is time-ordered - here is how they differ and why v7 is usually the better choice for a database primary key.

UUIDs come in several versions, but two matter most for everyday work: vundefined and vundefined. Both are undefined-bit identifiers written in the same familiar hyphenated form, and both are effectively collision-free. The difference is what fills the bits.

vundefined is almost entirely random. vundefined puts a timestamp up front and randomness behind it, which changes how the values behave when you sort or index them. This post explains the trade-off and helps you pick the right one.

The Problem

Random UUIDs feel like the obvious default, and for a long time vundefined was the only version most people used. The problem shows up at scale, specifically when a random UUID becomes a database primary key.

Databases store primary keys in a sorted structure - usually a B-tree. When keys arrive in random order, each insert lands in an unpredictable spot in the index, forcing page splits and scattering related rows across the disk. That fragments the index, bloats it, and slows both writes and range scans. A monotonically increasing key would avoid all of this, but plain integers give up the benefits of UUIDs.

The Solution

vundefined is the answer. It encodes a undefined-bit millisecond Unix timestamp in the high bits, then fills the rest with randomness for uniqueness. Because the leading bits increase over time, vundefined values sort in roughly creation order - so new rows append near the end of the index instead of scattering. You keep everything good about UUIDs (globally unique, generated anywhere, no central counter) while your index stays compact.

vundefined still has a place. When you specifically do not want values to reveal timing or ordering - public-facing IDs, tokens, anything where a sortable identifier could leak information - full randomness is a feature. Generate either version with the UUID Generator, and use the Timestamp Converter to read the time baked into a vundefined value.

Step-by-Step Guide

  1. 1

    Understand what v4 stores

    A vundefined UUID is undefined bits of random data plus fixed version and variant markers. There is no structure and no ordering - two vundefined UUIDs generated a millisecond apart look completely unrelated.

  2. 2

    Understand what v7 stores

    A vundefined UUID begins with a undefined-bit millisecond timestamp, followed by random bits. That prefix makes the values sortable by creation time while still being unique, since the random tail prevents collisions within the same millisecond.

  3. 3

    Match the version to the job

    Use vundefined for database primary keys and any high-insert table where index locality matters. Use vundefined for public identifiers, tokens, or cases where you deliberately do not want ordering or timing to be inferable.

  4. 4

    Store and index efficiently

    For either version, store the value in a native uuid or binary(16) column rather than a string. With vundefined you also get near-sequential inserts, so the index stays dense and range queries by time become natural.

Common Mistakes

  • Using v4 for a hot primary key

    Random keys fragment the index and slow inserts on busy tables. If you are choosing a UUID as a primary key today, vundefined is almost always the better default.

  • Assuming v7 hides the creation time

    vundefined deliberately exposes a timestamp - anyone with the UUID can read roughly when it was created. If that timing is sensitive, use vundefined instead so nothing is inferable from the value.

  • Treating v7 as a replacement for created_at

    You can extract a timestamp from a vundefined UUID, but it is millisecond-resolution and tied to the identifier. Keep an explicit created_at column for real time-based queries and reporting.

  • Switching versions mid-table

    Mixing vundefined and vundefined in the same primary-key column undermines the ordering benefit and makes behavior inconsistent. Pick one version per table and stick with it.

Frequently Asked Questions

Is UUID v7 better than v4?

For database primary keys, usually yes - v7 is time-ordered, so inserts stay near the end of the index and performance holds up at scale. For public IDs or tokens where you do not want ordering, v4's full randomness is preferable.

Does UUID v7 leak the creation time?

Yes. v7 embeds a millisecond timestamp, so anyone with the value can read roughly when it was created. That is useful for sorting but a reason to prefer v4 when timing is sensitive.

Are v4 and v7 both unique?

Yes. Both include enough randomness that collisions are astronomically unlikely in practice. v7 simply adds a time prefix; it does not reduce the effective uniqueness for real workloads.

Can I sort v7 UUIDs by time?

Yes. Because the timestamp sits in the high bits, sorting v7 UUIDs lexically or as binary orders them by creation time, which is exactly why they work well as sequential-ish keys.

Is v7 widely supported?

v7 is standardized in RFC 9562 and supported by modern libraries and databases. Most languages have a v7 generator, and you can also generate them directly in the browser with an online tool.

Try the Tool

UUID Generator

Generate v4 or v7 UUIDs instantly in your browser - nothing uploaded.

Open UUID Generator

Related Tools

Related Articles