How to do when redis memory is full?

It Abu 2021-01-23 21:45:44
redis memory

Redis Occupied memory size

We know Redis It's memory based key-value database , Because the memory size of the system is limited , So we're using Redis You can configure Redis The maximum memory size that can be used .

1、 Configure through profile

By means of Redis Under the installation directory redis.conf Add the following configuration settings to the configuration file to set the memory size

// Set up Redis The maximum occupied memory size is 100M
maxmemory 100mb

redis The configuration file of does not have to be under the installation directory redis.conf file , start-up redis When serving, you can pass a parameter to specify redis Of the configuration file

2、 Modify... By command

Redis It supports dynamic modification of memory size by command at runtime

// Set up Redis The maximum occupied memory size is 100M> config set maxmemory 100mb
// Get the set Redis The maximum memory size that can be used> config get maxmemory

If you do not set the maximum memory size or set the maximum memory size to 0, stay 64 Unlimited memory size under bit operating system , stay 32 Bit operating system is the most commonly used 3GB Memory

Redis Memory obsolescence

Now that you can set Redis Maximum occupied memory size , Then the configured memory will be used up . When the memory runs out , And go on to Redis If you add data to it, there will be no memory available ?

actually Redis Several strategies are defined to deal with this situation :

noeviction( The default policy ): No more services for write requests , Direct return error (DEL Except for requests and some special requests )

allkeys-lru: From all key Use in LRU The algorithm is eliminated

volatile-lru: From the set expiration time of key Use in LRU The algorithm is eliminated

allkeys-random: From all key Random elimination of data in

volatile-random: From the set expiration time of key In random elimination

volatile-ttl: After setting the expiration time key in , according to key The expiration time for elimination , The earlier they expire, the better they will be eliminated

When using volatile-lru、volatile-random、volatile-ttl These three strategies are , without key Can be eliminated , And noeviction Return error as well

How to get and set memory retirement strategy

Get the current memory retirement strategy :> config get maxmemory-policy

Set the obsolescence policy through the configuration file ( modify redis.conf file ):

maxmemory-policy allkeys-lru

Modify the elimination strategy by command :> config set maxmemory-policy allkeys-lru

LRU Algorithm

What is? LRU?

It says Redis The maximum available memory is used up , Yes, you can use LRU Algorithm for memory elimination , So what is LRU The algorithm ?

LRU(Least Recently Used), Least recently used , Is a cache replacement algorithm . When using memory as a cache , The size of the cache is generally fixed . When the cache is full , At this time, continue to add data to the cache , We need to eliminate some old data , Free up memory to store new data . It can be used at this time LRU The algorithm . The central idea is this : If a data hasn't been used in the last period of time , So the possibility of being used in the future is very small , So it can be eliminated .

LRU stay Redis In the implementation of

The approximate LRU Algorithm

Redis It's an approximation LRU Algorithm , It's like the regular LRU The algorithm is not quite the same . The approximate LRU The algorithm uses random sampling to eliminate data , Every time you randomly come out 5( Default ) individual key, Get rid of the least recently used key.

Can pass maxmemory-samples Parameter changes the number of samples : example :maxmemory-samples 10
maxmenory-samples The larger the configuration , The closer the elimination result is to the strict LRU Algorithm

Redis In order to achieve approximation LRU Algorithm , For each key Added an extra one 24bit Field of , Used to store the key Last time visited .

Redis3.0 To approximate LRU The optimization of the

Redis3.0 To approximate LRU The algorithm has been optimized . The new algorithm maintains a pool of candidates ( The size is 16), The data in the pool is sorted according to the access time , For the first time key Will be put into the pool , And then each time I randomly selected key Only when the access time is less than the minimum time in the pool will it be put into the pool , Until the candidate pool is full . When it's full , If there's a new one key Need to put in , The last access time in the pool will be the maximum ( Recently interviewed ) The removal of .

When it comes to elimination , Then select the least recent access time directly from the pool ( The longest time I haven't been interviewed ) Of key Just get rid of it .

LRU Comparison of algorithms

We can compare each other through an experiment LRU The accuracy of the algorithm , The first Redis Add a certain amount of data n, send Redis Out of available memory , Go back to Redis Add inside n/2 New data for , At this time, we need to eliminate some of the data , If strictly LRU Algorithm , What should be eliminated is the first one to join n/2 The data of . Generate the following LRU Comparison of algorithms ( picture source ):

 Insert picture description here

You can see three different colors in the picture :

  • Light grey is the data that was eliminated

  • Grey is the old data that hasn't been eliminated

  • Green is new data

We can see that Redis3.0 The number of samples is 10 The resulting graph is closest to the strict LRU. And also use 5 Number of samples ,Redis3.0 Better than Redis2.8.

LFU Algorithm

LFU The algorithm is Redis4.0 A new elimination strategy . Its full name is Least Frequently Used, Its core idea is based on key The frequency of recent visits is eliminated , Rarely visited priorities are eliminated , Many of the people interviewed were left behind .

LFU The algorithm can better represent a key The heat of being interviewed . If you use LRU Algorithm , One key I haven't been interviewed for a long time , Just once in a while , So it's considered hot data , Will not be eliminated , And some of them key What is likely to be visited in the future will be eliminated . If you use LFU This is not the case with algorithms , Because using one at a time doesn't make one key Become hot data .

LFU There are two strategies :

  • volatile-lfu: After setting the expiration time key Use in LFU Algorithm elimination key

  • allkeys-lfu: Of all the key Use in LFU Algorithms eliminate data

Set up and use the two elimination strategies as mentioned above , But it's important to note that the two-week strategy can only be in Redis4.0 And the above settings , If in Redis4.0 The following settings will report an error


One last question , Some people may have noticed , I didn't explain why Redis Use approximation LRU Algorithms without using accurate LRU Algorithm , You can give your answer in the comments area , Let's talk about learning .

Focus , Neverlost

All right, everyone , The above is the whole content of this article , You can see the people here , All are personnel . I said before ,PHP There are many technical points in this aspect , It's also because there are so many , I can't write it down , We won't read too much after writing it , So I've organized it into PDF And documentation , If you need anything, you can

Click to enter the code : PHP+「 platform 」

 Insert picture description here

 Insert picture description here

More learning can be found in 【 Benchmarking big factories 】 The high-quality goods PHP Architect tutorial catalog , As long as you can finish it, make sure your salary goes up one step ( Continuous updating )

I hope the above will help you , quite a lot PHPer There are always some problems and bottlenecks in the advanced stage , There is no sense of direction in the business code , I don't know where to start to improve , I've compiled some information about it , Including but not limited to : Distributed architecture 、 Highly scalable 、 High performance 、 High concurrency 、 Server performance tuning 、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql Optimize 、shell Script 、Docker、 Microservices 、Nginx If you need advanced advanced dry goods, you can share them for free , You can join me if you need PHP Technology exchange group

本文为[It Abu]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云