Video in progress

Strong Junior 🏗️ System design

What is the difference between a local cache (for example Caffeine) and a distributed cache like Redis, and when do you pick each?

A local cache lives in the application's memory — very fast, no network, but every instance has its own copy and they don't know about each other. Redis is one for all instances and invalidation is easy, but every read goes over the network.

📖 Answer

The difference is easy to see in numbers: a read from Caffeine takes nanoseconds, from Redis ~0.5–1 ms even within the same data center. At 50 thousand reads per second that adds up.

But a local cache has a cost. With 8 pods, you have 8 different caches. An admin changed a price, you cleared the cache on one pod — the other 7 keep showing the old price until the TTL runs out. When the user refreshes the page, they see the new price one time and the old one the next, because the LB sends them to a different pod.

Cache<Long, Category> categories = Caffeine.newBuilder()
        .maximumSize(10_000)
        .expireAfterWrite(Duration.ofMinutes(5))
        .build();

This is how I usually split them:

  • Local — data that rarely changes and where a bit of staleness doesn't hurt: categories, settings, exchange rates (1 minute). Always set maximumSize — the heap isn't unlimited.
  • Redis — user-specific or fast-changing data, sessions, counters, anything every pod must see the same way.

People also use both together: local first, then Redis, then the DB. In that case, keep the local cache's TTL short, or on a change send an "evict this key" message to every pod through Redis pub/sub.