Delete Policy
Wherein the data Redis
- Redis is a database-level memory, all data stored in memory, the data memory may be acquired by a status command ttl
- XX: time-sensitive data
- -1: permanent data
- -2: the data has expired or deleted data or undefined data
Wherein the data Redis
Data removal policies
- Regularly delete
- Inert delete
- Periodically delete
Data storage structure timeliness
Target data removal policy
To find a balance between memory consumption and CPU, a loss will result in a decline in overall Redis performance, server downtime or even lead to memory leaks
Regularly delete
- Create a timer, when the key is provided with the expiration time, and the expiration time arrives, executed immediately delete operation on key tasks by the timer
- Advantages: save memory, due to deletion, quick release out unnecessary memory footprint
Disadvantage: CPU pressure, no matter how high the amount of CPU load at this time, are occupied CPU, it affects the server redis instruction throughput and response time
Summary: in exchange for storage space with a processor performance (take time for space)
Inert delete
- The data expiration time arrives, without processing. The next time you access the data, etc.
- If not expired, return data
- Found to have expired, deleted, there is no return
- Advantages: saving CPU performance, must be deleted when it is found that deletion
- Disadvantages: large memory pressure, long-term memory for emerging data
- Summary: with storage space for processor performance
Periodically delete
- Redis periodically in rotation timeliness data library using random policy, accounting data using the expired elimination frequency is controlled
- Features 1: CPU occupancy provided with a peak performance, the detection frequency customizable settings
- Feature 2: Memory pressure is not great, long-term memory for the cold data will be continued clean-up
- Summary: periodic checking of storage space (random checks, targeted surveillance)
Deleting a policy comparison
Types of | Feature |
---|---|
Regularly delete | Save memory, no footprint regardless of time CPU resources, high-frequency take time for space |
Inert delete | Memory footprint serious delay execution, CPU utilization high to get space for time |
Periodically delete | Regular random memory cleanup cost per CPU resources required to maintain a fixed memory random checks, targeted surveillance |
Eviction policy
- Redis使用内存存储数据,在执行每一个命令前,会调用freeMemoryIfNeeded()检测内存是否充足。如果内存不满足新加入数据的最低存储要求,redis要临时删除一些数据为当前指令清理存储空间。清理数据的策略被称为逐出算法。
- 注意:逐出数据的过程不是100%能够清理出足够的可使用的内存空间,如果不成功则反复执行。当对所有数据尝试完毕后,如果不能达到内存清理的要求,将出现错误信息。
影响数据逐出的相关配置
- 最大可使用内存
maxmemory
占用物理内存的比例,默认值为0,表示不限制。生产环境中根据需求设定,通常设置在50%以上。
- 每次选取待删除数据的个数
maxmemory-samples
选取数据时并不会全库扫描,导致严重的性能消耗,降低读写性能。因此采用随机获取数据的方式作为待检测删除数据
- 删除策略
maxmemory-policy
达到最大内存后的,对被挑选出来的数据进行删除的策略
- 检测易失数据(可能会过期的数据集server.db[i].expires)
- volatile-lru: 挑选最近最少使用的数据淘汰
- volatile-lfu:挑选最近使用次数较少的数据淘汰
- volatile-ttl:挑选将要过期的数据淘汰
- volatile-random: 任意选择数据淘汰
- 检测全库数据(所有数据集server.db[i].dict)
- allkeys-lru: 挑选最近最少使用的数据淘汰
- allkeys-lfu: 挑选最近使用次数最少的数据淘汰
- allkeys-random: 任意选择数据淘汰
放弃数据驱逐
- no-enviction(驱逐):禁止驱逐数据(redis4.0默认策略),会引发OOM(out of memory)
maxmemory-policy volatile-lru
数据逐出策略配置依据
- 使用INFO命令输出监控信息,查询缓存hit和miss次数,根据业务需求挑优Redis配置