Lesson 06

IDs Without Coordination

Building block

Every record needs a name that no other record has. On one database a sequence hands them out, and nothing more needs saying. Once many machines create records, every new ID is a small agreement problem, and the answers trade coordination against the ID's size, its order and what it gives away.

The URL shortener of Chapter 7 already chose one: counter blocks with a scramble. This lesson shows the others, and why that one fits there.

One Sequence, Then Many

A database sequence promises uniqueness, not density. Numbers taken by transactions that rolled back are gone, and a session can take a cached batch of them at once, as PostgreSQL Deep Dive shows. Gaps are normal, and nothing in a design should depend on there being none.

With many writers, the first idea is a step and an offset. With ten servers, server three hands out 3, then 13, then 23, and server four hands out 4, then 14, then 24. It needs no coordination at run time, and it freezes the number of servers: adding an eleventh renumbers the scheme. The version with no offsets at all, fourteen nodes that each keep a plain auto-increment, hands out the same numbers fourteen times, which is the fourth probe on the URL shortener's first design.

Ranges Handed Out in Blocks

The shortener's design is the second idea, and it stays. A small replicated counter service hands each application server a block of 100,000 numbers to spend locally. At about 200 new links a second at peak, the fleet spends a block in 500 seconds. So the service is asked every few minutes, and it is never on the hot path.

The counter service must never hand out a block twice. So it records the new high mark durably, on a majority of its members or by a conditional write to a replicated row, before the block leaves.

A server that crashes loses the rest of its block, and the waste is worth sizing once. Assume 20 application servers, each restarted once a day, each losing half a block. Half a block is 50,000 numbers, so the fleet wastes a million numbers a day. Over 5 years that is about 1.8 billion. Add the 18 billion links themselves, and about 20 billion of the three and a half trillion seven-character keys are used: under one percent. The waste changes nothing.

What lost blocks waste in five years, against the key space
Wasted by restarts20 servers, 1 restart a day, half a block each
20 × 50,000 = 1 million a day. × 365 × 5 = about 1.8 billion numbers.
Share of the key space7 base-62 characters: 3.5 trillion keys
18 billion links + 1.8 billion lost = about 20 billion. Under 1 percent used: blocks need no recovery.

The costs are elsewhere. Numbers from different servers are not in creation order across the fleet, and numbers in sequence reveal how many links exist, until the keyed one-to-one mapping, the Scramble step on Chapter 7's diagram, hides them.

UUIDs

A UUID is 128 bits, 16 bytes, and it can be made anywhere, even on a phone that is offline, with no one to ask. Version 4 is random. Version 7 starts with a timestamp, so new ones land near each other in an index; the standard, RFC 9562, says that versions which are not time-ordered have poor database-index locality.

The cost is size and scatter. Every index, and every other table that refers to the key, holds twice the bytes of a 64-bit key. Version 4's inserts land all over the index, the B-tree cost that PostgreSQL Deep Dive explains. And if a UUID ever has to become a short key, it takes 22 base-62 characters, against the shortener's 7.

Time-Ordered 64-Bit IDs

Twitter's Snowflake, as its 2010 README describes it, packs three fields into one number. Forty-one bits count milliseconds from a chosen start, enough for about 70 years. Ten bits name the machine, up to 1,024 of them. Twelve bits are a sequence: 4,096 IDs per millisecond on each machine, about 4 million a second.

Forty-one plus ten plus twelve is 63 bits, so the ID fits a signed 64-bit integer. It needs nobody at run time, and it sorts roughly by time across machines and exactly within one.

The coordination has moved to startup. Every generator needs a machine number that nobody else holds, from configuration or from a lease in a coordination service, and two generators with one number repeat each other's IDs.

The time-ordered 64-bit layout
Time41 bits of milliseconds
Buys: about 70 years from a chosen start.
Runs out: after about 70 years, so the start is set near the system's launch.
Machine10 bits
Buys: 1,024 generators at once.
Runs out: no number left for a new generator; two with one number repeat IDs.
Sequence12 bits
Buys: 4,096 IDs a millisecond per machine.
Runs out: the generator waits for the next millisecond.

Clock Skew and the Bits Budget

The time bits make the clock part of uniqueness. A clock stepped backward could repeat a millisecond already spent, so the original generator refuses to issue IDs until its clock passes the last time it used. Why clocks step at all is the lesson on clocks. Across machines, two IDs are ordered only as well as their clocks agree.

The 63 bits are a budget. Moving two bits from the sequence to the machine gives 4,096 machines at 1,024 IDs per millisecond each. And every ID gives away when it was made, to the millisecond, and which machine made it.

