Ch.16: Algorithms: Consistent Hashing Without the Shuffle
Outline
Transcript
0:00 Imagine an on-call engineer adds a 10th Memcached server when checkout traffic rises. Instead of relief, cache misses surge, the database gets extra requests, and checkout becomes slow. How can adding a cache server make more keys look missing? AWS's ElastiCache guide has a telling 9-to-10-server comparison. Modulo redirects roughly 90% of keys; consistent hashing redirects roughly 10%. Those are changed routes, not bytes copied. The AWS guide is linked below. Wait. 90 versus 10 is bigger than I expected.
0:33 We asked for one more cache server; if the database is already busy, a big reroute could hand us a queue. Take a cart key. We'll follow its next request and see whether the owner has the value. So the cart key's hash stays fixed; modulo changes its divisor from 9 servers to 10. The next checkout request may reach a different, empty cache. Put the servers on a circle for me. When one joins, what stays fixed? The old server positions stay put. Picture A, B, and C on a ring. A key routes clockwise to the next server.
1:07 Our cart's mark is just before C; where does it land? So C owns our cart key. Now put D between that key and C on the ring. What happens to the next checkout read? It reaches D. D takes that slice; a payment key between D and C still reaches C. A and B keep theirs. We redirected one wedge, not the whole checkout cache. But D has no cart value yet. Is that first read a miss, or does the ring move the value too? The first read misses. The app reads the database and fills D, so later requests can hit there.
1:42 Only keys newly routed to D face that miss because D joined. The ring did not teleport C's memory. I wish it did, especially with checkout pounding the database. So the route changes first, and the cache catches up on demand. But suppose that cart has to live on disk instead? But a Cassandra join cannot rely on that cache-style refill. Imagine it stores the same cart: C has the bytes, and D must stream that hash range before joining. My first thought is one position for D to keep it simple. Could that make one huge copy?
2:17 It could. Cassandra uses virtual nodes: several ring positions for D. For the cart's wedge, C is a source D needs. Other wedges come from other owners. Even so, several positions don't split one hot cart key: in our toy, it still reaches one owner. That can spread D's share across smaller pieces, but more source machines must stream during the rollout. I pictured one clean handoff. Smaller slices can balance D, but now it needs several old servers. Whose failure could hold up D? Take the cart wedge from C.
2:50 C starts streaming to D, then drops mid-copy. D has only part. An operator says, "Checkout is still waiting for capacity. D's process is up. Can we mark it ready?" So, no. C's stream stopped short; D is missing bytes from that range. Cassandra's default join won't accept the incomplete transfer. Hold on. We're adding D to relieve C, and now C gets a veto. If it comes back and the join succeeds, has it finally shed that slice? No. C keeps its old copy until an operator runs cleanup after D joins.
3:23 That removes the range C no longer owns. Oh, so if C's disk was nearly full, a successful join might leave the capacity alarm red? And yes, C's old bytes still count against its disk until cleanup finishes. I think we should leave the rollout open after the join. I'd wait for cleanup, then check that C's used space actually falls. Right. A green join with a red capacity alarm is not relief. The ring limits which keys change owners; it does not move or clear a byte. Cassandra's streaming and cleanup do that.
3:55 Thanks for listening to Learning Podcasts.