0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

Cache TTL and Invalidation

Manage expiration and eviction.

Cache TTL and Invalidation is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why TTL Matters

Cached data can go stale. A TTL (time-to-live) automatically expires entries after a set duration, bounding how stale data can get.

Setting a Default TTL

Apply a TTL to all caches via the default configuration.

RedisCacheConfiguration config =
    RedisCacheConfiguration.defaultCacheConfig()
        .entryTtl(Duration.ofMinutes(10));

Per-Cache TTL

Different data ages at different rates — give each cache its own TTL.

Map<String, RedisCacheConfiguration> configs = Map.of(
    "products",
        RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours(1)),
    "prices",
        RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(1)));

Caffeine TTL via Properties

For a local Caffeine cache, set expiry in properties.

spring.cache.caffeine.spec=expireAfterWrite=300s,maximumSize=1000

Explicit Invalidation

TTL handles time-based expiry; @CacheEvict handles event-based invalidation when data changes.

@CacheEvict(value = "products", key = "#id")
public void delete(Long id) {
    repository.deleteById(id);
}

Write-Through Updates

Pair an update with @CachePut so the cache reflects the new value immediately instead of waiting for expiry.

@CachePut(value = "products", key = "#product.id")
public Product update(Product product) {
    return repository.save(product);
}

Clearing a Whole Cache

After a bulk change, evict all entries to force fresh reads.

@CacheEvict(value = "products", allEntries = true)
public void importBatch(List<Product> items) {
    repository.saveAll(items);
}

Eviction Policies

When a cache hits its size limit, an eviction policy decides what to remove. Common policies: LRU (least recently used) and LFU (least frequently used).

Bounding Cache Size

Limit memory by capping the number of entries with Caffeine's maximumSize.

spring.cache.caffeine.spec=maximumSize=10000

Cache Stampede

When a popular key expires, many requests miss at once and all hit the database — a stampede. Mitigate with short staggered TTLs or refresh-ahead strategies.

Monitoring Hit Rate

Track the cache hit rate (hits ÷ total lookups). A low rate means the cache is not helping — revisit TTL, keys, or whether caching fits the access pattern.

Quick Check

Test your understanding of TTL and invalidation.

Recap

You learned cache expiry and invalidation:

  • TTL via entryTtl bounds staleness
  • Per-cache TTLs for different data
  • @CacheEvict / @CachePut for event-based invalidation
  • Eviction policies (LRU/LFU), size limits, and stampede awareness

Frequently asked questions

Is the “Cache TTL and Invalidation” lesson free?

Yes — the full text of “Cache TTL and Invalidation” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “Cache TTL and Invalidation”?

Manage expiration and eviction. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cache TTL and Invalidation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Spring Cache Abstraction
  2. @Cacheable, @CachePut, @CacheEvict
  3. Redis as a Cache Provider
  4. Cache TTL and Invalidation
← Back to Spring Boot 4 Microservices & REST APIs