Dynamo: Amazon's highly available key-value store
medium 35 minAmazon's shopping cart had to accept every add, even while servers and links failed.
See it run
One toy ring, four machines, one cart. Watch where cart:42 lives, how a write survives a dead node, and how copies agree again.
Operations
The hash ring
Hash the key to a spot, walk clockwise, stop at the first token. Each machine holds two tokens.
Step 1 of 4
A ring of 100 positions
Our toy ring has positions 0 to 99. The real one has 2^128, because the key is hashed with MD5 (section 4.1).
Step 1: A ring of 100 positions
ring = sorted(tokens) # (position, machine)spot = md5(key) % RING # cart:42 -> 5 on our toy ringi = first_index_after(ring, spot)owner = ring[i].machine # A at 10- Waits for
- one hash and a walk
- Gives up
- the token list on every node
- Real ring
- MD5, 2^128 positions
A token is one position of a machine on the ring. The paper calls it a virtual node.
In the real world
The same dynamo: amazon's highly available key-value store doing real jobs. Pick one and step through it.
Asha adds a book while a link between data centres is down.
Step 1 of 4
Two sides
A and D can talk. B and C are on the other side of the broken link.
Step 1: Two sides
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Treating R + W > N as strong consistency
criticalWith a sloppy quorum the nodes that took the write may not be the nodes a later read asks, and concurrent writers still make siblings.
WrongN, R, W = 3, 2, 2 # so reads are always freshRightN, R, W = 3, 2, 2 # overlap when the same N answer# still handle siblings and stale readsPutting two copies on one machine
criticalWith virtual nodes the next three tokens can belong to two machines. One failure then takes two copies.
Wrongprefs = [m for _, m in next_tokens(3)]Rightprefs = distinct_machines(walk_clockwise(), 3)Last write wins for a cart
majorTwo concurrent adds: the one with the later clock time silently replaces the other. An add to cart is lost.
Wrongkeep(max(siblings, key=timestamp))Rightkeep(merge(siblings)) # union, with remove markersMerging by union with no remove markers
majorAn item removed on one side reappears, which the paper itself warns about in section 4.4.
Wrongitems = set().union(*siblings)Rightitems = adds - removes # each with its own clockWriting without the read context
majorWithout the clock from the read, the store cannot tell that the new version replaces the old ones, so it keeps more siblings.
Wrongput(key, cart)Rightcart, ctx = get(key)put(key, change(cart), ctx)
Interview tips
How to explain it
- Ring, then N copies on distinct machines.
- W acks to write, R answers to read.
- Clocks tell newer from concurrent.
- Hints for short failures, Merkle trees for long ones, gossip for membership.
Memory trick
- Place, copy, count, stamp, cover, repair, chat.
- Sloppy quorum: any N healthy, not the N owners.
- Siblings mean the app must merge.
Always say
- What you give up: reads may be stale, merges are your job.
- Why distinct machines in the list.
- Deletes can come back with a union merge.
- DynamoDB today is a different design.
Numbers to know
- (N, R, W) = (3, 2, 2) is the common setting.
- SLA example: 300 ms for 99.9% of requests at 500 per second.
- 99.94% of cart reads saw one version.
- Gossip every second; clients poll every 10 s.