Choosing an ID scheme by requirement
A short key in a small space→Counter blocks
Sorted by time, with no service at run time→A time-ordered 64-bit ID
Made anywhere, even offline, and size is no concern→A UUID, version 7 for index order
No gaps at all→One counter every insert waits on, at one writer's speed
Counter Blocks vs Time-Ordered IDs

Counter blocks fill a small space densely and give the shortest IDs; they need a counter service every few minutes and give no time order. Use them when people see the ID or it must fit a short key.

Time-ordered 64-bit IDs need no service at run time and sort by time; they cost 10 to 11 base-62 characters, a clock that must not step back, and a machine number per generator. Use them when records are listed newest first and the ID never has to be short.

How Interviewers Probe This

The first probe is "Design the ID scheme for a service that creates 50,000 records a second on 40 machines." A good answer asks whether the IDs must be short, sorted or hidden, then picks a time-ordered 64-bit layout and checks it aloud. Forty machines fit in ten bits. Fifty thousand a second over forty machines is 1,250 a second each, against the 4 million a second one machine's sequence allows. It then says how machine numbers are assigned, and what a clock that steps back does.

The second is "Why doesn't the URL shortener just use Snowflake IDs?" A good answer gives ten or eleven characters against seven, says that time order is something the shortener never needs, and keeps the blocks because they fill the space densely while the scramble hides their order. You might say: "A Snowflake ID needs ten or eleven characters, and nobody sorts short links by time, so I'd keep the blocks and scramble them."

The third is "Your generator's clock just jumped back two seconds. What happens?" A good answer says the generator could repeat IDs, so it refuses or waits until its clock passes the last timestamp it used, alerts on it, and names the cost: two seconds without IDs from that machine.

Misconceptions
  • "UUIDs are free because they never collide." Collisions are not the cost. The cost is 16 bytes in every index and reference and, for random ones, inserts scattered over the whole index.
  • "A Snowflake ID needs no coordination at all." It needs a unique machine number per generator, settled before the first ID, and a clock that does not run backward.
  • "Time-ordered IDs give a global order of events." Across machines they are ordered only as well as the clocks agree. Within one machine they are exact.
  • "Numbers lost with a block are a leak to fix." The space is not scarce. Under one percent of the shortener's keys in five years is the price of never asking per link.
  • "Sequential IDs are harmless." They tell anyone how many records exist and let them walk to the next one, which is why the shortener scrambles its numbers.
Why It Matters
  • Ask what the ID must do before choosing a scheme: be short, sort by time, be made offline, or hide volume.
  • Size the space with the problem's own numbers, including what crashes waste, and say the share used.
  • Give every time-based generator a machine number that is configured or leased, never guessed, and have it refuse IDs while its clock is behind its last one.
  • Pass counter numbers through a keyed one-to-one mapping whenever users who should not count them can see them.
RelatedURL Shortener where the keys come fromPostgreSQL Deep Dive sequences, UUIDs and the B-treeLeader Election and Consensus the counter's high markClocks and Ordering a clock that steps back

Knowledge Check

Ten servers hand out IDs by step and offset: server k gives k, k + 10, k + 20. What does the scheme freeze?

  • The ID length, which can never grow past the digits of the first ID
  • The number of servers, since adding one changes the step for all of them
  • The order of creation, since no server can hand out a number out of turn
  • The rate per server, since each must wait for the others before each ID

The counter service hands out blocks of 100,000, and the fleet creates about 200 links a second at peak. How often is the service asked, and what does a server crash cost?

  • On every new link, and a crash may hand the same number out twice
  • Once a day, and a crash loses the whole day's block for every server
  • Every few minutes, and a crash wastes the rest of one block, harmlessly
  • Every few minutes, and a crash forces every server to fetch a new block

A table keyed by random version 4 UUIDs grows to billions of rows. What do the keys cost?

  • Collisions, which grow likely once the table passes a billion rows
  • Nothing extra, since a UUID is the same size as a 64-bit integer key
  • A central registry, which every machine must ask before it makes one
  • Twice the bytes of a 64-bit key, and inserts scattered over the index

In the 64-bit layout of 41 time bits, 10 machine bits and 12 sequence bits, what does the sequence field buy?

  • 4,096 IDs per millisecond on each machine, about 4 million a second
  • 4,096 machines, each making one ID per millisecond on its own clock
  • About 70 years of IDs from the chosen start before the field wraps
  • A global order of every ID made by every machine in the whole fleet

A time-ordered ID generator's clock is stepped back two seconds by NTP. What should it do?

  • Keep issuing IDs, since the sequence field keeps every ID unique
  • Switch to random IDs until the clock is back where it was before
  • Refuse IDs until its clock passes the last time it used, and alert
  • Reset its last-used time to the new reading of its clock and go on

You got correct