AlgoViz

Amazon ElastiCache

medium 28 min

Amazon ElastiCache is AWS's managed in-memory store.

Video lesson · Chapter 1 of 5

A price lookup misses the cache

0:00 / 4:08

ElastiCache in 90 seconds

1:30Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Amazon ElastiCache is a service that runs an in-memory data store for you inside your AWS network. You choose one of three engines: Valkey, Redis OSS or Memcached. Your application talks to it with an ordinary client for that engine.

There are two ways to rent it. A node-based cluster is a number of machines whose type and count you choose. A serverless cache has no nodes to choose: it grows and shrinks, and you pay for the stored data and for the work your requests cause.

It is most often used as a cache beside a database. With replicas, backups and, for newer Valkey versions, a durable log, it can also hold data that lives only there.

Why it exists

Reading a value from memory on another machine takes well under a millisecond plus the network. A database query often takes tens of milliseconds and uses resources that are hard to add.

Running such a store yourself means replacing broken machines, promoting replicas, patching, resizing without losing data, and watching all of it at night. ElastiCache takes that work. What it cannot take is the design of the cache: see cache-aside and choosing a cache pattern.

It is the usual answer in designs on AWS when you say the words "in-memory cache". Knowing what is inside, and what is lost in a failover, makes the answer strong.

A short history

  1. 22 August 2011

    AWS announces ElastiCache, at first with Memcached only.

  2. 4 September 2013

    Redis is added as a second engine.

  3. 27 November 2023

    Serverless caches for Redis OSS and Memcached.

  4. 28 March 2024

    After a licence change of Redis, the Linux Foundation announces Valkey, a fork of Redis 7.2.4 that stays open source.

  5. 8 October 2024

    ElastiCache supports Valkey, starting with 7.2. For Redis OSS it offers versions up to 7.1.

  6. 2026

    Valkey 9.0 and 9.1 arrive in ElastiCache. Since 9.0 a node-based cluster can write to a durable log in several zones, and serverless caches can have a public endpoint.

Words to know

Engine
The software inside: Valkey, Redis OSS or Memcached.
Node
One cache machine of a chosen type.
Shard
A primary node and up to five replicas that hold the same part of the data.
Cluster mode
Enabled: the data is split over several shards. Disabled: one shard holds everything.
Hash slot
One of 16,384 groups of keys. Each shard owns some of them.
Multi-AZ
Replicas stand in other availability zones, and one is promoted if the primary fails.
ECPU
The unit of work in serverless billing. Simple reads and writes cost one per kilobyte transferred.
Endpoint
The DNS name your client connects to: primary, reader or configuration endpoint.

How it works

Step through the idea, or press Play.

Step 1 of 3

Ask. The client finds the shard that owns the key

Understand it

Start here

The rented pantry

Imagine a kitchen that fetches every jar from a market twenty minutes away. The cook could build a pantry. Instead the kitchen rents a walk-in pantry next door, and the rent includes a keeper.

A pantry with a keeper
MARKET20 minutesEvery jar from the market

The keeper replaces broken shelves and minds the spare. Which jars to keep is still the cook's decision.

Engines

Valkey, Redis OSS or Memcached

The three engines, November 2026

ValkeyRedis OSSMemcached
Versions offered7.2 to 9.14.0 to 7.11.4 to 1.6.22
Data typesStrings, hashes, lists, sets, sorted sets, streams and moreThe same familyStrings only
Replicas and failoverYesYesNo on node-based clusters
ShardsYes, with cluster modeYes, with cluster modeYes, the client spreads keys over nodes
BackupsYesYesServerless only
Durable log, search, vectorsYes, in newer versionsNoNo
ThreadsOne thread for commands, others for network workThe sameMany threads
Versions 4 and 5 of Redis OSS left standard support on 31 January 2026, and version 6 follows on 31 January 2027. After that, extended support costs extra.
  • Valkey

    The default choice for something new. AWS puts new features here, and it accepts the commands of Redis OSS 7.2.

  • Redis OSS

    For existing clusters. You can upgrade to Valkey in place, and you cannot go back without a new cluster.

  • Memcached

    The simplest model: a big, multi-threaded hash table. Losing a node loses its share of the cache.

The word OSS matters. Redis changed its licence in 2024, and current Redis versions from Redis Ltd. are not what ElastiCache runs. The engines have their own pages: Redis, Memcached and the comparison Memcached and Redis.

The shape

Shards, replicas and zones

Two shards, one replica each

App servers inside our virtual network write to the primary node of shard 1 and of shard 2 in zone A. Each primary copies to its replica in zone B with a dashed arrow. A separate database is asked on a miss.
The client chooses the shard by the key. Replicas stand in another zone.
Cluster mode disabledCluster mode enabled
Shards11 to 500
Replicas per shard0 to 50 to 5
Grow for readsAdd replicasAdd replicas
Grow for writes or sizeA larger node typeMore shards, online
Client connects toPrimary and reader endpointConfiguration endpoint, with a cluster client
Automatic failoverOptional, recommendedAlways on
The default quota is 90 nodes per cluster, so 500 shards need a quota increase.

