Keeping generators apart
Illustrative exampleNumbers here are illustrative: round figures to reason with, not measurements.
Each generator owns a part of the id space no other live generator has. When an instance starts, it claims a generator number from 0 to 4,095 in etcd, the registry in its region, by creating the key /idgen/7 only if it doesn’t exist, attached to a lease; etcd deletes every key on a lease when the lease expires (etcd API (opens in a new tab)). Every id carries the number, and the counter separates ids made in the same millisecond, so two instances can’t make the same id.
| Compared on | Number from a registry | Blocks from a counter | Ticket servers | Random UUIDs |
|---|---|---|---|---|
| Shared per id | Nothing | Nothing until the block runs out | A database write | Nothing |
| Size | 64 bits | 64 bits | 64 bits | 128 bits |
| Order | By time, within clock error | Rough, within a block’s life | Rough; the two servers drift | None (v4), or by time (v7) |
- Instances A (holding 7) and B (holding 12) each make an id at 14:00:00.123: same time, different numbers, different ids.
- A new instance in a deploy claims a free number before it takes traffic.
- An instance crashes and restarts a minute later: it claims a new number, so nothing it made before can repeat.
Read:Learn: Consistency trade-offsLearn: Leader election / consensus
Keep reading+1 min
Blocks from a counter
A generator takes the next 10,000 ids with one conditional update on a counter row and hands them out from memory. It is unique and simple, and a counter per region avoids cross-region calls. The costs: ids are ordered only roughly, and to ride out a ten-minute counter outage the busiest instance needs blocks of about 30 million.
Ticket servers
Flickr described its ticket servers in 2010: a MySQL table whose auto-increment id is bumped with REPLACE INTO and read with LAST_INSERT_ID(), on two servers, one issuing odd ids and the other even (Flickr (opens in a new tab)). The post notes that the two sides drift out of sync. It is unique and simple; here it would need a round trip per id, or blocks.