Sub topic 1
Why Unique IDs Are Hard When Nodes Cannot Coordinate
Prerequisites
Assume you know basic Python, relational database primary keys and indexes, binary numbers, and the idea that a distributed system has multiple machines that can fail independently.
Why Unique IDs Are Hard
An identifier (ID) names one record. A single process can use a counter: read the current value, add one, and return the result. In a distributed system, several nodes may perform that operation at the same time.
If two nodes read 41 before either writes 42, both can return 42. Network delays, retries, crashes, and partitions make coordination harder. A node may also need to create an ID while it cannot reach the service that owns the counter.
A distributed ID scheme must therefore define how generators avoid overlapping values, even when they cannot communicate for every ID.
What to Ask of an ID
Judge an ID scheme by these properties:
| Property | Question |
|---|---|
| Uniqueness | Can two valid generators produce the same ID? |
| Ordering | Do IDs sort by creation time or only identify records? |
| Size | How many bytes does storage and indexing require? |
| Coordination | Must generators contact a shared service? |
| Predictability | Can someone guess another valid ID? |
These properties trade off against one another. Random IDs need little coordination but are not naturally ordered. Counters are ordered and compact but require coordination. Time-based schemes can be ordered, but they must handle clock errors.
Central Counters and Auto-Increment
A database sequence or auto-increment column is simple. One database assigns each value, so uniqueness is easy and values usually increase.
The database becomes part of the write path, however. Every ID request may contend for the same sequence state. Replicas cannot independently assign values without a conflict-resolution scheme. If the database is unreachable, new records may not receive IDs. A highly available database can reduce these problems, but it does not remove the central coordination point.
This approach fits a single database or systems where occasional coordination is acceptable.
Block Allocation
Block allocation reduces coordination. A central allocator gives a node a range, such as 1000–1999. The node then issues IDs locally until its range is exhausted.
Only one request is needed per block instead of one request per ID. Nodes can continue creating IDs during a short allocator outage if they have unused values.
The trade-offs are gaps and unused values. A crashed node may abandon most of its range. Ranges also require careful persistence. If a node loses its assigned range and reuses it after restarting, it can create duplicates.
Exercise
Why does block allocation reduce coordination but not eliminate it?
Show answer
Nodes coordinate when requesting a new range, then generate IDs locally until that range is used.