redis的maxmemory设置以及淘汰策略介绍
转载地址:http://www.2cto.com/database/201507/420889.html
redis的maxmemory参数用于控制redis可使用的最大内存容量。如果超过maxmemory的值,就会动用淘汰策略来处理expaire字典中的键。
关于redis的淘汰策略:
Redis提供了下面几种淘汰策略供用户选择,其中默认的策略为noeviction策略:
· noeviction:当内存使用达到阈值的时候,所有引起申请内存的命令会报错。
· allkeys-lru:在主键空间中,优先移除最近未使用的key。
· volatile-lru:在设置了过期时间的键空间中,优先移除最近未使用的key。
· allkeys-random:在主键空间中,随机移除某个key。
· volatile-random:在设置了过期时间的键空间中,随机移除某个key。
· volatile-ttl:在设置了过期时间的键空间中,具有更早过期时间的key优先移除。
PS:

关于maxmemory的设置,如果redis的应用场景是作为db使用,那不要设置这个选项,因为db是不能容忍丢失数据的。
如果作为cache使用,则可以启用这个选项(其实既然有淘汰策略,那就是cache了。。。)
但是在集群环境下(尤其是有多个slavers的情形),maxmeomory的值并不是实际redis使用的内存,这个选项值并没有包括slaver的output buffer。
redis早期版本出过一个bug,在多个slaver的情形下,设置了maxmemory值,同时设定了淘汰策略,会造成master上的数据被渐渐擦除。
antirez先生给出了这个问题的原因:
|
1
2
3
4
5
6
7
|
The issue happens for the following reason:Redis reached the configured limit, so it tries to expire keys.Evicting keys turns into explicit DELs sent to slaves, since masters control the eviction of slaves for well known reasons.But this way if there are enough slaves, emitting the protocol in the output buffers will actually take more memory than the amount freed removing keys...So the key eviction process starts to enter into an infinite loop.Up to a given point the fact that there is a static buffer part in the output queue of every client (including slaves) mitigate this in certain conditions, but once Redis can't use the output buffer but must use the queue of objects the infinite loop is triggered. |
简单说来,删除过期键,需要产生del命令发送给slaver,如果slaver足够多,output buffer将会占用足够多的内存,导致更多的键过期,如此往复,陷入了无线循环。
解决方案有多种,比如output buffer可以不计入maxmemory。
因此,在3.0版本的配置说明中有了以下表述:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
# WARNING: If you have slaves attached to an instance with maxmemory on,# the size of the output buffers needed to feed the slaves are subtracted# from the used memory count, so that network problems / resyncs will# not trigger a loop where keys are evicted, and in turn the output# buffer of slaves is full with DELs of keys evicted triggering the deletion# of more keys, and so forth until the database is completely emptied.## In short... if you have slaves attached it is suggested that you set a lower# limit for maxmemory so that there is some free RAM on the system for slave# output buffers (but this is not needed if the policy is 'noeviction').## maxmemory <bytes></bytes> |
由此可见,如果有slaver的情况下,建议适当调低maxmemory,给output buffer留出一定的可用空间是合理的。
redis的maxmemory设置以及淘汰策略介绍的更多相关文章
- Redis系列之----Redis的过期设置及淘汰策略
Redis的过期时间机制和内存淘汰策略 Redis的数据是存储在内存中的,而服务器的内存大小是有限制的,除非宕机,否则这些数据会一直存在,对于一些不再使用的key,也应当进行删除,否则会浪费内存 ...
- redis 提供 6种数据淘汰策略
淘汰策略的原因 在 redis 中,允许用户设置最大使用内存大小 server.maxmemory,在内存限定的情况下是很有用的.譬如,在一台 8G 机子上部署了 4 个 redis 服务点,每一个服 ...
- Redis篇:持久化、淘汰策略,缓存失效策略
关注公众号,一起交流,微信搜一搜: 潜行前行 redis 持久化 redis 的数据是保存再系统内存里面的.持久化就是把内存的数据转移到磁盘中,redis 的持久化策略有两种:RDB.AOF RDB ...
- 动手实现 LRU 算法,以及 Caffeine 和 Redis 中的缓存淘汰策略
我是风筝,公众号「古时的风筝」. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 那天我在 LeetCode 上刷到一道 LRU 缓存机制的问题, ...
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
作者:小林coding 计算机八股文网站:https://xiaolincoding.com 大家好,我是小林. Redis 的「内存淘汰策略」和「过期删除策略」,很多小伙伴容易混淆,这两个机制虽然都 ...
- redis数据淘汰策略
概述 在 redis 中,允许用户设置最大使用内存大小 server.maxmemory,在内存限定的情况下是很有用的.譬如,在一台 8G 机子上部署了 4 个 redis 服务点,每一个服务点分配 ...
- Redis的内存淘汰策略
Redis占用内存大小 我们知道Redis是基于内存的key-value数据库,因为系统的内存大小有限,所以我们在使用Redis的时候可以配置Redis能使用的最大的内存大小. 1.通过配置文件配置 ...
- Redis 内存满了怎么办? Redis的内存淘汰策略
https://juejin.im/post/5d674ac2e51d4557ca7fdd70 Redis占用内存大小 我们知道Redis是基于内存的key-value数据库,因为系统的内存大小有限, ...
- Redis达到最大占用内存后的淘汰策略
1. 查询Redis最大占用内存 # 查询最大占用内存 config get maxmemory # 为0时在64操作系统中不限制内存,在32位操作系统中最大为3GB 2. Redis设置最大占用内存 ...
随机推荐
- Java安全 – JCE Blowfish算法报错
代码里用Blowfish算法加解密,结果jdk升到1.7后算法初始化失败 java.lang.RuntimeException: java.lang.RuntimeException: PANIC: ...
- 通过TortoiseGit来使用Github或Visual Studio Online版本控制管理
一.前言 关于Git源码不再多阐述,它就是一款开源分布式版本控制工具,它在源码管理领土上目前为止,使用者比例很大,越来越多的人使用该工具来管理项目源码,且相当多的开源的项目都移步到Github中,如: ...
- ceph journal更换位置
只在这里做简单的演示 ceotos7 环境 3个mon节点 3个osd节点 环境搭建我这里不再叙述 我们查看一下分区情况: [root@ceph_1 ~]# lsblkNAME MAJ: ...
- svg_png
#!/usr/bin/env python#-*- encoding=UTF-8 -*-from __future__ import print_functionimport sysimport ra ...
- 第五章 服务容错保护: Spring Cloud Hystrix
在微服务架构中, 存在着那么多的服务单元, 若一个单元出现故障, 就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构更加不稳定.为了解决这样的问题, 产生了断路器等一系 ...
- BIOS设置图解教程-看完就没有不明白的了
BIOS(基本输入/输出系统)是被固化在计算机CMOS RAM芯片中的一组程序,为计算机提供最初的.最直接的硬件控制.BIOS主要有两类∶AWARD BIOS和AMI BIOS.正确设置BIOS可大大 ...
- [ExtJs6] 环境搭建及创建项目
1. 环境搭建 sencha cmd 和 extjs6 sdk. sencha cmd: https://www.sencha.com/products/extjs/cmd-download/ ext ...
- Win7 Wifi 老断线
在cmd命令窗口 netsh wlan set autoconfig enabled=no interface="无线网络连接" 此时你再来查看Win7系统任务栏处的网络菜单中查找 ...
- Delphi C++Builder RAD XE Ver 版本 官方发布时间
RAD 新版本发布时间记录 代号,官方发布时间 RIO 10.3.1,VER330,Product Ver 26 Program File 20,2019.2.14 发布 24周年 RIO 10.3, ...
- Java使用 VelocityEngine模板引擎快速生成HTML等各种代码
https://blog.csdn.net/icannotdebug/article/details/79725297 一.简介 Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言 ...