Distributed Caching 00:00:01 Find one key among several cache machines A game lobby stores profile pages across cache servers. Before a read can hit, every client must agree on which machine owns a particular key under the current membership view. Each node holds part of the keys. The application knows the rule, not the contents. user:101 hashes to node B. No other node is asked. Node B does not have it, so the database is read once and the value is stored on B. A second application server uses the same rule, also reaches cache B, and gets a hit. The original database remains the recovery source. Cache placement determines who receives a lookup and how disruptive adding or losing a machine will be. 00:00:45 A new machine changes the remainder Compare taking a hash modulo the current server count with the earlier routing rule. Adding one server changes the divisor even for keys whose old server remains perfectly healthy. The remainder after division by 3 spreads the hashes evenly. Divide by 4 for the same hashes. The numbers shift. Only hashes whose two remainders agree stay. In this list 12, 13 and 14 stay and the other five move. A reshuffle creates compulsory misses on newly selected machines. Count origin traffic during membership changes instead of judging placement solely by steady-state balance. 00:01:24 Give the newcomer just one portion of the ring Add a member to an ordered hash ring. To find a key's owner, walk in the defined direction to the next responsible token. Observe which key intervals move. Each key belongs to the next node clockwise. order:999 at 200 wraps round to A. Only keys between 91 and 120 change owner. That is product:55 at 100. Its first reads all miss. Load the hot keys first, then let the clients use it. Virtual nodes can distribute responsibility more evenly. A balanced number of keys can still produce a highly uneven request load if one key is extremely popular. 00:02:06 A local hit never reaches the shared cache Add a short-lived application cache above the distributed pool. Follow one request through local memory, shared cache and the original database, then ask how a recent edit propagates. The read passes L1 and L2 and reaches the database. The value is kept for 300 seconds in L2 and for 5 seconds in L1. L1 answers. Nothing crosses the network. L2 was updated, but L1 may show the old value for up to 5 seconds. Choose that number on purpose. A local copy economizes on network reads while adding a separate freshness window. Keep its key definition, access scope and invalidation policy compatible with the shared layer. 00:02:50 One profile is hotter than an entire machine A famous player's page sits on one owner while neighboring machines have spare cycles. Moving unrelated keys cannot divide a single conventional cache key across workers. The key lives on node B. A and C have time to spare. The other keys spread a little more. The hot key stays where it is, so B is still busy. Each application server keeps the page for one second. Node B now gets one read per server per second for it. A bounded application-local copy can spread read load if the lobby tolerates its age. Measure the owner and the origin under a cold restart before announcing spare capacity.