Imagine you own a busy café. Every day, customers order popular items like masala chai, cold coffee, and sandwiches.
If the chef has to collect every ingredient from the storeroom and prepare everything from scratch for every order, service will be slow. But if the café keeps popular ingredients ready near the counter, the chef can work faster.
That is the basic idea behind caching in system design.
1. What Is Caching?
Caching means storing frequently accessed data in a temporary, fast storage location so future requests can be served faster.
Café analogy
- Storeroom: Database
- Ready-to-use ingredients near the counter: Cache
- Chef: Application server
- Customer order: User request
2. How Does Caching Work?
Suppose a customer asks for cold coffee. The café first checks whether the needed information is already available nearby.
Cache hit
The required data is found in the cache, so the application can return it quickly without querying the database.
Cache miss
The data is not in the cache. The application fetches it from the database and commonly stores it in the cache for future requests.
3. Types of Caching
A. In-Memory Cache
Imagine keeping popular ingredients beside the chef so they can be accessed almost immediately.
In system design, data is stored in RAM, which is typically much faster to access than data stored on disk.
Examples include Redis and Memcached.
Use it when: You need fast access to frequently requested data, such as product details, user sessions, or popular posts.
B. Browser Cache
Imagine a regular customer has a copy of the café menu saved on their phone, so they do not need to request the same menu every time.
In system design, the browser stores resources such as images, CSS, and JavaScript files according to caching rules.
Use it when: You want repeat visitors to load websites faster and reduce unnecessary network requests.
C. CDN Cache
Imagine your café has branches in several cities. Instead of sending every customer to the main café for the menu, each branch keeps a copy of popular menu items.
In system design, a Content Delivery Network (CDN) stores cached content at locations closer to users.
Use it when: You need to serve static content such as images, videos, CSS, and JavaScript to users in different geographical locations.
D. Database Cache
Imagine the café keeps a prepared list of popular orders so the chef does not need to check the main records for every request.
In system design, frequently accessed query results or database pages may be cached to avoid repeated expensive reads. Databases can have internal caches, and applications can also cache query results.
Use it when: The same data is read repeatedly and fetching it from the database is expensive.
4. What Is Cache Expiration (TTL)?
Suppose the café prepares fresh chutney in the morning. It should not keep serving the same chutney indefinitely.
Caching works similarly.
TTL (Time To Live) defines how long a cached item can remain valid before it expires.
Examples:
- Café menu data: cache for one hour.
- Product prices: use a shorter TTL if prices change frequently.
- Static logo: cache for a long period if its URL changes whenever the logo is updated.
The correct TTL depends on how frequently data changes and how much stale data the application can tolerate.
5. The Biggest Challenge: Stale Data
Imagine the café changes the price of cold coffee from ₹100 to ₹120, but the cached menu still shows ₹100.
This is called stale cache data.
Common ways to handle it
- TTL: Let cached data expire after a defined duration.
- Cache invalidation: Remove or update cached data when the original data changes.
- Write-through: Update the cache and underlying data store as part of the write path.
- Write-behind: Update the cache first and persist the change to the database later, with appropriate reliability safeguards.
6. What Is Cache Eviction?
Suppose your café counter has room for only 20 ingredient containers. When it becomes full, you must decide which container to remove to make room for a new one.
A cache also has limited memory, so it may need to evict existing entries.
Common eviction strategies
| Strategy | Café example |
|---|---|
| LRU (Least Recently Used) | Remove the ingredient that has not been used for the longest time. |
| LFU (Least Frequently Used) | Remove the ingredient used the fewest times. |
| FIFO (First In, First Out) | Remove the ingredient that was added first. |
7. When Should You Use Caching?
| Scenario | Cache benefit |
|---|---|
| Popular products | Faster product page loads |
| Frequently viewed profiles | Fewer database reads |
| Static images and files | Faster delivery through browser or CDN caches |
| Repeated expensive calculations | Avoid recalculating the same result |
| Data that changes constantly | Caching may need short TTLs or careful invalidation |
Caching is not always the right choice. It adds complexity, consumes memory, and can return outdated data if freshness is not managed correctly.
8. Interview Question
Your café receives 1,000 requests for its menu every minute. The menu rarely changes, but every request currently queries the database.
What would you do?
- A. Increase database queries
- B. Cache the menu and set an appropriate expiration or invalidation strategy
- C. Delete the database
- D. Restart the server after every request
Answer
Option B: Cache the menu and set an appropriate expiration or invalidation strategy.
Caching lets repeated requests be served quickly and reduces database reads. Update or invalidate the cached copy when the menu changes.
Key Takeaway
Think of caching as keeping popular café items ready instead of fetching everything from the storeroom for every order.
In system design, caching helps reduce latency, database load, and repeated computation. The important part is deciding what to cache, where to cache it, how long to keep it, and how to keep it fresh.
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.