Developer & Network Tools

UUIDs Explained

See what a UUID contains, how the versions differ, and when to use one as a database key.

Key takeaways

  • A UUID is 128 bits, normally shown as 8-4-4-4-12 hexadecimal digits.
  • Version 4 is almost entirely random and is a good general-purpose choice.
  • Version 7 starts with a timestamp, so new values sort roughly in creation order and suit database keys.
  • Unique is not the same as secret, and a UUID should never be your only access control.
  • Use your platform's built-in generator instead of assembling one yourself.

A UUID, short for universally unique identifier, is a 128-bit value that any computer can generate on its own with a very low chance of ever matching one made elsewhere. It's usually written as 36 characters: 32 hexadecimal digits split into five groups by hyphens. Microsoft calls the same thing a GUID.

UUIDs let separate systems label records without asking a central authority for the next number. This article explains what is inside one, how the versions differ, and when they're the right choice.

The problem they solve

A common way to number records is to let the database count: 1, 2, 3. That works well while one database hands out every number. It gets awkward when a phone app creates records while offline, when several servers write at once, or when you merge data from two systems that both have a record number 42.

With UUIDs, whoever creates the record can name it immediately. A browser can make an order identifier before sending anything. A retried request can carry the same identifier, so the server recognizes a duplicate, which is the heart of safe retries.

The trade-off is size and readability. A UUID takes 16 bytes in binary form, more than a typical integer, and nobody wants to read one over the phone.

Anatomy of a UUID

The current specification is RFC 9562, published in 2024, which replaced the older RFC 4122. Here is a version 4 example:

f47ac10b-58cc-4372-a567-0e02b2c3d479

Two positions have fixed meaning. The first digit of the third group is the version. The first digit of the fourth group carries the variant, which tells software which layout rules apply. For the common variant, that digit is 8, 9, a or b.

Parts of an example version 4 UUID
f47ac10b-58cc 1 4 2 a 3 0e02b2c3d479 4 Leading bits Random in version 4. Other versions put time or hash data here. 1 Version digit First digit of the third group names the version. 2 Variant digit 8, 9, a or b marks the standard layout. 3 Final group More random bits in version 4. 4 f47ac10b-58cc 1 4 2 a 3 0e02b2c3d479 4 Leading bits Random in version 4. Other versions put time or hash data here. 1 Version digit First digit of the third group names the version. 2 Variant digit 8, 9, a or b marks the standard layout. 3 Final group More random bits in version 4. 4

Hexadecimal letters can be written in either case. Compare UUIDs as 128-bit values, or normalize the case first; the standard form for output is lowercase.

Two special values exist. The nil UUID is all zeros and often stands for "no value". RFC 9562 also defines a max UUID, all f digits.

The versions you'll meet

Version 1 combines a high-resolution timestamp with a node identifier, historically the network card's MAC address. It can reveal when and on which machine a value was made, which is a privacy consideration.

Version 3 and version 5 are not random. They hash a namespace UUID together with a name, using MD5 for version 3 and SHA-1 for version 5. The same inputs always give the same output. That's useful when two systems need to derive the same identifier for the same thing, such as a given web address, without talking to each other.

Version 4 fills 122 of the 128 bits with random data. It's the one most people mean when they say "a UUID".

Version 6 rearranges the version 1 timestamp so values sort by time. It mainly helps projects that already use version 1.

Version 7 puts a Unix timestamp in milliseconds in the first 48 bits, followed mostly by random bits. It sorts by creation time and doesn't expose hardware addresses.

Version 8 is reserved for custom layouts, where the application defines the meaning of most bits.

Which UUID version fits?
Must the same input always produce the same identifier? YES Use version 5 with a namespace you define and document. 1 NO Will it be a database key where insert order matters? YES Consider version 7, if your tools support it. 2 NO Do you just need a unique label that reveals nothing? YES Use version 4 from a secure random source. 3 NO Check the existing system Match whatever format the system you integrate with expects. Must the same input always produce the same identifier? 1 IF YES Use version 5 with a namespace you define and document. NO Will it be a database key where insert order matters? 2 IF YES Consider version 7, if your tools support it. NO Do you just need a unique label that reveals nothing? 3 IF YES Use version 4 from a secure random source. NO Check the existing system Match whatever format the system you integrate with expects.

Could two ever collide?

For random UUIDs, a collision is possible in principle but extraordinarily unlikely in practice. With 122 random bits, you would need to generate billions of billions of values before the chance of any pair matching became large.

That math assumes a good random number generator. Real collisions are far more likely to come from broken code than from luck: a weak or badly seeded generator, a virtual machine image cloned with its random state, a copied record that kept its original identifier, or a developer who pasted the same sample UUID into several places.

So treat uniqueness as very reliable, and still put a unique constraint on the database column. It costs little and catches mistakes.

