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.
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
Server by server
Server
Machine id
Clock
IDs made
Held
Refused as duplicates
Server 1
1
right
437
0
0
Server 2
2
right
445
0
0
The newest ID, part by part
Part
Bits
Value
What it is
Time
41
400000002997
Milliseconds since the system's start date. It comes first, so newer IDs are larger.
Machine id
10
1
Which server made the ID. It keeps two servers apart inside one millisecond.
Sequence
12
1
A 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
Time
IDs made
Refused as duplicates
1s
272
0
1s
310
0
2s
260
0
2s
290
0
3s
324
0
3s
308
0
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.