A cache inside each server, or one shared cache for all of them? Speed against consistency.
A local cache lives inside each app server's own memory. A distributed cache is a separate service (like Redis or Memcached) that all app servers share.
A local cache (also called an in-process or in-memory cache) is just a data structure inside your program: a map with a size limit and expiry times. Libraries such as Caffeine or Guava in Java give you this ready-made. Reading from it takes nanoseconds to microseconds, because there is no network, no copying between processes and no waiting.
The catch is that if you run ten copies of your app behind a load balancer, you have ten separate caches. Each one fills up on its own, and each one can hold a different version of the same data.
A distributed cache moves the cache out of the app. All the app servers talk to the same cache service over the network. There is one copy of each item (or a few, for safety), so everyone sees the same value.
| Local cache | Distributed cache | |
|---|---|---|
| Where it lives | Inside each app process | A separate cache service (Redis, Memcached) |
| Read speed | Memory speed, no network | One network round trip (often well under a few milliseconds) |
| Copies of the data | One per app server, can differ | One shared copy (maybe replicas) |
| Size limit | Part of the app heap, repeated on every server | Spread over many machines, much larger |
| After a restart | Starts empty (cold) for that server | Keeps its data (if the cache itself stays up) |
| Update everywhere | Hard: needs expiry or a broadcast | Easy: change the one copy |
| If it fails | Only that server is affected | Every server loses it at once |
| Extra cost | None to run | Servers to run, secure and monitor |
| Method | How it works | Good and bad |
|---|---|---|
| Short TTL | Each local copy expires after a few seconds | Simple. The data can be stale for up to the TTL. |
| Broadcast invalidation | On a change, publish a message (for example with pub/sub) and every server drops that key | Fresher. Messages can be lost or late, so keep a TTL as a safety net. |
| Version check | Store a version number in the shared cache. The local copy is used only if its version matches | Always correct, but every read still costs a small check. |
Each app server keeps its own copy in memory. Fast, but the copies do not know about each other, and nobody tells them when the data changes.
A simplified, deterministic model. Read times are illustrative (0.05 ms local, 1 ms to the shared cache, 20 ms to the database), not measurements. The writer updates the database and, when there is a shared cache, replaces its copy at the same moment. Servers start 2 seconds apart so their local copies do not all expire together.
| Term | Meaning in easy words |
|---|---|
| Local cache | A cache inside the app process, in the appβs own memory |
| In-process cache | Another name for a local cache |
| Distributed cache | A cache service shared by many app servers, spread over many machines |
| L1 / L2 | First-level (local) and second-level (shared) cache |
| Round trip | One request out and one answer back over the network |
| Stale data | A cached value that no longer matches the truth |
| Inconsistency | Different servers returning different answers for the same key |
| TTL | Time to live: how long a cached copy may be used |
| Invalidation message | A message telling servers to drop a cached key (often through pub/sub) |
| Cold start | A cache that has just started and is empty, so nearly every read misses |
| Heap | The memory area of a program. A local cache has to fit inside it |
| Choice | We gain | We lose |
|---|---|---|
| Local only | Speed and simplicity | Consistency between servers, shared capacity |
| Shared only | One truth, big capacity | A network trip on every read, a shared dependency |
| Two levels | Speed and bounded staleness | More moving parts, more places to be stale |
| Long local TTL | More local hits, fewer shared reads | Staler answers |
| Short local TTL | Fresher answers | More trips to the shared cache |
| Broadcast invalidation | Fast refresh after a change | Needs a message channel, and lost messages must be handled |