Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Compare a local cache such as Caffeine with a distributed cache such as Redis in a Spring Boot service using the Spring cache abstraction.

A local cache such as Caffeine lives inside the JVM, so a hit costs nanoseconds to microseconds, but each instance keeps its own copy and copies can disagree. A distributed cache such as Redis is shared by all instances, so they see the same data, but each lookup is a network call plus serialization. Spring's cache abstraction, @Cacheable with a CacheManager, works with both by switching the cache manager. Local caches need size limits and short TTLs or invalidation messages, and layering both, local in front of Redis, is common for hot, rarely changing data.

Why this question is asked

Caching is often the first tool we reach for when a service gets slow, and choosing where the cache lives decides how fast it is, how much memory it uses and how stale the data can be. Interviewers ask this comparison to see whether we understand those trade-offs and how Spring lets us switch between them. In this answer, we will recap the Spring cache abstraction, then look at Caffeine and Redis in turn, see how copies diverge, and finish with how to configure limits and when to layer both.

1A Quick Recap: Spring's Cache Abstraction

Spring separates what we cache from where it is stored. We annotate a method with @Cacheable("products"), and Spring, through a proxy around the bean, checks the cache named products before calling the method. On a hit it returns the stored value; on a miss it calls the method and stores the result. @CacheEvict removes entries and @CachePut updates them.

Where the entries live is decided by the CacheManager bean. Spring Boot creates one automatically based on what is on the classpath and the spring.cache.type property. So the same annotated code can use Caffeine or Redis with a configuration change.

Java
@Service
class ProductService {
@Cacheable(cacheNames = "products", key = "#id")
public ProductView find(long id) { // runs only on a cache miss
return repository.loadView(id); // about 8 ms from the database
}
@CacheEvict(cacheNames = "products", key = "#id")
public void update(long id, ProductChange c) { ... }
}
// first call find(42): 8 ms (miss) second call: microseconds (hit)

2Caffeine: In-Process and Fastest

Caffeine is a high-performance cache library that stores entries in the JVM heap. A lookup is a hash table read in the same process, with no network and no serialization, so it takes well under a microsecond. It also has a smart eviction policy that keeps the entries most likely to be used again when the cache is full.

The costs follow from living inside the instance. Entries use heap memory, so a large cache increases GC work. The cache is empty after every restart and deployment. And each instance has its own copy: with 12 instances, the same product may be loaded 12 times and held 12 times.

Properties
# application.properties
spring.cache.type=caffeine
spring.cache.cache-names=products
spring.cache.caffeine.spec=maximumSize=10000,expireAfterWrite=60s
# at most 10,000 entries per instance, each at most 60 s old

3Redis: Shared Across Instances

Redis is an in-memory data store running as its own server. With Spring's Redis cache manager, every instance reads and writes the same entries. A product loaded by one instance is a hit for all the others, the cache survives deployments of our service, and an eviction is seen by everyone at once.

The cost is a network round trip, typically a fraction of a millisecond to a millisecond inside a data centre, plus serializing the value to bytes and back. Values must be serializable, and the serialization format matters: JDK serialization is the default in some setups and is brittle when classes change, so JSON or another explicit format is a common choice. Redis is also another system that must be available; if it is down, every lookup becomes a miss or an error.

Java
spring.cache.type=redis
spring.cache.redis.time-to-live=10m
spring.cache.redis.key-prefix=catalog::
spring.data.redis.host=redis
# every instance shares the same entries; each lookup is a network call

4How Local Copies Diverge

The main risk of a local cache is that instances disagree. When the price of product 42 changes, the instance that handled the update evicts its entry, but the other 11 instances keep the old price until their entries expire. A user who refreshes the page and reaches a different instance sees the price jump back and forth.

Step throughAn update evicts only one local cache
AdminInstance A (Caffeine)Instance B (Caffeine)Database
update price of product 42
UPDATE price = 19.99
evict products::42 in A only
still holds price 24.99 until expiry
user on B sees the old price

5 messages in order. Play them, or step through them one at a time.

With local caches, an eviction on one instance does not reach the others.

So local caches need two safeguards. A size limit, maximumSize or maximumWeight, protects the heap. A short time-to-live bounds how stale an entry can get, for example 60 seconds; or, when staleness must be shorter, invalidation messages: the instance that changes data publishes an event, for example through Redis pub/sub or Kafka, and every instance evicts the key.

5Layering Both for Hot, Rarely Changing Data

The layers can be combined. A small Caffeine cache with a short TTL sits in front of Redis. Most lookups for hot items hit the local cache in microseconds; misses go to Redis, which is shared; and only Redis misses reach the database. This suits data read very often and changed rarely, such as product details, configuration or reference data.

Spring does not ship a two-level cache manager, so layering means a small custom Cache implementation that checks local, then Redis, or a library that provides it. The local TTL must be short, because the local layer is the one that can go stale.

Caffeine, Redis or both
Caffeine (local)Redis (distributed)Both
Lookup costunder 1 microsecondnetwork round trip + serializationlocal for hot keys
Consistency across podseach pod differsone shared copylocal layer may lag
After a deploymentemptystill warmRedis still warm
Memoryservice heap, per podRedis serversmall local + Redis
Good forsmall, hot, tolerant datashared, larger datahot, rarely changing

6Follow-up questions

When is a local cache clearly the better choice?

When the data is small, read very often, changes rarely and a short delay in seeing changes is acceptable, for example feature configuration, country lists or exchange rates updated once a minute. The speed and the absence of another dependency outweigh the duplication.

What does @Cacheable(sync = true) do?

It makes concurrent misses for the same key wait while one thread loads the value, instead of all of them calling the method. With Caffeine it uses the cache's own per-key loading; with other caches the coordination may be per instance only, so with many instances one load per instance can still happen.

Why does serialization matter for Redis?

Every hit deserializes the value, which costs CPU, and the format must survive class changes between deployments. A value serialized by version 1 of a class may fail to deserialize in version 2 with JDK serialization. JSON with explicit types, and versioned key prefixes, make deployments safer.

7Conclusion

In this answer, we compared Caffeine and Redis behind Spring's cache abstraction. Caffeine is in-process and fastest but each instance has its own copy, so it needs size limits and short TTLs or invalidation messages. Redis is shared and survives deployments but adds a network call and serialization. The same @Cacheable code works with either through the CacheManager, and layering a small local cache in front of Redis is a common choice for hot, rarely changing data.

What to say in the interviewAbout 80 seconds aloud

Open with

Caffeine is a cache inside the JVM, so a hit is a heap read with no network or serialization, well under a microsecond. The trade-off is that every instance has its own copy: it is empty after a deployment, it uses heap, and copies diverge, so when one instance evicts product 42 after an update, the other instances still serve the old value until expiry.

Then, if they want more

Redis is a separate server shared by all instances, so there is one copy, it survives our deployments and evictions are seen by everyone, but each lookup is a network round trip plus deserialization, and Redis becomes a dependency.

In Spring both sit behind the same abstraction: I put Cacheable on the method and choose the CacheManager, for example spring.cache.type caffeine with a spec like maximumSize and expireAfterWrite, or redis with a time-to-live. For local caches I always set a size limit and a short TTL, or publish invalidation messages if staleness must be very short.

And for hot data that rarely changes, like product details, I would layer them: a small Caffeine cache with a short TTL in front of Redis, which Spring does not provide out of the box, so it needs a small custom cache or a library.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare a local cache such as Caffeine with a distributed cache such as Redis in a...": https://scrivio-interview-library.netlify.app/spring-boot/scalability/44f8cc9900/