Monitoring

Redis Big Keys: How to Find Them and Remove Them Safely

One hash with ten million fields or a list nobody ever trims can stall Redis for seconds, break replication and pin a whole cluster shard. Here is how to find big keys without hurting production, how to delete them without blocking, and how to stop them coming back.

· 12 min read · AG Data Team

Redis big keys are single keys that hold far more data than the rest: a hash with millions of fields, a list used as an unbounded queue, a sorted set leaderboard nobody ever trims, a multi-megabyte JSON string. Redis executes commands on one main thread, so one operation that touches the whole key, including deleting it, holds every other client until it finishes.

Why one key can hurt everything

Finding big keys without hurting production

# biggest keys by element count, per type (uses SCAN)
redis-cli -h replica1 --bigkeys -i 0.1

# biggest keys by memory
redis-cli -h replica1 --memkeys

# both, with a top-N list and size distribution (newer redis-cli)
redis-cli -h replica1 --keystats --top 20

# only one key prefix
redis-cli -h replica1 --memkeys --pattern 'session:*'

Measuring one key

MEMORY USAGE cart:91823              # estimate, samples 5 nested values
MEMORY USAGE cart:91823 SAMPLES 0    # exact, walks every element (slow on huge keys)
TYPE cart:91823
HLEN cart:91823      # or LLEN / SCARD / ZCARD / XLEN / STRLEN
OBJECT ENCODING cart:91823

MEMORY USAGE reports the bytes a key and its value need in RAM, including administrative overhead. For nested types it samples 5 nested values by default and extrapolates; SAMPLES 0 samples all of them (MEMORY USAGE). Sampling everything is exact but O(N), so on a ten-million-element key it is itself a slow command. Use the default for discovery and the full count only when you need the real number, ideally on a replica.

Deleting a big key without blocking

# frees memory in a background thread instead of blocking
UNLINK feed:global

# or make DEL behave like UNLINK (Redis default: no)
CONFIG SET lazyfree-lazy-user-del yes

# trim a huge hash gradually instead of deleting it at once
redis-cli --no-raw HSCAN big:hash 0 COUNT 500
# ... HDEL big:hash field1 field2 ... field500, repeat with the next cursor
Do not run discovery with KEYS or full reads. KEYS * blocks the server for the whole keyspace, and "just checking" a suspicious key with HGETALL is exactly the slow command you are trying to find. Count first (HLEN, LLEN, SCARD, ZCARD), measure second.

Stopping them from coming back

On large datasets and clusters

On a test instance with a few thousand keys, nothing here is noticeable. At tens of gigabytes and hundreds of thousands of operations per second, big keys change how the whole system behaves:

Key takeaways

Redis Valkey Memory Performance
Work with us

Redis stalling and you suspect big keys?

We audit Redis and Valkey keyspaces on production, find the keys that hurt latency and memory, and plan safe cleanups and data-model fixes.

Get in touch More from the blog