Redis memory is full solution:
1, increase memory;
2, use memory elimination strategy.
3, Redis cluster.
Let’s focus on the second and third solutions:
The second one:
We know that the redis settings configuration file The maxmemory parameter can control its maximum available memory size (bytes).
So what should I do when the required memory exceeds maxmemory?
At this time, the maxmemory-policy in the configuration file comes into play.
The default value is noeviction.
Below I will list the elimination rules for deleting redis keys when the available memory is insufficient.
LRU algorithm, least recently used, least recently used algorithm. That is to say, the least recently used key is deleted by default.
But you must pay attention to one thing! Redis does not accurately delete the least recently used key among all keys, but randomly selects 3 keys and deletes the least recently used key among these three keys.
Then the number 3 can also be set, and the corresponding location is maxmeory-samples in the configuration file.
The third method:
Redis only supports single instance, memory Generally up to 10~20GB. For systems with 100 to 200GB of memory, it needs to be supported through clustering.
Redis cluster has three methods: client sharding, proxy sharding, RedisCluster
Client sharding
Implement routing through business code
Advantages: You can control the sharding algorithm yourself, and the performance is better than that of the proxy
Disadvantages: High maintenance costs, expansion/shrinking and other operation and maintenance operations require your own research and development
Agent Sharding
The agent receives data requests from the business program and distributes these requests to the correct Redis instance according to the routing rules and returns them to the business program. Implemented using middleware such as Twemproxy and Codis.
Advantages: Easy operation and maintenance, the program does not need to worry about how to connect the Redis instance
Disadvantages: It will cause performance consumption (about 20%), cannot smoothly expand/shrink, and needs to execute scripts to migrate data , inconvenient (Codis optimizes and implements pre-sharding based on Twemproxy to achieve Auto Rebalance).
Redis Cluster
Advantages: Official cluster solution, no central node, direct connection to the client, good performance
Disadvantages: The solution is too Heavy, unable to smoothly expand/shrink, need to execute the corresponding script, inconvenient, too new, no corresponding mature solution case
For more redis knowledge, please pay attention to the redis introductory tutorial column.
The above is the detailed content of Solution to redis memory full. For more information, please follow other related articles on the PHP Chinese website!