ElastiCache — Concept
What it is
Amazon ElastiCache = managed in-memory data store. Two engines: Redis (now "Valkey / Redis OSS-compatible") and Memcached. Used as a cache or a real-time data store to offload databases.
Why it exists
RDBMS reads can be slow and expensive; many web apps repeat the same queries. Putting an in-memory layer in front gives sub-ms latency and reduces DB load and cost.
Engines comparison (exam favorite)
| Feature | Redis (OSS / Valkey) | Memcached |
|---|---|---|
| Data structures | Strings, hashes, lists, sets, sorted sets, streams, geo, hyperloglog | Strings only |
| Persistence | Snapshots + AOF (optional) | None (cache only) |
| Replication / HA | Yes — Multi-AZ, automatic failover | No replication |
| Pub/Sub & Streams | Yes | No |
| Transactions / Lua | Yes | No |
| Cluster mode (sharded) | Yes | Yes (no replication) |
| Use | Cache + real-time store, sessions, leaderboards, queues | Pure ephemeral cache, multi-thread scale-out |
Architectures
- Redis (cluster mode disabled) = 1 primary + up to 5 replicas, Multi-AZ.
- Redis (cluster mode enabled) = up to 500 shards, each with its own primary + replicas.
- Memcached = pool of nodes, app-side hash partitioning.
Caching strategies (commonly tested)
| Strategy | How |
|---|---|
| Lazy loading (cache-aside) | App reads cache; on miss, read DB, write cache. Simple, can serve stale. |
| Write-through | App writes both cache and DB. Cache always fresh, but extra write latency. |
| Time-to-live (TTL) | Set expiration so stale data drops automatically. |
Use cases
- Database query cache → drop ms→µs.
- Session store for stateless web tier.
- Leaderboards (Redis sorted sets).
- Rate limiting / counters.
- Pub/sub messaging (Redis).
- Geospatial queries (Redis).
Security
- VPC SG (Redis default 6379, Memcached 11211).
- Redis: AUTH password, encryption in transit & at rest, RBAC.
- Memcached: no encryption/auth historically (newer versions add some).
When to use vs alternatives
| Use ... | Instead of ... | When ... |
|---|---|---|
| Redis | Memcached | Need HA, persistence, advanced data structures |
| Memcached | Redis | Simple cache, multi-threaded scale-out, you don't care about persistence |
| DAX | ElastiCache | DynamoDB is the backing store |
| DynamoDB | ElastiCache | Persistent key-value DB, not just cache |
| MemoryDB for Redis | ElastiCache Redis | Need Redis API as a durable primary DB (Multi-AZ transactional log) |
Common exam scenarios
- "Stateless web tier needs session storage" → ElastiCache Redis (HA + persistence).
- "Cache DB query results, sub-ms reads" → ElastiCache.
- "Real-time leaderboard for game" → Redis sorted sets.
- "Pure ephemeral cache, multi-thread scale-out" → Memcached.
- "Acceleration for DynamoDB reads" → DAX, not ElastiCache.
- "Redis as primary durable DB" → MemoryDB for Redis.
Exam tip
Whenever you see "sub-millisecond reads", "reduce DB load", "session store", "leaderboard", "rate limiter" → think ElastiCache (Redis). For DynamoDB workloads → DAX.