AWS Builder Center

Your Cache Is Swapping — Here’s How to Stop the Cascade Before It Starts

A field guide to understanding why undersized ElastiCache/Valkey nodes silently swap to disk and cascade into full outages — plus the three CloudWatch alarms that give you weeks of warning before it happens.

Teams right-size compute and databases but forget cache nodes. An undersized cache fills up silently. The OS swaps to disk. Latency spikes. The cluster cascades into a full outage. Three CloudWatch alarms would have given you weeks of warning.



The Review

I'm doing an architecture review with a team running Valkey on ElastiCache. Standard engagement. We're going through their infrastructure, checking configurations, making sure things are production-ready.
I pull up their CloudWatch metrics and notice memory utilization on their cache cluster sitting at 88%.
"What node type is this?"
cache.t3.micro. 384MB of RAM. They picked it months ago because "it's just a cache." The dataset started small. It grew. Nobody watched memory.
They haven't had an outage yet. But they're about to.



What the Metrics Are Telling Me

I ask them to run  INFO memory  on their primary node.
used_memory: 347,000,000
used_memory_rss: 112,000,000
mem_fragmentation_ratio: 0.32

Here's what this means.
 used_memory  says Valkey thinks it's holding 347MB. But  used_memory_rss  says only 112MB is actually in physical RAM right now. The rest, around 235MB, has been paged into  swap space  on disk.
The  fragmentation ratio  confirms it. This is  used_memory_rss  divided by  used_memory . Healthy is 1.0 or above. Theirs is 0.32. Most of their data is already on disk.
Every time Valkey needs that data it reads from disk instead of RAM. Disk is orders of magnitude slower. They haven't felt it yet because traffic is light, but the moment they get a spike, this node will go unresponsive.
I check CloudWatch for swap. 155MB. For a cache, that number should always be zero.



What Would Have Happened Next

If we hadn't caught this, here's how it plays out:
  1. Traffic spike hits. Node A is already swapping. Responses slow down.
  1. Clients timeout. They reconnect and retry.
  1. The connection storm adds more memory pressure.
  1. Node A goes unresponsive. The cluster marks it failed.
  1. Traffic floods the surviving nodes. Same node type. Same 384MB.
  1. They start swapping too.
  1. Full cluster outage.
All the nodes are identically undersized. Moving traffic around just moves the problem.



What I Told Them to Do

Scale up immediately. They moved to m6g.large (6.38GB). Swap went to zero. Fragmentation ratio came back to 1.2. Latency dropped back to sub-millisecond.
Then we set up the alarms they should have had from day one.



Three Alarms Every Cache Cluster Needs

Getting full
  • Metric:  DatabaseMemoryUsagePercentage 
  • Threshold: above 70 to 80% for 5 minutes (tune this based on how spiky your traffic is)
In trouble
  • Metric:  SwapUsage 
  • Threshold: above 0 MB for 5 minutes
Drowning
  • Metric: Fragmentation Ratio (custom metric from  INFO memory )
  • Threshold: below 1.0 for 5 minutes
The swap alarm is the most universal one. Caches should never swap, regardless of workload. The memory threshold is the one you calibrate. If your traffic is bursty, set it lower so you have time to react. If it's steady, you can set it higher.



What To Do When They Fire

Memory above 70%. Check your TTLs. Look for memory leaks. Start planning a scale-up.
Memory above 80%. Scale up or scale out (add shards). Don't wait.
Swap above 0 MB. Investigate immediately. Something is wrong.
Fragmentation ratio below 1.0. This confirms swapping is active. Scale now.
Your working set should stay below 75% of node memory. The remaining 25% is headroom for fragmentation, connections, and replication buffers. That's a starting point. Adjust based on your workload.



Set This Up Now

Whatever monitoring tool you use (CloudWatch, Datadog, Grafana, Prometheus, New Relic), create these two alerts:
  1. Memory utilization above a threshold you're comfortable with (70 to 80% is a reasonable starting range)
  1. Swap usage above 0 MB for 5 minutes
Route them to wherever your on-call lives. Slack, PagerDuty, email, whatever.
In CloudWatch, the metric names are  DatabaseMemoryUsagePercentage  and  SwapUsage  under the  AWS/ElastiCache  namespace. In Datadog, look for  aws.elasticache.database_memory_usage_percentage  and  aws.elasticache.swap_usage . Other tools pull the same underlying CloudWatch metrics.
Five minutes to set up. Catches the problem before your customers do.


Be sure to join the  NoSQL Launchpad in Builder Center . That's where all the AWS NoSQL specialists hang out, knowledge share, and post cool stuff.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article