SpunkArt
Data, text and encoding

What a UUID is and how to read one

A UUID is a 128-bit identifier that you can create anywhere, without asking a central server for the next number. The version digit inside it tells you how it was made. This page decodes the layout and works out how likely a clash really is.

Generate version 4 UUIDs

Anatomy of a UUID

A UUID is 16 bytes, written as 32 hex digits in five groups separated by hyphens: 8-4-4-4-12. Take this example:

3f2a9c1e-7b4d-4e21-a8f0-5c6d7e8f9a0b
              ^    ^
          version  variant
  • The first digit of the third group is the version. Here it is 4, so the rest of the bits are random.
  • The first digit of the fourth group holds the variant. For standard UUIDs it is always 8, 9, a or b, because the top two bits are fixed at 10.
  • Everything else carries data. In version 4 that leaves 128 − 4 − 2 = 122 random bits.

You can check any UUID by eye: if the 13th hex digit is not 1–8, or the 17th is not 8, 9, a or b, it is not a standard UUID.

Versions you will meet

VersionBuilt fromSorts by time?Typical use
1Timestamp plus a node id (originally the MAC address)Not in string orderLegacy systems
4122 random bitsNoGeneral-purpose ids, the default in most libraries
5SHA-1 hash of a namespace and a nameNoThe same input must always give the same id
748-bit Unix time in milliseconds, then random bitsYesDatabase primary keys

Version 7 is defined in RFC 9562. Because its first 48 bits are a timestamp, new values sort after old ones, which keeps database indexes compact.

How likely is a collision?

With 122 random bits there are 2122 possible values, about 5.3 × 1036. The birthday approximation gives the chance that any two of n random UUIDs match:

p ≈ n² ÷ (2 × 2122)

Worked examples (rounded to two significant figures):

  • One million UUIDs: p ≈ 9.4 × 10−26
  • One billion UUIDs: p ≈ 9.4 × 10−20
  • To reach a 50% chance you need about 2.7 × 1018 UUIDs (from n = √(2 × 2122 × ln 2)).

Those odds only hold if the random source is good. A bug such as seeding a weak generator with the clock is far more likely to cause duplicates than chance. The generator on this page uses the browser’s cryptographic source, crypto.randomUUID(), or crypto.getRandomValues() where that is missing.

UUIDs as database keys

  • Store them as 16 bytes where the database has a native UUID type. As text they take 36 characters.
  • Random v4 keys scatter inserts across a B-tree index. On large, write-heavy tables, v7 or an auto-increment integer behaves better.
  • Do not use a UUID as a secret. It is unique, not secret. Use a proper token for password-reset links; see the API key guide.
  • Compare in one case. ABC… and abc… are the same UUID. Normalise to lowercase before comparing strings.

Questions

Is a GUID different from a UUID?

GUID is the name Microsoft uses. The format is the same 128-bit value, sometimes shown in uppercase or with braces.

Can two UUIDs from this page ever match?

In principle yes, in practice the odds above apply. The page keeps no list, so it cannot check against earlier results.

What is the nil UUID?

It is all zeros, 00000000-0000-0000-0000-000000000000, used as a placeholder for “no value”.