Case study demo

Snowflake IDs

Generate unique, time-ordered 64-bit IDs without a central counter, and see how the bits are laid out.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How unique IDs are made

Every new row needs an ID that no other row has. Two app servers make IDs in one of three ways. They use a clock with a machine id and a sequence number, or one shared counter, or random numbers. Stop the counter, share a machine id, or set a clock back, and watch which IDs break.

Try it

Lab time 3.0 s
write rowServer 1, machine id 1Server 1machine id 1clock rightCounter server, next number 1, IDLECounter servernext number 1not usedIDLEServer 2, machine id 2Server 2machine id 2clock rightDatabase, 872 rows storedDatabase872 rows stored0 IDs refused
  • Server 1 — machine id 1 — healthy — clock right
  • Counter server — next number 1 — IDLE — not used
  • Server 2 — machine id 2 — healthy — clock right
  • Database — 872 rows stored — healthy — 0 IDs refused
  • Write a row with its ID (1 dot for 10 rows)
  • Ask the counter server
  • The next number
  • ID refused: a row already has it
IDs made
882/ 882
of the rows that asked for one
Refused as duplicates
0
the database already held that ID
Failed
0
no ID after 1 s, so no row
In time order
100%
IDs larger than the one before, last 3 s
Time to get an ID
under 1 ms
at the app server, last 3 s
Held for the clock
0
requests that wait for a clock to catch up

Numbers add up:882 asked = 882 made + 0 failed + 0 in progress · 882 made = 872 stored + 0 refused + 10 on the wayThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

How an ID is made
Where the ID comes from

Each server builds the ID itself from its clock, its machine id and a small count.

300 a second

The counter server needs 2 ms for each number, so it can make 500 a second. Ask for more, and the line there grows until every number comes back too late.

Time, machine, sequence

A mistake in a config file: server 2 is given server 1's machine id.

A server remembers the last time it used. If its clock reads an earlier time, it makes no ID until the clock has caught up.

These three change only IDs that are built from the clock.

Counter server
Counter server

A stopped counter gives no answer. A server waits 1 s, then the row fails.

Server by server

ServerMachine idClockIDs madeHeldRefused as duplicates
Server 11right43700
Server 22right44500

The newest ID, part by part

PartBitsValueWhat it is
Time41400000002997Milliseconds since the system's start date. It comes first, so newer IDs are larger.
Machine id101Which server made the ID. It keeps two servers apart inside one millisecond.
Sequence121A count of the IDs this server made in this millisecond. It starts at 0.

Put side by side, the three parts make one 63-bit number: 1677721612570333185.

IDs made, and IDs refused as duplicates, each second
IDs madeRefused as duplicates
01252503755001s2s3s
Show data table
TimeIDs madeRefused as duplicates
1s2720
1s3100
2s2600
2s2900
3s3240
3s3080

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • There are 2 app servers. A real system has hundreds, which is why the machine id has 10 bits: room for 1,024 machines.
  • One trip over the network takes 40 ms here, so that you can see it. Inside one data centre it takes about 1 ms.
  • A count of 12 bits holds 4,096 IDs in one millisecond for each server. This model never fills it.
  • A clock is set back 2 s in one jump. Real clocks drift by a few milliseconds and are put right in small steps, and a real server often refuses requests while it waits.
  • One counter server stands for every shared counter, such as a database that numbers its rows. Real systems hand each server a block of numbers at a time, so one trip covers many IDs.
  • The database remembers every ID from the last 30 seconds. A real unique index remembers every row.
  • A random ID here has 63 bits, so that it is as long as the others. That is too few for a big system. Real random IDs, such as UUIDs, use 122 random bits.
  • Asked for more than it can number, the counter server answers in order of arrival, so every answer comes too late. Real services refuse new requests early, so that the ones they accept are answered in time.