Caching on AWS: Amazon ElastiCache for Redis and Memcached, Caching Strategies, TTL and Session Stores

AWS Developer Associate 45 min min read Updated: Nov 17, 2026 Intermediate
Caching on AWS: Amazon ElastiCache for Redis and Memcached, Caching Strategies, TTL and Session Stores
Intermediate Topic 2 of 3

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

LayerServiceCaches
Browser / edgeCloudFrontStatic files and API responses near users
APIAPI Gateway cacheMethod responses (0.5 to 237 GB, TTL up to 1 hour)
ApplicationElastiCache (Redis OSS/Valkey, Memcached)Anything: query results, sessions, computed values
DatabaseDAX (DynamoDB), Aurora and RDS buffer poolsHot DynamoDB items. Engine memory caches

ElastiCache: Redis vs Memcached

FeatureRedis OSS / ValkeyMemcached
Data typesStrings, hashes, lists, sets, sorted sets, streams, bitmaps, geospatial, pub/subSimple key-value strings
PersistenceSnapshots and AOF (some modes)None
Replication and failoverYes, Multi-AZ with automatic failoverNo (data lost if a node dies)
ScalingCluster mode (shards) and replicasMulti-threaded, scale by adding nodes, client-side sharding
Backup and restoreYesNo
Encryption and authAt rest, in transit, AUTH, ACLs, IAMIn transit and at rest (newer versions)
Typical useSessions, leaderboards, queues, rate limiting, caches that must survive failoverSimplest 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))
StrategyFresh dataRead latencyWrite costComplexity
Lazy loadingCan be staleMiss is slowLowLow
Write-throughFreshFastHigherMedium
TTL onlyStale until expiryDependsLowVery 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?

SituationChoose
DynamoDB hot reads, no code changes beyond the clientDAX
Static web content and API GET responses globallyCloudFront
Cache SQL results, sessions, counters, leaderboardsElastiCache Redis
Very simple, multi-threaded, disposable cacheElastiCache Memcached
Redis API with durable storage as primary DBMemoryDB

Practice Tasks

  1. Create an ElastiCache Redis cluster in private subnets and connect from EC2
  2. Implement lazy loading with a TTL and measure miss and hit latency
  3. Add write-through on updates and compare freshness
  4. Store sessions in Redis and kill one web server without losing sessions
  5. 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators