当前位置:   article > 正文

Redis的常用命令和配置文件

默认值是300s,我个人觉得300s还是太长

行业解决方案、产品招募中!想赚钱就来传!>>> hot3.png

常用命令

关键字忽略大小写,Redis的命令手册见redis中文网

redis中常用的数据类型 string, map, list, set, sortedset

常用命令

String

set key value, get key, del key (set命令集增改命令于一体)

list

lpush key value, rpush key value, lrange key start end, lpop key, rpop key

hashmap (注:下面的field是字段名)

hset key field value, hget key field, hdel key field, hgetall key

set (无需集合,元素不重复。key是一个set的名字,一个key中可以有好多元素)

sadd key value, smembers keysrem key value

sortedset(score是排序的依据,默认从小到大排序)

zadd key score value, zrange key start end [withscores], zrem key value

全局命令

keys *, type key, del key

select dbId dbId是数据库的id(默认0~15)

dbsize 当前库中key数量

fulshdb 清空当前库

fulshall 清空所有库

EXPIRE key "seconds" 为key设置过期时间(单位是秒)

PEXPIRE key "milliseconds" 为key设置过期时间(单位是毫秒)

TTL keyPTTL key返回剩余生存时间(前者返回的时间的单位是秒,后者是毫秒)如果key是永久的,返回-1;如果key不存在或者已过期,返回-2。

PERSIST key 移除key的过期时间,将其转换为永久状态。如果返回1,代表转换成功。如果返回0,代表key不存在或者之前就已经是永久状态。

SETEX key "seconds" "value" 等价于SET和EXPIRE合并的操作,区别之处在于SETEX是一条命令,而命令的执行是原子性的,所以不会出现并发问题

配置文件

