Memory, Persistence and Operations
What happens when it fills up or restarts.
Everything so far assumed the server has room and stays up.
Where the memory went
MEMORY USAGE prices one key in bytes, name and overhead included:
SET greeting hello
MEMORY USAGE greeting
HSET user:1 name ada age 36
MEMORY USAGE user:1INFO memory prices the server. Four lines matter:
redis-cli INFO memory | grep -E 'used_memory_human|used_memory_dataset:|maxmemory_human|maxmemory_policy'maxmemory
0 means no limit: Redis grows until the kernel kills it.
CONFIG GET maxmemory
CONFIG GET maxmemory-policyCONFIG SET applies live, but only to the running server — redis.conf needs the same line:
CONFIG SET maxmemory 100mb
CONFIG GET maxmemory
CONFIG SET maxmemory 0The default policy evicts nothing
Set a limit below current usage and the next write fails:
SET a 1
SET b 2
CONFIG SET maxmemory 1mb
SET c 3
GET a
DEL b
CONFIG SET maxmemory 0Reads and deletes still work, so you can DEL your way out or raise the limit.
Choosing a policy
There are ten, and an invalid one lists them:
CONFIG SET maxmemory-policy lruThe prefix matters more than the algorithm. allkeys-* evicts anything, volatile-* only
keys with a TTL:
CONFIG SET maxmemory-policy allkeys-lru
CONFIG GET maxmemory-policy
CONFIG SET maxmemory-policy noevictionSame 5000 writes against a 10mb limit, once per policy:
for i in $(seq 1 5000); do echo "SET pad:$i $value"; done | redis-cli --pipeWith an lfu policy, OBJECT FREQ shows the counter eviction reads:
redis-cli OBJECT FREQ page:1A volatile-* policy with no TTLs behaves like noeviction — nothing is eligible.
OBJECT ENCODING
Every type has a compact representation for small values and a general one above a threshold:
SET n 12345
OBJECT ENCODING n
SET s hello
OBJECT ENCODING s
HSET h a 1 b 2
OBJECT ENCODING hThe difference is large. Both hashes hold the same five fields:
HSET small a 1 b 2 c 3 d 4 e 5
MEMORY USAGE small
CONFIG SET hash-max-listpack-entries 0
HSET big a 1 b 2 c 3 d 4 e 5
MEMORY USAGE big
CONFIG SET hash-max-listpack-entries 512The thresholds are configuration — you can see where each type switches:
CONFIG GET hash-max-listpack-entries
CONFIG GET set-max-listpack-entries
CONFIG GET zset-max-listpack-entriesRDB snapshots
A snapshot is the whole dataset in one file. BGSAVE forks and writes it in the background:
SET saved yes
BGSAVEThe default schedule is three rules at once:
CONFIG GET saveWrites made after the snapshot are gone on a crash. Two keys, one SIGKILL:
docker kill -s KILL some-redis && docker start some-redisAOF
The append-only file logs every write command instead:
docker run -d --name some-redis redis:8.12.0 redis-server --appendonly yesBoth keys survived the identical crash. The file is plain text — your own commands:
cat /data/appendonlydir/appendonly.aof.1.incr.aofA clean SHUTDOWN saves first. Only a crash or a power cut exposes the gap.
What you can still lose is set by appendfsync, and the default is not the safest:
CONFIG GET appendfsyncRun both: AOF for recovery, RDB for a compact file to copy elsewhere.
Replication
One command makes a second server a replica:
redis-cli -p 6380 REPLICAOF redis-main 6379The replica copied data written before it existed, and refuses writes. From the master:
redis-cli INFO replicationReplication is asynchronous — the master replies OK before the replica has the write.
WAIT blocks until replicas confirm:
redis-cli WAIT 2 100Cluster
A cluster hashes each key name into one of 16384 slots, split across nodes:
redis-cli CLUSTER KEYSLOT user:1000Two keys one digit apart land on different nodes. Single-key commands redirect:
redis-cli SET user:1001 bA cluster-aware client follows that. Multi-key commands cannot:
redis-cli -c MGET user:1000 user:1001A hash tag fixes it — Redis hashes only the part inside {}:
redis-cli CLUSTER KEYSLOT '{user:1000}:profile'SLOWLOG
Redis logs commands slower than a threshold, in microseconds:
CONFIG GET slowlog-log-slower-than
CONFIG GET slowlog-max-lenSLOWLOG GET returns them. Here, a SORT over 300,000 members:
redis-cli SLOWLOG GET 1The fields are id, timestamp, microseconds, the command, and the client. Clear it with
SLOWLOG RESET:
SLOWLOG RESET
SLOWLOG LEN