Distributed Cache system design: how it works
Spreads cached data over several machines with consistent hashing, so adding or losing one moves only a small share of keys.
Loading the interactive diagram…
How Distributed Cache works, step by step
Spreads cached data over several machines with consistent hashing, so adding or losing one moves only a small share of keys.
People
- User
- Client · Hypothetical. Someone opening a profile page.
App, config and database
- App server
- Service · Hypothetical. Runs the site, and decides which cache node holds each key.
- Cluster config
- Service · Hypothetical. Keeps the list of healthy cache nodes, which is the ring every app server uses.
- Database
- Database · Hypothetical. The source of truth. Much slower per read than the cache, and much more expensive to scale.
Cache cluster
- Cache node A
- Cache · Hypothetical. Holds about a third of the keys, in memory.
- Cache node B
- Cache · Hypothetical. Holds about a third of the keys, including user:42.
- Cache node C
- Cache · Hypothetical. Holds about a third of the keys.
Flow: Read from the cache (hit)
The usual case: the key's node has it, and the database isn't touched.
- User → App server (GET /profile/42). Someone opens user 42's profile.
- App server → Cache node B (GET user:42). The app server hashes "user:42" onto the ring: it belongs to node B. No lookup service is asked.
- Cache node B → App server (Hit: Asha, Pune…). Node B has it in memory. Well under a millisecond.
- App server → User (Profile page). The page is built without touching the database.
Flow: Another key, another node
Different keys hash to different nodes, so the load and the memory are shared across the cluster.
- User → App server (GET /profile/7). Someone opens a different profile.
- App server → Cache node A (GET user:7). "user:7" hashes to a different point on the ring, which belongs to node A. Each node holds about a third of the keys.
- Cache node A → App server (Hit: Meera, Chennai…). Node A has it.
- App server → User (Profile page). Served from node A, without touching node B or the database.
Flow: Read a key the cache doesn't have (miss)
Cache-aside: read the database, then store the answer in the cache with an expiry time.
- User → App server (GET /profile/77). Someone opens a profile nobody has viewed lately.
- App server → Cache node B (GET user:77). "user:77" also hashes to node B.
- Cache node B → App server (Miss). Node B doesn't have it: it was never cached, or it expired, or it was pushed out to make room.
- App server → Database (SELECT user 77). So the app server reads the database.
- Database → App server (Ravi, Delhi…). The database answers, more slowly than the cache would have.
- App server → Cache node B (SET user:77, 10 min). It stores the answer on node B with a 10-minute expiry, so stale data can't live forever.
- Cache node B → App server (OK). The next read is a hit.
- App server → User (Profile page). The page is served.
Flow: Change the data
Write the database, then delete the cached copy, so the next read fetches the new version.
- User → App server (Rename to Asha K). User 42 changes their name.
- App server → Database (UPDATE user 42). The database is updated first: it's the source of truth.
- Database → App server (Updated). The change is saved.
- App server → Cache node B (DELETE user:42). Then the cached copy is deleted rather than overwritten, which avoids most races between two writers. One rare race remains: a slow read can put an old value back just after the delete. That's one more reason every cached copy has an expiry time.
- Cache node B → App server (Deleted). Gone from node B.
- App server → User (Saved). The user sees their new name on the next page.
Flow: A cache node fails
Node B stops answering. The ring drops it, and only B's keys move, onto the other nodes; for a while those reads miss and land on the database.
- User → App server (GET /profile/42). Another visit to user 42's profile.
- App server → Cache node B (GET user:42). The app server asks node B, as the ring says.
- Cache node B → App server ((timeout)). Node B has crashed. After a short timeout, the app server gives up on it.
- App server → Cluster config (Current ring?). It fetches the latest ring from the cluster config.
- Cluster config → App server (A and C only (v15)). Health checks already removed B. With consistent hashing, only B's third of the keys move; A's and C's keys stay where they are. With plain "hash mod 3", almost every key would have moved.
- App server → Cache node C (GET user:42). On the new ring, user:42 belongs to node C.
- Cache node C → App server (Miss). C has never held it.
- App server → Database (SELECT user 42). So this read, and many like it, falls through to the database until C warms up. This burst is why the database must cope with a lost cache node, or why nodes have replicas.
- Database → App server (Asha K, Pune…). The database answers.
- App server → Cache node C (SET user:42, 10 min). Now node C has it.
- Cache node C → App server (OK). Node C now has it; later reads are hits again.
- App server → User (Profile page). The user just saw a slightly slower page.
HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.