Amazon ElastiCache
medium 28 minAmazon ElastiCache is AWS's managed in-memory store.
See it run
A read through the cache, slots, a failover, durable writes and serverless growth.
Operations
Cache-aside
One product is read twice, then changed.
Step 1 of 5
Miss
The app asks the cache. The key is not there.
Step 1: Miss
def read(key): value = cache.get(key) if value: return value value = db.select(key) cache.set(key, value, ex=300) return valuedef write(key, value): db.update(key, value) cache.delete(key)- What it costs
- 1 query per miss
- Where it lives
- Lifetime 300 s
Add jitter to the lifetime.
In the real world
The same amazon elasticache doing real jobs. Pick one and step through it.
10,000 reads a second, 95 percent hits. The database manages 2,000.
Step 1 of 4
Normal
The database sees 500 a second.
Step 1: Normal
Common mistakes
The bugs interviewers watch for, with the fix beside each.
No replica
criticalIf the node fails, all data of the shard is gone and the database gets its traffic.
Wrong--num-node-groups 2 --replicas-per-node-group 0Right--num-node-groups 2 --replicas-per-node-group 1--automatic-failover-enabled --multi-az-enabledNo lifetimes
majorWith volatile-lru nothing can be evicted, so writes fail when memory is full.
Wrongcache.set(key, value)Rightcache.set(key, value, ex=300 + random.randint(0, 60))A plain client on a cluster
majorIt does not follow MOVED, so keys of other shards fail.
Wrongvalkey.Valkey(host=config_endpoint)RightValkeyCluster(host=config_endpoint, ssl=True)Trusting the total CPU
minorThe command thread can be full while a big node shows little load.
Wrongalarm on CPUUtilization > 90Rightalarm on EngineCPUUtilization > 80Failing when the cache fails
majorA cache that is down should cost speed, not availability.
Wrongvalue = cache.get(key) # raisesRighttry: value = cache.get(key)except Exception: value = None
Interview tips
How to explain it
- Managed Valkey, Redis OSS or Memcached.
- Shards for size, replicas for failures.
- OK can come before the copy.
Size it
- Three quarters of node memory.
- At most 80 percent of that.
- Times one plus the replicas.
Reach for it when
- The system runs on AWS.
- The same data is read often.
- Servers share sessions or counters.
Before production
- Multi-AZ, encryption, users.
- Rehearse failover.
- Test with an empty cache.