⚡ System Design · System Building Blocks
Caching
Temporarily store frequently used data for faster access.
Avoid repeating expensive work when the answer has not changed.
Cache a product catalog for five minutes instead of querying the database for every page view.
💡 Like keeping milk in the fridge instead of driving to the shop every time you want a cup of tea.
💡 Why it matters
Fetching or computing the same answer over and over wastes time and overloads the database. A cache keeps a ready-made answer close by, so repeated requests are served instantly and the backend stays calm. The hard part is knowing when the cached answer is out of date, which is why caching needs care.
🛠️ How to achieve it
⚠️ Signs of trouble
- •The same expensive query runs again for every page view.
- •The database is busy serving data that rarely changes.
- •Users see stale data because the cache is never refreshed.
- •A cache stampede hammers the database when many items expire at once.
🎯 Quick check
Score: 0/3Three questions to test the idea.
1. What kind of data is the best fit for caching?
Answer: Data read often but changed rarely
Read-heavy, rarely-changing data gives the most cache hits with the least risk of staleness.
2. What is the main risk of caching?
Answer: Serving stale data if it is not refreshed
A cache can hold an old answer, so you need an expiry and a refresh plan to keep it fresh.
3. What happens on a cache miss?
Answer: The answer is fetched, stored, then served
On a miss you fetch the real answer, put it in the cache, and the next request becomes a fast hit.
