What if your app could serve millions instantly without crashing the database?
Why Cache-aside pattern in HLD? - Purpose & Use Cases
Imagine you run a busy online store. Every time a customer wants to see a product, your system asks the main database for details. When many customers come at once, the database gets overwhelmed and slows down.
Relying only on the database means slow responses during high traffic. The database can become a bottleneck, causing delays and unhappy users. Also, repeated requests for the same data waste resources and increase costs.
The cache-aside pattern helps by keeping a fast, temporary storage (cache) near your application. When data is requested, the system first checks the cache. If the data is there, it returns quickly. If not, it fetches from the database and stores it in the cache for next time.
data = database.get(key)
return datadata = cache.get(key) if not data: data = database.get(key) cache.set(key, data) return data
This pattern enables fast, scalable systems that handle many users smoothly by reducing database load and speeding up data access.
Think of a popular news website that shows trending articles. Instead of asking the database every time, it uses cache-aside to quickly serve popular articles to millions of readers.
Manual database calls slow down under heavy load.
Cache-aside stores data temporarily for quick access.
It improves speed and scalability by reducing database hits.
Practice
cache-aside pattern in system design?Solution
Step 1: Understand cache-aside pattern behavior
The cache-aside pattern loads data into cache only when the application requests it and the data is not already cached.Step 2: Compare options with this behavior
To load data into cache only when it is requested correctly states this lazy loading approach. Other options describe preloading or unsynchronized caching, which are incorrect.Final Answer:
To load data into cache only when it is requested -> Option AQuick Check:
Cache-aside = lazy loading on demand [OK]
- Thinking cache preloads all data
- Assuming cache updates happen before DB writes
- Believing cache and DB are unsynchronized
Solution
Step 1: Recall cache-aside read flow
When reading, the system first checks the cache. If data is missing (cache miss), it reads from the database and then updates the cache.Step 2: Match this flow to options
Read from cache -> If miss, read from database -> Update cache matches this exact sequence. Other options either skip cache miss handling or update cache before reading.Final Answer:
Read from cache -> If miss, read from database -> Update cache -> Option BQuick Check:
Cache read -> DB read on miss -> Cache update [OK]
- Updating cache before reading database
- Ignoring cache miss and reading only cache
- Reading database before checking cache
function getUser(id) {
user = cache.get(id)
if (user == null) {
user = database.query(id)
cache.set(id, user)
}
return user
}
What will getUser(5) return if the cache is empty and the database has user with id 5?Solution
Step 1: Analyze cache miss handling
Since cache is empty,cache.get(5)returns null, triggering database query.Step 2: Database query and cache update
The function queries the database for user id 5, finds the user data, updates the cache, and returns the user data.Final Answer:
User data from database -> Option DQuick Check:
Cache miss triggers DB read and cache update [OK]
- Assuming cache miss returns null directly
- Expecting error on cache miss
- Ignoring cache update after DB read
Solution
Step 1: Identify cause of stale cache
Stale cache means cache has old data not reflecting recent database updates.Step 2: Check cache update after DB write
If cache is not updated or invalidated after database writes, stale data remains in cache.Final Answer:
Not updating cache after database write -> Option AQuick Check:
Cache must update after DB write to avoid stale data [OK]
- Ignoring cache invalidation after writes
- Assuming cache updates automatically
- Confusing read and write cache operations
Solution
Step 1: Understand cache consistency needs
For frequent updates and reads, cache must reflect latest data quickly to avoid stale reads.Step 2: Evaluate update strategies
On product update, write to DB then immediately update cache with new data updates DB then cache immediately, ensuring cache consistency and fast reads. On product update, write to DB only and let cache expire naturally risks stale cache. Preload entire product catalog into cache at service start and never update cache ignores updates. Write updates only to cache and asynchronously sync to DB later risks data loss and inconsistency.Final Answer:
On product update, write to DB then immediately update cache with new data -> Option CQuick Check:
Update DB then cache for consistency and performance [OK]
- Relying on cache expiration alone
- Preloading cache without updates
- Writing only to cache without DB sync
Ready to prove it?
Pass the test and this pattern moves unknown
