System design

Stage 1 · Lesson 8 of 10

Caching patterns, eviction and invalidation

Core concepts and building blocks

A cache keeps popular data in memory so most reads skip the slower database. The usual pattern is : read from the cache first, and on a miss, read the database and put the result in the cache. Write-through updates the cache and the database on every write.

Write-back writes to the cache and saves to the database later, which is faster but can lose data if the cache fails. Memory is limited, so a decides what to throw out, for example LRU, which drops the least recently used item.

The hard part is : any cached value can be out of date. Choose a TTL (time to live) you can defend, or delete the key when the data changes. Also watch for a : a popular key expires and thousands of requests hit the database at once.

(one request refills the key while the others wait) or refreshing early prevents it.

A question to check yourself, an AI practice session and 4 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CoursePaid
    Grokking System Design Fundamentals

    Design Gurus · Beginner · Subscription

    Load balancing, API gateway, proxies, caching, CAP and Bloom filters: week 1 of the plan in order

  • GitHub repoFree
    The System Design Primer

    Donne Martin · Intermediate

    Free: its scalability, latency, availability, CDN, load balancer and cache sections

Keep handy

  • Free Design Gurus concept index for looking up a term in the middle of a lesson

Also worth your time

Mark this stage as not done?