UUID Versions Explained: v1, v4, v7 and Which One to Use
6 min read · Published 1 November 2026 · Updated 2 November 2026
A UUID (universally unique identifier) is a 128-bit value you can generate on any machine without asking a central server for a number, and still be almost certain that nobody else generated the same one. They are popular for database keys, request IDs and file names. The word "UUID" covers several versions that are made in very different ways, and the version you pick affects privacy and database performance.
In this guide
What a UUID looks like
A UUID is written as 32 hexadecimal digits in five groups separated by hyphens, in the pattern 8-4-4-4-12. For example: 3f2504e0-4f89-41d3-9a0c-0305e82c3301. The first digit of the third group is the version number (4 in this example), and the first digit of the fourth group encodes the variant. GUID is simply Microsoft's name for the same thing. Uppercase letters, missing hyphens or surrounding braces change only how it is written, not what it is.
Version 1: time and machine
Version 1 combines a timestamp with a node identifier that is often based on the network card address. It is unique and roughly time-ordered, but it can leak when and on which machine an identifier was created, which is a privacy concern. It is rarely the right choice for new systems that expose identifiers publicly.
Versions 3 and 5: names
Versions 3 and 5 are derived from a name and a namespace using a hash (MD5 and SHA-1 respectively). The same input always produces the same UUID, which is useful when you need a stable identifier for something that already has a name, such as a URL. They are not random.
Version 4: random
Version 4 fills 122 of the 128 bits with random data; the rest hold the version and variant. It is the most widely used version because it needs no coordination and reveals nothing about the creator. Generating one in a browser is a single call to crypto.randomUUID(), which uses a secure random source.
Version 7: time-ordered and random
Version 7, defined in RFC 9562 (2024), starts with a Unix timestamp in milliseconds followed by random bits. New values sort roughly in creation order, which is much friendlier to database indexes. The trade-off is that the creation time is visible in the identifier. If that matters for privacy, use version 4 for public identifiers.
How likely is a collision?
With 122 random bits there are about 5.3 × 10^36 possible version 4 values. By the birthday paradox, you would need to generate around 2.7 × 10^18 UUIDs to reach a 50% chance of a single duplicate. At a billion per second that would take about 85 years. For practical purposes a collision from a correct random generator is not something to plan around. Problems almost always come from a poor random source, copied test data or buggy code, not from the maths.
UUIDs as database keys
- Random version 4 keys are inserted at random positions in a B-tree index. On large tables this causes more page splits and cache misses than a steadily increasing key.
- Time-ordered values (version 7, or similar schemes) keep new rows near each other in the index and usually perform better as primary keys.
- Store UUIDs in a compact native type where available (for example the uuid type in PostgreSQL, or BINARY(16) in MySQL) rather than as a 36-character string. It halves the space and speeds up comparisons.
What UUIDs are not
A UUID is an identifier, not a secret. Do not use one as a password, an API key or a reset token unless you are sure it comes from a cryptographically secure source and you treat it as a secret. For passwords, use a password generator with a proper length and character mix.
Quick recommendation
- Need an ID that must not reveal anything and is exposed publicly? Use version 4.
- Need a database primary key on a large, busy table? Prefer version 7.
- Need the same ID from the same input every time? Use version 5.
- Avoid version 1 for new public-facing systems.
Generating UUIDs in common languages
All of these use the operating system or runtime secure random source. Avoid building a UUID by hand from Math.random() or similar functions, which are not designed to be unpredictable.
Version 4 examples
// JavaScript (browser and Node.js)
crypto.randomUUID();
# Python
import uuid; str(uuid.uuid4())
// Java
java.util.UUID.randomUUID().toString();
-- PostgreSQL 13 and later
SELECT gen_random_uuid();Validating a UUID string
When a value arrives from a user or another system, check its shape before using it. This pattern accepts the standard 8-4-4-4-12 layout for versions 1 to 8 and the standard variant, in either letter case. It rejects the special nil (all zeros) and max (all ones) values, which you would need to allow as separate cases:
Regex (use the i flag)
^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$Frequently asked questions
- Are UUIDs case sensitive?
- No. The hexadecimal digits mean the same in upper and lower case, and the specification recommends producing lowercase. Compare UUIDs case-insensitively, or normalise them to lowercase before storing.
- Can I make a UUID shorter?
- Yes. The 36-character text form is just a way of writing 16 bytes. Encoding those 16 bytes in Base64URL gives 22 characters. Keep the standard form in your database and use the short form only where it is shown to people.
- Is it safe to put a UUID in a URL?
- It is fine to use as an identifier. But a hard-to-guess ID is not access control: always check on the server that the user is allowed to see the record, because anyone who learns the ID can try to use it.
- What are the nil and max UUIDs?
- The nil UUID is 00000000-0000-0000-0000-000000000000 and the max UUID is ffffffff-ffff-ffff-ffff-ffffffffffff. They are reserved special values, sometimes used as placeholders, and should not be generated as normal identifiers.
Sources and further reading
Try it yourself: UUID Generator runs in your browser, free and with no sign-up.