Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Today we’ll pick up where we left off with configuration in Redis. No more words, just get started.

MEMORY MANAGEMENT

Maximum cache policy

maxmemory-policy:

  • Volatile – lru: useLRU(least recently used) algorithm removalkey, only for keys with expiration time set
  • Allkeys – lru: useLRUAlgorithm to removekey(all thekey)
  • Volatile – LFU: Use the LFU (least frequently used) approximation algorithm for expired keys
  • Allkeys-lfu: lfu approximation algorithm is used for allkeys
  • Volatile -random: Removes random from expired collectionskey, only for keys with expiration time set
  • Allkeys-random: Removes randomkey
  • Volatile – TTL: removes thoseTTLValue is the smallestkeyThat is, those that have recently expiredkey
  • Noeviction: Will not be removed. For write operations, only error message is returned (default) (go to company to observe dimension, should not select this)

LRU algorithm, LFU algorithm or TTL algorithm are not very accurate algorithm, but an approximate algorithm.

Using policy rules:

  1. If the data has a power-law distribution, that is, some data is accessed with high frequency and some data is accessed with low frequency, useallkeys-lru.
  2. If the data is equally distributed, that is, all data are accessed at the same frequencyallkeys-random.

Sample size

To set the sample size, the algorithms mentioned above are not exact algorithms, but estimates, so you can set the sample size.

maxmemory-samples 5
Copy the code

The default value is 5, which means Redis randomly picks five keys and selects the one that best fits the criteria. 5 is appropriate for LRU. 10 is close to a true LRU, but consumes more CPU. Three is faster but not as precise.

The replica ignores maximum memory

replica-ignore-maxmemory yes
Copy the code

Starting with Redis 5, the MaxMemory setting is ignored by default by replica nodes (unless the node is promoted to master after a failover).

This means that only master executes the expired delete policy, and master sends DEL to Replica after deleting the key.

This behavior ensures the consistency of the master and Replicas, and is usually what you need, but if your replica node is writable, or if you want the replica node to have different memory configurations, and you ensure that all writes to replicas are idempotent, Then you can modify the default behavior (make sure you understand what you’re doing).

Note that by default, the Replica node does not execute an expiration policy. It may use more memory than maxMemory sets. Therefore, you need to monitor the machine where the Replicas node resides and ensure that it does not exceed the size of the physical memory when the master node reaches its configured maxMemory size.

That’s it for today, but we’ll cover the master-slave replication and persistence part of the configuration file in a future article.

Add AH Q can join the technical exchange group for soul exchange! Background message get Java dry goods information: study notes and big factory interview questions!