Skip to main content

Command Palette

Search for a command to run...

Caching in System Design: The Art of Remembering Things Fast.

Published
โ€ข5 min read
A

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.โ€ ๐Ÿง ๐Ÿ’ก

More from this blog

Stack OverFlowed

14 posts