AlgoViz

Dynamo: Amazon's highly available key-value store

medium 35 min

Amazon'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

A10B22A30C40D55B70C85D95

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

One Dynamo instance, N = 3put cart:42copycopycopyhand backCart serviceNode AcoordinatorNode Bcut offNode Ccut offNode Dnext in list

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

    critical

    With a sloppy quorum the nodes that took the write may not be the nodes a later read asks, and concurrent writers still make siblings.

    Wrong
    N, R, W = 3, 2, 2   # so reads are always fresh
    Right
    N, R, W = 3, 2, 2   # overlap when the same N answer# still handle siblings and stale reads
  • Putting two copies on one machine

    critical

    With virtual nodes the next three tokens can belong to two machines. One failure then takes two copies.

    Wrong
    prefs = [m for _, m in next_tokens(3)]
    Right
    prefs = distinct_machines(walk_clockwise(), 3)
  • Last write wins for a cart

    major

    Two concurrent adds: the one with the later clock time silently replaces the other. An add to cart is lost.

    Wrong
    keep(max(siblings, key=timestamp))
    Right
    keep(merge(siblings))   # union, with remove markers
  • Merging by union with no remove markers

    major

    An item removed on one side reappears, which the paper itself warns about in section 4.4.

    Wrong
    items = set().union(*siblings)
    Right
    items = adds - removes   # each with its own clock
  • Writing without the read context

    major

    Without the clock from the read, the store cannot tell that the new version replaces the old ones, so it keeps more siblings.

    Wrong
    put(key, cart)
    Right
    cart, 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.