Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 62Data · Redis

How to check Redis memory and keys

Redis (or Valkey) lives in RAM. When memory is full it either removes keys or refuses writes — and the app breaks in strange ways. This how-to shows the memory numbers that matter, finds the biggest keys without slowing down production, checks keys without an expiry, reads the eviction policy, and finds slow commands.

  • 5 Lessons
  • 5 Commands

$ run as a normal user# run as root (or with sudo)

62.1

The quick answer

export REDISCLI_AUTH='<password>'
redis-cli INFO memory | grep -E 'used_memory_human|maxmemory_human|maxmemory_policy|mem_fragmentation_ratio'
redis-cli INFO keyspace
db0:keys=184233,expires=12041,avg_ttl=86012345
redis-cli --bigkeys
redis-cli --memkeys

REDISCLI_AUTH keeps the password out of the process list (better than -a). On RHEL 10 the client is valkey-cli — same commands.

62.2

Step by step

  1. The memory numbers:
FieldMeans
used_memory_humanMemory used by the data.
maxmemory_humanThe limit (0B = no limit — dangerous on a shared server).
maxmemory_policyWhat happens at the limit (see step 4).
mem_fragmentation_ratioAbove about 1.5 = much wasted RAM (fragmentation).
evicted_keys (in INFO stats)Keys removed because memory was full.
  1. Find big keys — --bigkeys and --memkeys scan with SCAN (safe, small steps). Then look at one key:
redis-cli MEMORY USAGE session:4f1c2a
(integer) 1048712
redis-cli TYPE session:4f1c2a
redis-cli TTL session:4f1c2a
(integer) -1

TTL -1 = no expiry — this key lives forever.

  1. Count keys by prefix, safely — never KEYS * on production (it blocks Redis until it is done):
redis-cli --scan --pattern 'session:*' | wc -l
redis-cli --scan --pattern 'cache:*' --count 1000 | head
  1. The eviction policy decides what happens at maxmemory:
PolicyUse
noevictionWrites fail with an error — for data you must not lose (queues).
allkeys-lruRemove the least used keys — a pure cache.
volatile-lru / volatile-ttlRemove only keys with an expiry.
redis-cli CONFIG GET maxmemory-policy
redis-cli CONFIG SET maxmemory 2gb
redis-cli CONFIG REWRITE

CONFIG REWRITE saves the change in the config file (or set it in redis.conf directly).

  1. Slow commands and latency:
redis-cli SLOWLOG GET 10
redis-cli --latency -i 1
redis-cli INFO clients | grep connected_clients
  1. Keep it healthy: set a maxmemory below the server RAM, give every cache key a TTL, monitor used_memory, evicted_keys and connected_clients (redis_exporter + Prometheus).

62.3

Check that it worked

used_memory is clearly below maxmemory, evicted_keys does not grow (unless it is a cache by design), big keys are known and have a TTL, and the app shows no OOM errors.

62.4

Common problems

ProblemCauseFix
OOM command not allowed when used memory > 'maxmemory'Limit reached with noeviction.Free keys, add TTLs, raise maxmemory, or use an eviction policy for a cache.
NOAUTH Authentication requiredPassword not given.REDISCLI_AUTH or redis-cli --user NAME --askpass.
Redis froze for secondsKEYS *, a huge DEL, or SAVE on a big dataset.Use SCAN, UNLINK (deletes in the background), BGSAVE.
Memory keeps growingKeys without TTL (sessions, caches).Set TTLs in the app; find them with --scan + TTL.
High mem_fragmentation_ratioMany deletes/updates of different sizes.CONFIG SET activedefrag yes (Redis/Valkey with jemalloc), or a planned restart.
The server swapsmaxmemory too high for the RAM, or no limit.Set maxmemory to about 70–80 % of free RAM.

62.5

Go further

1Why must you never run KEYS * on a production Redis?

It blocks Redis until it has read every key; use SCAN (redis-cli --scan) instead.

2What does TTL key returning -1 mean?

The key exists and has no expiry — it stays until it is deleted.

All commands in this chapter