Part 19 · Applications

Caching: keys, freshness and invalidation

Prerequisites: 01-ecosystem

What happens on a cache hit and miss?

Read request: tenant + policy ID; Cache lookup: complete key; Return cached value: hit; Database load: miss; Populate cache: provider policy. Connections: Read request to Cache lookup (lookup); Cache lookup to Return cached value (hit); Cache lookup to Database load (miss); Database load to Populate cache (loaded result)
Cache-aside conceptual flow. TTL, stampede control and update consistency are provider/application choices. [S36] [S37]

Goal & mental model verified

Spring’s cache abstraction delegates storage to a backing implementation. @Cacheable can reuse results; eviction and updates define freshness behavior. An annotation alone does not define TTL or make a distributed cache consistent.

[S36] [S37]

Worked example · design exercise synthesis

For a policy summary, use a key containing tenant and policy ID. Evict or update affected summaries after successful policy changes under an explicitly chosen consistency policy.

[S36] [S37]

Engineering decision synthesis

Cache expensive, repeatedly read data with an acceptable staleness budget. Choose local versus shared storage by consistency, footprint and latency requirements.

[S36] [S37]

Pitfall & diagnosis synthesis

Missing tenant identity in a key can reuse another tenant’s data. Default proxy-based caching also has self-invocation limits. Cache invalidation and database commit ordering need care.

[S36] [S37]

Improve & validate synthesis

Measure hit ratio, stale reads and miss cost. Exercise concurrent misses and updates; bound entry size and TTL using the provider’s configuration.

[S36] [S37]
Keep this: Every cache needs an owner, a key contract and a freshness promise.
Check yourself: Does @Cacheable itself choose a TTL?

No. Expiration is generally a backing-cache/provider concern.

Sources & further reading

  1. [S36] Cache abstraction

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: CacheManager delegates to backing cache

    Read the linked section to validate the mechanism and its version-specific constraints.

  2. [S37] Cache annotations

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: Cacheable and eviction; Key design and proxy behavior

    Read the linked section to validate the mechanism and its version-specific constraints.