Spring Cache Abstraction and Redis Integration

Caching in Spring applications: cache-aside and other patterns, @Cacheable, @CachePut, @CacheEvict and @Caching, keys and conditions, cache providers (Caffeine locally, Redis distributed), Redis configuration with TTLs and serialisation, multi-level caching, invalidation and consistency, cache stampede protection, Hibernate second-level cache, and monitoring.

On this page

Simple definition#

The Spring Cache abstraction lets you cache method results declaratively with @Cacheable and friends. Where data is cached is pluggable: Caffeine in local memory, Redis (or Valkey) shared across instances, Hazelcast and others.

The problem#

Repeated expensive calls (database aggregations, remote price lookups, reference data) waste latency and load. Ad-hoc Map caches have no eviction or TTL, are not shared, and are scattered through the code.

Caching patterns#

PatternHowNotes
Cache-aside (lazy loading)Read the cache; on a miss, load from the source and storeWhat @Cacheable does; most common
Read-throughThe cache loads on a miss itselfCache library feature
Write-throughWrite the cache and the database togetherConsistent, but slower writes
Write-behindWrite the cache, flush to the DB asynchronouslyFast; risk of loss
Refresh-aheadRefresh hot entries before expiryAvoids misses on hot keys

Annotations#

@Service
@CacheConfig(cacheNames = "products")
class ProductService {

    @Cacheable(key = "#sku", unless = "#result == null")              // cache-aside
    public ProductDto find(String sku) { return mapper.toDto(repository.findBySku(sku)); }

    @CachePut(key = "#result.sku")                                    // always run, then update the cache
    public ProductDto update(UpdateProduct cmd) { ... }

    @CacheEvict(key = "#sku")                                         // remove on delete
    public void delete(String sku) { ... }

    @CacheEvict(allEntries = true)                                    // flush the whole cache (use sparingly)
    @Scheduled(cron = "0 0 3 * * *")
    public void nightlyReset() { }

    @Cacheable(cacheNames = "prices", key = "#sku + ':' + #currency", sync = true)   // sync: one loader per key per JVM
    public Price price(String sku, String currency) { ... }
}
  • Enable caching with @EnableCaching. Spring Boot auto-configures a CacheManager from the classpath.
  • Caching is AOP, so self-invocation and private methods are not cached. See AOP and proxies.
  • Keys are SpEL. See SpEL. Cached values should be immutable DTOs, not JPA entities.

Providers#

Local vs distributed cache
flowchart LR
    subgraph POD1["Instance 1"]
        A1["@Cacheable"] --> L1["Caffeine (L1)"]
    end
    subgraph POD2["Instance 2"]
        A2["@Cacheable"] --> L2["Caffeine (L1)"]
    end
    L1 --> R["Redis / Valkey (L2, shared)"]
    L2 --> R
    R --> DB[("Database / remote API")]
Caffeine (local)Redis / Valkey (distributed)
LatencyNanosecondsAbout 0.2–1 ms network round trip
Shared between instances❌ (each pod has its own)✅
Survives restarts❌✅ (with persistence)
EvictionW-TinyLFU, size and time basedTTL, maxmemory policies
Best forHot, small reference data; tolerating a little stalenessShared state, sessions, rate limits, larger caches

Valkey is the open-source fork of Redis (Linux Foundation), API-compatible. Spring Data Redis works with both.

Redis configuration#

spring:
  cache:
    type: redis
  data:
    redis:
      host: redis
      port: 6379
@Bean
RedisCacheManagerBuilderCustomizer cacheTtls() {
    var json = RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json());
    return b -> b
        .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(10))
                .disableCachingNullValues()
                .serializeValuesWith(json))
        .withCacheConfiguration("prices", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(30)));
}
  • Always set TTLs. Unbounded caches become stale and grow.
  • Use JSON serialisation (not JDK serialisation) for safety and readability. Version cache key prefixes when DTOs change shape.

Invalidation and consistency#

There are only two hard things in computer science: cache invalidation and naming things. (Phil Karlton)

  • TTL bounds staleness. Pick it per data type.
  • Evict on write (@CacheEvict) after the transaction commits. Evicting before commit lets another request re-cache the old value. Spring’s TransactionAwareCacheManagerProxy or @TransactionalEventListener(AFTER_COMMIT) handles this.
  • Multiple instances with local caches: broadcast invalidations (Redis pub/sub, Kafka) or keep local TTLs very short.
  • Never cache authorisation-sensitive data under keys that ignore the user or tenant.

Cache stampede#

When a hot key expires, many requests miss at once and hammer the database. Mitigations:

  • @Cacheable(sync = true) (one loader per JVM)
  • jittered TTLs
  • refresh-ahead (Caffeine refreshAfterWrite)
  • distributed locks for very expensive loads
  • serving stale data while revalidating.

Hibernate second-level cache#

JPA’s second-level (L2) cache caches entities across sessions (with JCache providers such as Ehcache or Caffeine). It suits read-mostly reference entities (countries, product categories). It is risky for frequently updated or bulk-updated data. The Spring cache on service DTOs is usually clearer.

Monitoring#

Micrometer exposes cache metrics (cache.gets{result=hit|miss}, cache.puts, cache.evictions) for Caffeine and Redis. Track the hit ratio and load times; a cache with a 10% hit ratio is just overhead.

Common mistakes#

Quick check#

What pattern does @Cacheable implement? Cache-aside: check the cache, call the method on a miss, and store the result.

@CachePut vs @CacheEvict? @CachePut always executes the method and updates the cache with the result. @CacheEvict removes entries.

Caffeine or Redis? Caffeine for very fast, per-instance caching of hot or reference data. Redis or Valkey when the cache must be shared across instances or survive restarts.

What is a cache stampede, and one fix? Many concurrent misses for an expired hot key overload the source. Fixes: sync = true, jittered TTLs, refresh-ahead or locks.

Why evict after commit? Evicting before commit lets another request reload and re-cache the old data before the new data is committed.

Summary#

  • Spring Cache is declarative caching through AOP; the provider is pluggable.
  • Caffeine locally, Redis or Valkey for shared caches; always set TTLs and use JSON serialisation.
  • Invalidate after commit, guard hot keys against stampedes, and monitor the hit ratio.

References#