Do replicas apply eviction policies if maxmemory is set?

Last updated 22, Mar 2024

Question

In a Redis replicated topology having the eviction policy set to a value different from noeviction, is the decision to evict the kays based on just master reads/writes or also on the read operations on the replica/s?

Answer

Redis OSS replicated topologies allow read operations on replicas, so the question applies to Redis or Redis Stack replicated topologies. Redis Enterprise and Redis Cloud databases do not support read operations on replica shards. Read operations influence the last access or frequency of access of a key, which could potentially determine different keys evicted on the master and the replica, leading to the inconsistency of data between the master and its replicas.

From the documentation:

When a master and a replica instances are well-connected, the master keeps the replica updated by sending a stream of commands to the replica to replicate the effects on the dataset happening in the master side due to: client writes, keys expired or evicted, any other action changing the master dataset.

So eviction and expirations are replicated to the replicas. Note also that replicas do not take the initiative to evict keys because of an out-of-memory (OOM) condition.

By default, a replica will ignore maxmemory (unless it is promoted to master after a failover or manually). It means that the eviction of keys will be handled by the master, sending the DEL commands to the replica as keys evict in the master side.

You can verify how the master propagates the eviction of commands by setting strict maxmemory value on a master server:

CONFIG SET maxmemory <BYTES>

Then you can connect a redis-cli session to the master in replica mode:

redis-cli --replica
sending REPLCONF capa eof
SYNC with master, discarding 213 bytes of bulk transfer...
SYNC done. Logging commands from master.

Then insert some data on the master, until an OOM condition is reached:

SET hello world
(error) OOM command not allowed when used memory > 'maxmemory'

And verify from redis-cli that the DEL command is propagated.

"ping"
"ping"
"DEL","collection"
"DEL","hello"
"ping"

References

Refer to the documentation for an explanation of the eviction mechanism.