What Is a UUID? Versions and Trade-offs Explained
- Why not just use an auto-incrementing ID?
- Why collisions aren't a practical concern
- The versions that matter
- Version 1: timestamp and MAC address
- Version 4: random
- Version 5: name-based and deterministic
- Choosing between UUIDs and integers
- Frequently asked questions
- Is a UUID safe to use as a security token?
- Are UUIDs case-sensitive?
- Can I shorten a UUID for use in a URL?
On this page
- Why not just use an auto-incrementing ID?
- Why collisions aren't a practical concern
- The versions that matter
- Version 1: timestamp and MAC address
- Version 4: random
- Version 5: name-based and deterministic
- Choosing between UUIDs and integers
- Frequently asked questions
- Is a UUID safe to use as a security token?
- Are UUIDs case-sensitive?
- Can I shorten a UUID for use in a URL?
A UUID is a 128-bit number used as an identifier, written as 32 hexadecimal digits split by hyphens into five groups in an 8-4-4-4-12 pattern — 36 characters in total, like 3f2504e0-4f89-41d3-9a0c-0305e82c3301. The name stands for Universally Unique Identifier, and Microsoft's equivalent term GUID refers to exactly the same thing.
What makes UUIDs interesting isn't the format, it's the guarantee. A UUID is designed to be generated independently, by any machine, at any time, without asking permission from anything — and still be unique. That property is why they turn up in databases, message queues, offline mobile apps and distributed systems everywhere, and it's also the source of the trade-offs that make them a poor default for every table in your schema.
Why not just use an auto-incrementing ID?
Auto-incrementing integers are excellent when one authority hands out every ID. The database assigns 1, then 2, then 3; the values are small, sortable and perfectly ordered. The trouble starts the moment more than one thing needs to create records.
- Client-side creation: a mobile app working offline can't ask the server for the next ID, but it still needs to identify the record it just created and sync it later.
- Multiple services or shards: two independent databases both issuing ID 5001 makes merging their data a manual reconciliation job.
- Pre-assigned relationships: with UUIDs you can build a parent and its children in memory, wiring up foreign keys before anything is written, then insert the whole graph in one transaction.
- Information leakage: an order at /orders/1043 tells any visitor roughly how many orders you've ever processed, and invites them to try 1042.
- Idempotent writes: a client-generated UUID doubles as a natural deduplication key when a request is retried after a timeout.
Each of these is solvable other ways, but UUIDs solve all of them at once by making the ID space large enough that independent generators never need to coordinate.
Why collisions aren't a practical concern
A random UUID doesn't actually use all 128 bits for randomness. Four bits encode the version and two encode the variant, leaving 122 random bits — roughly 5.3 undecillion possible values. The probability of generating a duplicate is small enough that the usual illustration is that you'd need to produce billions of UUIDs per second for around a century before a collision became likely.
The important caveat is that this holds only when the randomness is good. A UUID from a weak or badly-seeded source — Math.random(), or an embedded device with no entropy at startup — can and does collide in practice. Use a generator backed by a cryptographically secure random source; in browsers that's the Web Crypto API, and every server platform has an equivalent.
The versions that matter
The version is encoded in the UUID itself — it's the first hex digit of the third group, so a v4 UUID always has a 4 in that position. Several versions exist; three account for nearly all real usage.
Version 1: timestamp and MAC address
A v1 UUID is built from the current time (in 100-nanosecond intervals since 1582, an artefact of the original spec) combined with the generating machine's MAC address and a clock sequence. Because time forms most of the value, v1 UUIDs generated on the same machine are roughly ordered, which is friendlier to database indexes than pure randomness.
The cost is disclosure. A v1 UUID reveals when it was created and, via the embedded MAC address, which network interface created it. That was famously used to trace the author of the Melissa virus. Treat v1 as unsuitable for anything user-facing unless you're using a variant that randomises the node field.
Version 4: random
A v4 UUID is 122 random bits plus the version and variant markers. It carries no timestamp, no machine identity and no ordering — which is precisely why it's the default that virtually every library and online generator produces, and the right choice when the identifier will ever be visible outside your system.
The trade-off is that randomness is hostile to B-tree indexes. Each new value lands in an arbitrary position in the index rather than appending at the end, so pages are split and rewritten across the whole structure instead of staying hot at the tail. On a small table this is invisible; on a high-write table with hundreds of millions of rows it is very much not.
Version 5: name-based and deterministic
A v5 UUID is derived by hashing a namespace UUID together with a name string using SHA-1, then formatting the result as a UUID. Its defining property is determinism: the same namespace and name always yield the same UUID, on any machine, forever. (Version 3 does the same thing with MD5 and exists mainly for backwards compatibility — prefer v5.)
That makes v5 ideal when an identifier should be reproducible rather than novel — deriving a stable internal ID from an external system's key, or generating consistent IDs for the same logical entity across independent services without any shared state. Note that it is a hash, not encryption: someone who knows the namespace can compute the UUID for any name, so a v5 UUID is not a secret.
Note
Newer time-ordered versions, v7 in particular, combine a millisecond Unix timestamp with random bits. They aim to give the index-friendliness of v1 without leaking a MAC address, and are worth knowing about if UUID write performance is a real concern in your database. Library support is now broad, but check your specific stack before relying on it.
Choosing between UUIDs and integers
The honest answer is that this is a storage and indexing trade-off, and it only becomes significant at scale. A UUID is 16 bytes as a native type versus 4 or 8 for an integer — and 36 bytes if you store it as text, which is the mistake that turns a modest overhead into a serious one.
- Lean towards UUIDs when IDs are created outside a single database, when identifiers appear in public URLs, or when you need to merge datasets from separate systems.
- Lean towards auto-increment integers for a single-database application where compact keys and sequential inserts matter and IDs stay internal.
- A common middle ground: keep an internal integer primary key for joins and indexing, plus a UUID column as the public-facing identifier.
- Whatever you choose, store UUIDs in a native uuid or 16-byte binary column, never as a 36-character string — the text form roughly doubles storage and slows every index comparison.
Tip
Before switching a large table's primary key to random UUIDs, benchmark it with a realistic row count. Index fragmentation from random inserts doesn't show up on ten thousand test rows, but it can dominate write performance at a hundred million. If the benchmark hurts and you still need coordination-free IDs, a time-ordered version is the usual answer.
Frequently asked questions
Is a UUID safe to use as a security token?
Not as a general rule. A v4 UUID from a cryptographically secure source has 122 bits of entropy, which is genuinely unguessable, but v1 and v5 UUIDs are largely predictable if you know the inputs — and nothing about a UUID tells a reader which version generated it unless they check. Uniqueness and unguessability are separate properties; for session tokens, password resets and API keys, use a purpose-built secure random token instead.
Are UUIDs case-sensitive?
The specification says UUIDs should be generated in lowercase but compared case-insensitively, since the hex digits are the same value either way. In practice, string comparisons in your code are case-sensitive, so normalise to lowercase on the way into storage rather than relying on every consumer to compare correctly.
Can I shorten a UUID for use in a URL?
Yes — the 36-character form is only a display convention. Encoding the underlying 16 bytes in base64url gives 22 characters, and Base32 or Base58 encodings are also common where readability matters. The identifier is unchanged; you're just writing the same number in a denser alphabet, and you'll need to decode it back before comparing against stored values.
In summary: a UUID is 128 bits formatted as 36 characters, v4 is random and the sensible default, v1 encodes a timestamp and MAC address you probably don't want to publish, and v5 is deterministic by design. Pick UUIDs when independent generation genuinely buys you something, store them as a native 16-byte type, and don't mistake uniqueness for secrecy.
Ready to put this into practice?
Generate a UUID Free