SkillSprout
Chapter 12 · Lesson 2
Beginner
3 min read

Caching Strategies and Eviction

Overview

Having a cache is not enough: you must decide how data gets into it, how writes are handled, and what to throw out when it is full. These choices decide how fast, fresh and safe your system is.

Detailed Notes

What is a caching strategy

A strategy is the rule for how the application, the cache and the database work together on reads and writes. An eviction policy is the rule for which entry to remove when the cache is full.

The problem it solves

Memory is limited and the cache must stay in sync with the database well enough. Without clear rules you either serve wrong data, lose writes, or fill the cache with junk nobody reads.

How it works

Read strategies

  • Cache-aside (lazy loading): the app checks the cache; on a miss it reads the database, then stores the result. The most common pattern. Only requested data is cached, and if the cache dies the app still works (just slower).
  • Read-through: same idea, but the cache itself loads from the database on a miss. The app only talks to the cache.

Write strategies

  • Write-through: write to the cache and the database together. Always fresh, but each write is slower.
  • Write-back (write-behind): write to the cache, acknowledge, and flush to the database later. Very fast writes, but data can be lost if the cache crashes before flushing.
  • Write-around: write only to the database; the cache fills on later reads. Good when written data is rarely read soon.

With cache-aside, the usual way to handle updates is to update the database, then delete the cache entry. The next read reloads fresh data. Deleting is safer than updating the cache, because two concurrent writers could otherwise leave an old value behind.

Eviction policies (what to remove when full)

  • LRU (least recently used): remove what has not been touched for the longest. The popular default.
  • LFU (least frequently used): remove what is used least often.
  • FIFO: remove the oldest entry.
  • TTL: entries expire after a set time regardless of use. Often combined with LRU.

A simple example

A small shelf near your desk holds your most-used tools. When a new tool needs space, you put back the one you have not touched for months. That is LRU.

System design example

A profile service uses cache-aside with Redis: read key user:7, on a miss load from the database and cache it with a 10-minute TTL and LRU eviction. When a user edits their profile, the service writes to the database and deletes user:7. A payments counter that must never lose a write would use write-through, or skip the cache entirely, never write-back.

Summary

  • Cache-aside: app loads on a miss. Read-through: the cache loads.
  • Write-through is safe but slower; write-back is fast but can lose data.
  • After a database write, delete the cache entry instead of updating it.
  • LRU is the common eviction default; TTL limits staleness.

Architecture Diagram

Cache-aside with delete on write
Architecture
Application
CacheTTL + LRU
Databasesource of truth

The database stays the source of truth.

  1. 1The app asks the cache first.
  2. 2On a miss it reads the database.
  3. 3It stores the result in the cache with a TTL.
  4. 4On a write it updates the database and deletes the cache key.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course