Lesson 1 of 2 Amazon ElastiCache and Purpose-Built Databases

Reducing Database Load with ElastiCache Caching Strategies

This lesson explains how ElastiCache provides managed in-memory caches that reduce database load and latency, and the strategies for using them.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between the Valkey, Redis OSS and Memcached engines.
  • Distinguish lazy loading from write-through.
  • Identify the caching service at each layer.

Engines

ElastiCache offers several engines. Valkey and Redis OSS are feature-rich: they replicate to replicas in other Availability Zones with automatic failover, persist data with snapshots, shard data in cluster mode, and provide data structures such as sorted sets, as well as publish-subscribe messaging and geospatial queries. They are the answer when the cache must stay available or needs rich data types. Memcached is a simple, multi-threaded key-value cache with no replication or persistence, for the simplest caching needs.

ElastiCache Serverless removes node selection and scales automatically, for the least operational effort. The following table summarises the options.

EngineFeaturesChoose for
Valkey / Redis OSSReplication, Multi-AZ failover, persistence, backups, cluster-mode sharding, sorted sets, pub/sub, geospatialHigh availability and rich data types
MemcachedSimple multi-threaded key-value, sharding; no replication or persistenceThe simplest cache
ElastiCache ServerlessScales automatically, no nodesLow operational effort

Uses: query caching, session stores for stateless web tiers, leaderboards, rate limiting, pub/sub, real-time analytics.

Caching strategies

The two main caching strategies differ in when data enters the cache. With lazy loading, also called cache-aside, the application reads the cache first and, on a miss, reads the database and writes the result to the cache. Only data that is actually requested is cached, but each miss adds latency, and cached data can become stale when the database changes.

With write-through, the application updates the cache every time it writes to the database, so cached data is always current, but the cache fills with data that may never be read. The following table summarises the two strategies.

StrategyHowTrade-off
Lazy loading (cache-aside)Read cache; on a miss, read the DB and populateOnly requested data cached; misses add latency; data can go stale
Write-throughWrite to the cache whenever writing to the DBAlways current; caches data that may never be read

Combine either with TTLs. Unlike DAX, ElastiCache needs application code changes.

The following diagram compares the steps of the two strategies.

The two caching strategies. With lazy loading, the application reads the cache and, on a miss, reads the database and writes the result to the cache, so only requested data is cached but data can go stale. With write-through, the application writes to the cache whenever it writes to the database, so the cache is always current but holds data that may never be read.
Many designs combine them, writing through for data known to be read and using TTLs to limit staleness.
Sort into groups

Lazy loading or write-through?

Exam skills 3.3.5

Decide which caching strategy each statement describes.

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

Caching at every layer

Caching can be applied at several layers of an application. The following table identifies the service used at each layer.

LayerService
EdgeCloudFront
API responsesAPI Gateway caching
Application dataElastiCache
DynamoDB readsDAX

The closer to the user, the bigger the latency win. ElastiCache runs in your VPC behind security groups; Valkey and Redis OSS support encryption in transit and at rest, plus AUTH or IAM.

The following diagram arranges the layers from the user to the data.

Caching at every layer of an application. CloudFront caches at the edge, API Gateway caches API responses, ElastiCache caches application data, and DAX caches DynamoDB reads. The closer to the user a request is answered, the larger the latency saving.
A request continues to the next layer only on a miss, so each layer reduces the load on the ones behind it.
Match the pairs

Caching at every layer

Exam skills 3.3.5 · 4.4.4

Match each layer with the service that caches there.

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

Taking load off a database

A product catalogue database runs at 90% CPU, almost all from repeated reads of the same popular products.

OptionEffect
Larger database instanceMore cost; the same repeated work
Read replicasSpreads reads, still hits disk-based databases
ElastiCache with lazy loading and a 5-minute TTLPopular products served from memory; most reads never reach the database

Caching is the most effective answer when the same data is read repeatedly. Valkey or Redis OSS suits this case because it supports replication and Multi-AZ failover; Memcached would be chosen only for a simple, multi-threaded cache where losing data on a node failure is acceptable.

Check yourself

Which engine should be chosen when the cache needs replication, persistence and automatic failover?

Show answer

Valkey or Redis OSS. Memcached is a simple, multithreaded cache without replication or persistence.

Exam Tips

Remember for the exam

  • For a highly available cache with failover and persistence, use ElastiCache for Valkey or Redis OSS with Multi-AZ.
  • For the simplest multi-threaded cache, when high availability is not needed, use Memcached.
  • To store user sessions outside the instances, use ElastiCache or DynamoDB.
  • For a cache with the least operational effort, use ElastiCache Serverless.
  • To cache API responses without changing the application, use API Gateway caching.
  • To require authentication to the cache, use ElastiCache with IAM or AUTH and encryption in transit.