UUID Versions Explained: v1, v4, v7 and When to Use Each
A UUID is a 128-bit identifier that you can create anywhere, at any time, without asking a central authority, and still expect it to be unique. That is its whole appeal. But there are several versions with quite different properties, and picking the wrong one has real consequences: leaked hardware addresses, slow database inserts, or identifiers that someone treats as a secret when they are not.
The layout: 128 bits in 8-4-4-4-12
A UUID is written as 32 hexadecimal digits in five groups separated by hyphens: 8-4-4-4-12 characters, 36 in total with the hyphens. Each hex digit is 4 bits, so 32 digits give 128 bits. Of those, a few bits are not payload: 4 bits say which version it is and 2 to 3 bits say which variant (the layout family) it follows. A random v4 therefore has 122 random bits.
The version is the first digit of the third group. The variant is the first digit of the fourth group, and for the standard (RFC 9562 / RFC 4122) variant it is always 8, 9, a or b. Let us read one:
f47ac10b-58cc-4372-a567-0e02b2c3d479
^ ^
version = 4 variant = a (binary 10xx, the standard variant)
The third group begins with 4, so this is a version 4 UUID, and the fourth begins with a (binary 1010), so it follows the standard variant. A quick glance at those two positions tells you what you are holding. The Number Base Converter is handy if you want to look at the bits yourself.
Version 1: timestamp and MAC address
Version 1 combines a 60-bit timestamp (100-nanosecond ticks since 15 October 1582), a clock sequence and the 48-bit MAC address of the generating machine. Example: c232ab00-9414-11ec-b3c8-9f6bdeced846. The third group starts with 1, and the last twelve digits are the node.
That design has two problems. First, it leaks: anyone can read when the ID was made and, if the real MAC was used, which machine made it. Second, the timestamp is stored with its low bits first, so the IDs look random in sort order even though they were generated in sequence. Today v1 is mostly seen in legacy systems. Do not choose it for new work.
Version 4: random
Version 4 is simply random: 122 bits from a good random source, with version and variant bits stamped in. It needs no coordination, reveals nothing and is available in every language. It is what the UUID Generator produces: that tool generates version 4 only, using the browser's crypto.getRandomValues(), lets you choose 1 to 100 values, and offers lowercase, UPPERCASE or brace-wrapped output. It does not generate v1, v7 or name-based IDs.
How likely is a collision? With 2122 (about 5.3 × 1036) possible values, the birthday bound says you would need roughly 2.7 × 1018 UUIDs before the chance of any duplicate reaches 50%. Generating a billion v4 UUIDs gives a collision probability around 9 × 10-20. In other words, collisions from honest randomness are not a practical concern; broken random generators, copied seeds or a cloned virtual machine image are the real ways to get duplicates. Use a cryptographically secure source, as the browser and every modern language library do.
Version 7: time-ordered (RFC 9562)
RFC 9562, published in 2024, standardised version 7. Its first 48 bits are a Unix timestamp in milliseconds, followed by the version nibble and then random bits (the rest of 74 bits are random or a counter). Because the timestamp comes first and is big-endian, v7 values sort in creation order, both as bytes and as text. Take the RFC example 017f22e2-79b0-7cc3-98c4-dc0c0c07398f: the first 48 bits, 017f22e279b0, equal 1645557742000 ms, which is 22 February 2022 19:22:22 UTC. You can verify such a value with the Epoch Time Converter; the idea of timestamps is covered in Unix timestamps and time zones.
The trade-off is that v7 exposes creation time to the millisecond. For most records that is harmless or even useful, but for IDs shown publicly where timing must stay private, v4 is the better pick.
Why v7 helps database indexes
Most relational databases store a primary key in a B-tree. When new keys always land at the right-hand end, as with an auto-increment integer, inserts touch the same few hot pages and stay in memory. Random v4 keys land anywhere in the tree. Each insert may need a different page loaded from disk, existing pages split, and the index grows fragmented and larger than it needs to be. On big tables with clustered primary keys (InnoDB in MySQL is the standard example) that costs real write throughput and cache efficiency.
v7 keeps most of the benefit of both worlds. Keys are globally unique and generated by the application, yet recent rows cluster together, so inserts behave much like an append. Exact gains depend on the database, row size and workload, so measure on your own data rather than trusting a headline number.
Name-based and newer versions
- v3 and v5: deterministic. They hash a namespace plus a name (MD5 for v3, SHA-1 for v5), so the same input always yields the same UUID. Use v5 when you need a stable ID derived from, say, a URL. They are identifiers, not security features. For the difference between hashing and secrecy, see hashing vs encryption.
- v6: a v1 with the timestamp reordered so it sorts. It exists mainly for systems already on v1.
- v8: a vendor-defined layout where you decide the bits.
Primary key trade-offs and storage
UUIDs versus a plain bigint is a real design choice. Bigints take 8 bytes against 16, so every index and foreign key is smaller and joins are cheaper. They are also easy to read in logs. UUIDs let clients or multiple services create IDs offline, make merging databases painless, and do not reveal how many rows you have (an auto-increment ID in a URL lets anyone guess the next one). A common compromise is an internal bigint key with a UUID as the public identifier.
However you store them, avoid char(36) where you can. That is 36 bytes of text against 16 of binary, and every index is more than twice as large. PostgreSQL has a native uuid type, SQL Server has uniqueidentifier, and MySQL or MariaDB users typically use BINARY(16) with conversion functions. Compare the text form case-insensitively, because F47A... and f47a... are the same UUID.
UUIDs are identifiers, not secrets
A v4 UUID has plenty of randomness, but the specification does not promise it is suitable as a credential, and v1 and v7 are partly predictable by design. Do not use a UUID as a password reset token, session cookie or API key unless you know it came from a cryptographically secure generator and you treat it as a full secret. Even then, a dedicated token of 128 or more random bits, encoded for transport, expresses the intent better. Authorisation still has to be checked on every request; "unguessable URL" is not access control. For how signed tokens actually prove identity, read how JWT works.
Quick rule of thumb: use v7 for new database keys if your library supports it, v4 when you need pure randomness or non-revealing IDs, v5 for deterministic IDs, and leave v1 alone.