Skip to Content
Asterrr's Handbook
2 · New solutions

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.

AWS RegionVPCPrivate subnets1 read2 on miss3 refillApplication tierElastiCacheAurora
Cache-aside in front of a relational database. Reads try the cache first, and a miss goes to the database and refills the cache.
VPC only
No public access. Reach it from inside the VPC or over a private link
Multi-AZ
Needs at least one replica. A replica is promoted when the primary fails
5
Replicas per shard
Valkey, Redis OSS
Persistence, replication, Multi-AZ, Global Datastore
Memcached
No replication and no persistence. A lost node loses its data

Valkey or Redis OSS vs Memcached

Valkey or Redis OSSMemcached
Data typesStrings, hashes, lists, sets, sorted sets, streams, geospatialStrings (simple key/value)
Replication and failoverYes: replicas, Multi-AZ, automatic failoverNone. A lost node loses its data
Persistence and backupSnapshotsNo
Cross-RegionGlobal DatastoreNo
Pub/sub, transactions, LuaYesNo
ThreadsMostly single-threaded command processing, with I/O threadingMulti-threaded per node
ScaleShards (cluster mode) and replicasAdd 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

Primary RegionShard 1Shard 2Secondary RegionreplicatereplicateGlobal DatastoreGlobal DatastorePrimary, AZ aReplica, AZ bPrimary, AZ bReplica, AZ cRead-only shard1Read-only shard2
ElastiCache keeps each shard's replica in another AZ. Global Datastore copies the data asynchronously to a read-only cluster in a second Region.
  • 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. Use allkeys-lru for 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
HowOn a miss, read the DB and write the result to the cacheEvery DB write also updates the cache
Data in cacheOnly what's been requestedEverything written, even if never read
Stale dataPossible until the TTL expiresRare
Miss penaltyThree trips: cache, DB, cache writeLow, but every write costs more
Node failureCache refills graduallyEmpty 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?

ElastiCacheMemoryDB
RoleA cache in front of another databaseA durable primary database
DurabilityReplicas, optional snapshotsMulti-AZ transaction log, no data loss on failover
LatencyMicrosecondMicrosecond reads, single-digit millisecond writes
Use it forSessions, leaderboards, offloading readsA system of record with Redis-compatible APIs

Scenarios

Scenario
A web application stores user sessions in memory on its web servers. After a deployment replaced the servers, every user was signed out. The team wants sessions to survive server replacement and a single node failure in the cache layer. Which design fits?
Scenario
A product catalog reads the same 5,000 popular rows from Aurora thousands of times per second, and the database CPU is high. Catalog changes may be visible after up to one minute. Which change reduces load most simply?

Further reading

On this page