BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 7

Design a Unique ID Generator in Distributed Systems

System Design Interview Vol 1 · Ch 7 · Alex Xu · 2020

中文 →

What is this chapter about?

Every order you place and every message you send needs a number — one that is never handed out twice, and ideally one you can glance at to tell what came first. On a single machine this is a non-problem: the database just counts up from 1. This chapter is about what happens when the back end becomes a few hundred machines, why "just count up by 1" collapses, and how to rebuild it.

An analogy

Think of a ticket dispenser in a chain of shops. With one shop, the dispenser counts 1, 2, 3 and never gets it wrong. Open 32 branches and the trouble starts: if every branch counts from 1, the country now has 32 different customers holding ticket "3", and the clerk has no idea which one to serve.

Three obvious fixes, all of which fail

One: make every branch phone head office for a number. No duplicates — but every single ticket now costs a phone call, which is slow, and if head office loses power, all 32 branches stop dispensing.

Two: let each branch pick a very long random string. Collisions become vanishingly unlikely, but the string is long and tells you nothing about order — you cannot look at two of them and say which came first.

Three: have each branch count in steps. Branch 1 counts 1, 33, 65…; branch 2 counts 2, 34, 66…. Clever — until you open a new branch or close one, at which point the whole country has to be renumbered.

The idea: build the number out of three pieces

Twitter's answer was to stop asking anyone for a number and instead assemble one on the spot out of three things that are already unique: the first piece records what time it is, down to the millisecond; the middle piece records which branch I am; the last piece records how many I have handed out during this particular millisecond.

Put the three together and nobody has to ask anybody: different millisecond, the first piece differs; same millisecond but different branch, the second differs; same branch and millisecond, the third differs. And because time sits at the front, sorting the numbers from small to large is roughly sorting them from earliest to latest — the ordering comes for free. This dispenser can hand out 4,096 tickets per millisecond per branch, and the scheme lasts 69 years.

What it costs

The whole thing rests on every machine's clock moving forward: if one machine's time gets nudged backwards, it will live that millisecond over again and hand out numbers it has already given away.

Remember this

The hard part of distributed IDs was never "don't repeat" — any long random number does that. The hard part is being unique and short and visibly ordered, without machines ever talking to each other. The answer is to cut the ID into time + who I am + which one this instant, trading a clock for a conversation.

Want the mechanisms, the notation and the diagrams? → Switch to the deep read