Lesson 2 of 3 Amazon ElastiCache

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.

Lazy loading as a sequence. The application reads the cache first; on a hit it uses the value. On a miss it reads the database and writes the result to the cache, three trips in all. Only requested data is cached and a failed node causes only misses, but data can go stale because writes do not update the cache.
The TTL limits how long stale data can survive after the database changes.

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.

StrategyAdvantagesDisadvantages
Lazy loadingOnly requested data cached; resilient to node lossMiss penalty; data can be stale
Write-throughData always currentWrite penalty; caches unread data
Lazy loading + write-through + TTLBalanced freshness and efficiencyMore application logic
Sort into groups

Lazy loading or write-through?

Exam skills 2.1.2

Decide which caching strategy each statement describes.

Log in to try this activity Interactive activities are free with an account.

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.

Worked example

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.

Check yourself

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.