Encrypt Online
Theme

Certificates & Site Ops

UUID v1 vs v4 vs v7: which version should you use?

Compare UUID v1, v4 and v7 for database keys and public IDs. See which versions expose a timestamp and how two v7 IDs sort within a millisecond.

Encrypt Online Editorial Team2 min read

Choose UUID v4 for a random ID with no embedded creation time. Choose UUID v7 when IDs should sort by creation time to the nearest millisecond. Use UUID v1 when an existing system requires its timestamp and node layout.

Encrypt Online guide cover on a sand background with the headline "UUID v1, v4, v7". The full UUID v7 identity replaces the short ID or UL marker, using a second row only for the version.

In brief

Version What it contains When to use it
v1 Timestamp, clock sequence and node identifier Compatibility with an existing v1 system
v4 122 random bits plus the fixed version and variant bits Random public or internal identifiers
v7 Unix timestamp in milliseconds in the first 48 bits Time-ordered keys and approximate creation times

v1, v4, and v7 solve different operational problems

A v1 timestamp is spread across fields. Sorting its standard text form does not reliably put records in creation order. A v7 timestamp comes first, so sorting the standard text in a consistent case and binary collation groups IDs by millisecond.

Sortability vs exposure is the main tradeoff

Anyone who receives a v7 UUID can recover the timestamp supplied by the generator. A v1 node identifier may be based on a network address. A v4 value carries neither field.

Pick for the system you are building next

For a public record ID, start with the random UUID generator. For a database key, try the UUID v7 generator and check your database's native UUID ordering. If the same name must produce the same ID every time, use UUID v5 with a fixed namespace.

See it in a small example

Text
01890f3a-1234-7000-8000-000000000001
01890f3a-1234-7000-8000-000000000002

Both v7 values begin with the same 12 hexadecimal digits. They have the same millisecond timestamp. Their final digits differ, but the IDs alone cannot establish which event happened first.

What to verify

Common questions

Is v7 always better than v4?

Use v7 when time ordering is useful and revealing creation time is acceptable. Use v4 when you want a random ID.

Should I start new work on v1?

Check whether another system requires v1. For a new time-ordered key, v7 places the timestamp in the most significant bits.

References