In the last module, we learned how to store data in a database. But even the best database is “slow” compared to the speed of a processor.

If you have a piece of information that 1,000,000 people want to see (like a viral tweet), you don’t want to go to the database 1,000,000 times. You want to Cache it.


1. The Analogy: Your Desk vs. The Library

Imagine you are writing a research paper.

  • The Library (Database): Contains 10 million books. It has everything, but it’s 10 miles away. Every time you need a fact, you have to drive there, find the book, and drive back.
  • Your Desk (Cache): Can only hold 5 books. But it’s right in front of you.

The strategy is simple: Keep the 5 books you are using right now on your desk. If you need a new book, drive to the library, get it, and swap it with one on your desk.


2. RAM vs. Disk: The Speed Gap

Inside a computer, a Database usually saves data to a Disk (like an SSD or HDD). A Cache usually saves data to RAM (Memory).

  • Disk: Huge and cheap, but slow. (The Library)
  • RAM: Small and expensive, but lightning-fast. (Your Desk)

[!NOTE] Reading from RAM is about 1,000 to 10,000 times faster than reading from a Disk. That’s the difference between a 1-second wait and a 0.0001-second blink.


3. The “Cache Miss” and “Cache Hit”

When your application needs data:

  1. Check the Cache: Is the data on the “Desk”?
    • Yes (Hit!): Return it immediately. Your site feels fast.
  2. Go to the Database: If it’s not in the cache.
    • No (Miss!): Go to the “Library,” get the data, save a copy in the “Desk Drawer,” and then return it.

4. When Does Caching Fail?

Caching is dangerous because data can get stale.

If your friend changes their profile picture, but you are still looking at the “cached” version in your drawer, you are seeing old data. Deciding when to “throw away” the old data is one of the hardest problems in System Design.


Beginner’s Checklist

  • Do I understand that RAM is faster than Disk?
  • Do I understand that Cache is a “Quick Copy” of slow data?
  • Do I understand the difference between a Hit and a Miss?

In the next chapter, we’ll look at Intermediate Caching, where we learn about “Cache-Aside” patterns and how to handle data that changes frequently.