Distributed caching
medium 26 minA distributed cache spreads keys over several servers, so it can hold more and answer more than one machine.
Video lesson · Chapter 1 of 5
Find one key among several cache machines
Theory
Read or listen first. About 1 min.
What it is
A distributed cache is a cache made of several servers that acts like one. Each key lives on one of them, or on a few if it is copied. All application servers see the same cached data.
The part that makes it work is a rule: from the key alone, any client can work out which server to ask. Nobody needs to search all of them.
Why it exists
One cache server has a limit on memory, on network and on requests. When the hot data no longer fits, or too many requests arrive, the choice is a bigger machine or more machines.
Sharing also removes waste. If ten application servers each keep their own copy, the same value is stored ten times and may differ between them. A shared cache stores it once.
A short history
1997
Karger and co-authors at MIT introduce consistent hashing, written for distributed caching on the web.
2003
Memcached is released. Its servers do not know each other: clients spread the keys.
2007
Amazon's Dynamo paper describes virtual nodes, where each server owns several points on the ring.
2013
Facebook publishes a USENIX paper about running Memcached as a large distributed cache.
2015
Redis 3.0 ships Redis Cluster, which assigns 16,384 slots to servers.
Words to know
- Node
- One cache server in the group.
- Partitioning
- Splitting the keys so each node holds a part. Also called sharding.
- Hash ring
- A circle of numbers. Keys and nodes get places on it, and a key belongs to the next node clockwise.
- Virtual node
- One of several places a server takes on the ring.
- L1 and L2
- A small cache inside the program, then the shared cache over the network.
- Serialisation
- Turning a value into bytes for the network, and back.
How it works
Step through the idea, or press Play.
Hash the key. Turn it into a number that any client computes the same way
Understand it
Start here
Hikers and rucksacks
A group of hikers carries snacks. One rucksack cannot hold them all, so they share them out with a rule everyone knows: the first letter of the snack says whose bag it is in.
The others did not have to swap anything. That is the sign of a good rule.
The idea
Why spread a cache
More room
Memory adds up over nodes, so the cache can grow to terabytes.
More requests
Load is divided, because each node answers only for its keys.
Shared state
All application servers see the same values.
Smaller failures
Losing a node loses a part of the cache, not all of it. Copies need replication.
One shared cache, three machines
How many nodes for the data
Alone first
Local cache or shared cache
Local cache (inside the program)
- Very fast: no network
- Simple to build
- But each server has its own copy
- But limited by one server's memory
- But copies may differ between servers
Shared cache (over the network)
- One copy for all servers
- Grows by adding nodes
- Keeps going when a node is lost, with more misses
- But each call costs a network trip, often about a millisecond
- But values must be turned into bytes and back, and it is one more system to run
When to use what
| Use | When |
|---|---|
| Local | Data belongs to one server, is small, must be read in microseconds, and may be a little old |
| Shared | Data is shared, larger than one machine, or must outlive a restart of an application server, such as sessions |
| Both | A few keys are very hot and the rest are many. Local for seconds, shared for longer |
The rule
Partitioning by remainder
The simplest rule is the remainder after division: hash(key) % number_of_nodes. With three nodes, remainder 0 goes to node 1, remainder 1 to node 2 and remainder 2 to node 3. It is even and easy, until the number of nodes changes.
One key before and after
| Nodes | Calculation | Node | Result |
|---|---|---|---|
| 3 | 100 mod 3 = 1 | Node 2 | hit |
| 4 | 100 mod 4 = 0 | Node 1 | miss, read the database again |
How many keys move from 3 to 4 nodes
What a change does to the cache
| Change | Remainder | Hash ring |
|---|---|---|
| Add a node, 3 to 4 | about 75% of keys move | about 25% move, to the new node |
| Remove a node, 4 to 3 | about 75% move | about 25% move, those of the lost node |
| A node crashes | most reads miss together | only its share misses |
The better rule
A hash ring
This is a short version. Consistent hashing has the full story.
- 1
Place the nodes
Hash each node to a place on a circle of numbers, for example 0 to 2^32.
- 2
Place the key
Hash the key with the same function.
- 3
Walk clockwise
The first node you meet owns the key. Past the end, continue from the start.
- 4
Change the group
A new node takes only the keys between itself and the node before it.
Walk clockwise
Virtual nodes make the shares even
| Points per server | Largest share of 3 servers |
|---|---|
| 1 | 52% |
| 10 | 41% |
| 150 | 36% |
Who decides
Three places for the rule
Client, proxy or cluster
| System | Rule | Who decides |
|---|---|---|
| Memcached | Usually a hash ring | Client library, or a proxy |
| Redis Cluster | 16,384 hash slots, not a ring | Servers hold the map, clients learn it |
| Dynamo (the paper) | Ring with virtual nodes | Any node |
| Cassandra | Token ring | Any node |
Growing
Adding and removing nodes
Adding a node without a stampede
Adding a node
- It joins at its points on the ring
- It takes keys from each neighbour before its points
- About 1 in N+1 keys move, the rest stay
- Load hot keys first, then shift traffic slowly
Removing a node
- Its keys go to the next nodes clockwise
- All other keys stay
- About 1 in N keys miss
- If it is planned, copy its keys first
Rules of thumb for automatic scaling
| Signal | Action |
|---|---|
| Memory above 80% and evictions rise | Add a node |
| Memory below 30% for days | Remove a node, slowly |
| One node much hotter than the rest | Look for a hot key first, a new node will not help |
Two levels
A local cache in front
L1, then L2, then the database
Average read time with two levels
Trade-offs
What you gain and what you pay
Advantages
- Grows by adding nodes
- All application servers share the same data
- Can survive a node loss, and stays available with copies
- Few keys move when the group changes
- Load is spread
Challenges
- A network trip for each call
- Turning values into bytes and back
- More to operate
- Hot spots: one very hot key still lives on one node
- A network split can leave clients with different views of the group
A node does not answer. What now?
Summary
The whole topic in one minute.
A distributed cache spreads keys over servers by a rule every client can follow. The rule should move as few keys as possible when the group changes, which is what a hash ring or a slot table does.
You pay for it with a network trip, serialisation and more parts that fail. Use a local cache for small hot data, a shared one for everything else, and size the database for the moment a node is lost.
- A distributed cache spreads keys over nodes by a rule all clients share.
- A remainder moves about 75% of keys from 3 to 4 nodes. A ring moves about 25%.
- Virtual nodes even out the shares and spread the load of a lost node.
- A shared cache costs a network trip. A local cache in front hides it for hot keys.
- Size the database for the misses after a node is lost.