~learn.
0:00 / 1:15
Interview problem · Unique ID generator

A million unique IDs a second, and nobody asks anybody

transcript

A million unique IDs a second. Across every data centre. And no server ever asks another one for permission.

The first instinct: the database's auto-increment column. It works. Until you have two databases, or a million writes a second.

One sign bit, always zero. Then 41 bits of milliseconds. About 69 years. 10 bits of worker ID. That's 1,024 generators. 12 bits of sequence. 4,096 IDs every millisecond, per generator.

Read the clock. A new millisecond? Sequence starts at zero. Glue time, worker and sequence together. That's your ID. No network call. Same millisecond as last time? Add one to the sequence. The 4,097th request in one millisecond? It waits for the next one. Never a duplicate.

Assume the whole fleet needs a million IDs a second. One generator can make about 4 point 1 million. Alone. So the sequence isn't your limit. The 1,024 worker IDs are.

Timestamp first, so IDs sort by time. Rows append to the index. Time, worker, sequence. 64 bits, and nobody asks anybody.

Full breakdown in the lecture.