What does an ElastiCache Redis cluster actually store, and how do you measure it?
Right-sizing means matching provisioned memory to the working set you have measured, not to a peak someone estimated before launch. If your cluster's node type was picked from a capacity spreadsheet or copied from another environment, start here. The best times to measure are after traffic has been stable for a few weeks, once a launch has settled into its steady pattern, and before you lock in a reserved-node commitment. A commitment fixes your node type for its term, so any oversizing becomes a long-term cost.
Reading INFO memory
Four fields matter most:
used_memoryis the total bytes the Redis allocator holds. This includes keys and values plus internal overhead such as client buffers, replication backlog, and data structure metadata.used_memory_datasetis the part of that total attributed to your data. It is the closest figure to the size of your working set.used_memory_rssis what the operating system sees the process occupying.mem_fragmentation_ratiois RSS divided byused_memory. A value well above 1 means freed memory hasn't been returned to the OS, so the node is using physical RAM that your data doesn't need.
CloudWatch equivalents
BytesUsedForCache tracks the allocator total, the same thing used_memory reports. DatabaseMemoryUsagePercentage expresses usage as a fraction of maxmemory, not as a fraction of the node's advertised RAM.
Nominal RAM vs usable memory
The node type's listed memory is not all available to your data. ElastiCache sets aside a portion, controlled by the reserved-memory-percent parameter, for forks during snapshots, replication buffers, and OS needs. maxmemory is what remains after that reservation. Use maxmemory as your sizing ceiling, never the number on the pricing page.
#!/usr/bin/env bash
set -euo pipefail
HOST="my-cache-001.abc123.0001.use1.cache.amazonaws.com" # placeholder node endpoint
NODE_ID="my-cache-001" # CacheClusterId of that node
redis-cli -h "$HOST" -p 6379 INFO memory | grep -E \
'^(used_memory|used_memory_dataset|used_memory_rss|mem_fragmentation_ratio|maxmemory):'
START=$(date -u -d '14 days ago' +%Y-%m-%dT%H:%M:%SZ)
END=$(date -u +%Y-%m-%dT%H:%M:%SZ)
for METRIC in BytesUsedForCache DatabaseMemoryUsagePercentage; do
echo "== $METRIC (hourly Maximum, last 14 days) =="
aws cloudwatch get-metric-statistics \
--namespace AWS/ElastiCache \
--metric-name "$METRIC" \
--dimensions Name=CacheClusterId,Value="$NODE_ID" \
--start-time "$START" --end-time "$END" \
--period 3600 \
--statistics Maximum \
--query 'max(Datapoints[].Maximum)' \
--output text
done
A shell check that reads the key INFO memory fields, including maxmemory, from an ElastiCache Redis node and pulls 14-day peak BytesUsedForCache and DatabaseMemoryUsagePercentage from CloudWatch so you can size against measured usage.
Who should use memory-based right-sizing?
This process fits engineers who own an ElastiCache Redis bill or its reliability:
- Platform and SRE teams that run shared cache clusters for several services and need to defend each node type with data.
- Application teams that own a dedicated cache and control its key design, TTLs and serialization format. Many of the cheapest fixes later in this article are code changes only they can make.
- Engineers reviewing cloud spend who need to tell an oversized cluster apart from one that is correctly sized for its traffic.
It applies most directly to clusters whose node type was chosen by estimate, copied from another environment, or scaled up during an incident and never revisited. It is less useful for a cluster that is already on the smallest practical node type with one shard, or for a cache that has no stable traffic history to measure yet.
When should you right-size a Redis cluster?
Run this process when you have trustworthy measurements and a reason to act on them:
- After traffic has stabilized. You need at least a few weeks of steady traffic, including full weekly cycles, so that peak figures reflect real usage.
- Before buying reserved nodes. A reservation fixes the node type for its term, so measure first and commit afterwards.
- After a launch or data-model change settles. New features change key counts, value sizes and TTL behaviour.
- When signals disagree with the bill. Examples include high memory with zero evictions and a flat hit rate, or sustained evictions on a cluster everyone believed had headroom.
- During scheduled cost reviews, so sizing is checked on a regular cadence and not only when something breaks.
Hold off during migrations, right before a known seasonal peak, or when your metrics window doesn't include the busiest period of the year. In those cases the measured peak understates what the cluster will actually need.
How does memory-based right-sizing work?
The method is a loop of measurement and verification. It does not start from a capacity guess.
- Measure the real peak. Read
INFO memoryfor a point-in-time breakdown and use CloudWatch maxima (BytesUsedForCache,DatabaseMemoryUsagePercentage) for the trend. Compare the peak againstmaxmemory, not against the node's advertised RAM. - Attribute the memory. Split
used_memoryinto dataset, overhead (buffers, replication backlog) and fragmentation. Then find which key prefixes and encodings account for the dataset. - Check whether the memory is doing useful work. Read evictions, item count and hit rate together to decide whether the cache is starving or bloated.
- Shrink what is stored. Add TTLs, remove dead prefixes and compact values before you change hardware.
- Compute a target. Add explicit margins for fragmentation, buffers and growth, divide by the usable fraction left after reserved memory, and check CPU and network per shard.
- Resize online and verify. Use online vertical scaling or resharding with a snapshot as a rollback point, and watch latency, evictions, replication lag and engine CPU.
- Keep it right-sized. Alarms on memory percentage, evictions and swap catch drift. Scheduled reviews catch the slower changes.
The sections below cover each step in that order.
Where does Redis memory go beyond the dataset?
Every key carries fixed costs before its value is counted: the entry in the main dictionary, the key string itself, object headers, and expiry metadata if a TTL is set. For datasets with millions of small values, that per-key overhead can rival the payload. Ten million 20-byte values will occupy far more than 200 MB.
Encoding matters as much as key count. Small hashes and sets are stored in compact encodings (listpack, or intset for integer-only sets) until they exceed configured entry-count or value-size thresholds. Past those limits, Redis converts them to a hashtable, which uses several times more memory per element. A schema that keeps hashes just under the threshold can cost far less than one that crosses it.
Beyond the keyspace, the replication backlog and client output buffers count toward used_memory. Replicas and Pub/Sub subscribers that fall behind can make those buffers grow quickly. Use INFO memory to separate dataset memory from overhead.
To find hotspots, run redis-cli --bigkeys to find the largest key per type by element count, and --memkeys to rank keys by bytes. Then run MEMORY USAGE on a random sample of keys from each prefix to estimate per-pattern cost.
Fragmentation, the ratio of RSS to used_memory, can look like a capacity shortage: the node reports high memory while the live dataset is modest. Be careful comparing before and after a resize. A freshly populated node lays data out compactly and reports low fragmentation, and that number rises as writes and deletes churn the allocator.
#!/usr/bin/env bash
set -euo pipefail
HOST="my-cache-ro.example.internal" # placeholder reader endpoint
PORT=6379
RCLI=(redis-cli -h "$HOST" -p "$PORT")
"${RCLI[@]}" INFO memory | grep -E '^(used_memory|used_memory_rss|used_memory_dataset|used_memory_overhead|mem_replication_backlog|mem_clients_slaves|mem_clients_normal|mem_fragmentation_ratio):'
"${RCLI[@]}" --bigkeys -i 0.01
"${RCLI[@]}" --memkeys -i 0.01
for prefix in 'session:' 'user:' 'cart:'; do
total=0; n=0
while read -r key; do
bytes=$("${RCLI[@]}" MEMORY USAGE "$key")
enc=$("${RCLI[@]}" OBJECT ENCODING "$key")
total=$((total + ${bytes:-0})); n=$((n + 1))
echo "$enc"
done < <("${RCLI[@]}" --scan --pattern "${prefix}*" | head -n 200) \
| sort | uniq -c | sed "s/^/${prefix} encoding: /"
done
for prefix in 'session:' 'user:' 'cart:'; do
"${RCLI[@]}" --scan --pattern "${prefix}*" | head -n 200 \
| while read -r key; do "${RCLI[@]}" MEMORY USAGE "$key"; done \
| awk -v p="$prefix" '{s+=$1; n++} END {if (n) printf "%s avg %.0f bytes over %d keys\n", p, s/n, n}'
done
A redis-cli script that splits INFO memory into dataset, buffer and fragmentation figures, runs --bigkeys and --memkeys, and samples MEMORY USAGE and OBJECT ENCODING per key prefix to estimate per-pattern cost and spot hashtable conversions.
How do eviction and hit-rate signals show whether a cache is starving or bloated?
Memory metrics tell you how full a node is. Eviction and hit-rate metrics tell you whether that fullness is doing useful work. Read Evictions, CurrItems, and hit rate (CacheHits against CacheMisses) together over at least a full weekly cycle, because any one of them alone can mislead.
A cluster with zero evictions and high memory use looks like it has headroom, but it often doesn't. If CurrItems climbs steadily while hit rate stays flat, the keyspace is growing with entries nobody reads again. Keys written without a TTL never expire, so Redis has no reason to evict them until maxmemory is reached. That memory is paid for but not used. Sample keys with TTL to confirm. A value of -1 means no expiry is set. The fix is to set expiries in the application, not to keep the node size.
Under-provisioning looks different, and its cost is easy to underestimate:
| Signal | Bloated cache | Starving cache |
|---|---|---|
Evictions |
Zero or near zero | Sustained, rising at peak |
CurrItems |
Grows without plateau | Plateaus at a ceiling |
| Hit rate | Flat despite growth | Drops as evictions climb |
| Write behaviour | Normal | OOM errors under noeviction |
| Latency | Stable | Spikes from misses hitting the database |
Under noeviction, a full node rejects writes instead of evicting keys, and that surfaces as application errors. Under LRU or LFU policies, the failure is quieter: hot keys get evicted, misses fall through to the primary datastore, and load and tail latency rise there. If the downsizing forces you to scale up that database, the extra cost can exceed what the smaller cache node saved.
How do you calculate the right node type and shard count?
The arithmetic below is an illustrative estimate with assumed inputs, not a measured result. Substitute your own numbers from the memory metrics discussed earlier.
Assume a peak dataset of 120 GB. Add 20% for fragmentation and client/replication buffers (144 GB), then a 25% growth margin (180 GB). That 180 GB is the total used_memory the cluster must hold. Next, account for reserved memory, which is set in the parameter group and assumed here to be 25%. It is not wasted space. When a snapshot or a replica full sync forks the process, pages that are modified during the fork get duplicated through copy-on-write, and the reserve absorbs that duplication. A node therefore needs roughly per-shard target ÷ 0.75 of memory.
| Shards | Per-primary target | Minimum node memory | Nodes with 1 replica |
|---|---|---|---|
| 4 | 45 GB | 60 GB | 8 |
| 6 | 30 GB | 40 GB | 12 |
| 9 | 20 GB | ~27 GB | 18 |
Memory is only the first filter. Redis executes commands on a single main thread per node, so per-shard throughput matters. Smaller node types also come with lower baseline network bandwidth, and some can only burst above that baseline for limited periods. Check peak engine CPU and network bytes per shard at the proposed shard count. A shard that is fine on memory but saturates CPU or bandwidth will add latency long before it evicts anything.
Cost scales as shards × (1 + replicas) × node price. Each extra replica multiplies every shard, so a layout that looks cheaper per node can cost more in total. Price each row of the table before choosing.
Should you scale up, scale out, or cut replicas?
| Factor | Fewer, larger shards | More, smaller shards |
|---|---|---|
| Blast radius | A failover affects a large share of keys | Each failure touches a smaller slice |
| Hot keys | Many keys share one node's CPU | A hot key stays pinned to one shard regardless |
| Command throughput | One main thread per node runs all commands | Command execution spreads across more threads |
| Multi-key ops | Fewer cross-slot errors in practice | Needs hash tags to keep related keys together |
| Client connections | Fewer endpoints per client | More connections per client process |
| Cost granularity | Coarse steps between sizes | Finer steps, one shard at a time |
Scaling out adds command throughput because Redis executes commands on a single thread per node. A larger node adds memory and network capacity, but its command throughput barely grows. Scaling out does not fix a single hot key, though, and every multi-key command must target one hash slot.
When comparing node families, check memory-optimized against general-purpose types, newer generations against older ones, and data tiering for large datasets that are mostly cold. Price each option against your measured peak memory and CPU. None of them is a guaranteed saving.
Base the replica count on need. A pure cache that can rebuild from the source of truth may need only one replica, or none outside production. Session stores and primary-store use cases need Multi-AZ with replicas, because losing that data is an outage. Add replicas for read load only when clients actually route reads to them.
Avoid aggressive downsizing in these cases:
- Memory headroom: peak memory leaves less than 25% headroom. This is an illustrative threshold, so set your own.
- Copy-on-write: snapshots or replica syncs cause memory spikes from copy-on-write.
- Seasonal traffic: traffic peaks fall outside your metrics window.
Right-size non-production clusters separately. Use smaller nodes and fewer replicas there, and schedule deletion when they sit idle.
What are the alternatives to buying smaller nodes?
Before you change node types, shrink what the cluster stores. Memory reduction often makes the resize smaller, and sometimes unnecessary.
- Add TTLs to unbounded keys. Session data, rate-limit counters and idempotency tokens are often written without expiry, so they accumulate until someone notices. Use
INFO keyspaceto compare the count of keys with an expiry against total keys. If most keys have no TTL, set one on every write path. - Compact serialization. Verbose JSON with repeated field names costs bytes on every value. A binary format or shorter field names reduce the per-value footprint. Compression helps with large values but adds CPU cost on the client.
- Hash-based bucketing. Millions of tiny top-level keys each carry per-key overhead. Grouping them as fields inside small hashes lets Redis use its compact encoding for small hashes. This only works while each hash stays under the configured encoding thresholds.
- Delete dead key prefixes. Features get retired but their keys stay. Find them with
SCANandMATCH, notKEYS, and remove them withUNLINKso large deletions don't block the server. - Match
maxmemory-policyto the workload. A pure cache usually fitsallkeys-lruorallkeys-lfu. Avolatile-*policy only evicts keys that have a TTL, so a cluster full of keys without TTLs fills up anyway and writes start failing.
The cost comparison is about where the work happens. These changes take developer time once, in code review and a backfill. Resizing costs money every month, and it does nothing to stop the growth that caused the problem. Fixing the write paths removes the recurring capacity alerts and emergency scale-ups. That also makes later right-sizing decisions hold, because the dataset stops growing on its own.
#!/usr/bin/env bash
set -euo pipefail
HOST="my-cache.xxxxxx.0001.use1.cache.amazonaws.com" # placeholder endpoint
PORT=6379
DEAD_PREFIX="legacy_reco:*" # keys left behind by a retired feature
rcli() { redis-cli --tls -h "$HOST" -p "$PORT" "$@"; }
rcli INFO keyspace
rcli INFO memory | grep -E '^(used_memory_human|maxmemory_human|maxmemory_policy):'
COUNT=$(rcli --scan --pattern "$DEAD_PREFIX" --count 1000 | wc -l)
echo "keys matching $DEAD_PREFIX: $COUNT"
if [[ "${CONFIRM:-no}" == "yes" ]]; then
rcli --scan --pattern "$DEAD_PREFIX" --count 1000 \
| xargs -r -n 100 redis-cli --tls -h "$HOST" -p "$PORT" UNLINK > /dev/null
rcli INFO memory | grep '^used_memory_human:'
else
echo "Dry run only. Re-run with CONFIRM=yes to delete."
fi
A redis-cli audit script that checks TTL coverage and the active maxmemory-policy, then counts and optionally removes a dead key prefix using SCAN and batched UNLINK before any node resize.
How does right-sizing save engineering time?
The direct saving is money. The indirect saving is the engineering time that undersized or unexplained caches consume:
- Fewer capacity incidents. Most unplanned cache work comes from clusters that fill up without warning: OOM write errors under
noeviction, eviction storms that push load onto the database, or emergency scale-ups during peak traffic. If you size from measured peaks with explicit headroom and add alarms on memory and evictions, these become planned changes made during business hours. - Faster diagnosis. Once you have split memory into dataset, overhead and fragmentation, and know which prefixes dominate, a later alert points to a specific cause. Engineers don't have to rediscover the cluster's layout each time.
- Shorter sizing debates. A table of measured peaks, assumed margins and per-row costs replaces arguments about whether a cluster "needs" its current node type. The decision can be reviewed and repeated.
- Reusable runbooks. The scripts in this article cover measurement, hotspot discovery, pre-resize audit, and resize with rollback and alarms. They work for every cluster, so the second and later reviews take much less effort than the first.
- Fixes that stay fixed. TTLs on write paths and removal of dead prefixes stop the growth that triggers repeated resizes. Without them, each resize only postpones the next one.
- Safer rollbacks. A snapshot plus a recorded configuration makes reversal a known procedure, which shortens any incident caused by a resize that went too far.
How do you execute a resize safely and keep it right-sized?
Before touching a production cluster, take a manual snapshot and record the current node type, shard count and parameter group. That record is your rollback plan, and with it a reversal is just the same resize run in the opposite direction.
Running the change
ElastiCache supports online vertical scaling, which changes the node type, and online resharding for cluster-mode-enabled clusters, which adds or removes shards. Both keep the cluster serving traffic. Neither is free: data is copied to new nodes or slots migrate between shards, which adds CPU and network load and can briefly disrupt client connections. Schedule the change for your lowest-traffic window so that this load lands on spare headroom, not on peak demand.
During and after the change, watch these signals:
- Client-side p99 latency: what users actually feel.
Evictions: a rise means the new size is too small for the working set.ReplicationLag: replicas falling behind on a busy primary.EngineCPUUtilization: Redis runs commands on a single thread, so a smaller node can saturate even when memory looks fine.
If evictions or latency stay elevated through a full traffic cycle, roll back rather than waiting.
Staying right-sized
Set CloudWatch alarms on DatabaseMemoryUsagePercentage, Evictions and SwapUsage. Any sustained swap means the node is short on memory and needs attention immediately. Review sizing on a fixed schedule, and again a few weeks after any feature launch that changes key counts or value sizes.
Buy reserved nodes only after sizing has held steady for several review cycles. A reservation locks in a node type for its term. If you reserve before sizing settles, you end up keeping an oversized node because you already paid for the discount.
#!/usr/bin/env bash
set -euo pipefail
RG_ID="orders-cache" # replication group to resize
NEW_TYPE="cache.r6g.large" # target node type from memory analysis
NODE_ID="orders-cache-0001-001" # member cluster used for node-level alarms
SNS_ARN="arn:aws:sns:us-east-1:123456789012:cache-alerts"
aws elasticache create-snapshot \
--replication-group-id "$RG_ID" \
--snapshot-name "${RG_ID}-pre-resize-$(date +%Y%m%d)"
aws elasticache describe-replication-groups \
--replication-group-id "$RG_ID" > "${RG_ID}-before.json"
aws elasticache modify-replication-group \
--replication-group-id "$RG_ID" \
--cache-node-type "$NEW_TYPE" \
--apply-immediately
aws elasticache wait replication-group-available --replication-group-id "$RG_ID"
aws cloudwatch put-metric-alarm \
--alarm-name "${NODE_ID}-memory-high" \
--namespace AWS/ElastiCache --metric-name DatabaseMemoryUsagePercentage \
--dimensions Name=CacheClusterId,Value="$NODE_ID" \
--statistic Average --period 300 --evaluation-periods 3 \
--threshold 80 --comparison-operator GreaterThanThreshold \
--alarm-actions "$SNS_ARN"
aws cloudwatch put-metric-alarm \
--alarm-name "${NODE_ID}-evictions" \
--namespace AWS/ElastiCache --metric-name Evictions \
--dimensions Name=CacheClusterId,Value="$NODE_ID" \
--statistic Sum --period 300 --evaluation-periods 1 \
--threshold 0 --comparison-operator GreaterThanThreshold \
--alarm-actions "$SNS_ARN"
An AWS CLI script that takes a snapshot and records the current configuration as a rollback point, runs an online node-type change, and adds CloudWatch alarms on memory percentage and evictions.
How OneEnv can help
Disclosure: OneEnv is the product behind this blog.
OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.
Applied to elasticache cost optimization, this is where it helps:
Saves time
- Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
- One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
- Changing a shared variable is one edit and one review instead of editing every service's
.envby hand.
Saves cost
- Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
- Less duplicated configuration to maintain: one shared definition inherited by many services.
- A wrong or stale value is caught at review time by the services it affects, not by a failed deploy.
- OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.
These are qualitative benefits, not measured results — how much you save depends on how many services share the value.
Want to try it? Start at oneenv.io.