Sub topic 1
Why unique IDs are hard when nodes cannot coordinate
Prerequisites
This chapter assumes you can read basic Python and know what a primary key is in a relational database. You should also have a rough idea of what a distributed system is: several nodes that talk over a network and can fail independently. Bit shifting appears in later chapters, not here.
The problem
On one machine, unique IDs are easy. You keep a counter, add one for each new record, and hand out the result. Two records never get the same number because only one piece of code is counting.
A distributed system breaks that assumption. Suppose three application servers each accept new orders. Each server needs to give an order an ID right now, without waiting for the others. If every server keeps its own counter starting at 1, they will all issue order 1, order 2, and so on. The IDs collide as soon as the records meet in one table.
class Node:
def __init__(self, name):
self.name = name
self.counter = 0
def next_id(self):
self.counter += 1
return self.counter
a, b = Node("a"), Node("b")
print(a.next_id(), b.next_id()) # both print 1The code is correct on each node. The failure comes from the nodes not knowing about each other.
Why coordination is the hard part
To avoid duplicates, nodes must either agree on who issues what, or issue IDs from spaces that cannot overlap. Agreement costs something:
- Latency. Asking another machine over the network adds a round trip to every write.
- Availability. If the node that decides is down or unreachable, nobody can create records.
- Throughput. One decider becomes a bottleneck when many nodes ask at once.
A network partition makes this sharper. A partition is a failure where two groups of nodes are both running but cannot reach each other. If each group keeps accepting writes, they cannot check each other's IDs. If they stop accepting writes until the link returns, the system is unavailable. You cannot have both, and ID generation inherits that trade-off.
Two ways out
Every ID scheme in this tutorial takes one of two routes, or mixes them.
- Coordinate, but rarely. A central service, or a service that hands out blocks of IDs, keeps nodes from overlapping. Coordination happens on every ID, or once per block.
- Partition the ID space by construction. Each node builds IDs from parts that no other node can produce. Examples are a long random number, or a combination of time, a node number, and a local counter. No message is needed at ID creation time.
The second route moves the problem rather than removing it. Random IDs can collide, with a probability you can calculate. Time-and-node IDs rely on clocks and on every node having a distinct number, and both can go wrong. Later chapters cover these failure modes.
What "unique" really means
Uniqueness has a scope and a strength. Before choosing a scheme, decide which you need:
| Question | Example answers |
|---|---|
| Unique within what? | One table, one service, all services, all time |
| Guaranteed or probable? | Guaranteed by design, or collision chance so small it can be ignored |
| Who can create IDs? | One server, any server, or clients offline |
An ID that is unique in one table is enough for many systems. IDs created on phones before they reach a server need a much wider scope.
A first check on your own thinking
Many bugs here come from a scheme that works in testing on one node and fails in production on several. A useful habit is to ask, for any ID design: "What happens if two nodes create an ID at the same instant and cannot talk to each other?" If the answer is "they might clash and nothing detects it," you have found the weak point.
Exercise
Three servers each keep a local counter starting at 0 and write orders into one shared table keyed by that counter. Name the failure, and name the two broad strategies for preventing it.
Show answer
The servers issue the same IDs, so inserts collide on the primary key (duplicate IDs). The strategies are to coordinate (a central counter or block allocation) or to partition the ID space by construction (random IDs, or IDs built from time, node number, and sequence) so no coordination is needed at creation time.