| Users | Without Cache | With Cache | Latency Impact |
|---|---|---|---|
| 100 | Low latency (~50ms) | Very low latency (~10ms) | Cache reduces latency by 80% |
| 10,000 | Moderate latency (~200ms) | Low latency (~30ms) | Cache reduces latency by 85% |
| 1,000,000 | High latency (~500ms) | Moderate latency (~50ms) | Cache reduces latency by 90% |
| 100,000,000 | Very high latency (seconds) | High latency (~200ms) | Cache reduces latency by 80-90% |
Why caching reduces latency in HLD - Scalability Evidence
When users increase, the database becomes the first bottleneck because it takes longer to fetch data from disk or perform complex queries. Network delays add to this latency. Without caching, every request hits the database, causing slow responses.
- Cache Layer: Store frequently requested data in fast memory (like Redis or Memcached) to serve requests instantly.
- Read Replicas: Use database replicas to distribute read load, but caching is faster.
- CDN: Cache static content closer to users to reduce network delay.
- Cache Invalidation: Keep cache updated to avoid stale data.
- Horizontal Scaling: Add more cache servers to handle more requests.
Assuming 1 million requests per second (RPS):
- Without cache: Database must handle 1M QPS, likely overloaded (typical DB max ~10K QPS).
- With cache: Cache handles 900K QPS (90% hit rate), database only 100K QPS.
- Latency drops from ~500ms to ~50ms per request.
- Network bandwidth reduced as fewer DB queries cross network.
- Cache memory needed depends on data size; e.g., 100GB RAM for hot data.
Start by explaining the latency problem and why the database is the bottleneck. Then introduce caching as a solution to serve data faster from memory. Discuss cache hit rates, invalidation strategies, and how caching reduces load on the database and network. Finally, mention scaling cache horizontally and using CDNs for static content.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Introduce a caching layer to serve frequent requests from memory, reducing load on the database and lowering latency.
Practice
Solution
Step 1: Understand caching location
Caching stores data near the user or application, such as in memory or edge servers.Step 2: Effect on access time
Accessing data from cache is faster than fetching from the main database or server, reducing waiting time.Final Answer:
Because it stores frequently accessed data closer to the user, reducing access time. -> Option AQuick Check:
Caching reduces latency by storing data close [OK]
- Thinking caching increases servers
- Confusing caching with data compression
- Believing caching deletes data
Solution
Step 1: Define caching purpose
Caching temporarily holds data in a fast-access place like memory or local storage.Step 2: Identify correct description
Only caching temporarily stores data in a fast-access location to reduce retrieval time correctly states caching reduces retrieval time by temporary storage.Final Answer:
Caching temporarily stores data in a fast-access location to reduce retrieval time. -> Option CQuick Check:
Caching = temporary fast storage [OK]
- Confusing caching with backup storage
- Thinking caching deletes data
- Mixing caching with encryption
Solution
Step 1: Calculate cache hit and miss times
Cache hit latency = 10ms (80% requests), cache miss latency = 100ms (20% requests).Step 2: Compute weighted average latency
Average latency = (0.8 * 10) + (0.2 * 100) = 8 + 20 = 28 ms.Final Answer:
28 ms -> Option DQuick Check:
Average latency = hit%*hit_time + miss%*miss_time [OK]
- Ignoring cache miss latency
- Adding times instead of weighted average
- Using wrong percentages
Solution
Step 1: Analyze cache hit rate impact
If cache hit rate is low, most requests bypass cache and hit slow database, increasing latency.Step 2: Evaluate other options
Permanent storage or encryption are not typical causes of high latency; large cache size usually improves speed.Final Answer:
Cache hit rate is very low, so most requests go to the slow database. -> Option BQuick Check:
Low cache hit rate = high latency [OK]
- Assuming cache size slows system
- Confusing cache permanence with latency
- Thinking encryption is main latency cause
Solution
Step 1: Understand freshness and latency tradeoff
Short expiration keeps data fresh; asynchronous updates reduce latency by avoiding blocking calls.Step 2: Evaluate other options
Never expiring cache risks stale data; disabling cache increases latency; caching rarely accessed data wastes resources.Final Answer:
Use a cache with a short expiration time and update cache asynchronously on data changes. -> Option AQuick Check:
Short expiry + async update = fresh data + low latency [OK]
- Choosing never expiring cache causing stale data
- Disabling cache increases latency
- Caching rarely accessed data wastes resources
Ready to prove it?
Pass the test and this pattern moves unknown
