May we use cookies so Meta, Google, LinkedIn and ChatGPT see your visit and we learn which ads work? PrivacyMay we use cookies to see which pages help, and let Meta, Google, LinkedIn and ChatGPT see your visit, so we know which of our ads work? That way we spend less on ads that do not work. We do not sell your data, and the site works the same if you decline. You can change your answer at any time under Cookie settings at the foot of the page. Privacy policy
Loading...
System Design Masterclass · Core Fundamentals
Free preview: the first 2 parts of this 11-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$35 once, by card or PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$35 once, by card or PayPal
See the System Design coursein Core Fundamentals
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
Here's a number that should bother you: reading from a database takes about 1-10 milliseconds. Reading from an in-memory cache takes about 0.1 milliseconds. That's 10-100x faster.
Now multiply that by thousands of requests per second. If your API makes 5 database queries per request and you're handling 10,000 requests per second, that's 50,000 database queries per second. Your database is crying. Your users are waiting. Your cloud bill is growing.
But here's the thing. A large share of those queries are asking for the exact same data. Ten thousand people loading your homepage are all fetching the same featured products, the same trending posts, the same navigation menu. Yet your database dutifully executes the same query 10,000 times, returning the same result each time.
How large a share depends entirely on your traffic, and anyone who quotes you a single percentage for every application is guessing. What is not a guess is the shape: on almost every read-heavy site, a small number of items are requested enormously more often than the rest. That skew is what makes caching work at all.
Caching says: "Why not do that work once, store the result somewhere fast, and serve it from memory for the next 9,999 requests?"
This isn't a nice-to-have optimization. It's a fundamental architecture pattern, and the companies you use every day depend on it completely. Netflix's cache layer alone absorbs upwards of 30 million requests every second at peak. Facebook built a system called TAO whose own paper says it processes a billion reads a second. Both numbers are in the last slide of this lesson, with their sources.
Take those two figures seriously for a moment. Every one of those reads that the cache answers is a read the database never sees. Remove the cache and that traffic does not disappear, it lands somewhere else.
Caching is storing a copy of data in a faster storage layer so future requests for that data can be served without going to the slower original source.
The analogy that makes this click: imagine you're a librarian. Someone asks you for a popular book. You walk to the back of the library, find it on the shelf (10 minutes), bring it to the front desk. Five minutes later, someone else asks for the same book. Do you walk all the way to the back again?
No. You keep a copy on a shelf behind the front desk. Next time someone asks, you grab it in 5 seconds instead of 10 minutes.
That shelf behind the front desk? That's your cache.
In software, the "slow source" is usually a database, an external API, or an expensive computation. The "fast copy" lives in memory (RAM), either on the same server (local cache) or on a dedicated caching server like or Memcached.
| Storage Tier | Speed | Cost | Capacity |
|---|---|---|---|
| CPU Cache (L1/L2) | ~1 nanosecond | $$$$$ | Kilobytes |
| RAM (application memory) | ~100 nanoseconds | $$$$ | Gigabytes |
| In-memory cache (Redis) | ~0.1 millisecond (+ network) | $$$ | Gigabytes-Terabytes |
| SSD (database storage) | ~0.1-1 millisecond | $$ | Terabytes |
| Hard Drive (HDD) | ~5-10 milliseconds | $ | Petabytes |
| Network call (API) | ~10-200 milliseconds | $ | Unlimited |
Caching is fundamentally a trade-off: you sacrifice freshness and memory for speed. The cached data might be slightly stale (more on that soon), and you're using RAM to store it, but the performance gain is dramatic.