Amazon ElastiCache
Valkey, Redis OSS and Memcached compared, replication and failover, Global Datastore, security, eviction and caching strategies.
Exam tasks: 2.5 (caching for performance), 3.3, 2.1 (session state)
The decision: is the cache allowed to lose data, and does it need replication, persistence or data structures? That chooses the engine. Then decide how it fails over and who can reach it.
ElastiCache runs managed in-memory caches inside your VPC. It sits in front of a database or holds session and leaderboard data, and it cannot be reached from the internet. Clients outside the VPC need a VPN, Direct Connect or peering path.
Valkey or Redis OSS vs Memcached
| Valkey or Redis OSS | Memcached | |
|---|---|---|
| Data types | Strings, hashes, lists, sets, sorted sets, streams, geospatial | Strings (simple key/value) |
| Replication and failover | Yes: replicas, Multi-AZ, automatic failover | None. A lost node loses its data |
| Persistence and backup | Snapshots | No |
| Cross-Region | Global Datastore | No |
| Pub/sub, transactions, Lua | Yes | No |
| Threads | Mostly single-threaded command processing, with I/O threading | Multi-threaded per node |
| Scale | Shards (cluster mode) and replicas | Add nodes; client hashes keys across them |
Valkey is the open-source fork of Redis that AWS now leads with, and it's API-compatible with Redis OSS. For the exam, treat them the same. ElastiCache Serverless removes node sizing entirely for either engine.
Exam signal
"Sorted leaderboard", "pub/sub", "session store that must survive a node failure", "replicate to another Region" all mean Valkey or Redis OSS. "Simplest cache, multi-threaded, data loss acceptable" means Memcached.
Fault tolerance and failover
- Multi-AZ with automatic failover: a replica in another AZ is promoted when the primary fails, and the primary endpoint follows it. Needs at least one replica.
- Cluster mode disabled: one shard, up to five replicas. Scale reads with replicas, scale writes only by a bigger node.
- Cluster mode enabled: data is partitioned across shards, so writes and memory scale horizontally, and one shard failing affects only its slice of keys. Resharding can happen online.
- Global Datastore: a primary cluster replicates to read-only clusters in other Regions. Promote a secondary if the primary Region fails. Local reads for global users, plus a cross-Region DR option.
- If the data must never be lost (it's the system of record, not a cache), use MemoryDB instead. It keeps a durable, Multi-AZ transaction log. See purpose-built databases.
Memcached for high availability
Spreading Memcached nodes across AZs limits how much of the cache one AZ outage empties, but there's no replication. An answer that needs a cache to fail over without losing data needs Valkey or Redis OSS with replicas and Multi-AZ.
Eviction, security and sizing
- When memory fills, the eviction policy decides what goes. The default is
volatile-lru, which removes the least recently used keys that have a TTL. If keys have no TTL and memory fills, writes fail. Useallkeys-lrufor a pure cache. - Encryption at rest and in transit are options you choose when you create the cluster. Authenticate with an AUTH token, role-based access control users, or IAM authentication on supported engines.
- Snapshots to S3 are available for Valkey and Redis OSS only. Memcached has none.
- Scaling: scale up with a bigger node type, scale reads with replicas, and scale writes and memory with more shards in cluster mode, using online resharding.
- Data tiering on supported node types moves cold items to local SSD to lower cost per GB.
Caching strategies
| Lazy loading (cache-aside) | Write-through | |
|---|---|---|
| How | On a miss, read the DB and write the result to the cache | Every DB write also updates the cache |
| Data in cache | Only what's been requested | Everything written, even if never read |
| Stale data | Possible until the TTL expires | Rare |
| Miss penalty | Three trips: cache, DB, cache write | Low, but every write costs more |
| Node failure | Cache refills gradually | Empty cache until data is written again |
Combine them: write-through for data that must be fresh, plus a TTL on every key so mistakes and orphaned keys expire. Add a little random jitter to TTLs so many keys don't expire at once and stampede the database.
ElastiCache or MemoryDB?
| ElastiCache | MemoryDB | |
|---|---|---|
| Role | A cache in front of another database | A durable primary database |
| Durability | Replicas, optional snapshots | Multi-AZ transaction log, no data loss on failover |
| Latency | Microsecond | Microsecond reads, single-digit millisecond writes |
| Use it for | Sessions, leaderboards, offloading reads | A system of record with Redis-compatible APIs |
Scenarios
A replicated cache outside the servers keeps sessions through deployments, and Multi-AZ with automatic failover keeps them through a node failure. Memcached nodes in several AZs lose their share of data when one fails, because there is no replication. Sticky sessions still lose data when the server goes away. A bigger instance changes nothing.
A cache with a TTL absorbs repeated reads and bounds staleness at the TTL, which matches the one-minute tolerance. Aurora has a single writer per cluster by default and the load is reads. S3 is not a low-latency query store for rows. Multi-AZ on the cache adds availability, not offload.
Related
- Caching: the full stack of cache layers.
- DynamoDB: DAX for DynamoDB read caching.
- Purpose-built databases: MemoryDB and the other engines.
Further reading
Amazon RDS and Aurora
RDS engines, Multi-AZ and read replicas, Aurora storage and Global Database, backups, encryption, RDS Proxy, Blue/Green deployments and the exam traps.
Caching
Where to cache (CloudFront, API Gateway, ElastiCache, DAX), how to raise the CloudFront cache hit ratio, and how to make a cache layer fault tolerant.