Home / AWS

AWSRunbookIntermediate

Sev1: ElastiCache for Redis or Valkey rejects writes with OOM command not allowed

Every write to an ElastiCache cluster fails with OOM command not allowed. Confirm memory pressure, fix the eviction policy or free memory, scale up safely, and stop it happening again.

Deepak Bhardwaj6 min read

Applies toAmazon ElastiCache for Redis OSS 6.x/7.x and Valkey 7.x/8.x (node-based clusters), AWS CLI v2, redis-cli / valkey-cli

Symptoms

  • Application logs show OOM command not allowed when used memory > 'maxmemory'. on SET, HSET, LPUSH, INCR and similar commands.
  • Reads still work, so dashboards may look half-healthy while sessions, carts, rate limiters or job queues fail.
  • DatabaseMemoryUsagePercentage in CloudWatch is at or near 100%.
  • Evictions is flat at zero while memory is full. That combination is the important clue: the engine is full and has nothing it is allowed to evict.

Redis and Valkey only return this error when maxmemory is reached and the eviction policy cannot free space. Either the policy is noeviction, or it is a volatile-* policy and the keys have no TTL.

Step 1: confirm memory pressure (5 minutes)

Pull the last hour of the key metrics for the primary node. Node IDs look like my-redis-001.

NODE=my-redis-001
for M in DatabaseMemoryUsagePercentage Evictions CurrItems SwapUsage; do
  echo "== $M"
  aws cloudwatch get-metric-statistics --namespace AWS/ElastiCache --metric-name $M \
    --dimensions Name=CacheClusterId,Value=$NODE \
    --start-time $(date -u -d '-1 hour' +%FT%TZ) --end-time $(date -u +%FT%TZ) \
    --period 300 --statistics Maximum --query "sort_by(Datapoints,&Timestamp)[].[Timestamp,Maximum]" --output text
done

Then check the engine directly. Use --tls if in-transit encryption is on, and --user/--pass or -a if AUTH or RBAC is enabled.

redis-cli -h my-redis.xxxxxx.ng.0001.euw1.cache.amazonaws.com -p 6379 --tls INFO memory | \
  grep -E "used_memory_human|maxmemory_human|maxmemory_policy|mem_fragmentation_ratio"
redis-cli -h <endpoint> -p 6379 --tls INFO keyspace

INFO keyspace shows keys= and expires= per database. If expires is far lower than keys and the policy is volatile-lru, you have found the cause.

What you see Cause Fix
Policy volatile-lru (the ElastiCache default), few keys with TTL Nothing is eligible for eviction Switch to allkeys-lru or add TTLs
Policy noeviction Engine never evicts by design Change the policy if the data is a cache; scale if it is not
Evictions high, memory still full, latency up Working set is larger than the node Scale up or out
mem_fragmentation_ratio well above 1.5 Fragmented memory after heavy churn Enable activedefrag (where supported) or fail over to a fresh node
One key type dominates memory A runaway list, stream or set Find and trim the big key
SwapUsage climbing reserved-memory-percent too low for background saves Raise reserved memory, scale up

Step 2: decide what the data is

Before changing anything, ask the owning team one question: can any of this data be lost?

  • A pure cache (rebuildable from a database) can use an allkeys-* policy. Eviction is safe.
  • Sessions, queues, locks or counters that only exist in Redis must not be evicted silently. For those, free memory deliberately or scale up.

Warning

Changing the policy to allkeys-lru on a cluster that holds the only copy of queue messages or sessions will start deleting that data the moment the change applies. Get explicit agreement from the data owner first.

Step 3: restore writes

Option A: change the eviction policy (cache workloads)

ElastiCache blocks the CONFIG SET command, so the change must go through a parameter group. You cannot edit the default group. Create a custom one for your engine family. Family names vary by version (redis7, valkey7, valkey8); check yours:

aws elasticache describe-replication-groups --replication-group-id my-redis \
  --query "ReplicationGroups[].MemberClusters" --output text
aws elasticache describe-cache-clusters --cache-cluster-id my-redis-001 \
  --query "CacheClusters[].[Engine,EngineVersion,CacheParameterGroup.CacheParameterGroupName]" --output table
