Applying Caching Strategies
This lesson explains the classic caching strategies and their trade-offs, since a cache improves scalability only if the application reads from it effectively and keeps its data fresh enough.
Learning outcomes
After completing this lesson, you will be able to:
- Implement lazy loading.
- Combine write-through with TTLs.
- Use ElastiCache as a session store.
Lazy loading
With lazy loading (cache-aside), the application reads from the cache first. On a cache hit it uses the cached value; on a cache miss it reads from the database and writes the result into the cache. Only requested data is cached, and a node failure only causes misses, not errors. The drawbacks are a miss penalty of three trips (cache, database, cache) and stale data when the database changes, because the cache is not updated on writes.
The following diagram shows a cache miss as a sequence of calls.

Write-through and TTL
With write-through, the application writes to the cache whenever it writes to the database, so cached data is never stale. The drawbacks are a write penalty, caching data that may never be read, and missing data after a node is replaced until it is written again. Most applications combine the two strategies and add a time to live (TTL) to every key, so that stale or unused data expires and memory is reclaimed. Adding a small random jitter to TTLs avoids many keys expiring at once and overloading the database (a thundering herd). The following table compares the strategies.
| Strategy | Advantages | Disadvantages |
|---|---|---|
| Lazy loading | Only requested data cached; resilient to node loss | Miss penalty; data can be stale |
| Write-through | Data always current | Write penalty; caches unread data |
| Lazy loading + write-through + TTL | Balanced freshness and efficiency | More application logic |
Lazy loading or write-through?
Session stores and other uses
ElastiCache is also the standard place for session state, so that web servers in an Auto Scaling group are stateless and any instance can serve any user; this removes the need for load balancer stickiness and prevents sessions being lost when instances are replaced. Other uses include rate limiting with counters, leaderboards with sorted sets, and pub/sub messaging.
Caching expensive queries
A product page triggers a five-table join on RDS that takes 400 ms and is executed 2,000 times per second, pushing the database to 90% CPU. The team adds lazy loading with a Valkey cache keyed by product ID and a 10-minute TTL with jitter. When a product is updated through the admin tool, the tool also writes the new value to the cache (write-through), so price changes appear immediately. The cache hit ratio reaches 97%, database CPU drops to 25%, and the read replica the team had planned is no longer necessary.
Why add jitter to cache TTLs?
Show answer
So that many keys do not expire at the same moment and overload the database.
Exam Tips
Remember for the exam
- Lazy loading caches on miss; data can be stale.
- Write-through keeps the cache current but caches unread data.
- Always set a TTL; add jitter to avoid many keys expiring together.
- Store sessions in ElastiCache to make web tiers stateless.