Sub topic 1
Why and when to cache
Caching stores the result of an expensive operation so that later requests can reuse it instead of repeating the work. This chapter covers when caching pays off and what it costs. The later chapters in this tutorial show how to do it in Spring Boot.
Prerequisites
This chapter assumes you know:
- Basic Java and the structure of a Spring Boot application.
- How Spring beans and dependency injection work.
- How to add a dependency with Maven or Gradle.
- How to set values in
application.propertiesorapplication.yml.
What caching does
A cache is a fast store that sits in front of a slower source. The source can be a database, a remote HTTP service, or a heavy computation. On a request, the code first looks in the cache. If the value is there, that is a hit, and the slow source is skipped. If it is not there, that is a miss. The code then calls the source and saves the result for next time.
The benefit is lower latency and less load on the source. A memory lookup takes microseconds. A database query over a network takes milliseconds or more, and a remote API call can take far longer.
When caching helps
Caching works best when these conditions hold:
- Reads far outnumber writes. A product catalog is read thousands of times for each change.
- The same inputs repeat. If every request asks for a different key, the cache rarely hits.
- The result is expensive. Slow queries, rate-limited APIs, and heavy calculations all qualify.
- Slightly old data is acceptable. A product description that is a few minutes behind is usually fine.
- The result depends only on its inputs. The same arguments should always produce the same answer.
When caching hurts
Caching is a trade-off, not a free speedup. Watch for these costs:
| Cost | What happens |
|---|---|
| Stale data | The cache keeps an old value after the source changes. |
| Memory use | Entries take heap space or space in an external store. |
| Complexity | You must decide on keys, expiry, and how to remove entries. |
| Harder debugging | A bug may come from a cached value, not from current data. |
| Inconsistency across instances | With a local cache, each application instance holds its own copy. |
Avoid caching data that must always be exact, such as an account balance during a payment. Avoid caching results that depend on the current user or time unless that information is part of the key.
Measure before you cache
Do not add a cache on a guess. Find the slow operation first, using a profiler, query logs, or metrics. Then estimate the expected hit rate. A cache with a low hit rate adds cost and gives little benefit. After you add one, keep measuring. The final chapters in this tutorial show how to read hit and miss counters from Actuator.
Caching in Spring Boot
Spring Boot supports caching through an abstraction. You mark a method with an annotation, and Spring stores its return value in a cache. You can switch the storage, such as a local Caffeine cache or a shared Redis cache, without changing the method. The next chapter explains how this abstraction works.
Exercise
A service method returns a customer's current account balance, and the balance changes many times per minute. Is it a good candidate for caching? Give two reasons.
Show answer
Usually not. The data changes often, so cached values would quickly become stale. The balance must also be exact, so serving an old value is unacceptable. Frequent changes would also keep invalidating entries, which gives a low hit rate.
Exercise
Name three conditions that make an operation a good candidate for caching.
Show answer
Any three of these: reads greatly outnumber writes, the same inputs repeat, the operation is expensive, slightly stale data is acceptable, and the result depends only on its arguments.