UUID v4 vs v7: Which One to Use
Both are 128-bit identifiers written the same way. The difference is what the bits contain: v4 is almost entirely random, while v7 starts with a timestamp.
Updated
Try it now: UUID Parser. It runs in your browser, with no signup.
How the two versions are built
A UUID is written as 32 hexadecimal digits in the 8-4-4-4-12 layout. The first digit of the third group is the version, and the first digit of the fourth group is 8, 9, a or b for the standard variant (RFC 9562, which replaces RFC 4122).
- Version 4: 122 random bits, with 6 bits fixed for the version and variant. Nothing in it relates to time.
- Version 7: a 48-bit Unix timestamp in milliseconds, then the version and variant bits, then random bits.
018f3c5e-7b1a-7cc2-8f2e-9a4b3c2d1e0f ^^^^^^^^ ^^^^ ^ version digit = 7 48-bit millisecond timestamp (first 12 hex digits)
Why v7 is friendlier to databases
Random v4 keys land at random positions in a B-tree index, so inserts touch many pages and can cause page splits and a bigger, more fragmented index. Version 7 values created later sort after earlier ones, so new rows are added near the end of the index, much like an auto-increment key, while staying unique across machines. The sort order is by millisecond; two IDs from the same millisecond are ordered by their random bits unless the generator adds a counter.
When v4 is still the right choice
Neither version is a secret. Do not use a UUID as a password, an API key or an authorization token.
- You do not want the creation time to be visible in the identifier.
- The ID is used for lookups only and index locality does not matter.
- Your platform has no v7 support and you want zero extra dependencies.
Read the timestamp out of a v7 UUID
Take the first 12 hex digits (ignore the hyphens), read them as a number, and treat it as Unix milliseconds.
// JavaScript
const id = "018f3c5e-7b1a-7cc2-8f2e-9a4b3c2d1e0f";
const ms = parseInt(id.replace(/-/g, "").slice(0, 12), 16);
console.log(new Date(ms).toISOString()); // 2024-05-03T02:52:55.962Z
# Python
from datetime import datetime, timezone
h = "018f3c5e-7b1a-7cc2-8f2e-9a4b3c2d1e0f".replace("-", "")
print(datetime.fromtimestamp(int(h[:12], 16) / 1000, tz=timezone.utc))Generating each version
For version 7 in other languages and databases, check whether your runtime or library version already includes it before adding a dependency. You can also generate v4 and v7 values in the UUID Generator and decode any UUID in the UUID Parser.
// JavaScript (browser and Node): version 4 crypto.randomUUID(); // C# (.NET 9 and later) Guid.NewGuid(); // version 4 Guid.CreateVersion7(); // version 7 # Python: version 4 import uuid uuid.uuid4()
Frequently asked questions
Can I tell when a v4 UUID was created?
No. A version 4 UUID has no timestamp. Only versions 1, 6 and 7 carry creation time.
Are v7 UUIDs guaranteed to be unique?
In practice yes: the random bits make collisions extremely unlikely, as with v4. Uniqueness is not a guarantee in the mathematical sense for either version.
Is UUID the same as GUID?
They share the same 128-bit format. GUID is the name used in Microsoft environments such as .NET and SQL Server.

