Card 04/ 07
All 7 cards
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.
| Strategy | On a read | On a write |
|---|---|---|
| Cache-aside | Check the cache; miss, then read the database and store the result | Write the database, then evict or update the cache entry separately |
| Write-through | Check the cache; misses are rare because writes keep it current | Write 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.