Ask anything about this post!
In our last post, we solved the memory problem.
We stopped asking individual waiters (Servers) to remember orders and gave them a "Kitchen Order System" (Database) to write everything down.
✅ Problem solved? Not quite.
Imagine this scenario in our restaurant: 1,000 customers sit down. 500 of them ask: "Can I see the menu, please?" Another 300 ask: "Do you have bottled water?"
If the waiters have to run to the Kitchen Order System (Database) and check the hard drive every single time someone asks for a menu... the kitchen is going to get very annoyed, very fast. 🤬
The database is smart, but it's slow. (In computer terms, reading from a disk is much slower than reading from RAM).
We need a way to answer these repetitive, simple questions instantly, without bothering the database.
The Solution: The Waiter’s Notepad (Cache). 📒
Here is the trick: The manager prints out 50 copies of the Menu and hands one to every waiter.
Now, when a customer asks, "Can I see the menu?", the waiter doesn't run to the kitchen. They just hand over the paper from their pocket. Instant response. ⚡
In System Design, this is called Caching.
A Cache is a small amount of super-fast memory (usually RAM) that lives right next to your Server. Before the Server asks the Database for data, it checks its Cache:
"Do I have the menu in my pocket?" Yes? Give it to the user. (This is called a Cache Hit). No? Run to the Database (Kitchen), get the data, and maybe keep a copy for next time. (This is called a Cache Miss). System Design Lesson #4: Don't do the hard work if you don't have to. If you've already calculated something or fetched some data, keep it close by so you can use it again instantly.
Real Life Example: When you open Instagram, the first few photos load instantly. That's because your phone (Cache) stored them. If you scroll down for 2 hours to a photo from last week, that might take a second to load because your phone has to fetch it from the server (Database).
Question for you: If the restaurant changes the menu (Updates the data), how do we make sure the waiters throw away their old paper copies and get the new one? 🤔
That’s a tricky problem called Cache Invalidation. Let me know your theory in the comments!
Thank you for reading our blog!
We have a Discord community where you can ask questions and get help from the community.
No comments yet. Be the first to share your thoughts!