redis参数详解

简介

1. 网上虽然关于redis参数介绍的文章有很多,但都语焉不详,甚至大多是无脑复制来的

2. 本篇博客希望能详细地,通俗地给大家讲明白那些晦涩难懂的参数配置,简单的就不再啰嗦

3. 我用的redis版本是 redis-7.0.4

参数目录

1. daemonize     是否以守护进程的方式启动redis

2. hz                    表示Redis执行定期任务的频率

3. dynamic-hz    可根据客户端连接数动态调整hz的值,需Redis 5.0或以上

4. protected-mode 是否保护模式(是否只允许本机连接该redis)

5. logfile             日志文件名字

1. daemonize     是否以守护进程的方式启动redis

这个变量在redis配置文件 redis.conf 中如下所示

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

注释说得也很清楚:默认情况下redis不是以守护进程的模式运行。如果把该变量的值设置为yes,redis将会把进程的pid(进程号)写到 /var/run/redis.pid,该值由参数 pidfile 指定

先简单解释下守护进程(daemon),守护进程是linux中后台运行的进程,执行过程中打印的信息不显示在终端,完全不受任何终端影响

linux 系统,在终端上默认开启的进程会受到这个终端的影响,如果终端退出,进程会收到SIGHUP信号,默认处理是关闭进程。如果在终端上按下 "Ctrl + c" 组合键,进程会收到SIGINT信号,默认处理也是关闭进程

综上:把 daemonize 的值配置成yes,那么redis会以守护进程的方式启动,完全不受终端的影响,打印信息也不会输出到终端上,非常推荐。要注意的是如果把 daemonize 配置成yes,并且变量 logfile 配置为空,那么redis的日志会被丢弃掉,如果redis启动失败就没办法查看报错日志了,详情参考变量 logfile 的说明

2. hz           表示Redis执行定期任务的频率

这个变量在redis配置文件redis.conf中的如下所示

# The range is between 1 and 500, however a value over 100 is usually not
# a good idea. Most users should use the default of 10 and raise this up to
# 100 only in environments where very low latency is required.
hz 10

前面还有其他注释,空间不足就不展示了。该变量表Redis执行后台任务的频率,用于计算LRU信息并清除过期key关闭超时的客户端连接执行RDB或AOF持久化相关操作更新统计信息等等。这些定期任务是Redis服务正常运行的保障,它们的执行频率由hz参数的值指定,默认为10,即每秒执行10次。

举例:主动清除过期key的执行过程如下

1. 从设置了过期时间的key的集合中随机检查20个key。

2. 删除检查中发现的所有过期key。

3. 如果检查结果中25%以上的key已过期,则重新开启新一轮任务。

hz的取值范围为1~500。增大hz参数的值会提升各项定期任务的执行频率,超时处理会更加精准。但也会提高Redis服务的CPU使用率。默认值10在一般情况下已经可以满足需求,如果业务场景对于某些定期任务的执行频率有很高的要求,您可以尝试在100以内调整参数值。

将hz的值增加到100以上对CPU使用率有相对较大的影响,请谨慎操作

3. dynamic-hz       可根据客户端连接数动态调整hz的值,需Redis 5.0或以上

该变量在redis配置文件redis.conf中的如下所示

# Normally it is useful to have an HZ value which is proportional to the
# number of clients connected. This is useful in order, for instance, to
# avoid too many clients are processed for each background task invocation
# in order to avoid latency spikes.
#
# Since the default HZ value by default is conservatively set to 10, Redis
# offers, and enables by default, the ability to use an adaptive HZ value
# which will temporary raise when there are many connected clients.
#
# When dynamic HZ is enabled, the actual configured HZ will be used as
# as a baseline, but multiples of the configured HZ value will be actually
# used as needed once more clients are connected. In this way an idle
# instance will use very little CPU time while a busy instance will be
# more responsive.
dynamic-hz yes

上面的 hz 参数用于指定Redis定期任务的执行频率,这些任务包括关闭超时的客户端连接、主动清除过期key等。

当定期任务的执行频率为一个固定值时,容易产生以下问题:

  • 如果该值过小,在超时的客户端连接数较多或过期key较多等情况下,资源无法被及时整理回收,可能影响Redis服务的性能甚至导致服务崩溃。
  • 如果该值过大,定期任务将会消耗过多的CPU资源,同样可能影响Redis服务的稳定性。

为了平衡Redis的CPU使用率和响应及时性,Redis 5.0版本新增了dynamic-hz参数,并分离了实际hz(hz)和已设置的hz(configured_hz)。

dynamic-hz 的可选值为yesno,分别代表开启动态hz和关闭动态hz,默认值为yes。当动态hz开启时,您设置的hz参数的值,即configured_hz,将作为基线值,而Redis服务中的实际hz值会在基线值的基础上根据已连接到Redis的客户端数量自动调整,连接的客户端越多,实际hz值越高,Redis执行定期任务的频率就越高

4. protected-mode 是否保护模式(是否只允许本机连接该redis)

该变量在redis配置文件redis.conf中的如下所示

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

翻译如下:protected-mode 是一个安全保护层,用于避免redis实例被互联网上的访问和使用。

保护模式打开时还有以下要求:1)没有使用 "bind"选项绑定一个地址,2)没有使用 "requirepass"选项设置密码

在保护模式下,redis实例仅仅接受 127.0.0.1 和 ::1的客户端连接

默认 protected-mode 的值为yes,即开启保护模式

注释已经很详细了,我们总结下:

如果想开启保护模式,即只允许本机的客户端才能访问,需同时满足一下三个配置

1. 配置选项 protected-mode 的值为yes

2. 不配置选项 bind

3. 不配置选项  requirepass

上面三个条件只要有一个不满足,保护模式就开启不了

有一个例外,如果你把bind选项的值设置为127.0.0.1,那么也只能本机才能连接该redis实例

5. logfile             日志文件名字

该变量在redis配置文件redis.conf中的如下所示

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""

说得很清楚,该变量用来指定日志文件的名字,如果配置为空,那么redis的日志就会重定向到标准输出。特别注意的是,如果你把redis日志重定向到标准输出(把logfile配置为空)并且把 daemonize 的值配置成yes,那么redis的日志会被发送到 /dev/null,也就是丢弃日志

这个变量有时候很有用,若启动redis时失败,是因为配置了daemonize为yes导致没有日志输出,这个时候就要把logfile打开,指定一个日志文件,方便查看报错,像下面这样

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "./redis.log"

再次启动redis,然后打开当前目录的redis.log即可查看redis的日志报错

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值