Generating UUIDs

Use the tools your platform already provides.

# macOS and most Linux systems
uuidgen
# Windows PowerShell
New-Guid

In a modern browser on a secure (HTTPS) page, and in current server-side JavaScript runtimes:

crypto.randomUUID()

Python includes a uuid module, where uuid.uuid4() produces a random value and uuid.uuid5() derives one from a namespace and name. Support for version 7 is newer and varies by language, database and library, so check your version's documentation.

For one-off values, the UUID and ULID generator works in the browser.

Don't build UUIDs from Math.random() or similar general-purpose functions. They aren't designed to be unpredictable and may repeat more than you expect.

UUIDs as database keys

Many databases keep the primary key in a sorted tree structure. Sequential keys are added near the end, which keeps recently used pages together in memory. Random version 4 keys land all over the tree, which can mean more page splits, more disk activity and larger indexes on big tables.

On small tables you'll never notice. On large, write-heavy ones, it can matter a lot. Version 7 keeps most of the benefit of decentralized generation while inserting in roughly time order.

Storage format matters too. A UUID stored as 36 characters of text takes more than twice the space of its 16-byte binary form, and every index that references it pays again. Use a native UUID column type where your database offers one, or a 16-byte binary column.

Some teams use both: a small integer key for internal joins, and a UUID that appears in public addresses and REST APIs.

Three common ways to identify a record
Counting integer Small and easy to read Needs one source to assign it Reveals order and rough volume Easy to guess the next one UUID version 4 Generated anywhere 16 bytes in binary Reveals no order or time Scattered inserts in sorted indexes UUID version 7 Generated anywhere 16 bytes in binary Reveals approximate creation time Inserts arrive roughly in order Counting integer Small and easy to read Needs one source to assign it Reveals order and rough volume Easy to guess the next one UUID version 4 Generated anywhere 16 bytes in binary Reveals no order or time Scattered inserts in sorted indexes UUID version 7 Generated anywhere 16 bytes in binary Reveals approximate creation time Inserts arrive roughly in order

UUIDs in files, queues and APIs

Databases are only one home for these identifiers. They name uploaded files so that two users who both send photo.jpg don't overwrite each other. They tag messages in message queues so a consumer can skip a delivery it has already handled. They also follow a single request across several services: the first server creates a request identifier, passes it along in a header, and every log line records it. When something fails, searching for that one value pulls up the whole story.

In a webhook system, an event identifier plays a similar part. Senders often retry when they don't get a prompt answer, so the receiver stores the identifiers it has processed and ignores repeats.

When you design an API, decide who creates the identifier. Letting clients propose one makes retries and offline work easy, but the server must validate the format, reject an identifier that already belongs to someone else, and never assume a client-supplied value is random. A malicious or buggy client can send whatever it likes.

Writing and validating them

The usual text form has hyphens, but you'll also meet UUIDs without them, wrapped in braces, or prefixed with urn:uuid:. Pick one canonical form for storage and convert everything to it at the edge of your system. Use your language's UUID parser for validation where one exists, since it handles these details more reliably than a handwritten pattern.

Unique is not secret

A random UUID in a web address is much harder to guess than the number 42. That's a useful extra layer, but it isn't authorization. Addresses end up in browser history, server logs, analytics tools, referrer headers and forwarded messages.

Your server must still check that the signed-in user is allowed to see the record. If a link itself grants access, as with some file-sharing features, treat the token as a password: generate it from a cryptographically secure source designed for secrets, let it expire, and make it revocable. Version 7 and version 1 values carry time information and are poor secrets.

Handling mistakes

A few problems come up repeatedly:

  • Validating too loosely, so any 36-character string passes.
  • Validating too strictly, so valid newer versions are rejected because the code only accepts 4.
  • Comparing text with different letter case or with surrounding braces, which some Windows tools add.
  • Converting between byte orders. Some Microsoft GUID binary layouts order the first three groups differently from the standard network order, so test round trips when moving data between systems.
  • Truncating a UUID to make it shorter, which throws away uniqueness and, for time-based versions, may keep only the part that nearby values share.

If you need a shorter or friendlier code for humans, create a separate field designed for that purpose and keep the full UUID underneath.

Common questions

Is a GUID different from a UUID?

For practical purposes they're the same 128-bit identifier. GUID is the name used across Microsoft products. Watch the binary byte order when exchanging raw bytes.

Can I tell when a UUID was made?

From a version 1, 6 or 7 value, yes, approximately, because a timestamp is stored inside it. See how Unix timestamps work for the time scale version 7 uses. Version 4 contains no time.

Do I need to register UUIDs anywhere?

No. That's the point. Generate them locally and store them like any other value.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Developer & Network Tools →

Latest news on this topic