What is a UUID? Unique IDs explained (and why they never collide)
Open almost any modern database or API and you'll see IDs like f47ac10b-58cc-4372-a567-0e02b2c3d479. That's a UUID, and it quietly solves a hard problem: how do you give things unique IDs when no single authority is handing out the numbers?
What a UUID is
A UUID (Universally Unique Identifier), also called a GUID, is a 128-bit value usually written as 32 hexadecimal characters in five dash-separated groups. Its whole job is to be unique — not just in your database, but across every system in the world, without any coordination.
Why not just use 1, 2, 3…?
Sequential IDs are simple but they need a central counter. That breaks down when:
- Multiple servers create records at the same time and would hand out the same next number.
- Offline or distributed apps need to generate IDs before they ever talk to a server.
- You don't want to leak information — a sequential ID reveals how many users or orders you have, and lets people guess the next one.
A UUID can be generated anywhere, anytime, by anyone, and still be safe to merge into one dataset later.
How can they never collide?
The trick is sheer size. A version-4 UUID is essentially random, drawn from a pool of about 340 undecillion values (that's 3.4 followed by 38 zeros). The odds of generating the same one twice are so astronomically small that, in practice, it never happens — you'd need to generate billions per second for a lifetime to have a meaningful chance of a single clash.
The versions you'll meet
- Version 4 — random. By far the most common; use this unless you have a specific reason not to.
- Version 1 — based on timestamp and machine. Sortable by creation time, but can leak the generating machine.
- Version 7 — a newer, time-ordered design that's random but also sorts chronologically, which databases love.
When to use one
Reach for a UUID whenever you need an identifier that's unique without a central authority: database primary keys, API request IDs, file names, session tokens, and idempotency keys. Generate a few and you'll see the format instantly.