AlgoViz

Distributed caching

medium 26 min

A distributed cache spreads keys over several servers, so it can hold more and answer more than one machine.

See it run

How a key finds its server, what happens when servers come and go, and what a local copy saves.

Operations

Route a key

A client works out the node from the key and asks only that one.

Step 1 of 4

Three nodes, one rule

Our systemApplicationknows the ruleCache A1/3 of keysCache B1/3 of keysCache C1/3 of keys

Each node holds part of the keys. The application knows the rule, not the contents.

Step 1: Three nodes, one rule

def get(key):    node = ring.node(key)          # same answer on every client    value = servers[node].get(key)    if value is None:        value = database.read(key)        servers[node].set(key, value, ttl=300)    return value
Keys that move
None
Extra work
One hash
Ask every node
3 requests for one value

Every client needs the same hash and the same list of nodes.

In the real world

The same distributed caching doing real jobs. Pick one and step through it.

A live match page gets 40% of all reads.

Step 1 of 3

One node is hot

Our systemApplicationknows the ruleCache A20% loadCache B95% loadCache C20% load
Node B
95%

The key lives on node B. A and C have time to spare.

Step 1: One node is hot

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Routing with a remainder

    critical

    One more node moves most keys. All those reads reach the database together.

    Wrong
    node = servers[hash(key) % len(servers)]
    Right
    node = ring.node(key)   # few keys move
  • Using a hash that differs between runs

    critical

    Python's built-in hash of text changes between processes, so each process routes differently.

    Wrong
    node = servers[hash("user:1") % 3]
    Right
    node = ring.node("user:1")   # md5 or crc, same everywhere
  • Waiting for a dead node

    major

    Without a timeout, each read that maps to it hangs and threads run out.

    Wrong
    value = client.get(key)
    Right
    value = client.get(key, timeout=0.05)# on a timeout, treat it as a miss
  • Testing for a hit by truth

    minor

    0 and empty text are real values. They look like misses and are loaded again and again.

    Wrong
    if not value:    value = load(key)
    Right
    if value is None:    value = load(key)

Interview tips

  • How to explain it

    • Several servers act as one cache.
    • A rule turns a key into a server.
    • The rule should move few keys when servers change.
  • Memory trick: RING

    • Rotate clockwise to find the node.
    • Insert moves only adjacent keys.
    • Nodes get virtual positions, and scaling is graceful.
  • Use it when

    • Hot data is larger than one machine.
    • Many application servers need the same data.
    • Sessions must outlive an application server.
  • Look out for

    • Cold nodes at peak time.
    • One hot key on one node.
    • Clients with different lists of servers.