以下是经过简单翻译的Redis v=5.0.5配置文件。转自这篇博客

  1. # Redis configuration file example. Redis配置文件示例
  2. #
  3. # Note that in order to read the configuration file, Redis must be
  4. # started with the file path as first argument:
  5. # 如果要使用自定义的Redis配置文件,则需要将配置文件的路径(绝对/相对)跟在"./redis-server"命令后的第一个参数,如:
  6. # ./redis-server /path/to/redis.conf
  7. # Note on units: when memory size is needed, it is possible to specify
  8. # it in the usual form of 1k 5GB 4M and so forth:
  9. # 下面是对Redis内存申请单位的注释,比如1k代表1000字节,而1kb代表1024字节,依次类推,并且Redis并不区分大小写,1k 1K都是1000字节
  10. # 1k => 1000 bytes
  11. # 1kb => 1024 bytes
  12. # 1m => 1000000 bytes
  13. # 1mb => 1024*1024 bytes
  14. # 1g => 1000000000 bytes
  15. # 1gb => 1024*1024*1024 bytes
  16. #
  17. # units are case insensitive so 1GB 1Gb 1gB are all the same.
  18. ################################## INCLUDES 包含 ###################################
  19. # Include one or more other config files here. This is useful if you
  20. # have a standard template that goes to all Redis servers but also need
  21. # to customize a few per-server settings. Include files can include
  22. # other files, so use this wisely.
  23. #
  24. # Notice option "include" won't be rewritten by command "CONFIG REWRITE"
  25. # from admin or Redis Sentinel. Since Redis always uses the last processed
  26. # line as value of a configuration directive, you'd better put includes
  27. # at the beginning of this file to avoid overwriting config change at runtime.
  28. #
  29. # If instead you are interested in using includes to override configuration
  30. # options, it is better to use include as the last line.
  31. #
  32. # 可以将公共配置抽取成模板,然后在主配置中使用"include"选项来外挂它,"include"可以在文件开始或者结束的时候使用
  33. # 如果在文件开始,那么主配置文件中指定的key会覆盖"include"挂进来的key,比如"include"挂进来配置文件port为6380,而主配置文件port=6379,那么这种情况Redis启动之后,监听的端口依然是6379
  34. # 如果在文件结束,上面举例的情况,Redis启动之后,监听的端口是6380
  35. # 其实在集群中可以使用这个特性,可以减少配置,如果使用共享文件(NFS),还可以做到一处修改处处修改的效果,但是后者是通过分发文件实现的,这里是通过共享磁盘
  36. # include /path/to/local.conf
  37. # include /path/to/other.conf
  38. ################################## MODULES 模块 #####################################
  39. # Load modules at startup. If the server is not able to load modules
  40. # it will abort. It is possible to use multiple loadmodule directives.
  41. # 挂载官方或者其他大牛写的module,有哪些module可以在Redis官网中的module中查看https://redis.io/modules,比如
  42. # redis-cell(漏洞限流) RedisBloom(布隆过滤器) RedisSearch(全文检索) rediSQL(SQL操作redis)等等module
  43. # loadmodule /path/to/my_module.so
  44. # loadmodule /path/to/other_module.so
  45. ################################## NETWORK 网络 #####################################
  46. # By default, if no "bind" configuration directive is specified, Redis listens
  47. # for connections from all the network interfaces available on the server.
  48. # It is possible to listen to just one or multiple selected interfaces using
  49. # the "bind" configuration directive, followed by one or more IP addresses.
  50. # 如果没有指定"bind"配置,则任何机器都可以连接到该Redis服务器,但也可以通过配置"bind",让一个或者多个地址可以连接该Redis服务器
  51. # Examples:
  52. #
  53. # bind 192.168.1.100 10.0.0.1
  54. # bind 127.0.0.1 ::1
  55. # 通过配置可以发现,该配置是可以支持范围的,另外如果配置是某一个IP,其实整个网段都可以访问该Redis服务器
  56. #
  57. # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
  58. # internet, binding to all the interfaces is dangerous and will expose the
  59. # instance to everybody on the internet. So by default we uncomment the
  60. # following bind directive, that will force Redis to listen only into
  61. # the IPv4 loopback interface address (this means Redis will be able to
  62. # accept connections only from clients running into the same computer it
  63. # is running).
  64. # 如果没有指定"bind",那么将Redis服务器将暴露在互联网上,这是非常危险的,因此在生产系统上应该禁止这样的设置
  65. # 默认情况下是"bind"指定到本机IPV4的回环地址上,因此只有本机上运行的程序才可以访问该Redis服务器
  66. # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
  67. # JUST COMMENT THE FOLLOWING LINE.
  68. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. bind 192.168.10.20
  70. # 虽然我配置的本地IP地址,但是我192.168.10.12主机一样访问,整个192.168.10网段都可以访问
  71. # Protected mode is a layer of security protection, in order to avoid that
  72. # Redis instances left open on the internet are accessed and exploited.
  73. # "protected mode"是一个安全保护层,可以避免Redis服务器被互联网上的机器访问和利用
  74. #
  75. # When protected mode is on and if:
  76. #
  77. # 1) The server is not binding explicitly to a set of addresses using the
  78. # "bind" directive.
  79. # 2) No password is configured.
  80. #
  81. # The server only accepts connections from clients connecting from the
  82. # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
  83. # sockets.
  84. # 当"protected mode"被设置为on(即设置为"protected-mode yes"),且没有显示用bind指定ip地址集合或者没有设置密码,那么Redis服务器只能被本机访问
  85. #
  86. # By default protected mode is enabled. You should disable it only if
  87. # you are sure you want clients from other hosts to connect to Redis
  88. # even if no authentication is configured, nor a specific set of interfaces
  89. # are explicitly listed using the "bind" directive.
  90. # 默认"protected mode"是开启的,如果确定自己的服务器需要暴露在互联网上,且不存在安全问题,可以将"protected mode"关闭掉
  91. protected-mode yes
  92. # Accept connections on the specified port, default is 6379 (IANA #815344).
  93. # If port 0 is specified Redis will not listen on a TCP socket.
  94. # Redis Server的监听端口
  95. port 6379
  96. # TCP listen() backlog.
  97. #
  98. # In high requests-per-second environments you need an high backlog in order
  99. # to avoid slow clients connections issues. Note that the Linux kernel
  100. # will silently truncate it to the value of /proc/sys/net/core/somaxconn so
  101. # make sure to raise both the value of somaxconn and tcp_max_syn_backlog
  102. # in order to get the desired effect.
  103. # 在高并发场景下,为了避免客户端连接缓慢问题,需要高的backlog,默认值是511。但是真正使用的值依赖于LINUX内核参数somaxconn,而somaxconn默认值是128,所以即便这里设置了511,最终生效的128。
  104. # 所以如果公司没有主机工程师一定要记得在安装新机器操作系统时就将一些内核参数改大一些,比如将somaxconn修改为20480
  105. tcp-backlog 511
  106. # Unix socket.
  107. #
  108. # Specify the path for the Unix socket that will be used to listen for
  109. # incoming connections. There is no default, so Redis will not listen
  110. # on a unix socket when not specified.
  111. # 利用Unix socket可以提升同一服务器上的进程间通信速度,而且是数量级的提升,但通常Redis服务器和应用服务器是分开的,所以下面的两个参数可以不管
  112. # unixsocket 指定一个文件作为通信的媒介
  113. # unixscoketperm 对unixsocket指定文件的访问权限(读-写-执行),如果真的用了这个特性,该值应该根据系统用户进行权限设置
  114. # unixsocket /tmp/redis.sock
  115. # unixsocketperm 700
  116. # Close the connection after a client is idle for N seconds (0 to disable)
  117. # 当连接变得空闲了之后多少秒关闭连接,默认设置为0,表示禁用这个选项带来的效果--连接不关闭
  118. timeout 0
  119. # TCP keepalive.
  120. #
  121. # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
  122. # of communication. This is useful for two reasons:
  123. #
  124. # 1) Detect dead peers.
  125. # 2) Take the connection alive from the point of view of network
  126. # equipment in the middle.
  127. # 如果tcp-keepalive值不为0,那么在客户端和服务器缺乏通信的情况下使用SO_KEEPALIVE,每隔"tcp-keepalive"指定的时间发送"TCP ACKS"给客户端
  128. # 这么做有两个原因:
  129. # 1.可以检测客户端是否还存活
  130. # 2.保持网络连接是活着的,这样避免客户端反复与服务器建立链接导致性能低下
  131. #
  132. # On Linux, the specified value (in seconds) is the period used to send ACKs.
  133. # Note that to close the connection the double of the time is needed.
  134. # On other kernels the period depends on the kernel configuration.
  135. #
  136. # A reasonable value for this option is 300 seconds, which is the new
  137. # Redis default starting with Redis 3.2.1.
  138. # 默认值是300S,我个人觉得300S还是太长了,即使大的集群60S发送一次也不会造成大的网络流量
  139. tcp-keepalive 300
  140. ################################# GENERAL #####################################
  141. # By default Redis does not run as a daemon. Use 'yes' if you need it.
  142. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
  143. # 将"daemonize"设置为yes,Redis会以守护进程的方式运行,并且会在/var/run目录下生成一个redis.pid文件
  144. daemonize no
  145. # If you run Redis from upstart or systemd, Redis can interact with your
  146. # supervision tree. Options:
  147. # supervised no - no supervision interaction
  148. # supervised upstart - signal upstart by putting Redis into SIGSTOP mode
  149. # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
  150. # supervised auto - detect upstart or systemd method based on
  151. # UPSTART_JOB or NOTIFY_SOCKET environment variables
  152. # 使用Linux系统的upstart或者systemd两种方式来管理redis的启动,需要结合linux的版本来决定,centos7设置为systemd,而ubuntu设置为upstart
  153. # Note: these supervision methods only signal "process is ready."
  154. # They do not enable continuous liveness pings back to your supervisor.
  155. supervised no
  156. # If a pid file is specified, Redis writes it where specified at startup
  157. # and removes it at exit.
  158. # 如果设置了pid文件,那么Redis启动时会写该pid文件到指定的目录下,退出时删除该pid文件
  159. #
  160. # When the server runs non daemonized, no pid file is created if none is
  161. # specified in the configuration. When the server is daemonized, the pid file
  162. # is used even if not specified, defaulting to "/var/run/redis.pid".
  163. # 如果redis是以守护进程方式运行的,如果没有指定"pidfile"的值,默认生成一个/var/run/redis.pid文件,否则使用指定的"pidfile"
  164. # 如果redis是以非守护进程方式运行,如果没有指定"pidfile"的值,则不会产生pid file
  165. #
  166. # Creating a pid file is best effort: if Redis is not able to create it
  167. # nothing bad happens, the server will start and run normally.
  168. pidfile /var/run/redis_6379.pid
  169. # Specify the server verbosity level.
  170. # This can be one of:
  171. # debug (a lot of information, useful for development/testing)
  172. # verbose (many rarely useful info, but not a mess like the debug level)
  173. # notice (moderately verbose, what you want in production probably)
  174. # warning (only very important / critical messages are logged)
  175. # 指定Redis服务的详细日志级别,有debug\verbose\notice\warning四种级别,debug当然是不推荐的,日志太多了,除非有特殊情况,开发环境可以试试
  176. loglevel notice
  177. # Specify the log file name. Also the empty string can be used to force
  178. # Redis to log on the standard output. Note that if you use standard
  179. # output for logging but daemonize, logs will be sent to /dev/null
  180. # 如果以非守护进程的方式运行,且没有指定"logfile",那么日志会发送到/dev/null(空文件),我们无法通过它看到任何日志,但如果指定了"logfile",则输出到配置的文件当中
  181. # 如果以守护进程的方式运行,且没有指定"logfile",那么日志会输出到标准输出(控制台),但如果指定了"logfile",则输出到配置的文件当中
  182. logfile ""
  183. # 以下3个(syslog-enabled/syslog-ident/syslog-facility)参数感觉不需要关注,它们的目的就是将日志输出使用系统自带的logger,而且可以修改syslog的参数来实现自己特殊的需求
  184. # 我自己没有去测试过,感觉应该很少会用到,也许大企业专门负责Redis集群的会使用它来定制Redis的日志输出格式,然后使用程序来统计最后通过UI来展示
  185. # To enable logging to the system logger, just set 'syslog-enabled' to yes,
  186. # and optionally update the other syslog parameters to suit your needs.
  187. # 如果要使用system logger,则将"syslog-enabled"设置为yes
  188. # syslog-enabled no
  189. # Specify the syslog identity.
  190. # 指定syslog的id,应该是随便指定吧,起到唯一标识的作用???
  191. # syslog-ident redis
  192. # Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
  193. # 指定localxxx,需要配合/etc/rsyslog.conf文件使用,意思就是将日志文件输出导出到rsyslog.conf指定的文件中。如果开启了syslog-enable,也许自己指定的logfile就失效了,需要通过rsyslog.conf指定localxxx将日志导出指定的文件中
  194. # syslog-facility local0
  195. # Set the number of databases. The default database is DB 0, you can select
  196. # a different one on a per-connection basis using SELECT <dbid> where
  197. # dbid is a number between 0 and 'databases'-1
  198. # 设置redis有多少个db,默认是16个,可以超过16,但是最大值是多少我也不知道。
  199. # 客户端连接上服务器之后,可以通过"select databases-1"来选择使用的db,比如要使用第15个db,则使用"select 14"
  200. databases 16
  201. # By default Redis shows an ASCII art logo only when started to log to the
  202. # standard output and if the standard output is a TTY. Basically this means
  203. # that normally a logo is displayed only in interactive sessions.
  204. #
  205. # However it is possible to force the pre-4.0 behavior and always show a
  206. # ASCII art logo in startup logs by setting the following option to yes.
  207. # 是否启动的时候输出(显示)Redis的ASCII LOGO,这个没事就不要去动他了吧,看看也不错至少晓得它正在启动了
  208. always-show-logo yes
  209. ################################ SNAPSHOTTING 持久化 ################################
  210. # 下持久化有三种,RDB\AOF\RDB+AOF混合,简答提一下对应的实现原理
  211. # RDB:将数据库以二进制存放在磁盘文件中,持久化的时间间隙比较大,丢失的数据比较多,单独只使用这种方式不推荐
  212. # AOF:将操作数据库的指令(包括协议信息)以文本方式存放在磁盘文件中,根据配置最多会丢失1S的数据,这种方式还可以
  213. # 混合:推荐这种方式,但是4.0开始才有此功能,混合持久化结合了RDB快速恢复数据和AOF丢失数据少的优点,而且减少了磁盘开销。。。关于它们更详细的介绍请查看
  214. # Redis设计与实现-RDB持久化 https://my.oschina.net/u/3049601/blog/3153571
  215. # Redis设计与实现-AOF持久化 https://my.oschina.net/u/3049601/blog/3153678
  216. # Redis设计与实现-混合持久化 https://my.oschina.net/u/3049601/blog/3158904
  217. #
  218. # Save the DB on disk:
  219. # 保存DB到磁盘中
  220. # save <seconds> <changes>
  221. #
  222. # Will save the DB if both the given number of seconds and the given
  223. # number of write operations against the DB occurred.
  224. #
  225. # In the example below the behaviour will be to save:
  226. # after 900 sec (15 min) if at least 1 key changed
  227. # 15分钟内至少有一个KEY改变了
  228. # after 300 sec (5 min) if at least 10 keys changed
  229. # 5分钟内至少有10个KEY改变了
  230. # after 60 sec if at least 10000 keys changed
  231. # 1分钟内至少有10000个KEY改变了
  232. #
  233. # Note: you can disable saving completely by commenting out all "save" lines.
  234. #
  235. # It is also possible to remove all the previously configured save
  236. # points by adding a save directive with a single empty string argument
  237. # like in the following example:
  238. #
  239. # save ""
  240. # 如果想禁止RDB持久化,可以将下面的三个save配置项使用"#"注释掉。还可以使用save ""来代替使用"#"来注释掉三个save配置项
  241. save 900 1
  242. save 300 10
  243. save 60 10000
  244. # By default Redis will stop accepting writes if RDB snapshots are enabled
  245. # (at least one save point) and the latest background save failed.
  246. # This will make the user aware (in a hard way) that data is not persisting
  247. # on disk properly, otherwise chances are that no one will notice and some
  248. # disaster will happen.
  249. #
  250. # If the background saving process will start working again Redis will
  251. # automatically allow writes again.
  252. #
  253. # However if you have setup your proper monitoring of the Redis server
  254. # and persistence, you may want to disable this feature so that Redis will
  255. # continue to work as usual even if there are problems with disk,
  256. # permissions, and so forth.
  257. # 如果RDB开启了且最近的BGSAVE失败了,那么Redis默认是不会再接收新增或者修改请求了,但如果BGSAVE又恢复工作,那么新增和修改操作可以继续(表示可以自动恢复)
  258. # 如果公司有自己的监控系统可以很好的检测Redis服务和持久化情况,那么可以将此功能关闭,这样可以提高系统的可用性
  259. # 如果使用集群,且从节点够的情况下有自己的监控,真的可以将这个功能关闭掉
  260. stop-writes-on-bgsave-error yes
  261. # Compress string objects using LZF when dump .rdb databases?
  262. # For default that's set to 'yes' as it's almost always a win.
  263. # If you want to save some CPU in the saving child set it to 'no' but
  264. # the dataset will likely be bigger if you have compressible values or keys.
  265. # 默认会采用LZF压缩dump出来的数据库并写入到xxx.rdb文件中。压缩会增加CPU的开销,如果想节约CPU的开销,可以将"rdbcompression"设置为"no",但是会占用更多的磁盘
  266. rdbcompression yes
  267. # Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
  268. # This makes the format more resistant to corruption but there is a performance
  269. # hit to pay (around 10%) when saving and loading RDB files, so you can disable it
  270. # for maximum performances.
  271. # 从Redis5版本开始,默认在RDB文件末尾有一个CRC64(一个随机算法,生成信息指纹用的)校验和,它可以让文件格式可以更强的抵抗风险,但是它会带来10左右的性能损失,我们可以禁止它以获得最大的性能输出
  272. #
  273. # RDB files created with checksum disabled have a checksum of zero that will
  274. # tell the loading code to skip the check.
  275. # 如果禁用掉"checksum",那么生成的RDB文件结尾的校验和为"0",那么加载程序则会跳过校验
  276. # 针对大企业(不差钱)个人觉得就保留默认设置应该比较好
  277. rdbchecksum yes
  278. # The filename where to dump the DB
  279. # 指定RDB文件的名字,建议使用ip+port来指定,运维可以更好的分辨,甚至可以通过程序扫描展示到UI上
  280. dbfilename dump.rdb
  281. # The working directory.
  282. #
  283. # The DB will be written inside this directory, with the filename specified
  284. # above using the 'dbfilename' configuration directive.
  285. #
  286. # The Append Only File will also be created inside this directory.
  287. #
  288. # Note that you must specify a directory here, not a file name.
  289. # 指定RDB和AOF文件的存储目录,注意:这里只能指定到目录,不要带文件名称
  290. dir ./
  291. ################################# REPLICATION 主从 #################################
  292. # Master-Replica replication. Use replicaof to make a Redis instance a copy of
  293. # another Redis server. A few things to understand ASAP(as soon as possible) about Redis replication.
  294. # Redis的主从复制,使用"replicaof"从一个Redis Server复制到另外一个去,下面有几个要点需要理解
  295. # +------------------+ +---------------+
  296. # | Master | ---> | Replica |
  297. # | (receive writes) | | (exact copy) |
  298. # +------------------+ +---------------+
  299. #
  300. # 1) Redis replication is asynchronous, but you can configure a master to
  301. # stop accepting writes if it appears to be not connected with at least
  302. # a given number of replicas.
  303. # 主从复制虽然是异步进行的,但是可以通过配置(min-replicas-to-write)让从节点小于特定值时,主节点不接受"write"请求
  304. #
  305. # 2) Redis replicas are able to perform a partial resynchronization with the
  306. # master if the replication link is lost for a relatively small amount of
  307. # time. You may want to configure the replication backlog size (see the next
  308. # sections of this file) with a sensible value depending on your needs.
  309. # Redis的老版本没有复制重同步,但是从2.8开始支持部分同步(使用复制挤压缓冲区实现),解决了断线后老版本"完全同步"低效、阻塞、循环同步的问题
  310. # 如果从节点与主节点断开联系一小段时间,则会发起部分同步,但是复制挤压缓冲区也有大小,可以设置缓冲区的大小来减少完全同步出现
  311. #
  312. # 3) Replication is automatic and does not need user intervention. After a
  313. # network partition replicas automatically try to reconnect to masters
  314. # and resynchronize with them.
  315. # 复制时自动进行的,且不需要人工介入。在出现网络分区后,从节点会自动尝试去重连主节点,连接成功之后发起部分同步,如果复制积压缓冲区中的数据丢失了,则会发起完全同步
  316. #
  317. # 这段英文解释如果是入门学习Redis,可能不会太看得懂,推荐先看看Redis设计与实现这本书,会对这段描述有比较深刻的认识
  318. # replicaof <masterip> <masterport>
  319. # replicaof 主节点IP 主节点端口,注意一定要保证主从节点网络是通的,检查本机防火墙和第三方防火墙
  320. # If the master is password protected (using the "requirepass" configuration
  321. # directive below) it is possible to tell the replica to authenticate before
  322. # starting the replication synchronization process, otherwise the master will
  323. # refuse the replica request.
  324. # 如果主节点设置了密码,那么在从节点的redis.conf中要设置masterauth配置项,将密码写在这里,如果没有
  325. # 设置,那么主节点会拒绝从节点的复制请求
  326. #
  327. # masterauth <master-password>
  328. # masterauth 主节点密码
  329. # When a replica loses its connection with the master, or when the replication
  330. # is still in progress, the replica can act in two different ways:
  331. # 如果从节点与主节点失去连接或者正在从主节点同步数据,那么从节点根据配置可以工作在两种模式下:
  332. #
  333. # 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will
  334. # still reply to client requests, possibly with out of date data, or the
  335. # data set may just be empty if this is the first synchronization.
  336. # 如果"replica-serve-stale-data"设置为"yes",这也是默认设置,那么从节点将会回复客户端的请求,但是得到的数据可能出现下面两种情况
  337. # 1.如果是与主节点失去连接,那么得到的数据可能是过时的
  338. # 2.如果是第一次从主节点同步数据,那么得到的数据集会是空的
  339. #
  340. # 2) if replica-serve-stale-data is set to 'no' the replica will reply with
  341. # an error "SYNC with master in progress" to all the kind of commands
  342. # but to INFO, replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG,
  343. # SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB,
  344. # COMMAND, POST, HOST: and LATENCY.
  345. # 如果"replica-serve-stale-data"设置为"no",从节点将回复客户端"SYNC with master in progress"错误
  346. # 但是INFO, replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG等命令是可以成功执行并得到相应结果
  347. #
  348. replica-serve-stale-data yes
  349. # You can configure a replica instance to accept writes or not. Writing against
  350. # a replica instance may be useful to store some ephemeral data (because data
  351. # written on a replica will be easily deleted after resync with the master) but
  352. # may also cause problems if clients are writing to it because of a
  353. # misconfiguration.
  354. # 我们可以配置从节点是否可以接受write请求,非常不建议将从节点设置为可接受write请求,因为同步可能会导致数据丢失
  355. # 因此从Redis2.6就将"replica-read-only"默认设置为"yes"了
  356. #
  357. # Since Redis 2.6 by default replicas are read-only.
  358. #
  359. # Note: read only replicas are not designed to be exposed to untrusted clients
  360. # on the internet. It's just a protection layer against misuse of the instance.
  361. # Still a read only replica exports by default all the administrative commands
  362. # such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
  363. # security of read only replicas using 'rename-command' to shadow all the
  364. # administrative / dangerous commands.
  365. # 从节点(只读)在架构时建议不要设计为暴露给互联网上的不可信任客户端,它可以起到滥用实例的保护作用
  366. # 从节点依然支持所有的管理命令,比如CONFI,DEBUG等等,为了提高从节点的安全性,可以使用"rename-command"来屏蔽所有的"管理命令"
  367. replica-read-only yes
  368. # Replication SYNC strategy: disk or socket.
  369. # 同步策略:通过磁盘(Disk-backed)或者通过SOCKET(Diskless)
  370. #
  371. # -------------------------------------------------------
  372. # WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY
  373. # -------------------------------------------------------
  374. # 很遗憾通过SOCKET同步还处在试验阶段
  375. #
  376. # New replicas and reconnecting replicas that are not able to continue the replication
  377. # process just receiving differences, need to do what is called a "full
  378. # synchronization". An RDB file is transmitted from the master to the replicas.
  379. # The transmission can happen in two different ways:
  380. # 新的从节点和重连接的从节点(从节点的最新偏移量不在主节点的复制积压缓冲区中),则会执行"full synchronization"操作,一个RDB文件会采用以下两种方式中的一种传输给从节点:
  381. #
  382. # 1) Disk-backed: The Redis master creates a new process that writes the RDB
  383. # file on disk. Later the file is transferred by the parent
  384. # process to the replicas incrementally.
  385. # 主节点fork一个子进程出来将RDB文件生成到磁盘上,然后父进程将RDB文件逐渐发给从节点
  386. # 2) Diskless: The Redis master creates a new process that directly writes the
  387. # RDB file to replica sockets, without touching the disk at all.
  388. # 主节点fork一个子进程,然后创建一个和从节点的SOCKET连接,直接将数据发送给从节点,而不借助磁盘
  389. #
  390. # With disk-backed replication, while the RDB file is generated, more replicas
  391. # can be queued and served with the RDB file as soon as the current child producing
  392. # the RDB file finishes its work. With diskless replication instead once
  393. # the transfer starts, new replicas arriving will be queued and a new transfer
  394. # will start when the current one terminates.
  395. # 如果使用磁盘(disk-backed),当子进程生成RDB文件后,多个从节点立即就可以使用RDB文件进行复制操作
  396. # 如果使用SOCKET(diskless),一旦复制开始,当新的节点复制请求必须等已开始复制完成之后才能进行。
  397. #
  398. # When diskless replication is used, the master waits a configurable amount of
  399. # time (in seconds) before starting the transfer in the hope that multiple replicas
  400. # will arrive and the transfer can be parallelized.
  401. # 当使用SOCKET(diskless)时,主节点可以等一段时间(可配置),这段时间内过来的复制请求可以并行开始
  402. #
  403. # With slow disks and fast (large bandwidth) networks, diskless replication
  404. # works better.
  405. # 如果磁盘效率低,而网络速度快且带宽也大的情况下,Diskless方式完复制效果更好
  406. repl-diskless-sync no
  407. # When diskless replication is enabled, it is possible to configure the delay
  408. # the server waits in order to spawn the child that transfers the RDB via socket
  409. # to the replicas.
  410. # 如果"repl-diskless-sync"设置为yes,就需要配置"repl-diskless-sync-delay"让主节点等待更多的复制请求过来,并让他们并发复制
  411. #
  412. # This is important since once the transfer starts, it is not possible to serve
  413. # new replicas arriving, that will be queued for the next RDB transfer, so the server
  414. # waits a delay in order to let more replicas arrive.
  415. # 因为一旦有复制开始进行,新来的复制请求就会排队,因此设置了延迟时间就可以让更多的复制同时执行
  416. #
  417. # The delay is specified in seconds, and by default is 5 seconds. To disable
  418. # it entirely just set it to 0 seconds and the transfer will start ASAP.
  419. # 延迟时间单位是"秒",默认值是5秒。可以将"repl-diskless-sync-delay"设置为0,这样复制就会立即执行
  420. repl-diskless-sync-delay 5
  421. # Replicas send PINGs to server in a predefined interval. It's possible to change
  422. # this interval with the repl_ping_replica_period option. The default value is 10
  423. # seconds.
  424. # 从节点使用"repl-ping-replica-period"指定的时长(单位:秒)定期发送"pings"给主节点,通过这个操作可以检测从节点是否和主节点失联。该值默认是10秒
  425. #
  426. # repl-ping-replica-period 10
  427. # The following option sets the replication timeout for:
  428. # "repl-timeout"会影响复制过程中一下三种情况的超时时间
  429. #
  430. # 1) Bulk transfer I/O during SYNC, from the point of view of replica.
  431. # 2) Master timeout from the point of view of replicas (data, pings).
  432. # 3) Replica timeout from the point of view of masters (REPLCONF ACK pings).
  433. #
  434. # It is important to make sure that this value is greater than the value
  435. # specified for repl-ping-replica-period otherwise a timeout will be detected
  436. # every time there is low traffic between the master and the replica.
  437. # 一定要确保"repl-timeout"的值大于"repl-ping-replica-period"的值,否则当主从节点之间通信量很低时,每次判断超时都是成功的,默认值是60秒
  438. #
  439. # repl-timeout 60
  440. # Disable TCP_NODELAY on the replica socket after SYNC?
  441. #
  442. # If you select "yes" Redis will use a smaller number of TCP packets and
  443. # less bandwidth to send data to replicas. But this can add a delay for
  444. # the data to appear on the replica side, up to 40 milliseconds with
  445. # Linux kernels using a default configuration.
  446. # 如果将"repl-disable-tcp-nodelay"设置为"yes",那么主节点会使用更小的TCP packet和更少的带宽发送数据到从节点
  447. # 但是这会让从节点的数据延迟40毫秒(LINUX默认配置,也许可以通过tcp_delack_min修改),关于tcp-nodelay可以看博客:https://blog.csdn.net/bytxl/article/details/17677495
  448. #
  449. # If you select "no" the delay for data to appear on the replica side will
  450. # be reduced but more bandwidth will be used for replication.
  451. # 如果将"repl-disable-tcp-nodelay"设置为no,那么从节点接收数据的延迟会减少,但是要求更多的带宽来完成复制工作
  452. #
  453. # By default we optimize for low latency, but in very high traffic conditions
  454. # or when the master and replicas are many hops away, turning this to "yes" may
  455. # be a good idea.
  456. # 默认我们使用低延迟的选项,也就是"repl-disable-tcp-nodelay"设置为no,但是在非常高的通信量情况下或者从主节点到从节点会经过很多次转发,将"repl-disable-tcp-nodelay"设置为yes是可能是更好的选择
  457. repl-disable-tcp-nodelay no
  458. # Set the replication backlog size. The backlog is a buffer that accumulates
  459. # replica data when replicas are disconnected for some time, so that when a replica
  460. # wants to reconnect again, often a full resync is not needed, but a partial
  461. # resync is enough, just passing the portion of data the replica missed while
  462. # disconnected.
  463. # backlog设置复制积压缓冲区的大小,用以存放从节点与主节点断开连接后这段时间的write等命令,当从节点重新连接上来时,通常不需要做"完全同步",只需要做部分同步(要求从节点偏移量在复制挤压缓冲区可以找到,表示数据可以使用偏移量后的命令进行恢复),将偏移量后面的命令发送给从节点执行
  464. #
  465. # The bigger the replication backlog, the longer the time the replica can be
  466. # disconnected and later be able to perform a partial resynchronization.
  467. # 更大的backlog意味着从节点可以断开连接更长时间,然后才可以执行部分同步
  468. #
  469. # The backlog is only allocated once there is at least a replica connected.
  470. # 一旦有从节点连接到主节点,复制积压缓冲区就会创建,此时并没有数据。
  471. #
  472. # 这个值到底设置多少,有一个计算公式:2*平均断线时间*每秒写入数据大小,因此和业务量强相关
  473. # repl-backlog-size 1mb
  474. # After a master has no longer connected replicas for some time, the backlog
  475. # will be freed. The following option configures the amount of seconds that
  476. # need to elapse, starting from the time the last replica disconnected, for
  477. # the backlog buffer to be freed.
  478. # 当主节点在最后一个从节点断线之后的一段时间后(repl-backlog-ttl设置),会将复制积压缓冲区释放
  479. #
  480. # Note that replicas never free the backlog for timeout, since they may be
  481. # promoted to masters later, and should be able to correctly "partially
  482. # resynchronize" with the replicas: hence they should always accumulate backlog.
  483. # 从节点永远都不会释放,因为它必须保存自己接收的最新偏移量,当出现断线重连时将这个偏移量发给主节点,主节点决定使用完全同步还是部分同步
  484. #
  485. # A value of 0 means to never release the backlog.
  486. # 如果设置为0,表示永不释放复制积压缓冲区
  487. #
  488. # repl-backlog-ttl 3600
  489. # The replica priority is an integer number published by Redis in the INFO output.
  490. # It is used by Redis Sentinel in order to select a replica to promote into a
  491. # master if the master is no longer working correctly.
  492. # "replica-priority"是一个整数值,在哨兵的集群模式下,当"主事哨兵"被选举(选举采用过半原则)出来之后,由它决定挂掉主节点下的某一个从节点作为新的主节点,当其他条件都相同的情况下,"replica-priority"值越小的从节点会被选中作为新的主节点
  493. #
  494. # A replica with a low priority number is considered better for promotion, so
  495. # for instance if there are three replicas with priority 10, 100, 25 Sentinel will
  496. # pick the one with priority 10, that is the lowest.
  497. # 总结起来就是值越小优先级越高,其对应的从节点会优先被选择作为新的主节点
  498. #
  499. # However a special priority of 0 marks the replica as not able to perform the
  500. # role of master, so a replica with priority of 0 will never be selected by
  501. # Redis Sentinel for promotion.
  502. # 如果将"replica-priority"设置为0,则该从节点永远都不会被选择为新的主节点,根本就不参与选举。可以减少选举过程中过多的网络通信,加快选举过程
  503. # 我还只是一个理论派,没实战经验,个人感觉如果机器硬件够好,且机器所在的网络质量够好,可以将其优先级设置得高一些
  504. #
  505. # By default the priority is 100.
  506. # 默认值是100
  507. replica-priority 100
  508. # It is possible for a master to stop accepting writes if there are less than
  509. # N replicas connected, having a lag less or equal than M seconds.
  510. # 如果连接的从节点小于N且它们的滞后时间小于或者等于M秒,那么主节点可能会停止接收写操作
  511. # 网上说这两个条件中一个不满足就可能导致主节点不能接收写操作,是很准确,必须是两个条件同时满足才会触发
  512. #
  513. # The N replicas need to be in "online" state.
  514. # 要求这N个从节点是"online"状态,还有在哨兵和Cluster模式下节点还有主观下线和客观下线状态
  515. #
  516. # The lag in seconds, that must be <= the specified value, is calculated from
  517. # the last ping received from the replica, that is usually sent every second.
  518. # 滞后时间的秒数必须小于指定值,滞后时间=当前时间-最后一次接收到的从replica发过来的ping时间
  519. #
  520. # This option does not GUARANTEE that N replicas will accept the write, but
  521. # will limit the window of exposure for lost writes in case not enough replicas
  522. # are available, to the specified number of seconds.
  523. # 这个选项并不能保证N个从节点接收写操作,但是可以将丢失的数据限制在指定秒数内
  524. #
  525. # For example to require at least 3 replicas with a lag <= 10 seconds use:
  526. #
  527. # min-replicas-to-write 3
  528. # min-replicas-max-lag 10
  529. #
  530. # Setting one or the other to 0 disables the feature.
  531. # 如果将这两个选项中的任何一个设置为0,表示禁用这个特征
  532. #
  533. # By default min-replicas-to-write is set to 0 (feature disabled) and
  534. # min-replicas-max-lag is set to 10.
  535. # 默认"min-replicas-to-write"被设置为0,即禁止了这个特征,"min-replicas-max-lag"默认值为10秒
  536. # A Redis master is able to list the address and port of the attached
  537. # replicas in different ways. For example the "INFO replication" section
  538. # offers this information, which is used, among other tools, by
  539. # Redis Sentinel in order to discover replica instances.
  540. # Another place where this info is available is in the output of the
  541. # "ROLE" command of a master.
  542. #
  543. # The listed IP and address normally reported by a replica is obtained
  544. # in the following way:
  545. #
  546. # IP: The address is auto detected by checking the peer address
  547. # of the socket used by the replica to connect with the master.
  548. #
  549. # Port: The port is communicated by the replica during the replication
  550. # handshake, and is normally the port that the replica is using to
  551. # listen for connections.
  552. #
  553. # However when port forwarding or Network Address Translation (NAT) is
  554. # used, the replica may be actually reachable via different IP and port
  555. # pairs. The following two options can be used by a replica in order to
  556. # report to its master a specific set of IP and port, so that both INFO
  557. # and ROLE will report those values.
  558. #
  559. # There is no need to use both the options if you need to override just
  560. # the port or the IP address.
  561. # 总结起来说:在主节点中使用info replication可以列出所有从节点的IP+PORT,本来主节点可以使用SOCKET拿到从节点的IP+PORT,但如果使用端口转发(docker,k8s)和NAT或者因为使用了代理,从节点不能直接通过IP+PORT到达,下面这两个从节点选项才有用,它可以将设置的IP和PORT报告给主节点,INFO和ROLE命令会显示设置的值
  562. #
  563. # replica-announce-ip 5.5.5.5
  564. # replica-announce-port 1234
  565. ################################## SECURITY ###################################
  566. # Require clients to issue AUTH <PASSWORD> before processing any other
  567. # commands. This might be useful in environments in which you do not trust
  568. # others with access to the host running redis-server.
  569. # 为了让不信任的客户端访问Redis Server,可以要求客户端在执行任何命令之前先校验密码
  570. #
  571. # This should stay commented out for backward compatibility and because most
  572. # people do not need auth (e.g. they run their own servers).
  573. # 如果客户端和Redis server运行在同一个机器,我们也可以将"requirepass"注释掉,客户端就不需要校验密码。
  574. # 可以推广到:如果在一个局域网里面,如果安全做得足够好,则都可以不设置"requirepass"
  575. #
  576. # Warning: since Redis is pretty fast an outside user can try up to
  577. # 150k passwords per second against a good box. This means that you should
  578. # use a very strong password otherwise it will be very easy to break.
  579. # 因为Redis可以每秒可以验证150K个密码,因此如果要设置密码,一定要设置一个非常强壮的密码,否则很容易被破解
  580. #
  581. # requirepass foobared
  582. # Command renaming.
  583. # 重命名command,可以保护我们的管理员命令和一些会导致Redis卡顿的命令
  584. #
  585. # It is possible to change the name of dangerous commands in a shared
  586. # environment. For instance the CONFIG command may be renamed into something
  587. # hard to guess so that it will still be available for internal-use tools
  588. # but not available for general clients.
  589. # 在共享环境中,可以将一些危险的命令进行重命名,这样可以让普通的客户端不可以使用那些被重命名的命令,只有内部工具可以使用
  590. #
  591. # Example:
  592. #
  593. # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
  594. #
  595. # It is also possible to completely kill a command by renaming it into
  596. # an empty string:
  597. # 可以将危险的命令重命名为一个空串,彻底的禁止该命令
  598. #
  599. # rename-command CONFIG ""
  600. #
  601. # Please note that changing the name of commands that are logged into the
  602. # AOF file or transmitted to replicas may cause problems.
  603. # 因为主节点的会将write命令使用缓冲区记录下来并传播给从节点执行,因此如果重命名的命令在从节点没有同步修改的话,这可能带来一些意想不到的问题,因此一定要小心这一点。
  604. # 比如将set命令重命名为myset,那么在主节点执行myset foo Messi之后,从节点并不会有foo这个key,因为从节点并不认识myset这个命令
  605. ################################### CLIENTS ####################################
  606. # Set the max number of connected clients at the same time. By default
  607. # this limit is set to 10000 clients, however if the Redis server is not
  608. # able to configure the process file limit to allow for the specified limit
  609. # the max number of allowed clients is set to the current file limit
  610. # minus 32 (as Redis reserves a few file descriptors for internal uses).
  611. #
  612. # Once the limit is reached Redis will close all the new connections sending
  613. # an error 'max number of clients reached'.
  614. # 设置同时可以连接到服务器的客户端数量,默认值是10000,但如果主机的最大文件打开数并没有比"maxclients"大,那么"maxclients"=最大文件打开数-32,这个32是提供给Redis内部使用的,比如集群之间的通信等也需要连接数
  615. #
  616. # maxclients 10000
  617. ############################## MEMORY MANAGEMENT ################################
  618. # Set a memory usage limit to the specified amount of bytes.
  619. # When the memory limit is reached Redis will try to remove keys
  620. # according to the eviction policy selected (see maxmemory-policy).
  621. # 设置Redis的工作最大内存为某一个特定的限制值。当内存使用达到限制值,根据设置的淘汰策略删除keys
  622. #
  623. # If Redis can't remove keys according to the policy, or if the policy is
  624. # set to 'noeviction', Redis will start to reply with errors to commands
  625. # that would use more memory, like SET, LPUSH, and so on, and will continue
  626. # to reply to read-only commands like GET.
  627. # 当Redis无法根据设置的淘汰策略删除keys时或者淘汰策略被设置为"noeviction",像set lpush等命令会收到报错,此时管理员就应该特别注意了,及时的增加内存,但是此时读命令还是可以继续正常使用的
  628. #
  629. # This option is usually useful when using Redis as an LRU or LFU cache, or to
  630. # set a hard memory limit for an instance (using the 'noeviction' policy).
  631. # 如果将Redis作为一个LRU或者LFU的缓存,再或者将Redis作为hard memory limit for an instance使用时,这个选项就非常有用
  632. #
  633. # WARNING: If you have replicas attached to an instance with maxmemory on,
  634. # the size of the output buffers needed to feed the replicas are subtracted
  635. # from the used memory count, so that network problems / resyncs will
  636. # not trigger a loop where keys are evicted, and in turn the output
  637. # buffer of replicas is full with DELs of keys evicted triggering the deletion
  638. # of more keys, and so forth until the database is completely emptied.
  639. # 如果一个设置了"maxmemory"的主节点连接了一个从节点,那么用于主从复制传递命令的输出缓冲区占用的内存也在maxmemory当中,如果当内存被占满时而出现大量的删除key的操作写到缓冲区,而缓冲区又不够,又会触发删除更多的key,这样就会造成一个死循环,直到整个数据库变成空的。
  640. # 因此
  641. #
  642. # In short... if you have replicas attached it is suggested that you set a lower
  643. # limit for maxmemory so that there is some free RAM on the system for replica
  644. # output buffers (but this is not needed if the policy is 'noeviction').
  645. # 因为used memory是可以大于maxmemory的,只不过出现这种情时会导致内存回收而触发删除KEY的操作。因此,如果在主从模式下,主节点的maxmemory在设置得足够大的情况下,还要给输出缓冲区留出一点空间来,避免出现死循环而导致数据库被清空。不要物理内存有多少就设置多少,况且还有操作系统和其他程序在运行,一般设置为3/4。
  646. #
  647. # maxmemory <bytes>
  648. # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
  649. # is reached. You can select among five behaviors:
  650. #
  651. # volatile-lru -> Evict using approximated LRU among the keys with an expire set.
  652. # allkeys-lru -> Evict any key using approximated LRU.
  653. # volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
  654. # allkeys-lfu -> Evict any key using approximated LFU.
  655. # volatile-random -> Remove a random key among the ones with an expire set.
  656. # allkeys-random -> Remove a random key, any key.
  657. # volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
  658. # noeviction -> Don't evict anything, just return an error on write operations.
  659. # 关于这几个策略的讲解百度可以找到非常好的描述,在这里就不详细描述了,篇幅也不够
  660. # 推荐一个:https://cloud.tencent.com/developer/article/1530553 讲了原理和使用说明
  661. #
  662. # LRU means Least Recently Used 最近没有被使用的
  663. # LFU means Least Frequently Used 最近使用频率最小的
  664. #
  665. # Both LRU, LFU and volatile-ttl are implemented using approximated randomized algorithms
  666. # LRU LFU 和volatile-ttl使用了较接近的随机算法
  667. #
  668. # Note: with any of the above policies, Redis will return an error on write
  669. # operations, when there are no suitable keys for eviction.
  670. # 选择上面的任何一种策略,如果没有适合的KEY被淘汰,那么下面的这些写操作就会报错
  671. #
  672. # At the date of writing these commands are: set setnx setex append
  673. # incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
  674. # sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
  675. # zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
  676. # getset mset msetnx exec sort
  677. #
  678. # The default is:
  679. # 默认设置是noeviction
  680. #
  681. # maxmemory-policy noeviction
  682. # LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated
  683. # algorithms (in order to save memory), so you can tune it for speed or
  684. # accuracy. For default Redis will check five keys and pick the one that was
  685. # used less recently, you can change the sample size using the following
  686. # configuration directive.
  687. # LRU LFU TTL三种方式并不是精准的算法,这是为了提高速度和节省内存,同时达到了近似的效果。。。很妙
  688. # 我们可以基于速度或者精准度的要求去调整采样的数据大小,"maxmemory-samples"值越大精准度越高,速度越慢,消耗的内存也越多,反之速度快,但是精准度低,内存开销少
  689. #
  690. # The default of 5 produces good enough results. 10 Approximates very closely
  691. # true LRU but costs more CPU. 3 is faster but not very accurate.
  692. # 默认值是5,如果设置为10就非常接近真正的LRU算法了,但是CPU开销也越多了。如果设置为3,速度快了,但是没那么准确
  693. #
  694. # maxmemory-samples 5
  695. # Starting from Redis 5, by default a replica will ignore its maxmemory setting
  696. # (unless it is promoted to master after a failover or manually). It means
  697. # that the eviction of keys will be just handled by the master, sending the
  698. # DEL commands to the replica as keys evict in the master side.
  699. # 从Redis 5开始,从节点默认是忽略掉maxmemory设置的,除非从节点在故障转移时变成了主节点
  700. # 正常情况下,从节点的Key淘汰是通过从主节点发送del命令过来实现的
  701. #
  702. # This behavior ensures that masters and replicas stay consistent, and is usually
  703. # what you want, however if your replica is writable, or you want the replica to have
  704. # a different memory setting, and you are sure all the writes performed to the
  705. # replica are idempotent, then you may change this default (but be sure to understand
  706. # what you are doing).
  707. # "replica-ignore-maxmemory"可以保证主从的数据一致性,除非你真的知道自己把"replica-ignore-maxmemory"
  708. # 设置为no带来的副作用,那建议你不要做着骚的操作,坑人哦
  709. #
  710. # Note that since the replica by default does not evict, it may end using more
  711. # memory than the one set via maxmemory (there are certain buffers that may
  712. # be larger on the replica, or data structures may sometimes take more memory and so
  713. # forth). So make sure you monitor your replicas and make sure they have enough
  714. # memory to never hit a real out-of-memory condition before the master hits
  715. # the configured maxmemory setting.
  716. # 由于从节点默认情况下是不主动删除KEY的,它可能比主节点消耗更多的内存(可能buffer更大,可能数据结构消耗的内存更多等等),所以要使用你的monitor实时监控你的从节点,并保证主节点达到maxmemory时间先于从节点的内存超过真正的物理内存
  717. #
  718. # replica-ignore-maxmemory yes
  719. ############################# LAZY FREEING 惰性回收 ####################################
  720. # Redis has two primitives to delete keys. One is called DEL and is a blocking
  721. # deletion of the object. It means that the server stops processing new commands
  722. # in order to reclaim all the memory associated with an object in a synchronous
  723. # way. If the key deleted is associated with a small object, the time needed
  724. # in order to execute the DEL command is very small and comparable to most other
  725. # O(1) or O(log_N) commands in Redis. However if the key is associated with an
  726. # aggregated value containing millions of elements, the server can block for
  727. # a long time (even seconds) in order to complete the operation.
  728. # Redis提供了两个命令来手动删除keys,其中一个是大家熟知的del,另外一个是unlink
  729. # "del"命令:删除是阻塞式(执行删除时,后续的命令就要排队等待)删除以便释放空间,如果一个Key比较小则删除很快,影响小,但如果这个Key对应的对象非常大,那么删除会很耗时,在高并发的系统里面会阻塞后面的请求,如果系统架构设计不合理则可能导致整个业务系统不可供,造成严重的生产事故
  730. # "unlink"命令:是异步的尽可能快的逐步删除,它所需的时间复杂度是O(1),Redis会启动另外一个线程来执行真正的删除并回收内存的操作,它不会阻塞后续命令。比如flushall flushdb命令也是异步执行的。
  731. #
  732. # For the above reasons Redis also offers non blocking deletion primitives
  733. # such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and
  734. # FLUSHDB commands, in order to reclaim memory in background. Those commands
  735. # are executed in constant time. Another thread will incrementally free the
  736. # object in the background as fast as possible.
  737. #
  738. # DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled.
  739. # It's up to the design of the application to understand when it is a good
  740. # idea to use one or the other. However the Redis server sometimes has to
  741. # delete keys or flush the whole database as a side effect of other operations.
  742. # Specifically Redis deletes objects independently of a user call in the
  743. # following scenarios:
  744. # 除了用户可以使用del,unlink,flushall,flushdb删除key,Redis Server在某些情况下不得不删除Key,甚至清空整个db以保证服务的可用性,下面列举了4种情况
  745. #
  746. # 1) On eviction, because of the maxmemory and maxmemory policy configurations,
  747. # in order to make room for new data, without going over the specified
  748. # memory limit.
  749. # 为了避免Redis使用的内存超过"maxmemory",且一直在这种状态下运行,Redis Server会根据选择的删除策略去自动删除一些Key,以释放空间给其他数据使用。
  750. #
  751. # 2) Because of expire: when a key with an associated time to live (see the
  752. # EXPIRE command) must be deleted from memory.
  753. # Key设置的过期时间到了,当用户访问这个Key会自动删除,或者Redis Server定期将这种Key删除。
  754. #
  755. # 3) Because of a side effect of a command that stores data on a key that may
  756. # already exist. For example the RENAME command may delete the old key
  757. # content when it is replaced with another one. Similarly SUNIONSTORE
  758. # or SORT with STORE option may delete existing keys. The SET command
  759. # itself removes any old content of the specified key in order to replace
  760. # it with the specified string.
  761. # 一些命令的底层实现就是先删除再新增,所以再使用这些命令的时候会执行删除操作,比如SET,SORT,RENAME
  762. #
  763. # 4) During replication, when a replica performs a full resynchronization with
  764. # its master, the content of the whole database is removed in order to
  765. # load the RDB file just transferred.
  766. # 主从模式下,如果断网重连后触发了"完全同步",也会将整个DB数据删除掉,然后再从RDB文件/SOCKET中加载所有数据
  767. #
  768. # In all the above cases the default is to delete objects in a blocking way,
  769. # like if DEL was called. However you can configure each case specifically
  770. # in order to instead release memory in a non-blocking way like if UNLINK
  771. # was called, using the following configuration directives:
  772. # 上面的4种情况,Redis Server删除数据都是阻塞式删除,就像"del"命令。我们可以将这4种情况的设置为异步删除,就像命令"unlink"一样
  773. lazyfree-lazy-eviction no
  774. lazyfree-lazy-expire no
  775. lazyfree-lazy-server-del no
  776. replica-lazy-flush no
  777. ############################## APPEND ONLY MODE ###############################
  778. # By default Redis asynchronously dumps the dataset on disk. This mode is
  779. # good enough in many applications, but an issue with the Redis process or
  780. # a power outage may result into a few minutes of writes lost (depending on
  781. # the configured save points).
  782. # Redis的"bgsave"可以异步的将数据集导出到RDB文件中,这种持久化方式满足了大多数的应用,但是有一种情况是当因为一些情况挂掉,比如断电,根据"save xxx"的配置可能会导致几分钟的数据丢失,在一些要求高的系统中这种情况是不被允许的。
  783. #
  784. # The Append Only File is an alternative persistence mode that provides
  785. # much better durability. For instance using the default data fsync policy
  786. # (see later in the config file) Redis can lose just one second of writes in a
  787. # dramatic event like a server power outage, or a single write if something
  788. # wrong with the Redis process itself happens, but the operating system is
  789. # still running correctly.
  790. # Redis提供了"Append Only File"新的持久化技术,该技术理论上可以做到当发生断电时让丢失的数据小于等于1秒,或者服务器本身没有挂,只是Redis Server程序挂了,甚至只有一个single write丢失
  791. #
  792. # AOF and RDB persistence can be enabled at the same time without problems.
  793. # If the AOF is enabled on startup Redis will load the AOF, that is the file
  794. # with the better durability guarantees.
  795. # AOF和RDB两种持久化技术可以同时开启,如果AOF开启了,那么启动Redis时,是从AOF文件中加载数据的,因为它保存的数据更完整,提供更好的持久化功能
  796. #
  797. # Please check http://redis.io/topics/persistence for more information.
  798. # 更多的信息请出门左转到:http://redis.io/topics/persistence for more information
  799. # 开启AOF,"appendonly"设置为yes
  800. appendonly no
  801. # The name of the append only file (default: "appendonly.aof")
  802. # 指定AOF文件名,此文件存放的目录和RDB是共用的,使用"dir"进行指定
  803. appendfilename "appendonly.aof"
  804. # 对于没有OS知识的朋友,接下来的appendfsync功能可以先要去百度找操作系统写文件缓冲区的知识点,fsync不同的选项决定了写入缓冲区的数据什么时候真正写到磁盘上
  805. # The fsync() call tells the Operating System to actually write data on disk
  806. # instead of waiting for more data in the output buffer. Some OS will really flush
  807. # data on disk, some other OS will just try to do it ASAP.
  808. # 系统调用"fsync()"告诉OS要真正的将数据写入到磁盘上,而不是写入到缓冲区当中。一些OS会立即写到磁盘,一些OS可能会尽可能快的藏尸将数据写到磁盘
  809. #
  810. # Redis supports three different modes:
  811. # Redis支持三种不同的模式:
  812. #
  813. # no: don't fsync, just let the OS flush the data when it wants. Faster.
  814. # 模式1-"no":不调用OS的fsync函数,让OS自己决定什么时候将缓冲区的数据写入到磁盘上,该模式对Redis来说速度最快
  815. #
  816. # always: fsync after every write to the append only log. Slow, Safest.
  817. # 模式2-"always":每次"写操作"都会调用一次fsync函数,这种方式最安全,但是速度是最慢的
  818. #
  819. # everysec: fsync only one time every second. Compromise.
  820. # 模式3-"everysec":每一秒钟调用一次fsync,这是一种这种折中方案。
  821. #
  822. # 看到这里顺便提一下,在Redis中随处可见这种思想,比如前面近似LRU的随机算法,有序集合底层数据结构中结合Hash表和跳跃表实现高效的单个和范围查询,过期key的惰性删除等等
  823. # 在我们自己设计系统、开发模块、甚至生活中也可以将这个思想好好运用
  824. #
  825. # The default is "everysec", as that's usually the right compromise between
  826. # speed and data safety. It's up to you to understand if you can relax this to
  827. # "no" that will let the operating system flush the output buffer when
  828. # it wants, for better performances (but if you can live with the idea of
  829. # some data loss consider the default persistence mode that's snapshotting),
  830. # or on the contrary, use "always" that's very slow but a bit safer than
  831. # everysec.
  832. # 默认模式是"everysec"的,这是结合速度和安全性的这种方案。如果你不考虑系统DOWN可能带来的数据丢失,可以将模式设置为"no",而如果你想数据完全不丢,且愿意牺牲性能,可以将模式设置为"always"
  833. #
  834. # More details please check the following article:
  835. # http://antirez.com/post/redis-persistence-demystified.html
  836. # 更多的细节请出门左转:http://antirez.com/post/redis-persistence-demystified.html
  837. # 另外大牛"antirez"还开发了基于Redis的神经网络训练模块(neural-redis)和分布式作业队列(Disque)
  838. # If unsure, use "everysec".
  839. # 如果自己不确定到底使用哪一种,就使用默认值everysec
  840. # appendfsync always
  841. appendfsync everysec
  842. # appendfsync no
  843. # When the AOF fsync policy is set to always or everysec, and a background
  844. # saving process (a background save or AOF log background rewriting) is
  845. # performing a lot of I/O against the disk, in some Linux configurations
  846. # Redis may block too long on the fsync() call. Note that there is no fix for
  847. # this currently, as even performing fsync in a different thread will block
  848. # our synchronous write(2) call.
  849. # 当AOF模式设置为"everysec"或者"always",执行后台保存AOF文件操作或者AOF文件重写(可以单独百度一下,有的面试官会问这个问题)会产生大量的IO,而一些LINUX OS的fsync调用会被阻塞很长时间(目前还未解决这个问题),这种情况会阻塞另外线程的同步写操作
  850. #
  851. # In order to mitigate this problem it's possible to use the following option
  852. # that will prevent fsync() from being called in the main process while a
  853. # BGSAVE or BGREWRITEAOF is in progress.
  854. # 为了减轻这个问题带来的影响,可以使用"no-appendfsync-on-rewrite"配置,一旦有BGSAVE和BGREWRITEAOF在执行,阻止fsync函数调用
  855. #
  856. # This means that while another child is saving, the durability of Redis is
  857. # the same as "appendfsync none". In practical terms, this means that it is
  858. # possible to lose up to 30 seconds of log in the worst scenario (with the
  859. # default Linux settings).
  860. # 简单点说就是:当"no-appendfsync-on-rewrit"设置为no,那么有一个进程在执行SAVE操作,AOF持久化模式相当于被设置成了"no",也就是说根据OS的设置,糟糕的情况下可能丢失30秒以上的数据
  861. #
  862. # If you have latency problems turn this to "yes". Otherwise leave it as
  863. # "no" that is the safest pick from the point of view of durability.
  864. # 如果你知道上面说的潜在风险,可以将"no-appendfsync-on-rewrite"设置为yes,否则就不要瞎搞,就保持为no
  865. no-appendfsync-on-rewrite no
  866. # AOF文件重写是Redis面试的一个点,也是优化Redis的一个点,将它设置得足够大,可以保存更多日志数据
  867. # Automatic rewrite of the append only file.
  868. # Redis is able to automatically rewrite the log file implicitly calling
  869. # BGREWRITEAOF when the AOF log size grows by the specified percentage.
  870. # 当AOF文件大小超过指定值"auto-aof-rewrite-min-size",就会发生AOF文件重写
  871. #
  872. # This is how it works: Redis remembers the size of the AOF file after the
  873. # latest rewrite (if no rewrite has happened since the restart, the size of
  874. # the AOF at startup is used).
  875. # Redis会记住AOF重写后的AOF文件大小,如果重启后还未发生重写,那么记住的就是刚开始加载AOF文件的大小
  876. # 这个文件大小值会与下面的配置项值进行比较,决定什么时候做AOF文件重写
  877. #
  878. # This base size is compared to the current size. If the current size is
  879. # bigger than the specified percentage, the rewrite is triggered. Also
  880. # you need to specify a minimal size for the AOF file to be rewritten, this
  881. # is useful to avoid rewriting the AOF file even if the percentage increase
  882. # is reached but it is still pretty small.
  883. # 如果当"当前大小/最后一次重写大小"的比值大于"auto-aof-rewrite-percentage"指定的值,则会触发AOF重写
  884. # 为了避免AOF已经很小还进行AOF重写的尴尬情况,因此需要设置一个AOF重写最小AOF文件大小
  885. # 比如"auto-aof-rewrite-min-size"设置为64M,只有当AOF文件超过64M,且"当前大小/最后一次重写大小">"auto-aof-rewrite-percentage"才会触发AOF重写
  886. #
  887. # Specify a percentage of zero in order to disable the automatic AOF
  888. # rewrite feature.
  889. # 如果将"auto-aof-rewrite-percentage"设置为0,表示不允许执行自动AOF重写
  890. auto-aof-rewrite-percentage 100
  891. auto-aof-rewrite-min-size 64mb
  892. # An AOF file may be found to be truncated at the end during the Redis
  893. # startup process, when the AOF data gets loaded back into memory.
  894. # This may happen when the system where Redis is running
  895. # crashes, especially when an ext4 filesystem is mounted without the
  896. # data=ordered option (however this can't happen when Redis itself
  897. # crashes or aborts but the operating system still works correctly).
  898. # 如果运行Redis的OS崩溃掉,特别是ext4格式的文件系统使用"data=ordered"选项执行mount操作,在这些情况下
  899. # AOF文件可能是截断(损坏)的,重启Redis时如果"aof-load-truncated"被设置为yes,那么AOF文件在加载时可能会丢失掉崩溃前的一些数据
  900. #
  901. # Redis can either exit with an error when this happens, or load as much
  902. # data as possible (the default now) and start if the AOF file is found
  903. # to be truncated at the end. The following option controls this behavior.
  904. # 针对损坏的AOF文件,在重启Redis的时候,支持两种方式
  905. # 1.发现文件损坏,直接报错
  906. # 2.尽可能的从找到的截断(损坏)文件中恢复数据到内存中,这是Redis的默认方式
  907. #
  908. # If aof-load-truncated is set to yes, a truncated AOF file is loaded and
  909. # the Redis server starts emitting a log to inform the user of the event.
  910. # 如果"aof-load-truncated"被设置为yes,且发现了被截断的AOF文件,那么在启动Redis时日志或者控制台中会输出日志,让运维人员或者监控看到这条信息
  911. # Otherwise if the option is set to no, the server aborts with an error
  912. # and refuses to start. When the option is set to no, the user requires
  913. # to fix the AOF file using the "redis-check-aof" utility before to restart
  914. # the server.
  915. # 如果将"aof-load-truncated"设置no,且发现了被截断的AOF文件,重启Redis会报错,这个时候就需要借用redis-check-aof工具修复AOF文件
  916. # 其实在主从模式下,是否可以到从节点拿AOF文件进行恢复,好像这个方法是多想了,因为哨兵、Codis、Cluster模式会自动进行故障转移,只有单机和纯主从模式也许这种方式可以尝试,但是现在的企业至少应该是哨兵模式了,大企业都用Cluster了或者豌豆荚搞的Codis
  917. #
  918. # Note that if the AOF file will be found to be corrupted in the middle
  919. # the server will still exit with an error. This option only applies when
  920. # Redis will try to read more data from the AOF file but not enough bytes
  921. # will be found.
  922. # 如果AOF文件在文件中间损坏了,即使"aof-load-truncated"设置为yes,重启Redis一样会报错且退出启动
  923. # 这个选项只适合AOF被截断的情况,也就是AOF没有足够的字节
  924. aof-load-truncated yes
  925. # 混合持久化,Redis 4提供的新功能
  926. # When rewriting the AOF file, Redis is able to use an RDB preamble in the
  927. # AOF file for faster rewrites and recoveries. When this option is turned
  928. # on the rewritten AOF file is composed of two different stanzas:
  929. # 如果"aof-use-rdb-preamble"设置为yes,那么AOF文件由"rdb file"+"aof tail"两部分组成,这种组合方式可以发挥RDB持久化加载速度快和压缩存储使用空间小的优势,与AOF持久化丢失数据小于1S的优势
  930. #
  931. # [RDB file][AOF tail]
  932. #
  933. # When loading Redis recognizes that the AOF file starts with the "REDIS"
  934. # string and loads the prefixed RDB file, and continues loading the AOF
  935. # tail.
  936. # 该混合持久化方式下的AOF文件用"REDIS"字符串区分,前面是RDB内容,后面是AOF内容
  937. aof-use-rdb-preamble yes
  938. ################################ LUA SCRIPTING LUA脚本 ###############################
  939. # LUA脚本我没有研究过,简单说下这个配置项是设置LUA脚本最大执行时间
  940. # 另外LUA脚本执行是原子的,因此可以用它做一些特殊的实现,不过就像Oracle的存储过程一样,维护不方便,比较这个脚本语言会的人太少了
  941. # 如果确实有需要,在考虑运维的情况下可以使用它来实现原子性等操作,慎用
  942. # Max execution time of a Lua script in milliseconds.
  943. #
  944. # If the maximum execution time is reached Redis will log that a script is
  945. # still in execution after the maximum allowed time and will start to
  946. # reply to queries with an error.
  947. #
  948. # When a long running script exceeds the maximum execution time only the
  949. # SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be
  950. # used to stop a script that did not yet called write commands. The second
  951. # is the only way to shut down the server in the case a write command was
  952. # already issued by the script but the user doesn't want to wait for the natural
  953. # termination of the script.
  954. #
  955. # Set it to 0 or a negative value for unlimited execution without warnings.
  956. # 如果设置为0或者负值,表示不限制执行时间
  957. lua-time-limit 5000
  958. ################################ REDIS CLUSTER 集群 ###############################
  959. # 在看下面的内容之前建议先去百度一下redis hash slots,以及集群的架构图
  960. #
  961. # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  962. # WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
  963. # in order to mark it as "mature" we need to wait for a non trivial percentage
  964. # of users to deploy it in production.
  965. # 虽然Redis Cluster被认为是稳定的,但是依然需要大量的用户在生产环境中使用它。。。这段注释应该从redis.conf中删除了,全世界已经有知名的大企业使用了Redis Cluster
  966. # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  967. #
  968. # Normal Redis instances can't be part of a Redis Cluster; only nodes that are
  969. # started as cluster nodes can. In order to start a Redis instance as a
  970. # cluster node enable the cluster support uncommenting the following:
  971. # 将"cluster-enabled"设置为yes,redis instance才能成为集群的一部分,但集群要真正开始工作,还需要将
  972. # 所有的slots分配给cluster node
  973. #
  974. # cluster-enabled yes
  975. # Every cluster node has a cluster configuration file. This file is not
  976. # intended to be edited by hand. It is created and updated by Redis nodes.
  977. # Every Redis Cluster node requires a different cluster configuration file.
  978. # Make sure that instances running in the same system do not have
  979. # overlapping cluster configuration file names.
  980. # 每个cluster node有自己的cluster configuration file,且该配置文件不能手工编辑,而是自动创建和更新的
  981. # cluster configuration file不能重名
  982. #
  983. # cluster-config-file nodes-6379.conf
  984. # Cluster node timeout is the amount of milliseconds a node must be unreachable
  985. # for it to be considered in failure state.
  986. # Most other internal time limits are multiple of the node timeout.
  987. # 集群节点在"cluster-node-timeout"规定的超时时间内,如果不可达,则被认为是失败状态
  988. # 注意:集群内的大多数其他内部时间限制是"cluster-node-timeout"的倍数
  989. #
  990. # cluster-node-timeout 15000
  991. # A replica of a failing master will avoid to start a failover if its data
  992. # looks too old.
  993. # 如果一个掉线主节点的从节点数据太老了,是不允许参与故障转移的
  994. #
  995. # There is no simple way for a replica to actually have an exact measure of
  996. # its "data age", so the following two checks are performed:
  997. # 没得撒子简单的办法可以一下计算出数据的年龄,因此Redis提供下面的两点来校验数据年龄,以决定集群节点是否参与故障转移过程:
  998. #
  999. # 1) If there are multiple replicas able to failover, they exchange messages
  1000. # in order to try to give an advantage to the replica with the best
  1001. # replication offset (more data from the master processed).
  1002. # Replicas will try to get their rank by offset, and apply to the start
  1003. # of the failover a delay proportional to their rank.
  1004. # 根据从节点的偏移量(主从复制-复制挤压缓冲区里面的偏移量,这个偏移量会跟着命令发给从节点,并保存下来)谁是最新的,并且根据偏移量排序,根据这个排序结果将从节点作为候选主节点
  1005. #
  1006. # 2) Every single replica computes the time of the last interaction with
  1007. # its master. This can be the last ping or command received (if the master
  1008. # is still in the "connected" state), or the time that elapsed since the
  1009. # disconnection with the master (if the replication link is currently down).
  1010. # If the last interaction is too old, the replica will not try to failover
  1011. # at all.
  1012. # 每个从节点都会计算它与主节点最后一次交互时间,比如最后一次ping时间、最后一次接收命令时间、与主节点断开连接过去的时长
  1013. # 如果最后一次交互时间太长,那么这个从节点也不会参与故障转移过程
  1014. #
  1015. # The point "2" can be tuned by user. Specifically a replica will not perform
  1016. # the failover if, since the last interaction with the master, the time
  1017. # elapsed is greater than:
  1018. # 前面讲到的第2点有一个计算公式来衡量"最后一次交互时间"是否太长
  1019. #
  1020. # (node-timeout * replica-validity-factor) + repl-ping-replica-period
  1021. #
  1022. # So for example if node-timeout is 30 seconds, and the replica-validity-factor
  1023. # is 10, and assuming a default repl-ping-replica-period of 10 seconds, the
  1024. # replica will not try to failover if it was not able to talk with the master
  1025. # for longer than 310 seconds.
  1026. # 假设"cluster-node-timeout"是30S,"replica-validity-factor"是10,"repl-ping-replica-period"是10S
  1027. # 如果"最后一次交互"时间超过"30*10+10=310"就被认为太长,而不能参与故障转移
  1028. #
  1029. # A large replica-validity-factor may allow replicas with too old data to failover
  1030. # a master, while a too small value may prevent the cluster from being able to
  1031. # elect a replica at all.
  1032. # "replica-validity-factor"太大,从节点数据可能会太久,如果太小可能选举不成功,集群不可用,所以要根据实际情况设置
  1033. #
  1034. # For maximum availability, it is possible to set the replica-validity-factor
  1035. # to a value of 0, which means, that replicas will always try to failover the
  1036. # master regardless of the last time they interacted with the master.
  1037. # (However they'll always try to apply a delay proportional to their
  1038. # offset rank).
  1039. # 如果为了保证最大的可用性,可以将"cluster-replica-validity-factor"设置为0。此时所有的从节点考虑最后一次交互时间的大小,总是会参与故障转移过程
  1040. #
  1041. # Zero is the only value able to guarantee that when all the partitions heal
  1042. # the cluster will always be able to continue.
  1043. # "cluster-replica-validity-factor"的"0"是唯一可以让集群总是可用的选项值
  1044. #
  1045. # cluster-replica-validity-factor 10
  1046. # Cluster replicas are able to migrate to orphaned masters, that are masters
  1047. # that are left without working replicas. This improves the cluster ability
  1048. # to resist to failures as otherwise an orphaned master can't be failed over
  1049. # in case of failure if it has no working replicas.
  1050. # 再不看下面的配置项功能时,有可能集群从节点会变成一个孤立的从节点,针对这种情况,如果它再发生故障,因为没有备选的从节点,所以故障转移动作没法完成。
  1051. #
  1052. # Replicas migrate to orphaned masters only if there are still at least a
  1053. # given number of other working replicas for their old master. This number
  1054. # is the "migration barrier". A migration barrier of 1 means that a replica
  1055. # will migrate only if there is at least 1 other working replica for its master
  1056. # and so forth. It usually reflects the number of replicas you want for every
  1057. # master in your cluster.
  1058. # 为了避免上面的情况发生,Redis Cluster默认配置要求一个主节点至少有两个从节点,一旦主节点挂了被新选举出来的主节点至少有一个从节点在工作。"cluster-migration-barrier"可以指定该值的大小,默认值是"1"
  1059. #
  1060. # Default is 1 (replicas migrate only if their masters remain with at least
  1061. # one replica). To disable migration just set it to a very large value.
  1062. # A value of 0 can be set but is useful only for debugging and dangerous
  1063. # in production.
  1064. # 默认值是1,如果要想禁止"migration",可以将"cluster-migration-barrier"设置为一个超大的值
  1065. # 可以为了调试或者你想让自己的系统存在高风险的运行,可以设置为0。。。no zuo no die
  1066. #
  1067. # cluster-migration-barrier 1
  1068. # By default Redis Cluster nodes stop accepting queries if they detect there
  1069. # is at least an hash slot uncovered (no available node is serving it).
  1070. # This way if the cluster is partially down (for example a range of hash slots
  1071. # are no longer covered) all the cluster becomes, eventually, unavailable.
  1072. # It automatically returns available as soon as all the slots are covered again.
  1073. # Redis Cluster默认情况下如果有一个hash slot没有被分配(用一个Cluster Node接收它),那么整个集群是不可用的
  1074. # 在这种模式下,一旦出现网络分区(一段hash slots 就变成未分配),整个集群就不可用了,直到所有hash slots被分配,集群会自动变得可用
  1075. #
  1076. # However sometimes you want the subset of the cluster which is working,
  1077. # to continue to accept queries for the part of the key space that is still
  1078. # covered. In order to do so, just set the cluster-require-full-coverage
  1079. # option to no.
  1080. # 也许有时你想即使出现hash slots unconverd,而集群的部分节点仍然是可用的,可以将"cluster-require-full-coverage"设置为no
  1081. #
  1082. # cluster-require-full-coverage yes
  1083. # This option, when set to yes, prevents replicas from trying to failover its
  1084. # master during master failures. However the master can still perform a
  1085. # manual failover, if forced to do so.
  1086. # 如果将"cluster-replica-no-failover"设置为yes,那么该集群从节点不会参与自动故障转移过程,但是可以手动强制执行故障转移
  1087. #
  1088. # This is useful in different scenarios, especially in the case of multiple
  1089. # data center operations, where we want one side to never be promoted if not
  1090. # in the case of a total DC failure.
  1091. # 在不同场景可能非常有用,比如有多个数据中心,而我们又不希望整个集群中的某一个数据中心的从节点被提升为主节点
  1092. #
  1093. # cluster-replica-no-failover no
  1094. # In order to setup your cluster make sure to read the documentation
  1095. # available at http://redis.io web site.
  1096. ########################## CLUSTER DOCKER/NAT support ########################
  1097. # In certain deployments, Redis Cluster nodes address discovery fails, because
  1098. # addresses are NAT-ted or because ports are forwarded (the typical case is
  1099. # Docker and other containers).
  1100. #
  1101. # In order to make Redis Cluster working in such environments, a static
  1102. # configuration where each node knows its public address is needed. The
  1103. # following two options are used for this scope, and are:
  1104. #
  1105. # * cluster-announce-ip
  1106. # * cluster-announce-port
  1107. # * cluster-announce-bus-port
  1108. #
  1109. # Each instruct the node about its address, client port, and cluster message
  1110. # bus port. The information is then published in the header of the bus packets
  1111. # so that other nodes will be able to correctly map the address of the node
  1112. # publishing the information.
  1113. #
  1114. # If the above options are not used, the normal Redis Cluster auto-detection
  1115. # will be used instead.
  1116. #
  1117. # Note that when remapped, the bus port may not be at the fixed offset of
  1118. # clients port + 10000, so you can specify any port and bus-port depending
  1119. # on how they get remapped. If the bus-port is not set, a fixed offset of
  1120. # 10000 will be used as usually.
  1121. #
  1122. # Example:
  1123. #
  1124. # cluster-announce-ip 10.1.1.5
  1125. # cluster-announce-port 6379
  1126. # cluster-announce-bus-port 6380
  1127. ################################## SLOW LOG 慢日志 ###################################
  1128. # The Redis Slow Log is a system to log queries that exceeded a specified
  1129. # execution time. The execution time does not include the I/O operations
  1130. # like talking with the client, sending the reply and so forth,
  1131. # but just the time needed to actually execute the command (this is the only
  1132. # stage of command execution where the thread is blocked and can not serve
  1133. # other requests in the meantime).
  1134. # 记录Redis执行耗时超过指定值的"查询命令",整个"耗时"仅仅是执行命令的耗时(在这段时间内,因为线程被阻塞,其他命令会被阻塞),不包括与客户端网络IO所耗时间或者发数据给客户端的耗时
  1135. #
  1136. # You can configure the slow log with two parameters: one tells Redis
  1137. # what is the execution time, in microseconds, to exceed in order for the
  1138. # command to get logged, and the other parameter is the length of the
  1139. # slow log. When a new command is logged the oldest one is removed from the
  1140. # queue of logged commands.
  1141. # 可以使用"slowlog-log-slower-than"指定耗时的阈值(单位是微妙),一旦执行超过这个时间就会记录日志到缓冲区
  1142. # 可以使用"slowlog-max-len 128"指定队列长度,如果超过队列,最老的元素会被覆盖
  1143. #
  1144. # The following time is expressed in microseconds, so 1000000 is equivalent
  1145. # to one second. Note that a negative number disables the slow log, while
  1146. # a value of zero forces the logging of every command.
  1147. # 单位是微妙,不能设置为负值,如果设置为0,那么所有的查询命令都会记录到队列中
  1148. #
  1149. slowlog-log-slower-than 10000
  1150. # There is no limit to this length. Just be aware that it will consume memory.
  1151. # 最大值没有限制,我们只需要考虑内存是否足够大
  1152. # You can reclaim memory used by the slow log with SLOWLOG RESET.
  1153. # 可以使用slowlog reset回收已使用的内存
  1154. slowlog-max-len 128
  1155. ################################ LATENCY MONITOR ##############################
  1156. # The Redis latency monitoring subsystem samples different operations
  1157. # at runtime in order to collect data related to possible sources of
  1158. # latency of a Redis instance.
  1159. # 延迟监控子系统通过采集运行时的不同操作去收集造成Redis实例延迟的相关可能来源
  1160. #
  1161. #
  1162. # Via the LATENCY command this information is available to the user that can
  1163. # print graphs and obtain reports.
  1164. # 可以通过latency命令获得可用信息的图表,比如latency docter xxx/latency graph等
  1165. #
  1166. # The system only logs operations that were performed in a time equal or
  1167. # greater than the amount of milliseconds specified via the
  1168. # latency-monitor-threshold configuration directive. When its value is set
  1169. # to zero, the latency monitor is turned off.
  1170. # 该监控子系统只会记录那些耗时>="latency-monitor-threshold"指定的值对应的操作,如果设置为0,表示关闭延时监控
  1171. #
  1172. # By default latency monitoring is disabled since it is mostly not needed
  1173. # if you don't have latency issues, and collecting data has a performance
  1174. # impact, that while very small, can be measured under big load. Latency
  1175. # monitoring can easily be enabled at runtime using the command
  1176. # "CONFIG SET latency-monitor-threshold <milliseconds>" if needed.
  1177. # Redis默认是关闭了延迟监控的,因为绝大多数时间是用不着的,因为开启它有一定的性能损失,除非你的服务发生了延时而开启监控
  1178. # 当Redis是运行着的时候,可以通过config set latency-monitor-threshold xxx轻松开启监控
  1179. latency-monitor-threshold 0
  1180. ############################# EVENT NOTIFICATION ##############################
  1181. # 下面的条件说明很多看上去挺复杂的,其实很简单:就是多个字符代表的意思组合到一起而已
  1182. # Redis can notify Pub/Sub clients about events happening in the key space.
  1183. # This feature is documented at http://redis.io/topics/notifications
  1184. # Redis可以将关于"键空间(简单理解为Hash表中的键值对)"发生的事件以通知的形式发送给Pub/Sub客户端
  1185. # 更详细的请参考Redis的官方文档:http://redis.io/topics/notifications
  1186. #
  1187. # For instance if keyspace events notification is enabled, and a client
  1188. # performs a DEL operation on key "foo" stored in the Database 0, two
  1189. # messages will be published via Pub/Sub:
  1190. # 如果通过配置开启了键空间和键时间的通知,如果通过客户端在第0号database上执行一个DEL foo操作,那么会
  1191. # 发布两条消息
  1192. # PUBLISH __keyspace@0__:foo del
  1193. # PUBLISH __keyevent@0__:del foo
  1194. #
  1195. # It is possible to select the events that Redis will notify among a set
  1196. # of classes. Every class is identified by a single character:
  1197. # 我们可以通过组合下面的分类将事件通知发给客户端
  1198. # "K"和"E"代表两大类,无论怎么组合,必须有其中一个,可以两个同时选择,K代表Keyspace事件,E代表Keyevent事件
  1199. # K以为着一个或多个数据类型的所有符合规则事件都会生成通知
  1200. # E以为着一个或多个数据类型的某一个命令的时间会生成通知
  1201. # 如果看到这里还没明白,建议去百度一下,推荐一个:http://redisdoc.com/topic/notification.html#id1
  1202. # K Keyspace events, published with __keyspace@<db>__ prefix.
  1203. # E Keyevent events, published with __keyevent@<db>__ prefix.
  1204. #
  1205. # 一般的命令,比如DEL SET EXPIRE RENAME等等,感觉像是所有会产生改变的命令都符合条件
  1206. # g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
  1207. #
  1208. # 下面的$ l s h z 分别代表大家都知道5种数据类型
  1209. # $ String commands
  1210. # l List commands
  1211. # s Set commands
  1212. # h Hash commands
  1213. # z Sorted set commands
  1214. #
  1215. # x 代表过期事件 e 代表内存使用超过maxmemory时KEY被淘汰的事件
  1216. # x Expired events (events generated every time a key expires)
  1217. # e Evicted events (events generated when a key is evicted for maxmemory)
  1218. #
  1219. # A 是一个别名,代表了"g$lshzxe"的组合,可以增强阅读性
  1220. # A Alias for g$lshzxe, so that the "AKE" string means all the events.
  1221. #
  1222. # The "notify-keyspace-events" takes as argument a string that is composed
  1223. # of zero or multiple characters. The empty string means that notifications
  1224. # are disabled.
  1225. # 可以给"notify-keyspace-events"设置0或者多个字符,如果设置为空字符串,则表示关闭此功能
  1226. #
  1227. # Example: to enable list and generic events, from the point of view of the
  1228. # event name, use:
  1229. #
  1230. # notify-keyspace-events Elg
  1231. #
  1232. # Example 2: to get the stream of the expired keys subscribing to channel
  1233. # name __keyevent@0__:expired use:
  1234. #
  1235. # notify-keyspace-events Ex
  1236. #
  1237. # By default all notifications are disabled because most users don't need
  1238. # this feature and the feature has some overhead. Note that if you don't
  1239. # specify at least one of K or E, no events will be delivered.
  1240. # 因为开启此功能是有一定开销的,会影响性能,而且大多数用户不需要此功能,所以默认是关闭了此功能的,不会有事件通知被发送
  1241. notify-keyspace-events ""
  1242. ############################### ADVANCED CONFIG 高级配置 ###############################
  1243. # 下面的配置需要对Redis的原理,特别5中数据类型的底层数据结构有比较清楚的了解才能看得懂,总的来说就是根据自己的键-值选择5中数据类型在某些条件下使用何种数据结构来存放数据。
  1244. # 最常见的高效数据结构就是ziplist、intset,但是他们通常只有元素(条目)较小且元素(条目)较小时才适合
  1245. # 要学习这部分内容可以看看redis设计与实现和Redis资深历险两本书,前一本书将原理很多,且深度足够,但是Redis的版本有点太老了,后一步本书可以在原理上对前一本书进行补充,且Redis版本很新,已经到5了。而且它还将了很多实战的知识。
  1246. #
  1247. # Hashes are encoded using a memory efficient data structure when they have a
  1248. # small number of entries, and the biggest entry does not exceed a given
  1249. # threshold. These thresholds can be configured using the following directives.
  1250. #
  1251. # hash数据类型:如果条目数小于512,且条目大小不超过64字节,则使用ziplist作为hash数据类型的底层数据结构
  1252. hash-max-ziplist-entries 512
  1253. hash-max-ziplist-value 64
  1254. # 新版本的Redis针对list数据类型的底层数据结构做了优化采用的是"链表+ziplist",其思想有点像Java HashMap的"数组+链表/红黑树"
  1255. # Lists are also encoded in a special way to save a lot of space.
  1256. # The number of entries allowed per internal list node can be specified
  1257. # as a fixed maximum size or a maximum number of elements.
  1258. # 可以通过"list-max-ziplist-size"设置链表中ziplist的条目数量,其值可以是条目数量,也可以最大字节数
  1259. # For a fixed maximum size, use -5 through -1, meaning:
  1260. # 下面是5个可能取值,建议使用-1 和 -2,其他选项不推荐使用,除非有特殊需求
  1261. # -5: max size: 64 Kb <-- not recommended for normal workloads
  1262. # -4: max size: 32 Kb <-- not recommended
  1263. # -3: max size: 16 Kb <-- probably not recommended
  1264. # -2: max size: 8 Kb <-- good
  1265. # -1: max size: 4 Kb <-- good
  1266. #
  1267. # Positive numbers mean store up to _exactly_ that number of elements
  1268. # per list node.
  1269. # 上面的负值就是单个链表节点所包含的条目数
  1270. #
  1271. # The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),
  1272. # but if your use case is unique, adjust the settings as necessary.
  1273. # 取值为-1 -2 发挥的性能是最好的
  1274. list-max-ziplist-size -2
  1275. # Lists may also be compressed.
  1276. # Compress depth is the number of quicklist ziplist nodes from *each* side of
  1277. # the list to *exclude* from compression. The head and tail of the list
  1278. # are always uncompressed for fast push/pop operations. Settings are:
  1279. # 0: disable all list compression
  1280. # 表示不压缩任何节点
  1281. # 1: depth 1 means "don't start compressing until after 1 node into the list,
  1282. # going from either the head or tail"
  1283. # So: [head]->node->node->...->node->[tail]
  1284. # [head], [tail] will always be uncompressed; inner nodes will compress.
  1285. # 表示除链表的头尾以外,其他链表节点都压缩
  1286. # 2: [head]->[next]->node->node->...->node->[prev]->[tail]
  1287. # 2 here means: don't compress head or head->next or tail->prev or tail,
  1288. # but compress all nodes between them.
  1289. # 依次类推,即前两个和后两个以外的都压缩
  1290. # 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail]
  1291. # 依次类推
  1292. # etc.
  1293. # 默认压缩深度为0,也就是说不压缩。。。无论如何设置头尾是不会压缩的,比如当list被当做队列使用时,如果压缩了,还需要解压,降低了性能。
  1294. list-compress-depth 0
  1295. # Sets have a special encoding in just one case: when a set is composed
  1296. # of just strings that happen to be integers in radix 10 in the range
  1297. # of 64 bit signed integers.
  1298. # 当集合(set)存放的值都是64位的无符号10进制整数时,且条目数小于512时会采用intset作为集合的底层数据结构
  1299. # The following configuration setting sets the limit in the size of the
  1300. # set in order to use this special memory saving encoding.
  1301. set-max-intset-entries 512
  1302. # 和hash数据类型类似,如果条目数小于128,且条目大小<64会使用ziplist作为有序集合的底层数据结构
  1303. # Similarly to hashes and lists, sorted sets are also specially encoded in
  1304. # order to save a lot of space. This encoding is only used when the length and
  1305. # elements of a sorted set are below the following limits:
  1306. zset-max-ziplist-entries 128
  1307. zset-max-ziplist-value 64
  1308. # HyperLogLog sparse representation bytes limit. The limit includes the
  1309. # 16 bytes header. When an HyperLogLog using the sparse representation crosses
  1310. # this limit, it is converted into the dense representation.
  1311. #
  1312. # A value greater than 16000 is totally useless, since at that point the
  1313. # dense representation is more memory efficient.
  1314. #
  1315. # The suggested value is ~ 3000 in order to have the benefits of
  1316. # the space efficient encoding without slowing down too much PFADD,
  1317. # which is O(N) with the sparse encoding. The value can be raised to
  1318. # ~ 10000 when CPU is not a concern, but space is, and the data set is
  1319. # composed of many HyperLogLogs with cardinality in the 0 - 15000 range.
  1320. # 这个是Redis高级功能,可以用这种数据结构统计网站的UV,能够去重,其准确度接近真实值
  1321. # 简单点说:当去重后统计出来的值小于"hll-sparse-max-bytes"指定的值时,Redis会使用稀疏矩阵来存放,一个Key占用的空间比稠密矩阵小,如果统计出来的值大于"hll-sparse-max-bytes"指定的值,那么使用稠密矩阵,此时一个Key占用的空间是12KB
  1322. # "hll-sparse-max-bytes"默认为3000,如果设置为16000以上完全是无用的,因为此时稠密矩阵效果更好
  1323. hll-sparse-max-bytes 3000
  1324. # Streams macro node max size / items. The stream data structure is a radix
  1325. # tree of big nodes that encode multiple items inside. Using this configuration
  1326. # it is possible to configure how big a single node can be in bytes, and the
  1327. # maximum number of items it may contain before switching to a new node when
  1328. # appending new stream entries. If any of the following settings are set to
  1329. # zero, the limit is ignored, so for instance it is possible to set just a
  1330. # max entires limit by setting max-bytes to 0 and max-entries to the desired
  1331. # value.
  1332. # 设置Stream的单个节点最大字节数和最多能有多少个条目,如果任何一个条件满足就会新增加一个节点用以保存新的数据
  1333. # 如果将任何一个配置项设置为0,表示不限制
  1334. stream-node-max-bytes 4096
  1335. stream-node-max-entries 100
  1336. # Redis数据库存放键值对数据结构是一个类型为字典长度为2的数组,假设这个数组名称为"ht",在rehash的时候就是将其中一个字典(ht[0])中的所有数据搬到另一个字典(ht[1])中,而且rehash是惰性的(因为redis要高效的响应查询或者写,不可能去一次完成rehash操作,不像Java的HashMap),当方式key时或者CPU比较空闲时会触发,因此也被称之为"渐进式hash"
  1337. # Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
  1338. # order to help rehashing the main Redis hash table (the one mapping top-level
  1339. # keys to values). The hash table implementation Redis uses (see dict.c)
  1340. # performs a lazy rehashing: the more operation you run into a hash table
  1341. # that is rehashing, the more rehashing "steps" are performed, so if the
  1342. # server is idle the rehashing is never complete and some more memory is used
  1343. # by the hash table.
  1344. #
  1345. # The default is to use this millisecond 10 times every second in order to
  1346. # actively rehash the main dictionaries, freeing memory when possible.
  1347. # 默认是使用1秒钟的10毫秒进行rehash,在适当的时候回收内存
  1348. #
  1349. # If unsure:
  1350. # use "activerehashing no" if you have hard latency requirements and it is
  1351. # not a good thing in your environment that Redis can reply from time to time
  1352. # to queries with 2 milliseconds delay.
  1353. # 如果系统有严格的延时要求,在2毫秒内不断的查询出结果,可以将"activerehashing"设置no
  1354. # 但是这对你的系统并不是一个好事情,因此不建议这样设置,所以保持不动吧
  1355. #
  1356. # use "activerehashing yes" if you don't have such hard requirements but
  1357. # want to free memory asap when possible.
  1358. # 如果没有非常严格的要求,建议将"activerehashing"设置为yes,这样可以让内存尽可能快的释放
  1359. activerehashing yes
  1360. # The client output buffer limits can be used to force disconnection of clients
  1361. # that are not reading data from the server fast enough for some reason (a
  1362. # common reason is that a Pub/Sub client can't consume messages as fast as the
  1363. # publisher can produce them).
  1364. # 可以通过设置客户端输出缓冲区大小将待接收数据超过缓冲区大小的客户端断开
  1365. # 通常使用pub/sub的时候,客户端没有及时消费而导致超过缓冲区大小
  1366. #
  1367. # The limit can be set differently for the three different classes of clients:
  1368. # 提供三种客户端的设置,分别是普通的、主从复制的、pub/sub的客户端,我们可以分别对这三种客户端的输出缓冲区设置大小
  1369. #
  1370. # normal -> normal clients including MONITOR clients
  1371. # replica -> replica clients
  1372. # pubsub -> clients subscribed to at least one pubsub channel or pattern
  1373. #
  1374. # The syntax of every client-output-buffer-limit directive is the following:
  1375. # 下面是三种客户端缓冲区大小设置的语法
  1376. #
  1377. # client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
  1378. #
  1379. # A client is immediately disconnected once the hard limit is reached, or if
  1380. # the soft limit is reached and remains reached for the specified number of
  1381. # seconds (continuously).
  1382. # 如果客户端输出缓冲区的大小达到了"hard limit",服务器会立即断开连接
  1383. # 如果客户端输出缓冲区的大小达到了"soft limit",且持续时间达到了"soft seconds",服务器会立即断开连接
  1384. #
  1385. # So for instance if the hard limit is 32 megabytes and the soft limit is
  1386. # 16 megabytes / 10 seconds, the client will get disconnected immediately
  1387. # if the size of the output buffers reach 32 megabytes, but will also get
  1388. # disconnected if the client reaches 16 megabytes and continuously overcomes
  1389. # the limit for 10 seconds.
  1390. # 这上面是一个举例,省略。。。。
  1391. #
  1392. # By default normal clients are not limited because they don't receive data
  1393. # without asking (in a push way), but just after a request, so only
  1394. # asynchronous clients may create a scenario where data is requested faster
  1395. # than it can read.
  1396. # 默认情况下普通的client不限制,因为它们都是发起请求后等待接收数据,并不像异步的客户端(比如主从复制客户端和PUB/SUB)会造成数据的挤压,挤压的原因就是客户端处理速度跟不上数据产生的速度
  1397. #
  1398. # Instead there is a default limit for pubsub and replica clients, since
  1399. # subscribers and replicas receive data in a push fashion.
  1400. #
  1401. # Both the hard or the soft limit can be disabled by setting them to zero.
  1402. # hard or soft limit 都可以通过设置为0而禁止掉
  1403. client-output-buffer-limit normal 0 0 0
  1404. client-output-buffer-limit replica 256mb 64mb 60
  1405. client-output-buffer-limit pubsub 32mb 8mb 60
  1406. # Client query buffers accumulate new commands. They are limited to a fixed
  1407. # amount by default in order to avoid that a protocol desynchronization (for
  1408. # instance due to a bug in the client) will lead to unbound memory usage in
  1409. # the query buffer. However you can configure it here if you have very special
  1410. # needs, such us huge multi/exec requests or alike.
  1411. # 客户端查询缓冲区会累加新命令,默认情况下,缓冲区大小是一个固定值以避免协议同步失效(如客户端的bug)导致查询缓冲区出现未绑定的内存(即客户端都已经不存在了,但是它发过来的命令还在缓冲区当中)
  1412. # 如果有巨大的multi/exec请求,则可以修改这个值以满足我们的特殊需求
  1413. #
  1414. # client-query-buffer-limit 1gb
  1415. # In the Redis protocol, bulk requests, that are, elements representing single
  1416. # strings, are normally limited ot 512 mb. However you can change this limit
  1417. # here.
  1418. # 如果一个大容量请求(即客户端单次发送过来的字符串)被限制为512MB,我们也可以通过修改"proto-max-bulk-len"值
  1419. # 不过我可能一辈子也不会用到
  1420. # proto-max-bulk-len 512mb
  1421. # Redis calls an internal function to perform many background tasks, like
  1422. # closing connections of clients in timeout, purging expired keys that are
  1423. # never requested, and so forth.
  1424. #
  1425. # Not all tasks are performed with the same frequency, but Redis checks for
  1426. # tasks to perform according to the specified "hz" value.
  1427. #
  1428. # By default "hz" is set to 10. Raising the value will use more CPU when
  1429. # Redis is idle, but at the same time will make Redis more responsive when
  1430. # there are many keys expiring at the same time, and timeouts may be
  1431. # handled with more precision.
  1432. #
  1433. # The range is between 1 and 500, however a value over 100 is usually not
  1434. # a good idea. Most users should use the default of 10 and raise this up to
  1435. # 100 only in environments where very low latency is required.
  1436. # 简单点说:Redis有后台任务,通过设置"hz"可提高或者降低检查这些任务是否应该执行的频率,值越大消耗的CPU越多,反之越少
  1437. # 值可以设置在1到500之间,通常不建议将该值设置得比100大,一般都使用10这个默认值,除非我们的系统有非常严格的延时要求,才会将"hz"设置得等于或者超过100
  1438. hz 10
  1439. # Normally it is useful to have an HZ value which is proportional to the
  1440. # number of clients connected. This is useful in order, for instance, to
  1441. # avoid too many clients are processed for each background task invocation
  1442. # in order to avoid latency spikes.
  1443. #
  1444. # Since the default HZ value by default is conservatively set to 10, Redis
  1445. # offers, and enables by default, the ability to use an adaptive HZ value
  1446. # which will temporary raise when there are many connected clients.
  1447. #
  1448. # When dynamic HZ is enabled, the actual configured HZ will be used as
  1449. # as a baseline, but multiples of the configured HZ value will be actually
  1450. # used as needed once more clients are connected. In this way an idle
  1451. # instance will use very little CPU time while a busy instance will be
  1452. # more responsive.
  1453. # 英文有时还真的描述很啰唆,还是中文编码更高效。。。吐槽一下英文
  1454. # 前面的"ht"配置项是固定值,当连接客户端非常多时,如果"ht"还是10,则可能会导致延迟比较高,因此Redis搞了一个
  1455. # "dynamic-hz"配置项,当设置为yes时,可以基于"ht"配置值动态的调整使用的"ht"值,比如连接的客户端很多事,动态将ht调高,可以减少延迟。而当连接客户端比较少,又可以动态降低"ht",这样消耗的CPU会很少
  1456. # 默认值是yes,这个根本不需要我们自己去动,有了它我们也不需要去动"ht"配置
  1457. dynamic-hz yes
  1458. # When a child rewrites the AOF file, if the following option is enabled
  1459. # the file will be fsync-ed every 32 MB of data generated. This is useful
  1460. # in order to commit the file to the disk more incrementally and avoid
  1461. # big latency spikes.
  1462. # 当子进程在重写AOF文件时,如果将"aof-rewrite-incremental-fsync"设置为yes,那么一旦生成32M数据才会调用一次OS的fsync函数,这样可以降低出现访问峰值时系统的延迟。因为可以减少fsync调用次数和IO请求
  1463. aof-rewrite-incremental-fsync yes
  1464. # When redis saves RDB file, if the following option is enabled
  1465. # the file will be fsync-ed every 32 MB of data generated. This is useful
  1466. # in order to commit the file to the disk more incrementally and avoid
  1467. # big latency spikes.
  1468. # 和"aof-rewrite-incremental-fsync"一个意思,只不过是用在生成RDB文件时用。
  1469. # 如果持久化采用的混合方式,即AOF文件是由"RDB部分+AOF部分"组成的话,我想"aof-rewrite-incremental-fsync"和"rdb-save-incremental-fsync"都会使用到
  1470. rdb-save-incremental-fsync yes
  1471. # Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good
  1472. # idea to start with the default settings and only change them after investigating
  1473. # how to improve the performances and how the keys LFU change over time, which
  1474. # is possible to inspect via the OBJECT FREQ command.
  1475. #
  1476. # There are two tunable parameters in the Redis LFU implementation: the
  1477. # counter logarithm factor and the counter decay time. It is important to
  1478. # understand what the two parameters mean before changing them.
  1479. #
  1480. # Redis的LFU实现有两个可调整的参数:计数器对数因子(couter logarithm factor)和计数器衰退时间(counter decay time)
  1481. # 一定要充分理解这两个参数之后才能去修改,如果不懂就不要去瞎搞了,如果非要修改,一定要使用"OBJECT FREQ"命令充分调查并知道如何提升性能的情况下才能进行
  1482. # The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis
  1483. # uses a probabilistic increment with logarithmic behavior. Given the value
  1484. # of the old counter, when a key is accessed, the counter is incremented in
  1485. # this way:
  1486. # 在介绍maxmemory的时候提到了两个参数作用的原理,这里就不赘述了。
  1487. #
  1488. # 1. A random number R between 0 and 1 is extracted.
  1489. # 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1).
  1490. # 3. The counter is incremented only if R < P.
  1491. #
  1492. # The default lfu-log-factor is 10. This is a table of how the frequency
  1493. # counter changes with a different number of accesses with different
  1494. # logarithmic factors:
  1495. # "lfu-log-factor"的默认值=10,下表是不同对数因子下计数器的改变频率:
  1496. #
  1497. # +--------+------------+------------+------------+------------+------------+
  1498. # | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits |
  1499. # +--------+------------+------------+------------+------------+------------+
  1500. # | 0 | 104 | 255 | 255 | 255 | 255 |
  1501. # +--------+------------+------------+------------+------------+------------+
  1502. # | 1 | 18 | 49 | 255 | 255 | 255 |
  1503. # +--------+------------+------------+------------+------------+------------+
  1504. # | 10 | 10 | 18 | 142 | 255 | 255 |
  1505. # +--------+------------+------------+------------+------------+------------+
  1506. # | 100 | 8 | 11 | 49 | 143 | 255 |
  1507. # +--------+------------+------------+------------+------------+------------+
  1508. #
  1509. # NOTE: The above table was obtained by running the following commands:
  1510. # 上面的表格可以通过下面的命令得到:
  1511. #
  1512. # redis-benchmark -n 1000000 incr foo
  1513. # redis-cli object freq foo
  1514. #
  1515. # NOTE 2: The counter initial value is 5 in order to give new objects a chance
  1516. # to accumulate hits.
  1517. # 默认counter的初始值是5,为了让新的对象有机会累加它的命中率
  1518. #
  1519. # The counter decay time is the time, in minutes, that must elapse in order
  1520. # for the key counter to be divided by two (or decremented if it has a value
  1521. # less <= 10).
  1522. # 计数器衰减时间是key计数器除以2(如果值小于<=10,则递减)所必须经过的时间,单位为分钟。
  1523. #
  1524. # The default value for the lfu-decay-time is 1. A Special value of 0 means to
  1525. # decay the counter every time it happens to be scanned.
  1526. # "lfu-decay-time" 的默认值为 1,0 表示每次都对计数器进行衰减
  1527. #
  1528. # lfu-log-factor 10
  1529. # lfu-decay-time 1
  1530. ########################### ACTIVE DEFRAGMENTATION #######################
  1531. ########################### 在线碎片整理 #######################
  1532. #
  1533. # WARNING THIS FEATURE IS EXPERIMENTAL. However it was stress tested
  1534. # even in production and manually tested by multiple engineers for some
  1535. # time.
  1536. # 这还只是一个实验功能,就像Redis Cluster一样,其实已经有很多人在使用了
  1537. # What is active defragmentation?
  1538. # -------------------------------
  1539. #
  1540. # Active (online) defragmentation allows a Redis server to compact the
  1541. # spaces left between small allocations and deallocations of data in memory,
  1542. # thus allowing to reclaim back memory.
  1543. #
  1544. # Fragmentation is a natural process that happens with every allocator (but
  1545. # less so with Jemalloc, fortunately) and certain workloads. Normally a server
  1546. # restart is needed in order to lower the fragmentation, or at least to flush
  1547. # away all the data and create it again. However thanks to this feature
  1548. # implemented by Oran Agra for Redis 4.0 this process can happen at runtime
  1549. # in an "hot" way, while the server is running.
  1550. # 活动碎片整理允许Redis服务器压缩内存中由于申请和释放数据块导致的碎片,从而回收内存,就好像window的磁盘整理一样
  1551. # 碎片是每次申请内存(幸运的是Jemalloc出现碎片的几率小很多)的时候会自然发生的
  1552. # 通常来说,为了降低碎片化程度需要重启服务,或者清除所有的数据然后重新创建。 得益于Oran Agra在Redis 4.0实现的这个特性,进程可以在服务运行时以"热"方式完成
  1553. #
  1554. # Basically when the fragmentation is over a certain level (see the
  1555. # configuration options below) Redis will start to create new copies of the
  1556. # values in contiguous memory regions by exploiting certain specific Jemalloc
  1557. # features (in order to understand if an allocation is causing fragmentation
  1558. # and to allocate it in a better place), and at the same time, will release the
  1559. # old copies of the data. This process, repeated incrementally for all the keys
  1560. # will cause the fragmentation to drop back to normal values.
  1561. # 通常来说当碎片化达到一定程度(查看下面的配置)Redis 会使用Jemalloc创建连续的内存空间,并在此内存空间对现有的值进行拷贝,拷贝完成后会释放掉旧的数据。
  1562. # 这个过程会对所有的导致碎片化的key以增量的形式进行,Redis处处使用渐进式的,真实辛苦设计者了
  1563. #
  1564. # Important things to understand:
  1565. # 要重点理解的三点:
  1566. #
  1567. # 1. This feature is disabled by default, and only works if you compiled Redis
  1568. # to use the copy of Jemalloc we ship with the source code of Redis.
  1569. # This is the default with Linux builds.
  1570. # 默认情况下,该功能是关闭的,并且只有在编译Redis时使用了代码中的Jemalloc才生效(这是 Linux 下的默认行为)
  1571. # 2. You never need to enable this feature if you don't have fragmentation
  1572. # issues.
  1573. # 如果没有碎片问题,我们永远也不需要启用该功能
  1574. #
  1575. # 3. Once you experience fragmentation, you can enable this feature when
  1576. # needed with the command "CONFIG SET activedefrag yes".
  1577. # 可以通过命令"CONFIG SET activefrag yes"来启用并试验
  1578. #
  1579. # The configuration parameters are able to fine tune the behavior of the
  1580. # defragmentation process. If you are not sure about what they mean it is
  1581. # a good idea to leave the defaults untouched.
  1582. # 相关的配置参数可以很好的调整碎片整理过程,如果你不知道这些选项的作用最好使用默认值。
  1583. # Enabled active defragmentation
  1584. # 开启在线整理
  1585. # activedefrag yes
  1586. # Minimum amount of fragmentation waste to start active defrag
  1587. # 有多少碎片时开始整理
  1588. # active-defrag-ignore-bytes 100mb
  1589. # Minimum percentage of fragmentation to start active defrag
  1590. # 有多少比例的碎片时开始整理
  1591. # active-defrag-threshold-lower 10
  1592. # Maximum percentage of fragmentation at which we use maximum effort
  1593. # 有多少比例的碎片时开始进行整理
  1594. # active-defrag-threshold-upper 100
  1595. # Minimal effort for defrag in CPU percentage
  1596. # 进行碎片整理时使用多少比例的CPU时间
  1597. # active-defrag-cycle-min 5
  1598. # Maximal effort for defrag in CPU percentage
  1599. # 进行整理时使用多少CPU时间
  1600. # active-defrag-cycle-max 75
  1601. # Maximum number of set/hash/zset/list fields that will be processed from
  1602. # the main dictionary scan
  1603. # 进行主字典扫描时处理的 set/hash/zset/list 字段的最大数量(就是说在进行主字典扫描时 set/hash/zset/list 的长度小于这个值才会处理,大于这个值的会放在一个列表中延迟处理)
  1604. # 因为如果某一个key过大,一次性处理完会非常耗时的
  1605. # active-defrag-max-scan-fields 1000
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/976336
推荐阅读
相关标签
  

闽ICP备14008679号