Card 04/ 07

ConceptDifficulty: Advanced1 min

Cache-Aside vs Write-Through

@Cacheable on its own is one specific strategy, not the only one — it's worth naming which, so the choice is deliberate rather than accidental.

Two caching strategies
StrategyOn a readOn a write
Cache-asideCheck the cache; miss, then read the database and store the resultWrite the database, then evict or update the cache entry separately
Write-throughCheck the cache; misses are rare because writes keep it currentWrite the cache and the database together, in the same call

@Cacheable plus @CacheEvict on a write is cache-aside: the read path and the write path touch the cache independently, and a miss is an expected, ordinary event. @Cacheable plus @CachePut on the same key is closer to write-through: the write call updates the cache itself, in the same method, rather than just invalidating what was there.