A key belongs to a slot: CRC16 of the key, modulo 16,384. Commands on several keys work only if the keys are in the same slot. Put the common part in braces, as in cart:{42}:items and cart:{42}:total, and only the part in braces is hashed. The general idea is data partitioning.

From key to node

Clients remember the map. A wrong guess is answered with MOVED and the right address.

Sizing

How much memory, how many nodes

Usable memory and shards

13.5 GB×(1−0.25)≈10.1 GB⌈4010.1×0.8⌉=5 shards13.5\,\text{GB} \times (1 - 0.25) \approx 10.1\,\text{GB} \qquad \left\lceil \frac{40}{10.1 \times 0.8} \right\rceil = 5\ \text{shards}
AWS keeps 25 percent in reserve by default (reserved-memory-percent). Assumed: 40 GB of data, nodes of 13.5 GB, at most 80 percent full. With one replica each, that is 10 nodes.
  1. 1

    Reserve

    The reserve is for copy-on-write, output buffers and fragmentation. AWS advises against lowering it, and recommends 30 percent on small and 50 percent on micro nodes.

  2. 2

    Overhead

    Each key costs some bytes on top of its value. Load a sample and measure instead of adding up sizes.

  3. 3

    Eviction

    The default policy is volatile-lru: only keys with a lifetime may be thrown out. If nothing has a lifetime, writes fail when memory is full. For a pure cache choose allkeys-lru.

  4. 4

    One hot key

    More shards do not help a single key. Cache it in the application, or read it from replicas. See high-concurrency caching.

Failure

What happens when a node dies

A failover, step by step

OK was said for a write that is gone. That is asynchronous replication.
SetupA primary failsA zone failsAWS uptime commitment
No replicaData of that shard is lost, a new empty node comesThe sameLower (single-AZ)
Replica, Multi-AZReplica with the least lag is promoted, writes resume in secondsSurvives99.99%
ServerlessHidden from you, data in three zonesSurvives99.99%
Memcached over several zonesThe node's share of keys is lostThe share in that zone is lost99.9%

How much can a failover lose?

lost writes≈w×lag5000 /s×0.1 s=500\text{lost writes} \approx w \times \text{lag} \qquad 5000\,/\text{s} \times 0.1\,\text{s} = 500
Assumed write rate and replication lag. Watch the ReplicationLag metric, and treat a cache as something that may forget.

Durability

From cache to data store

With Valkey 9.0 and later on node-based clusters you can switch on durability. Every write also goes to a log that is spread over several zones. There are two modes, and the mode can be changed on a running cluster.

ModeThe client gets OKWrite latencyCan be lost
OffWhen the primary has it in memoryMicrosecondsWhat was not yet replicated
Asynchronous logBefore the log has itMicrosecondsUp to 10 seconds of writes
Synchronous logAfter at least two zones have itSingle-digit millisecondsDesigned for nothing
Reads stay at microseconds in all modes, says AWS. These are latencies inside the service; your network is added.

Backups are something else: a snapshot once a day or on demand, kept for at most 35 days. They warm a new cluster or undo a mistake, and a restore is as old as the snapshot. AWS's other service in this area, MemoryDB, was built as a durable database from the start.

Serverless

Without choosing nodes

One endpoint, many hidden nodes

An app server connects over TLS to one endpoint of a serverless cache. Inside the service boundary, a proxy layer passes requests to storage in zone A, zone B and zone C. AWS scales everything inside the boundary.
Scaling, replacement and upgrades happen behind the endpoint.

What serverless traffic costs in units

20000 /s×3.2 KB=64000 ECPU/s×3600=230 million per hour20000\,/\text{s} \times 3.2\,\text{KB} = 64000\ \text{ECPU/s} \qquad \times 3600 = 230\ \text{million per hour}
One unit per kilobyte for simple reads and writes. Assumed: 20,000 reads a second of 3.2 KB. Multiply by the current price of your region and compare with nodes.

Serverless limits (Valkey and Redis OSS)

LimitValue
Data per cache5,000 GiB
Data per slot32 GiB
Work per cache15 million ECPU a second
Work per slot30,000 ECPU a second, up to 90,000 with reads from replicas
Caches per region40 (default quota)
Encryption in transitAlways on
Evictionvolatile-lru, not changeable

It scales fast and not instantly. For Valkey 8.0 AWS states that the supported request rate can double every 2 to 3 minutes. A launch that multiplies traffic tenfold in one minute needs a minimum set beforehand. AWS says a new minimum is in place within 60 minutes, and you pay for it from then on, used or not. A maximum protects the bill, and it also means throttled requests.

Serverless fits

  • New products and unknown traffic
  • Spiky or often idle use
  • Small teams
  • A cache reachable from outside the network (Valkey 9.0 and later)

