Skip to Content

Amazon ElastiCache

Managed in-memory caches with Valkey, Redis OSS or Memcached, how they compare, cache security, and the lazy loading, write-through, session store and leaderboard patterns.

What it is

Amazon ElastiCache is to caches what RDS is to relational databases: AWS runs the cache servers, and the customer uses them. A cache is an in-memory data store: it keeps data in RAM, so reads take well under a millisecond.

ElastiCache supports three engines:

  • Valkey, the open-source fork of Redis that AWS now leads with;
  • Redis OSS;
  • Memcached.

What a cache does:

  • Takes load off the database for read-heavy workloads: popular data is served from memory.
  • Makes the application stateless: user sessions live in the cache, not on one web server, so any server can handle any request.
  • AWS handles OS maintenance and patching, setup, monitoring, failure recovery and backups.

The catch: using ElastiCache means changing the application code. The app must decide what to cache, read from the cache first, and keep it up to date.

ElastiCache offers a serverless cache, ready in under a minute with no nodes to size, or a node-based cluster with chosen node types such as cache.m7g.large.

Lazy loading: the database cache pattern

Lazy loading: the application reads from ElastiCache first; on a hit it is done; on a miss it reads from the RDS database, then writes the result to the cache for next time Lazy loading: the application reads from ElastiCache first; on a hit it is done; on a miss it reads from the RDS database, then writes the result to the cache for next time

Check the cache first; fill it on a miss.
  1. The application asks ElastiCache for the data. If it's there (a cache hit), it's done.
  2. If it isn't (a cache miss), the application reads from the database, here RDS.
  3. It then writes the result to the cache, so the next request is a hit.

The cache needs an invalidation strategy so it only holds current data. That's the hard part. There's an old saying: the two hard things in computer science are cache invalidation and naming things.

Caching strategies

  • Lazy loading (cache-aside): only requested data is cached, so the cache stays small. But a miss costs three trips (cache, database, cache), and cached data can become stale when the database changes.
  • Write-through: whenever the app writes to the database, it also writes to the cache. Cached data is never stale, but every write does two writes, and the cache fills with data nobody may read.
  • TTL (time to live): give each cache entry an expiry time, so stale data disappears by itself. Combine TTL with either strategy.
  • Session store: keep temporary session data (who's logged in, the cart) in the cache with a TTL.

User session store

  1. A user logs in through any of the application servers.
  2. That server writes the session into ElastiCache.
  3. The user's next request reaches another server.
  4. That server reads the session from ElastiCache, and the user is still logged in.

Valkey and Redis OSS vs Memcached

Valkey and Redis OSS:

  • Multi-AZ with automatic failover: a replica is promoted if the primary fails.
  • Read replicas to scale reads and for high availability.
  • Durability: data can be persisted (AOF), and it supports back up and restore.
  • Rich data types, such as sets and sorted sets, plus pub/sub and more.
  • Cluster mode splits data across shards.

Memcached:

  • Multiple nodes for partitioning data (sharding).
  • No replication, so no high availability: a lost node loses its data.
  • Not persistent. (Serverless Memcached caches do support backup and restore.)
  • Multithreaded, and the simplest model.

Rule of thumb: Valkey or Redis OSS when availability, durability, backups or rich data types are needed; Memcached for a simple, multithreaded cache that can be lost.

Cache security

  • IAM authentication for Valkey and Redis OSS 7 or later: the client gets a short-lived token from its IAM identity instead of a password.
  • Otherwise, IAM policies on ElastiCache only control the AWS API (who can create or delete a cache), not access to the data.
  • Redis AUTH: a password or token set when a Valkey or Redis OSS cache is created, an extra layer on top of security groups. It's now superseded by role-based access control (RBAC), with users and permissions; serverless caches must use RBAC.
  • Encryption in transit (TLS) and at rest (KMS).
  • Memcached supports SASL authentication.
  • Security groups decide which clients can reach the cache, as for any VPC resource.

Updated since the slides

The slides name only Redis and Memcached. ElastiCache now offers Valkey (an open-source Redis fork) as a third engine, calls the other one Redis OSS, and has a serverless option. For the exam, treat Valkey like Redis.

Use case: a real-time leaderboard

Gaming leaderboards are expensive to compute in a relational database. A Valkey or Redis sorted set keeps members unique and ordered by score. Each new score is ranked as it's added, so the top ten is always one fast command away.

Real-world examples

  • A news site's home page is built from 40 database queries. With lazy loading and a 60-second TTL, the database sees one query per minute instead of thousands.
  • A web shop behind a load balancer keeps user sessions in ElastiCache, so it can drop sticky sessions and scale servers freely.
  • A mobile game keeps its global leaderboard in a Valkey sorted set and updates it in real time.

Exam tips

  • "Reduce read load on RDS", "frequently read data", "sub-millisecond reads" → ElastiCache.
  • "Store user sessions so the app is stateless" → ElastiCache (or DynamoDB).
  • "Cache must survive node failure", "Multi-AZ", "backups", "sorted sets" → Valkey / Redis OSS.
  • "Simple cache, multithreaded, data loss is fine" → Memcached.
  • "Data in the cache is stale" → write-through or a TTL. "Cache holds data nobody reads" → lazy loading.
  • "Leaderboard" → Redis/Valkey sorted sets.
  • "Least application change for caching DynamoDB" → DAX, not ElastiCache (see DynamoDB advanced).

Common traps

  • "ElastiCache needs no code changes." The app must be written to use the cache.
  • "Memcached for high availability." It has no replication; use Valkey or Redis OSS.
  • "IAM policies control who can read cache data." Only with IAM authentication (Valkey/Redis OSS 7+); otherwise IAM covers the API, and AUTH/RBAC covers the data.

Hands-on lab: a serverless Valkey cache

Goal: create a serverless cache, connect from EC2, and store a value with an expiry.

Costs money

A serverless Valkey cache costs a few cents an hour at most: a minimum storage charge plus tiny request charges. Delete it within the hour. Any Region works.

Before you start: you need saa-web in the default VPC.

Create the cache

ElastiCache → Valkey caches → Create Valkey cache. Choose Serverless, name saa-cache, default settings (default VPC and subnets). Under security groups, choose a group that allows TCP 6379 from saa-web's security group (create one if needed). Tag Project=saa-lab, create.

Connect from EC2

Copy the cache endpoint. From saa-web:

sudo dnf install -y valkey
valkey-cli -h <endpoint> --tls -p 6379

(If the valkey package isn't available, sudo dnf install -y redis6 and use redis6-cli the same way.)

Store and expire a value

SET session:42 "user=ana" EX 30
GET session:42
TTL session:42

Run GET session:42 again after 30 seconds: it returns (nil). The entry expired by itself, which is how a session store cleans up.

Expected result: a working in-memory cache reached over TLS, and a key that disappears when its TTL runs out.

Clean up: delete saa-cache (no final backup) and the security group you created for it.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt