Skip to content
Sign inGet all access
Chapter 10·18 minFree

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:1
OutputOK(integer) 40(integer) 2(integer) 50

INFO memory prices the server. Four lines matter:

terminal
redis-cli INFO memory | grep -E 'used_memory_human|used_memory_dataset:|maxmemory_human|maxmemory_policy'
Output$ redis-cli INFO memory | grep -E 'used_memory_human|used_memory_dataset:|maxmemory_human|maxmemory_policy'used_memory_human:1.49Mused_memory_dataset:0maxmemory_human:0Bmaxmemory_policy:noeviction

maxmemory

0 means no limit: Redis grows until the kernel kills it.

CONFIG GET maxmemory
CONFIG GET maxmemory-policy
Output1) "maxmemory"2) "0"1) "maxmemory-policy"2) "noeviction"

CONFIG 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 0
OutputOK1) "maxmemory"2) "104857600"OK

The 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 0
OutputOKOKOK(error) OOM command not allowed when used memory > 'maxmemory'."1"(integer) 1OK

Reads 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 lru
Output(error) ERR CONFIG SET failed (possibly related to argument 'maxmemory-policy') - argument(s) must be one of the following: volatile-lru, volatile-lfu, volatile-random, volatile-ttl, volatile-lrm, allkeys-lru, allkeys-lfu, allkeys-random, allkeys-lrm, noeviction

The 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 noeviction
OutputOK1) "maxmemory-policy"2) "allkeys-lru"OK

Same 5000 writes against a 10mb limit, once per policy:

terminal
for i in $(seq 1 5000); do echo "SET pad:$i $value"; done | redis-cli --pipe
Output$ # maxmemory-policy allkeys-lruerrors: 0, replies: 5000$ redis-cli DBSIZE(integer) 4121$ # maxmemory-policy noevictionerrors: 899, replies: 5000$ redis-cli DBSIZE(integer) 4101

With an lfu policy, OBJECT FREQ shows the counter eviction reads:

terminal
redis-cli OBJECT FREQ page:1
Output$ redis-cli CONFIG SET maxmemory-policy allkeys-lfuOK$ redis-cli SET page:1 cachedOK$ redis-cli OBJECT FREQ page:1(integer) 5$ redis-cli GET page:1; redis-cli GET page:1"cached""cached"$ redis-cli OBJECT FREQ page:1(integer) 6

A 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 h
OutputOK"int"OK"embstr"(integer) 2"listpack"

The 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 512
Output(integer) 5(integer) 56OK(integer) 5(integer) 262OK

The 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-entries
Output1) "hash-max-listpack-entries"2) "512"1) "set-max-listpack-entries"2) "128"1) "zset-max-listpack-entries"2) "128"

RDB snapshots

A snapshot is the whole dataset in one file. BGSAVE forks and writes it in the background:

SET saved yes
BGSAVE
OutputOKBackground saving started

The default schedule is three rules at once:

CONFIG GET save
Output1) "save"2) "3600 1 300 100 60 10000"

Writes made after the snapshot are gone on a crash. Two keys, one SIGKILL:

terminal
docker kill -s KILL some-redis && docker start some-redis
Output$ redis-cli SET saved yesOK$ redis-cli BGSAVEBackground saving started$ redis-cli SET afterwards yesOK$ docker kill -s KILL some-redis && docker start some-redis$ redis-cli GET saved"yes"$ redis-cli GET afterwards(nil)

AOF

The append-only file logs every write command instead:

terminal
docker run -d --name some-redis redis:8.12.0 redis-server --appendonly yes
Output$ docker run -d --name some-redis redis:8.12.0 redis-server --appendonly yes$ redis-cli SET saved yesOK$ redis-cli SET afterwards yesOK$ docker kill -s KILL some-redis && docker start some-redis$ redis-cli KEYS *1) "afterwards"2) "saved"

Both keys survived the identical crash. The file is plain text — your own commands:

terminal
cat /data/appendonlydir/appendonly.aof.1.incr.aof
Output$ cat /data/appendonlydir/appendonly.aof.1.incr.aof*3$3SET$5saved$3yes

A 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 appendfsync
Output1) "appendfsync"2) "everysec"

Run both: AOF for recovery, RDB for a compact file to copy elsewhere.

Replication

One command makes a second server a replica:

terminal
redis-cli -p 6380 REPLICAOF redis-main 6379
Output$ redis-cli -p 6380 REPLICAOF redis-main 6379OK$ redis-cli -p 6380 GET greeting"hello"$ redis-cli -p 6380 SET nope 1(error) READONLY You can't write against a read only replica.

The replica copied data written before it existed, and refuses writes. From the master:

terminal
redis-cli INFO replication
Output$ redis-cli INFO replicationrole:masterconnected_slaves:1slave0:ip=172.18.0.7,port=6379,state=online,offset=14,lag=1,io-thread=0

Replication is asynchronous — the master replies OK before the replica has the write. WAIT blocks until replicas confirm:

terminal
redis-cli WAIT 2 100
Output$ redis-cli SET x 1OK$ redis-cli WAIT 1 0(integer) 1$ redis-cli WAIT 2 100(integer) 1

Cluster

A cluster hashes each key name into one of 16384 slots, split across nodes:

terminal
redis-cli CLUSTER KEYSLOT user:1000
Output$ redis-cli CLUSTER KEYSLOT user:1000(integer) 1649$ redis-cli CLUSTER KEYSLOT user:1001(integer) 5712

Two keys one digit apart land on different nodes. Single-key commands redirect:

terminal
redis-cli SET user:1001 b
Output$ redis-cli SET user:1001 b(error) MOVED 5712 172.18.0.5:6379

A cluster-aware client follows that. Multi-key commands cannot:

terminal
redis-cli -c MGET user:1000 user:1001
Output$ redis-cli -c MGET user:1000 user:1001(error) CROSSSLOT Keys in request don't hash to the same slot$ redis-cli -c SINTER tags:a tags:b(error) CROSSSLOT Keys in request don't hash to the same slot

A hash tag fixes it — Redis hashes only the part inside {}:

terminal
redis-cli CLUSTER KEYSLOT '{user:1000}:profile'
Output$ redis-cli CLUSTER KEYSLOT '{user:1000}:profile'(integer) 1649$ redis-cli CLUSTER KEYSLOT '{user:1000}:sessions'(integer) 1649$ redis-cli -c MGET '{user:1000}:profile' '{user:1000}:sessions'1) "ada"2) "3"

SLOWLOG

Redis logs commands slower than a threshold, in microseconds:

CONFIG GET slowlog-log-slower-than
CONFIG GET slowlog-max-len
Output1) "slowlog-log-slower-than"2) "10000"1) "slowlog-max-len"2) "128"

SLOWLOG GET returns them. Here, a SORT over 300,000 members:

terminal
redis-cli SLOWLOG GET 1
Output$ redis-cli SLOWLOG GET 11) 1) (integer) 8 2) (integer) 1795763333 3) (integer) 68684 4) 1) "SORT" 2) "bigset" 3) "ALPHA" 4) "LIMIT" 5) "0" 6) "10" 5) "127.0.0.1:38868" 6) ""

The fields are id, timestamp, microseconds, the command, and the client. Clear it with SLOWLOG RESET:

SLOWLOG RESET
SLOWLOG LEN
OutputOK(integer) 0