AlgoViz

Amazon ElastiCache

medium 28 min

Amazon 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

Our systemUsersApp serverGET product:42ElastiCachenothingDatabase

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

Our systemUsersApp server10,000 a secondElastiCache95% hitsDatabase500 a second

The database sees 500 a second.

Step 1: Normal

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • No replica

    critical

    If 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 0
    Right
    --num-node-groups 2 --replicas-per-node-group 1--automatic-failover-enabled --multi-az-enabled
  • No lifetimes

    major

    With volatile-lru nothing can be evicted, so writes fail when memory is full.

    Wrong
    cache.set(key, value)
    Right
    cache.set(key, value, ex=300 + random.randint(0, 60))
  • A plain client on a cluster

    major

    It does not follow MOVED, so keys of other shards fail.

    Wrong
    valkey.Valkey(host=config_endpoint)
    Right
    ValkeyCluster(host=config_endpoint, ssl=True)
  • Trusting the total CPU

    minor

    The command thread can be full while a big node shows little load.

    Wrong
    alarm on CPUUtilization > 90
    Right
    alarm on EngineCPUUtilization > 80
  • Failing when the cache fails

    major

    A cache that is down should cost speed, not availability.

    Wrong
    value = cache.get(key)   # raises
    Right
    try: 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.