Caching on AWS: Amazon ElastiCache for Redis and Memcached, Caching Strategies, TTL and Session Stores
Why Cache?
- Databases are slower and more costly per read than memory
- A cache returns hot data in microseconds to sub-millisecond, reduces load on the database and lowers cost
- Good for read-heavy data that changes slowly and where slightly stale data is acceptable
Where Caches Live on AWS
| Layer | Service | Caches |
| Browser / edge | CloudFront | Static files and API responses near users |
| API | API Gateway cache | Method responses (0.5 to 237 GB, TTL up to 1 hour) |
| Application | ElastiCache (Redis OSS/Valkey, Memcached) | Anything: query results, sessions, computed values |
| Database | DAX (DynamoDB), Aurora and RDS buffer pools | Hot DynamoDB items. Engine memory caches |
ElastiCache: Redis vs Memcached
| Feature | Redis OSS / Valkey | Memcached |
| Data types | Strings, hashes, lists, sets, sorted sets, streams, bitmaps, geospatial, pub/sub | Simple key-value strings |
| Persistence | Snapshots and AOF (some modes) | None |
| Replication and failover | Yes, Multi-AZ with automatic failover | No (data lost if a node dies) |
| Scaling | Cluster mode (shards) and replicas | Multi-threaded, scale by adding nodes, client-side sharding |
| Backup and restore | Yes | No |
| Encryption and auth | At rest, in transit, AUTH, ACLs, IAM | In transit and at rest (newer versions) |
| Typical use | Sessions, leaderboards, queues, rate limiting, caches that must survive failover | Simplest large, throwaway cache |
Related services: MemoryDB is a durable, Redis-compatible primary database (multi-AZ transaction log). ElastiCache Serverless removes node sizing. Both exist for different needs than a plain cache.
Caching Strategies
Lazy Loading (Cache-Aside)
python
import json, redis
r = redis.Redis(host="shop-cache.abc.cache.amazonaws.com", port=6379, ssl=True, decode_responses=True)
def get_product(pid):
key = f"product:{pid}"
hit = r.get(key)
if hit:
return json.loads(hit) # cache hit
row = db_query("SELECT * FROM products WHERE id = %s", pid) # cache miss: read database
r.setex(key, 300, json.dumps(row)) # store with a 5 minute TTL
return row
- Pros: only requested data is cached. A cache failure only means slower reads
- Cons: a cache miss is slow (three steps). Data can become stale until the TTL ends
Write-Through
python
def save_product(p):
db_write("UPDATE products SET price=%s WHERE id=%s", p["price"], p["id"])
r.setex(f"product:{p['id']}", 3600, json.dumps(p)) # update the cache at the same time
- Pros: cache is always fresh, reads are fast
- Cons: every write costs two operations, and the cache fills with data nobody reads (wasteful). Missing data appears until it is written. Often combined with lazy loading
Write-Behind (Write-Back) and Others
- Write-behind: write to cache first, flush to the database later. Fast but risks data loss if the cache fails
- Read-through: the cache layer itself loads from the database (DAX works this way)
- TTL: expire entries automatically. The simplest answer to staleness. Add random jitter (for example 300 plus random 0 to 60 seconds) so many keys do not expire together and cause a thundering herd
- Explicit invalidation: delete the key when data changes (
r.delete(key))
| Strategy | Fresh data | Read latency | Write cost | Complexity |
| Lazy loading | Can be stale | Miss is slow | Low | Low |
| Write-through | Fresh | Fast | Higher | Medium |
| TTL only | Stale until expiry | Depends | Low | Very low |
Redis Data Structure Examples
python
# Session store with expiry
r.hset("session:abc", mapping={"user": "C-100", "cart": 3})
r.expire("session:abc", 1800)
# Leaderboard with a sorted set
r.zadd("leaders", {"asha": 1520, "ravi": 1830})
print(r.zrevrange("leaders", 0, 2, withscores=True))
# Rate limiter: 100 requests per minute per user
def allowed(user):
k = f"rl:{user}:{int(time.time()//60)}"
n = r.incr(k)
if n == 1: r.expire(k, 60)
return n <= 100
# Simple distributed lock
got = r.set("lock:report", "worker-1", nx=True, ex=30)
Sessions: Stateless Servers
Store sessions in ElastiCache or DynamoDB (not on the web server) so any instance can serve any user, scaling and failover work, and sticky sessions are not needed.
High Availability and Scaling (Redis)
- Replication group: one primary and up to 5 replicas, across AZs, with automatic failover
- Cluster mode enabled: data split into up to 500 shards (16,384 hash slots) for more memory and write throughput. Use cluster-aware clients
- Reader endpoint spreads read traffic over replicas
- Place the cache in private subnets and allow only the app security group on port 6379 (11211 for Memcached)
- Use TLS (
ssl=True) and AUTH or IAM
Eviction
When memory is full Redis follows the maxmemory policy: for caches use allkeys-lru (evict least recently used). noeviction returns errors instead. Monitor Evictions, CacheHitRate, CurrConnections and EngineCPUUtilization in CloudWatch.
Which Cache?
| Situation | Choose |
| DynamoDB hot reads, no code changes beyond the client | DAX |
| Static web content and API GET responses globally | CloudFront |
| Cache SQL results, sessions, counters, leaderboards | ElastiCache Redis |
| Very simple, multi-threaded, disposable cache | ElastiCache Memcached |
| Redis API with durable storage as primary DB | MemoryDB |
Practice Tasks
- Create an ElastiCache Redis cluster in private subnets and connect from EC2
- Implement lazy loading with a TTL and measure miss and hit latency
- Add write-through on updates and compare freshness
- Store sessions in Redis and kill one web server without losing sessions
- Build a rate limiter with INCR and EXPIRE
Conclusion
Cache read-heavy, slowly changing data. Use lazy loading with TTL (plus jitter) as the default, write-through when freshness matters, Redis when you need data structures, replication or persistence, Memcached for simple multi-threaded caching, DAX for DynamoDB and CloudFront for web content.
Quick Summary: why cache; layers; Redis vs Memcached; strategies; Redis examples; sessions; HA; eviction; selection.