Skip to the content

Contact usFree tools · No signup · Files stay on your device

Developer Tools

UUID v4 vs v7: which should you use for database keys?

UUID v4 vs v7: which should you use for database keys?

If you are choosing an ID type for a new table, the question comes up fast: UUID v4 or UUID v7? Both are 128-bit identifiers that you can create anywhere without a central counter. The difference is what is inside. A v4 UUID is random. A v7 UUID starts with the time, so new IDs sort after old ones. For a primary key in a database that will grow, that single difference matters.

You can see it for yourself in a minute: open the free UUID Generator, choose "Version 4 (random)" and press Generate, then switch to "Version 7 (time-ordered)" and look at the list again.

How v4 and v7 are built

UUID v4 UUID v7
Layout 122 random bits, plus version and variant markers 48-bit Unix time in milliseconds, then random bits
Looks like 58319c47-2c7c-4aab-905b-a89ba20f0c57 0190f5a8-7c3e-7b2a-9f1d-3c4e5a6b7c8d
Version digit 4 (third group starts with 4) 7 (third group starts with 7)
Sorts by creation time No Yes
Reveals creation time No Yes, to the millisecond
Standard RFC 4122 and RFC 9562 RFC 9562 (2024)

In the generator, the v7 list for one batch looks like this:

01a21924-aec0-7257-9101-8e3573ecedd8
01a21924-aec4-7508-ae3b-4da1ecc1b223
01a21924-aec4-7509-a9cb-84e42a99f356
01a21924-aec4-750a-a597-ea42e10f83ee

All four begin with the same 01a21924-aec, because they were made within a few milliseconds. The last three share the exact same millisecond, aec4, so a small counter in the next group (7508, 7509, 750a) keeps them in order. A v4 list shows no such pattern: each line looks unrelated to the others.

Why this matters for a database

Most databases keep a primary key in an index, a sorted tree of pages. The way new keys arrive decides how much work the database does.

With v7, new keys are always the biggest so far. They are appended at the end of the index. The recently used pages stay in memory, pages fill up neatly, and a range query such as "the latest 50 orders" reads neighbouring rows.

With v4, a new key can land anywhere. The database has to find the right page, which may be out of memory, and when a page is full it splits in two. On a big table with millions of rows, that means more disk reads, more splits and a bigger, less tidy index. For small tables you will not notice. The difference shows up as the table and its index outgrow memory.

This is also why a plain auto-increment number has been the classic key for so long: it is small and ordered. v7 gives you the ordering of a counter with the ability to create IDs on any machine, in any service or even on a phone, without asking the database first.

What v7 gives away

A v7 UUID carries its birth time. Paste one into the inspector on the generator page ("Validate / inspect a UUID") and it shows the timestamp. For example, 0190f5a8-7c3e-7b2a-9f1d-3c4e5a6b7c8d reports "v7 — Unix-time-ordered" and a time of 2024-07-27T19:26:18.174Z.

That is harmless for an internal order number, but think before using it in a public link. If a user can see /invoice/<id>, they can learn when it was created. Likewise, a competitor looking at your order IDs can estimate how many orders you create per day by comparing the timestamps in two IDs. If that matters, use v4 for the public identifier. The tool Timestamp Converter can turn the millisecond number into any time zone.

Which one should you choose?

Use v7 when:

  • The ID is the primary key of a table that will grow large.
  • You insert rows often and sort or page by newest first.
  • Several services or clients create IDs, so you cannot use a database counter.
  • The creation time is not a secret.

Use v4 when:

  • The ID appears in public links and must reveal nothing.
  • The table is small, or an index that fits in memory makes ordering irrelevant.
  • You are sharing IDs with systems that you do not control and you want no time information to leak.
  • You already have a library or database default generating v4 and have no performance problem.

A common pattern is both: an internal v7 primary key for the database, and a separate random public ID (v4) for use in URLs and APIs.

Mistakes to avoid

Treating a UUID as a secret

Neither version is a password. A v4 UUID is hard to guess, but it is an identifier, and IDs get copied into logs, URLs and screenshots. Do not rely on one as an access key. Use a proper random token for anything that grants access.

Storing UUIDs as text

A UUID is 16 bytes of data. Written as text it is 36 characters. If your database has a native UUID type, or a 16-byte binary column, use it: the index is smaller and comparisons are faster. This holds for both versions.

Expecting perfect time order across servers

v7 sorts by the millisecond in the ID, which comes from the machine's clock. IDs from two servers with drifting clocks can sort a little out of real order. For ordering inside one table that is almost always fine, but do not use the ID as a legal timestamp. Store a separate created_at column for that.

Mixing formats

Do not store some UUIDs with braces and others without. Normalise to lowercase with hyphens when you save them. The UUID Validator can validate a whole list and returns each one in normalised form, and the UUID Validator guide explains how.

Try it with your own data

  1. Open UUID Generator.
  2. Generate 1,000 IDs of Version 7, then 1,000 of Version 4, and download each with Download .txt.
  3. Open both in a spreadsheet or text editor and sort them. The v7 list is already in order; the v4 list sorts into a shuffled mess relative to the order you created them.
  4. Paste a v7 ID into the inspector to see its embedded time.

For the full set of controls, formatting options and limits, read the complete UUID Generator guide.

Quick checklist

  • v7 for big, growing tables with ordered inserts.
  • v4 for public IDs that should reveal nothing.
  • Store UUIDs in a native or binary type, not as text.
  • Keep a separate created-at column.
  • Never use a UUID as a secret.

Frequently asked questions

Is UUID v7 better than v4 for a primary key?
For a table that keeps growing, usually yes. Because v7 IDs start with a timestamp, new rows land at the end of the index instead of in random places, which is easier on the database. v4 is still the better choice when the ID must not reveal anything.
Can someone tell when a v7 UUID was created?
Yes, to the millisecond. The first 48 bits are the Unix time, and any UUID inspector can read them. Do not use v7 where the creation time of a record is private.
Are v7 UUIDs unique across servers?
Yes in practice, because the rest of the ID is random. What is not guaranteed is exact order between servers: two machines with different clocks can produce IDs that sort slightly out of real-time order.
Should I use a UUID as a secret token?
No. A UUID is an identifier, not a secret. Use a purpose-built random token for password resets and API keys, and treat UUIDs as public.
Can I mix v4 and v7 in one table?
You can, since both are valid 128-bit UUIDs and both fit the same column. But the v4 rows will not sort by time, so the ordering benefit applies only to the v7 rows.
Found this useful? Share it

Tools in this guide

Main tool

UUID Generator

Create random version 4 or sortable version 7 UUIDs (GUIDs) instantly, format them for your code or database, and inspect any UUID you paste.

Open tool

UUID Validator

Paste one UUID or a whole list to check whether each is valid — with the exact reason when it isn't, plus its version, variant, embedded time.

Open tool

Timestamp Converter

See the current Unix time, turn any epoch timestamp into a human-readable date in any time zone, or convert a date back to seconds and milliseconds.

Open tool

Back to all guides & blog