aws elasticache create-cache-parameter-group --cache-parameter-group-name my-redis7-allkeys \
  --cache-parameter-group-family redis7 --description "allkeys-lru for cache workloads"

aws elasticache modify-cache-parameter-group --cache-parameter-group-name my-redis7-allkeys \
  --parameter-name-values ParameterName=maxmemory-policy,ParameterValue=allkeys-lru

aws elasticache modify-replication-group --replication-group-id my-redis \
  --cache-parameter-group-name my-redis7-allkeys --apply-immediately

If the cluster already uses a custom group, run only the modify-cache-parameter-group command. maxmemory-policy is a dynamic parameter, so no reboot is needed. Watch Evictions rise and the OOM errors stop within a minute or two.

Option B: free memory by deleting known-disposable keys

Find what is using memory. --bigkeys and --memkeys scan the keyspace with SCAN, so they are safe to run but add load; run them against a replica endpoint if you have one.

redis-cli -h <reader-endpoint> -p 6379 --tls --bigkeys
redis-cli -h <reader-endpoint> -p 6379 --tls --memkeys

Delete a disposable prefix in batches with UNLINK, which frees memory in the background:

redis-cli -h <primary-endpoint> -p 6379 --tls --scan --pattern 'tmp:report:*' --count 1000 | \
  xargs -r -n 500 redis-cli -h <primary-endpoint> -p 6379 --tls UNLINK

Caution

FLUSHALL and FLUSHDB delete everything in seconds and cannot be undone. Use them only when the whole dataset is confirmed rebuildable, and take a manual backup first if time allows: aws elasticache create-snapshot --replication-group-id my-redis --snapshot-name pre-flush-$(date +%Y%m%d%H%M).

Option C: scale up or out

Vertical scaling of a replication group is online, but it takes time and it needs free memory to copy data, so do Option A or B first if you can.

aws elasticache list-allowed-node-type-modifications --replication-group-id my-redis
aws elasticache modify-replication-group --replication-group-id my-redis \
  --cache-node-type cache.r7g.xlarge --apply-immediately

For cluster mode enabled, add shards instead:

aws elasticache modify-replication-group-shard-configuration --replication-group-id my-redis \
  --node-group-count 4 --apply-immediately

Track progress with aws elasticache describe-replication-groups --replication-group-id my-redis --query "ReplicationGroups[].Status" until it returns available.

Step 4: verify

redis-cli -h <primary-endpoint> -p 6379 --tls SET oncall:probe ok EX 60
redis-cli -h <primary-endpoint> -p 6379 --tls INFO stats | grep -E "evicted_keys|rejected_connections"

Confirm the application error rate has dropped, then check for side effects: a cold cache after a flush pushes load onto the database behind it. Watch its CPU and connection count for the next 30 minutes.

Prevent it next time

  • Alarm before it is full. Alert at 80% DatabaseMemoryUsagePercentage, and separately on Evictions > 0 for clusters that should never evict.
aws cloudwatch put-metric-alarm --alarm-name my-redis-memory-80 \
  --namespace AWS/ElastiCache --metric-name DatabaseMemoryUsagePercentage \
  --dimensions Name=CacheClusterId,Value=my-redis-001 \
  --statistic Maximum --period 300 --evaluation-periods 2 --threshold 80 \
  --comparison-operator GreaterThanOrEqualToThreshold \
  --alarm-actions arn:aws:sns:eu-west-1:111122223333:oncall
  • Set the policy on purpose. Cache clusters: allkeys-lru or allkeys-lfu. Data that must not be lost: noeviction, plus a memory alarm and capacity headroom.
  • Give every cache key a TTL. Make it a code review rule; a missing TTL is how volatile-lru clusters fill up.
  • Separate workloads. Do not put a disposable cache and a job queue in the same cluster; they need opposite eviction policies.
  • Keep reserved-memory-percent at its default (25%) unless you have measured that backups and replication need less.
  • Load test with production-sized data after any change to key layout or serialization, which can double memory use per key.