Make unique IDs on many machines at once without asking a central counter every time.
Every row, order, message and file needs an ID. On one database, the database can count 1, 2, 3. On many machines, they must make IDs at the same time, without asking a central counter every time, and without ever making the same ID twice.
An ID generator has two jobs. It must be unique (never the same ID twice, on any machine). Often it should also be roughly sortable by time, so new IDs are bigger than old ones. Sorted IDs make database indexes faster and let you page through results in order.
There are three main ways to get uniqueness without a central counter. Randomness: pick so many random bits that two machines will practically never pick the same (UUID v4). Partitioning: give each machine its own slice of the ID space, such as a machine number in the ID (Snowflake). Ranges: a central service hands out a block of numbers, and each machine uses its block alone (ticket server).
Snowflake was made at Twitter. A 64-bit number is split into parts: 41 bits of time in milliseconds, 10 bits of machine number, and 12 bits of a counter that restarts every millisecond. Two machines never clash because their machine bits differ. One machine never clashes with itself because the counter increases inside the same millisecond. And since time is at the front, newer IDs are bigger.
0412-07-0031. No two shops can ever match, since the shop number differs, and tickets sort by date. A third way: head office gives each shop a box of 100 numbers at a time, and the shop only calls when its box is empty.| Scheme | Size | Sorted by time? | Needs | Weak spot |
|---|---|---|---|---|
| Auto-increment | 64 bits | Yes, strictly | One database | A bottleneck and a single point of failure |
| UUID v4 | 128 bits | No | Nothing | Big, random, bad for index locality |
| UUID v7 | 128 bits | Yes | A clock | Big, order within a millisecond is loose |
| ULID | 128 bits (26 chars) | Yes | A clock | Similar to UUID v7 |
| Ticket server | 64 bits | Roughly | A small service | Service must be highly available |
| Snowflake | 64 bits | Yes, roughly | A clock and a machine ID | Clock going back, duplicate machine IDs |
One database with AUTO_INCREMENT gives perfect 1, 2, 3, but all writes go through it. A well-known trick: run two or more databases, one makes odd IDs and one makes even IDs. A ticket server goes further: a machine asks for a block (say 1,000 numbers) and uses them locally. The service is called rarely, and a crash only wastes a block.
| Version | Made from | Notes |
|---|---|---|
| v1 | Time + MAC address | Leaks the machine, and is not sorted well |
| v4 | Random bits (122 of them) | Most common, simple, no order |
| v7 | 48-bit time + random | Sorts by time, a good modern default for keys |
Where the machine ID comes from matters: a config file, an environment variable, or a short lease from ZooKeeper or etcd. Where the counter overflows (more than 4,096 IDs in one millisecond) the generator waits for the next millisecond.
The "random" digits come from a fixed formula so the page looks the same every time. Real UUIDs and ULIDs use a secure random source. The clock step is a simple model: it replays an earlier millisecond.
| Term | Meaning in easy words |
|---|---|
| UUID | Universally Unique Identifier, a 128-bit ID usually written as 32 hex digits |
| Snowflake | A 64-bit ID made of time, machine ID and counter |
| ULID | A 128-bit, time-sorted ID written in 26 text characters |
| Epoch | The starting moment that time is counted from |
| Machine ID / worker ID | A number that is different on every ID generator |
| Sequence / counter | A number that grows for IDs made in the same millisecond |
| Ticket server | A service that hands out blocks of numbers to many machines |
| Collision | Two different things getting the same ID |
| k-sortable | Roughly sorted by time, with small errors |
| Clock skew | Two clocks (or one clock at two moments) disagreeing |
| Birthday paradox | Why collisions in random IDs happen sooner than you think |
| Monotonic | Always going up, never back |
| Choice | We gain | We lose |
|---|---|---|
| UUID v4 | Zero setup, no coordination | Size and no ordering |
| UUID v7 / ULID | Sorted, no machine IDs | 128 bits, and a clock |
| Snowflake | Small, sorted, fast | Machine ID management and clock risk |
| Ticket server | Strict ranges, no clock | A service to run, wasted numbers on crash |
| Auto-increment | Simplest, perfectly ordered | Does not scale writes |
| Fewer machine bits | More counter or time | Fewer generators |