0

Given a distributed cache such as Redis that is setup to use read replicas. How does the LRU or time to live get updated within the master instance when all the reads are happening on the replicas? Do the replicas have to also talk to the master to let it know that a read happened for a given key so that it can update the LRU? Wouldn't this essentially put the load back onto the master (since we were trying to move reads off it to begin with to support more connections)? Or do they do something more clever like say sending batches of keys that have been read since the last time they talked? It seems to me the master has to know when things get read in order to properly evict older items from the cache

Adam Ritter
  • 989
  • 1
  • 9
  • 19

1 Answers1

0

Master knows nothing about which keys have been accessed on replicas, i.e. these statistics won't sent back to master. So in your case, master's LRU expiration won't take replicas statistics into account. If master only in charge of write operations, only these writes operation are used to do LRU expiration.

Since Redis 5, by default, replicas won't do LRU eviction. Instead, it only syncs delete operations from master, i.e. if a key has been evicted on master node, it will sync a delete operation to replica, and then the replica can evict it.

If you enable LRU eviction on replica, i.e. replica-ignore-maxmemory no, instead of evicting keys notified by master, it will also remove keys on replica based on the read and write operations on replica.

for_stack
  • 21,012
  • 4
  • 35
  • 48
  • Does that mean replica can return expired values? – sonus21 Sep 17 '20 at 09:10
  • Sorry, my original answer might be confusing. I mean eviction when maxmemory limit reaches, not expiration. I've updated the answer. – for_stack Sep 17 '20 at 09:18
  • Replica can return evicted values, if master has not synced a delete operation to replica. However, when the sync finishes, it will be deleted from the replica, and no longer been returned. – for_stack Sep 17 '20 at 09:19
  • Replica returning evicted values can be a disaster afaik, Redis runs background job as well to expire/delete keys. Also, the delay could be long if no query was sent to the master node. Does not replica track key expiry? For use case you have configured Redis to use LRU with limited resources that means some keys need to be forcefully evicted. – sonus21 Sep 17 '20 at 09:25
  • 1
    Normally replica doesn't expire keys either. Instead it waits for master to sync delete operation. This is similar to eviction. However, if your slave is writable, it will do expiration for keys written to replica, since master has no idea on these keys. – for_stack Sep 17 '20 at 09:44