What if your website could serve millions instantly without crashing?
Why Distributed caching (Redis, Memcached) in HLD? - Purpose & Use Cases
Imagine a busy online store where every user request asks the main database for product details. When hundreds of users visit at once, the database gets overwhelmed, causing slow page loads and frustrated customers.
Relying only on the main database means every request waits for the same slow process. This causes delays, crashes, and unhappy users. Manually trying to speed this up by copying data everywhere is confusing and error-prone.
Distributed caching stores frequently used data in fast, easy-to-access places like Redis or Memcached. This means most requests get answers quickly without bothering the slow database, making the whole system faster and more reliable.
result = database.query('SELECT * FROM products WHERE id=123')result = cache.get('product_123') or database.query('SELECT * FROM products WHERE id=123')
It enables lightning-fast responses and smooth user experiences even when millions of people use the system at the same time.
Big websites like Amazon use distributed caching to quickly show product info and prices without waiting for the database every time you click.
Manual database calls slow down under heavy load.
Distributed caching stores data closer to users for quick access.
This improves speed, reliability, and user satisfaction.
Practice
Solution
Step 1: Understand the role of distributed cache
Distributed caches store frequently accessed data in fast memory to reduce the number of database queries.Step 2: Identify the main benefit
This reduces the load on the database and speeds up data retrieval for repeated requests.Final Answer:
To reduce database load by storing frequently accessed data in memory -> Option AQuick Check:
Distributed cache reduces DB load = C [OK]
- Thinking cache permanently stores all data
- Confusing cache with database backup
- Assuming cache replaces the database
Solution
Step 1: Recall Redis command for setting data
Redis uses the commandSET key valueto store a value under a key.Step 2: Verify other options
GETretrieves data,STOREandPUTare not valid Redis commands.Final Answer:
SET key value -> Option AQuick Check:
Redis set command = SET key value [OK]
- Confusing GET with SET
- Using non-existent commands like STORE or PUT
- Mixing Redis commands with SQL syntax
cache.set('user:1', 'Alice')
name = cache.get('user:1')
print(name)What will be printed?
Solution
Step 1: Analyze the cache set operation
The key 'user:1' is set with the value 'Alice' in the cache.Step 2: Analyze the cache get operation
Retrieving 'user:1' returns the stored value 'Alice', which is then printed.Final Answer:
'Alice' -> Option BQuick Check:
Set then get returns stored value = 'Alice' [OK]
- Expecting key name instead of value
- Assuming None if not careful with key format
- Thinking it causes an error without context
Solution
Step 1: Understand Memcached key expiration
Memcached keys have a TTL (time to live). If TTL is too low, keys expire quickly causing cache misses.Step 2: Eliminate incorrect options
Memcached supports key-value storage, does not depend on database status, and does not use SQL queries.Final Answer:
The cache keys are expiring too quickly due to a low TTL setting -> Option CQuick Check:
Low TTL causes cache misses = A [OK]
- Thinking Memcached lacks key-value support
- Assuming database status affects cache directly
- Confusing Memcached with SQL databases
Solution
Step 1: Identify concurrency challenges in distributed cache
When multiple servers update the same key, race conditions can cause inconsistent data.Step 2: Choose atomic update methods in Redis
Redis supports transactions and Lua scripts to ensure updates happen atomically, preventing conflicts.Step 3: Evaluate other options
Allowing uncoordinated updates risks inconsistency; disabling caching reduces performance; Memcached lacks built-in atomic scripting.Final Answer:
Use Redis transactions or Lua scripts to perform atomic updates -> Option DQuick Check:
Atomic updates ensure consistency = D [OK]
- Ignoring concurrency causing stale data
- Thinking Memcached handles concurrency automatically
- Disabling cache unnecessarily reduces speed
Ready to prove it?
Pass the test and this pattern moves unknown
