Spring Boot Caching for Speed (Beginner Guide)
Speed up Spring Boot apps with caching: how caching works, @EnableCaching, @Cacheable to store results, and @CacheEvict to avoid stale data. Plus when to use Redis.
If your app keeps asking the database for the same rarely-changing data over and over, you're doing needless work — and your users wait longer than they should. Caching fixes this: store the result of an expensive operation the first time, then serve it instantly from memory afterwards. Spring Boot makes caching remarkably easy with just a couple of annotations. Let's speed things up.
What caching is (with a simple analogy)
Imagine you're asked "what's the capital of France?" a hundred times. The first time you might look it up; after that, you just remember "Paris" and answer instantly. A cache is your app's memory of recent answers: the first request does the real work and stores the result; later identical requests get the stored answer immediately, skipping the expensive step (a database query, a slow calculation, an external API call).
When caching helps (and when it doesn't)
- Great for: data that's read often but changes rarely — categories, settings, a user's profile, reference lists.
- Be careful with: data that changes frequently, or where showing a slightly stale value would be a problem (e.g. account balances).
The core trade-off of caching is speed vs freshness: you gain performance but risk serving slightly out-of-date data. Choose what to cache with that in mind.
Step 1: Turn caching on
Add @EnableCaching to your main application class (or any config class). That one annotation activates Spring's whole caching system:
@SpringBootApplication @EnableCaching public class MyApp { ... }
Step 2: Cache a method's result with @Cacheable
Put @Cacheable on a method, and Spring caches whatever it returns. The next call with the same arguments skips the method entirely and returns the stored result:
@Service public class ProductService { @Cacheable("products") // cache named "products" public Product findById(Long id) { // this slow DB lookup runs ONCE per id, then is cached return repo.findById(id).orElseThrow(); } }
First call with id=5: the method runs and the result is stored under key 5 in the "products" cache. Every later call with id=5: Spring returns the cached Product without touching the database. The method body simply doesn't execute. Instant.
Step 3: Keep the cache fresh with @CacheEvict
Here's the catch: if a product is updated, the cache still holds the old version — that's the "stale data" problem. The fix is to evict (remove) the cached entry whenever the data changes, so the next read re-fetches fresh:
@CacheEvict(value = "products", key = "#product.id") public Product update(Product product) { return repo.save(product); // remove the stale cache entry so next read is fresh }
@Cacheable), evict on writes (@CacheEvict). Forgetting to evict is the #1 caching bug — it leads to users seeing outdated information. Pair every cache with its eviction.
Where the cache actually lives
Out of the box, Spring caches in your app's memory (fine for a single server). For bigger systems — multiple servers that need to share a cache — teams use a dedicated cache server like Redis, which Spring integrates with by adding a dependency and a little config. Same annotations, distributed storage. Start with the in-memory default; reach for Redis when you scale out.
Caching feels almost magical the first time you use it — I once turned a sluggish endpoint into an instant one just by adding @Cacheable, and it genuinely felt like cheating. But I also learned the hard lesson behind that magic: I once forgot to evict, and users kept seeing stale data long after it changed, which was a confusing bug to track down. So my advice is the rule above — cache on reads, evict on writes, always paired. On this website, rarely-changing things like settings and category lists are cached so pages render faster without hammering the database. Used carefully, caching is one of the easiest big performance wins you'll find.
Key takeaways
- Caching stores expensive results in memory so repeat requests are served instantly.
- Best for data read often but changed rarely; the trade-off is speed vs freshness.
- Enable with
@EnableCaching, cache method results with@Cacheable. - Always pair with
@CacheEvicton updates to avoid stale data; use Redis for shared caches at scale.
← Previous: Spring Lesson 13 — Actuator & Logging
Next: Spring Lesson 15 — Async & Scheduling →
↑ Back to the Spring Boot roadmap