Nodes fit better

  • Steady, predictable load, where you can plan nodes and commit to them
  • Durable log or copies in other regions
  • Clients that do not speak cluster mode
  • Own parameters, such as the eviction policy

Using it

Patterns in the application

Cache-aside, the usual pattern

AWS calls this lazy loading. The cache holds only what was asked for, and it can be empty without breaking the app.

Average read time

t=tc+(1−h) td=1+0.05×20=2 mst = t_c + (1-h)\,t_d = 1 + 0.05 \times 20 = 2\ \text{ms}
Assumed: 1 ms to the cache, 20 ms to the database, 95 percent hits. A miss pays both. The database sees 5 percent of the reads, and must survive 100 percent if the cache is empty.
UseEngine featureMind
Database resultsStrings with a lifetimeInvalidate or accept the age: cache consistency
SessionsHashes with a lifetimeReplicas, because a lost session logs somebody out
LeaderboardSorted setsOne board is one key, so one shard
Rate limitsCounters with a lifetimeDecide what happens if the cache is down
Vector and text searchValkey 8.2 and 9.0Memory for the index

Operate

Network, security and what to watch

  1. 1

    Network

    Caches live in your VPC. Allow the port only from the security group of the application. Only serverless Valkey caches can have a public endpoint, and that demands IAM authentication and TLS 1.3.

  2. 2

    Encryption and users

    Switch on encryption in transit and at rest when you create a node-based cluster, and use role-based access control with one user per application.

  3. 3

    Maintenance

    Each cluster has a weekly window of 60 minutes. Planned replacements are a failover, so replicas matter on quiet days too.

  4. 4

    Other regions

    A global datastore copies a node-based Valkey or Redis OSS cluster to other regions asynchronously. The copies are read-only until you promote one by hand.

  5. 5

    Version changes

    You can upgrade, not downgrade. Try the new version on a copy restored from a backup.

Alarms worth having

MetricMeaningAct
EngineCPUUtilizationThe thread that runs commandsNear 90%: more shards for writes, replicas for reads. Plain CPU misleads on big nodes
DatabaseMemoryUsagePercentage, EvictionsMemory is shortMore shards, shorter lifetimes
CacheHitRateIs it useful at all?A drop means the database is getting the traffic
ReplicationLagHow far replicas are behindIt is what a failover can lose
CurrConnections, NewConnectionsConnection leaks and stormsPool connections, especially from functions
Shell
# Node-based: 2 shards, 1 replica each, over two zones, encryptedaws elasticache create-replication-group \  --replication-group-id shop \  --replication-group-description "product cache" \  --engine valkey --cache-node-type cache.r7g.large \  --cluster-mode enabled \  --num-node-groups 2 --replicas-per-node-group 1 \  --automatic-failover-enabled --multi-az-enabled \  --transit-encryption-enabled --at-rest-encryption-enabled# Serverless, with an upper bound for data and workaws elasticache create-serverless-cache \  --serverless-cache-name shop-sls --engine valkey \  --cache-usage-limits 'DataStorage={Maximum=10,Unit=GB},ECPUPerSecond={Maximum=50000}'# Rehearse a failover of shard 1aws elasticache test-failover --replication-group-id shop --node-group-id 0001
Names are examples. Add your subnet group and security groups.

Decide

When to choose it, and when not

Which form?

Neighbours

OptionAlikeDifferentChoose it for
Valkey or Redis on your own serversSame engineYou operate it; every module and setting is openOther clouds, special needs
MemoryDBValkey and Redis OSS commands, managedBuilt as a durable database from the startPrimary data in memory
DynamoDB, with DAXManaged, fastA database; DAX caches only DynamoDB callsDurable key-value data
CloudFrontA managed cacheHTTP responses near the userPages, files, public APIs
Memory in the app processFastestPer process, small, no sharingA few hot values, in front of ElastiCache

Strengths

  • Failed nodes are replaced and replicas promoted for you
  • Standard clients and commands
  • Online resharding, backups, metrics
  • A serverless form without sizing

Costs

  • Only inside AWS
  • Some commands and settings are restricted
  • Failover can lose recent writes by default
  • Memory is expensive, and replicas multiply it

Summary

The whole topic in one minute.

ElastiCache rents you Valkey, Redis OSS or Memcached. Valkey is the engine that new features arrive in, Redis OSS stops at 7.1, and Memcached is the simple one without replicas.

Split data over shards for size and writes, add replicas in other zones for failures and reads, and leave a quarter of the memory free. Failover takes seconds and can lose the newest writes unless you turn on the durable log with synchronous writes. Serverless removes the sizing, and the limits that remain are per cache and per slot.

  • Three engines: Valkey for new work, Redis OSS up to 7.1, Memcached for the simplest case.
  • Shards for size and writes, replicas for failures and reads, up to five per shard.
  • A quarter of node memory is reserved; volatile-lru is the default policy.
  • Failover takes seconds and may lose the newest writes, unless the synchronous log is on.
  • Serverless: pay per stored data and ECPU, limits per cache and per slot.
  • The database must survive an empty cache.

Sources