redis的maxmemory设置

redis的maxmemory参数用于控制redis可使用的最大内存容量。如果超过maxmemory的值,就会动用淘汰策略来处理expaire字典中的键。

关于redis的淘汰策略,很多文章都有描述,就不做解释了。

关于maxmemory的设置,如果redis的应用场景是作为db使用,那不要设置这个选项,因为db是不能容忍丢失数据的。

如果作为cache使用,则可以启用这个选项(其实既然有淘汰策略,那就是cache了。。。)

但是在集群环境下(尤其是有多个slavers的情形),maxmeomory的值并不是实际redis使用的内存,这个选项值并没有包括slaver的output buffer。

redis早期版本出过一个bug,在多个slaver的情形下,设置了maxmemory值,同时设定了淘汰策略,会造成master上的数据被渐渐擦除。

antirez先生给出了这个问题的原因:

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版本的配置说明中有了以下表述:

# 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>

由此可见,如果有slaver的情况下,建议适当调低maxmemory,给output buffer留出一定的可用空间是合理的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 03:11:56

redis的maxmemory设置的相关文章

Redis的主从复制设置与安装

1 验证redis的主从复制,将过程抓图 实验步骤 A.  安装Redis B.  启动Master Redis C.  创建redis-slave.conf配置文件 修改Redis端口和打开slaveof 设置 port 6389 ....... slaveof 127.0.0.1 6379 D.  启动Redis Slave E.  查看进程,确认启动正常 F. 操作master redis G. 读取slave Redis Redis的主从复制设置与安装,布布扣,bubuko.com

Redis + keepalived 主从设置与搭建

Redis+keepalived安装及配置: 1.ip规划: 服务器 Ip地址 VIP 应用软件 Redis主 192.168.1.102 192.168.1.120 Redis+ keepalived Redis备 192.168.1.103 Redis+ keepalived 2.服务器基础配置安装 先安装好基础装机环境,具体参考  服务器基础环境配置 文档 redis安装: 1.#下载redies安装包 wget http://download.redis.io/releases/redi

Linux下Nginx、PHP、MySQL、Redis开机自启动设置

一.Nginx开机启动设置 1.在/etc/init.d/目录下创建脚本 vi /etc/init.d/nginx 2.更改脚本权限 chmod 775 /etc/init.d/nginx 3.编写脚本内容 #! /bin/shset -ePATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="nginx daemon"NAME=nginxDAEMON=/usr/local/nginx/sbin/$N

Windows环境下redis 配置文件中设置的密码无效

当我们安装了redis服务后,发现在其配置文件redis.windows.conf(或redis.conf)设置了密码:requirepass ****** 但是打开redis-cli.exe后输入命令config get requirepass发现: 这说明配置文件中密码设置后没有生效. 原因:问题在于我们启动redis服务时是直接在其安装目录中双击redis-server.exe启动的,这样启动的结果是,配置文件不会指定,此时redis并不会自动使用安装目录下的redis.windows.c

[redis] mac下redis安装、设置、启动停止

From: https://www.cnblogs.com/shoren/p/redis.html 下载安装 需要下载release版本,下载地址: http://download.redis.io/releases/ 我这里下载的是: http://download.redis.io/releases/redis-3.2.5.tar.gz 解压到/usr/local/redis目录中,然后依次执行以下命令: cd /usr/local/redis make test sudo make ins

redis 自启动服务设置

1.安装redis tar -zxvf redis-2.8.6.tar.gz  解压 cd redis-2.8.6 Make Make install   目的把Redis中生成的redis-server redis- cli 拷贝到 /usr/local/bin 目录中 好处:可以在Linux服务器中的任意位置 执行 redis-server  redis-cli 2.安装(我也执行了这一步) Mkdir /etc/redis   放置Redis的配置文件的   查看:find redis 或

redis安全基线设置

一.禁止使用root用户启动 | 访问控制 使用root权限去运行网络服务是比较有风险的(nginx和apache都是有独立的work用户,而redis没有).redis crackit 漏洞就是利用root用户的权限来替换或者增加authorized_keys,来获取root登录权限的 使用root切换到redis用户启动服务: ``` useradd -s /sbin/nolog -M redis sudo -u redis /<redis-server-path>/redis-serve

redis的maxmemory与maxmemory-policy关系

如果redis配置了maxmemory和maxmemory-policy策略,则当redis内存数据达到maxmemory时,会根据maxmemory-policy配置来淘汰内存数据,以避免OOM.redis提供了以下6种淘汰策略:1,noeviction:不执行任何淘汰策略,当达到内存限制的时候客户端执行命令会报错.2,allkeys-lru:从所有数据范围内查找到最近最少使用的数据进行淘汰,直到有足够的内存来存放新数据.3,volatile-lru:从所有的最近最少访问数据范围内查找设置到过

Redis登录密码设置

1. 更改Redis.conf配置 # requirepass foobared 去掉注释,foobared改为 自己的password , 我测试的时候用的是默认的 foobared 2.启动redis /usr/local/redis/src/redis-server -p 6371 3.测试 /usr/local/redis/src/redis-cli -p 6371 -a foobared redis>set a b (error) ERR operation not permitted