Caching in System Design: The Art of Remembering Things Fast.
DevOps engineer & developer passionate about building scalable, reliable systems. I design and automate pipelines, manage cloud infrastructure, and ensure deployments run smoothly. Turning complex workflows into seamless operations is my craft.
๐ The Forgetful Student and the Cheat Notes
There was once a student who had a terrible memory. Every exam, he would forget half the answers by the time the teacher handed out the question paper.
One day, he came up with a brilliant idea โ he wrote tiny cheat notes and stuck them inside his sleeve.
When he saw a repeated question โ BAM! he just peeked at his sleeve and answered instantly.
No more flipping through heavy textbooks in panic.
But there was a catch:
Sometimes, he forgot to update his notes โ and wrote last yearโs wrong answers.
Once, all his notes fell out at the same time, and the teacher caught him (cache crash).
Caching works exactly the same way.
Cheat notes = cache.
Textbook = database.
Fast answers = cache hits.
Wrong/outdated answers = stale cache.
Teacher catching you = cache invalidation problem.
Moral of the story: Caching makes you look smarter and faster, but only if you keep your notes fresh.
Imagine this: you walk into your favorite coffee shop every morning.
Day 1: The barista asks for your order โ you say โdouble latte with oat milk.โ
Day 2: She remembers โ โSame as yesterday?โ โ
Day 3: You donโt even have to speak โ your drink is already waiting.
Thatโs caching. Instead of re-computing or re-fetching something every single time, the system keeps a shortcut memoryto serve it faster, cheaper, and smarter.
In system design, caching is like the espresso shot of performance โ it gives your system an instant energy boost.
1. ๐ Why Do We Need Caching?
Without caching:
Every request โ hits the database โ does expensive computation โ returns result.
At scale (millions of requests/sec), this melts your servers.
With caching:
Popular or repeated data โ served directly from memory or distributed cache.
Faster response, reduced DB load, lower costs.
๐ Rule of thumb: If youโre reading more than writing, cache is your best friend.
2. ๐งฉ Types of Caches
Caching isnโt one-size-fits-all. Depending on where you put it, caching plays different roles.
๐น Client-Side Cache
Stored in browser/local app (cookies, local storage, browser cache).
Best for: static assets (images, CSS, scripts).
๐น CDN (Content Delivery Network) Cache
Globally distributed servers that cache static assets near the user.
Example: Cloudflare, Akamai, AWS CloudFront.
Best for: images, videos, static pages.
๐ Think Netflix buffering โ your movie is cached in a data center near you, not in California.
๐น Reverse Proxy / Edge Cache
Systems like Varnish, Nginx cache API responses before they hit your app.
Best for: REST APIs, HTML pages, expensive server computations.
๐น Application Cache
Caching inside your app (in-memory).
Example: Python โ
functools.lru_cache, Java โ Guava Cache.Best for: function-level memoization.
๐น Distributed Cache
External caching systems like Redis, Memcached.
Shared by multiple servers, scalable, high-performance.
Best for: frequently accessed DB queries, session storage, leaderboards.
๐ Interview gem: โIโll use Redis for caching product catalog lookups to reduce DB load.โ
3. ๐งฎ Cache Strategies
Caching isnโt just where you store; itโs also how you decide what to store and when to throw it out.
๐ Cache-Aside (Lazy Loading)
App first checks cache โ if miss, load from DB โ update cache.
Pro: Simple, popular.
Con: First request = slow.
๐ Write-Through
Every write goes to cache and DB simultaneously.
Pro: Cache always fresh.
Con: Slower writes.
๐ Write-Back (Write-Behind)
Writes go to cache โ flushed to DB later.
Pro: Fast writes.
Con: Risk of data loss if cache crashes before syncing.
โณ Time-to-Live (TTL)
Data expires after a set time.
Example: Cache stock prices for 5 seconds.
๐ Interview tip: Always talk about TTL. Stale cache is one of the biggest problems in design.
4. ๐ฆ Cache Eviction Policies
Caches are limited in size. When full, something must go.
LRU (Least Recently Used) โ kick out the item not used for longest.
LFU (Least Frequently Used) โ kick out the least accessed item.
FIFO (First-In-First-Out) โ oldest goes first.
Random โ sometimes used in high-performance systems.
๐ Memcached = LRU by default. Redis supports multiple strategies.
5. โ ๏ธ Cache Problems (a.k.a. The Dark Side of Caching)
Caching is powerful but tricky.
Cache Invalidation โ hardest problem. When DB updates, cache must update too.
Cache Stampede โ cache expires, thousands of requests flood the DB at once.
- Solution: โdogpile preventionโ โ use locks, random TTLs.
Stale Data โ users see outdated info.
Cold Start โ first request after a cache reset is slow.
๐ Quote you can drop in interviews:
โThere are only two hard problems in computer science: cache invalidation, naming things, and off-by-one errors.โ ๐
6. ๐ Real-World Examples
Twitter Timeline โ Cached in Redis. Without it, DB would choke.
Instagram Feed โ Popular posts cached, while comments are pulled fresh.
Amazon Product Pages โ Cached with TTL; prices/stock refreshed regularly.
๐ Pro tip: In interviews, mention multi-level caching (e.g., CDN + Redis + local app cache).
7. ๐ Closing Thoughts
Caching is like coffee for your system:
Too little = sluggish performance.
Too much = jittery bugs and stale data.
The right balance = smooth and efficient.
Always remember to discuss:
Where youโll place the cache (client, edge, app, distributed).
How youโll update/evict data.
What consistency guarantees you need.
๐ Interview one-liner:
โCaching is about trading memory for speed โ and knowing when fresh data matters more than fast data.โ
โจ Fun closer for your blog:
โIf your system was a brain, caching would be the sticky notes it leaves everywhere to look smarter than it really is.โ ๐ง ๐ก