elefcode
← All guides

Dev Basics · 4 min read

UUID v4 vs v7: Which Should You Use?

UUIDs let independent systems generate identifiers that never collide, without coordinating through a central database. Version 4 has been the default for years, but version 7 changes the calculus for database primary keys.

This guide explains the difference and when each is the right choice.

Try it yourself with the related tool.

Generate UUIDs

Advertisement

What a UUID is

A UUID is a 128-bit identifier written as 36 characters in five hyphenated groups. Because the space of possible values is astronomically large, two independently generated UUIDs are effectively guaranteed never to clash — you do not need to check for duplicates.

v4: fully random

UUID v4 fills its bits with randomness. That makes it simple and unpredictable, which is why it is so widely used. The downside appears at scale: random keys scatter across a database index, which can hurt insert performance and fragment the index.

v7: time-ordered

UUID v7 embeds a Unix millisecond timestamp in its leading bits, so newly generated values sort roughly in creation order. That keeps database inserts sequential and index-friendly while still being globally unique — often making v7 the better default for primary keys.

How to choose

If you want opaque, unordered identifiers or maximum compatibility, v4 is a safe choice. If you are picking primary keys for a database that supports it, v7 usually performs better. Either way, treat UUIDs as identifiers, not secrets.

Related guides