4 redis.conf学习
conf文件主要分成如下几个模块:
下面是conf文件(3.2.11)的中文注释,涵盖了大部分配置:
# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.
################################## INCLUDES ###################################
#引入其他配置文件,比如当你有多个server,而有些配置是它们公用的,那么可以将这些配
#置写入到一个配置文件中,然后这些server再include这个配置文件
# include /path/to/local.conf
# include /path/to/other.conf
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
#默认情况下,redis监听server所有可用的网络接口,bind配置监听一个或多个接口。
#监听多个接口时用空格隔开,例如,bind 127.0.0.1 10.0.0.1
bind 127.0.0.1
#开启后禁止公网访问redis。
protected-mode yes
#指定redis server监听的端口号,默认是6379,0是不监听
port 6379
#确定了TCP连接中已完成队列(完成三次握手之后)的长度,此值不许不大于Linux系统定义
#的/proc/sys/net/core/somaxconn值,默认是511,而Linux默认参数值是128。当系统并发
#量大并且客户端速度缓慢的时候,可以将这两个参数一起参考设定
tcp-backlog 511
#指定UNIX socket路径来让redis支持监听本地连接
#unixsocket /tmp/redis.sock
#指定unix socket file的权限
# unixsocketperm 700
#当client闲置多少秒后关闭连接,0为关闭该功能
timeout 0
# 将周期性的使用SO_KEEPALIVE检测客户端是否还处于健康状态,避免服务器阻塞,单位是s
tcp-keepalive 300
################################# GENERAL #####################################
# 守护进程开关
daemonize no
# 可以通过upstart和systemd管理Redis守护进程,这个参数是和具体的操作系统相关的
supervised no
# 配置pid文件路径。当Redis以守护模式启动时,如果没有配置pidfile,pidfile默认值是#/var/run/redis.pid
pidfile /var/run/redis_6379.pid
# 日志级别
# debug (记录大量日志信息,适用于开发测试阶段)
# verbose (较多日志信息)
# notice (适量日志信息,用于生产环境)
# warning (仅记录重要、关键信息)
loglevel notice
# 指定log路径,为空时,为标准输出,守护进程运行时,输出到 /dev/null
logfile ""
# 设置数据库个数。默认的数据库是DB0,可以在每个连接上使用select <dbid>命令选择一个#不同的数据库
databases 16
################################ SNAPSHOTTING ################################
#RDB自动触发条件,save m n,含义是m秒内至少有n个keys发生改变则保存一次
# save ""
save 900 1
save 300 10
save 60 10000
# bgsave过程出现错误是否停止接受写操作
stop-writes-on-bgsave-error yes
# 是否压缩rdb
rdbcompression yes
# 指定rdb file name
dbfilename dump.rdb
# 指定redis 工作目录
dir ./
################################# REPLICATION #################################
# 设置本机为slave
# slaveof <masterip> <masterport>
# 当master设置了密码保护,slave服务连接master的密码.
# masterauth <master-password>
# slave与master断连的时候是否继续响应客户端的request
#
slave-serve-stale-data yes
# slave是否为只读
slave-read-only yes
# 复制同步策略: disk or socket.
#
repl-diskless-sync no
# When diskless replication is enabled, it is possible to configure the delay
# the server waits in order to spawn the child that transfers the RDB via socket
# to the slaves.
#
# This is important since once the transfer starts, it is not possible to serve
# new slaves arriving, that will be queued for the next RDB transfer, so the server
# waits a delay in order to let more slaves arrive.
#
# The delay is specified in seconds, and by default is 5 seconds. To disable
# it entirely just set it to 0 seconds and the transfer will start ASAP.
repl-diskless-sync-delay 5
# 指定slave定期ping master的周期
# repl-ping-slave-period 10
# master 批量数据传输时间或者ping回复持剑间隔
# repl-timeout 60
#指定向slave同步数据时,是否禁用socket的no_delay option。如果是yes,则tcp协议栈#会合并小包统一发送,节省带宽,但会增加数据同步到slave的时间;若为no,则不会延迟
#小包的发送时机,这样数据同步的延时会减少,但需要更大的带宽。
repl-disable-tcp-nodelay no
# slaves被断开连接时存放slave数据的buffer大小
# repl-backlog-size 1mb
# 当master和slave断连多少s之后,情况backlog,设成0时永不清空
# repl-backlog-ttl 3600
# master不能正常工作的时候,redis sentinel会选最高优先级(值最小)作为master
slave-priority 100
# 当健康slave小于该配置值,master就停止写
# min-slaves-to-write 3
#延迟小于该配置s的slave才认为是健康的
# min-slaves-max-lag 10
#
# Setting one or the other to 0 disables the feature.
#
# By default min-slaves-to-write is set to 0 (feature disabled) and
# min-slaves-max-lag is set to 10.
# A Redis master is able to list the address and port of the attached
# slaves in different ways. For example the "INFO replication" section
# offers this information, which is used, among other tools, by
# Redis Sentinel in order to discover slave instances.
# Another place where this info is available is in the output of the
# "ROLE" command of a masteer.
#
# The listed IP and address normally reported by a slave is obtained
# in the following way:
#
# IP: The address is auto detected by checking the peer address
# of the socket used by the slave to connect with the master.
#
# Port: The port is communicated by the slave during the replication
# handshake, and is normally the port that the slave is using to
# list for connections.
#
# However when port forwarding or Network Address Translation (NAT) is
# used, the slave may be actually reachable via different IP and port
# pairs. The following two options can be used by a slave in order to
# report to its master a specific set of IP and port, so that both INFO
# and ROLE will report those values.
#
# There is no need to use both the options if you need to override just
# the port or the IP address.
#
# slave-announce-ip 5.5.5.5
# slave-announce-port 1234
################################## SECURITY ###################################
# 设置redis连接密码
# requirepass foobared
# 命令重命名。如果把某个命令重命名成空字符串就等于取消了这个命令
#
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to slaves may cause problems.
################################### LIMITS ####################################
# 设置最大客户端连接数量,0为不限制
#
# maxclients 10000
#指定Redis最大内存限制,redis达到最大内存后,会先清理已到期或即将到期的key,
#
# maxmemory <bytes>
#内存达到maxmemory时,redis采用哪种策略来减少内存
# volatile-lru -> 利用LRU算法移除设置过过期时间的key(LRU:Least Recently Used)
# allkeys-lru -> 利用LRU算法移除所有key
# volatile-random -> 移除设置过过期时间的随机key
# allkeys-random -> 随机移除key
# volatile-ttl -> 移除即将过期的key
# noeviction -> 不移除任何key,只返回一个错误
# The default is:
#
# maxmemory-policy noeviction
# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default Redis will check five keys and pick the one that was
# used less recently, you can change the sample size using the following
# configuration directive.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs a bit more CPU. 3 is very fast but not very accurate.
#
# maxmemory-samples 5
############################## APPEND ONLY MODE ###############################
#AOF开关.
appendonly no
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
# 硬盘同步策略
# appendfsync always
appendfsync everysec
# appendfsync no
# AOF策略设置为always或者everysec是,后台处理进程(后台保存或者AOF日志重写)会执行
#大量的I/O操作,在某些Linux配置中会阻止过长的fsync()请求。为了捡回这个问题,可以
#设置这个参数为no
no-appendfsync-on-rewrite no
# 自动触发AOF重写的参数
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# AOF不完整的时候,redis启动时否继续加载AOF。
#yes会正常启动;no拒绝启动,返回error,如果此时再想启动redis则需要在启动使用
#"redis-check-aof"工具来修复AOF
aof-load-truncated yes
################################ LUA SCRIPTING ###############################
# Max execution time of a Lua script in milliseconds.
#
# If the maximum execution time is reached Redis will log that a script is
# still in execution after the maximum allowed time and will start to
# reply to queries with an error.
#
# When a long running script exceeds the maximum execution time only the
# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be
# used to stop a script that did not yet called write commands. The second
# is the only way to shut down the server in the case a write command was
# already issued by the script but the user doesn't want to wait for the natural
# termination of the script.
#
# Set it to 0 or a negative value for unlimited execution without warnings.
lua-time-limit 5000
################################ REDIS CLUSTER ###############################
#是否enable集群
# cluster-enabled yes
# 设置集群配置文件名
# cluster-config-file nodes-6379.conf
# 节点相互连接的超时时间
# cluster-node-timeout 15000
# 在failover的时候,全部slave都会请求申请为master,但是有些slave可能与master断开连接
#一段时间了,导致数据过于陈旧,这样的slave不应该提升为master。
#该参数用来判断slave与master断连的时间是否过长。
#
#判断方法是:bijiao slave断连的时间和(node-timeout*slave-validity-factor)+repl-ping-slave-period
#如果节点超时时间设置为30s,并且cluster-slave-validity-factor为10s,假设默认的repl-ping-slave-period
#是10s,即如果超时310s slave将不会尝试进行failover。
#如果设置成0,则无论从节点与主节点失联多久,从节点都会尝试升级为master
# cluster-slave-validity-factor 10
# master需要的最小slave数,只有达到这个数,master失败时,它的从节点才会进行failover
#
# cluster-migration-barrier 1
# 当一定比例的键空间没有覆盖到(就是某一部分的哈希槽没了,有可能是暂时挂断了),集群就停止处理任何
#查询操作。如果该项设置为no,那么就算请求中只有一部分的key可以被查到,一样可以查询(但是有可能不全)
# cluster-require-full-coverage yes
# In order to setup your cluster make sure to read the documentation
# available at http://redis.io web site.
################################## SLOW LOG ###################################
# 执行时间比该值大的请求记录到slowlog里,单位是us。如果<0,会禁用slowlog,0则强制记录所有命令
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
# 延迟监控功能,用来监控redis中执行比较慢的一些操作,用LATENCY打印redis实例在
#跑命令时的好事图表。只记录>=该配置的操作。0的话就是关闭监视
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
# Redis can notify Pub/Sub clients about events happening in the key space.
# This feature is documented at http://redis.io/topics/notifications
#
# For instance if keyspace events notification is enabled, and a client
# performs a DEL operation on key "foo" stored in the Database 0, two
# messages will be published via Pub/Sub:
#
# PUBLISH [email protected]__:foo del
# PUBLISH [email protected]__:del foo
#
# It is possible to select the events that Redis will notify among a set
# of classes. Every class is identified by a single character:
#
# K Keyspace events, published with [email protected]<db>__ prefix.
# E Keyevent events, published with [email protected]<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name [email protected]__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
# 是否发送键空间的改变的事件
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
# hash数据类型使用ziplist编码的阈值
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
# Lists are also encoded in a special way to save a lot of space.
# The number of entries allowed per internal list node can be specified
# as a fixed maximum size or a maximum number of elements.
# For a fixed maximum size, use -5 through -1, meaning:
# -5: max size: 64 Kb <-- not recommended for normal workloads
# -4: max size: 32 Kb <-- not recommended
# -3: max size: 16 Kb <-- probably not recommended
# -2: max size: 8 Kb <-- good
# -1: max size: 4 Kb <-- good
# Positive numbers mean store up to _exactly_ that number of elements
# per list node.
# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),
# but if your use case is unique, adjust the settings as necessary.
#list使用ziplist编码的阈值
list-max-ziplist-size -2
# Lists may also be compressed.
# Compress depth is the number of quicklist ziplist nodes from *each* side of
# the list to *exclude* from compression. The head and tail of the list
# are always uncompressed for fast push/pop operations. Settings are:
# 0: disable all list compression
# 1: depth 1 means "don't start compressing until after 1 node into the list,
# going from either the head or tail"
# So: [head]->node->node->...->node->[tail]
# [head], [tail] will always be uncompressed; inner nodes will compress.
# 2: [head]->[next]->node->node->...->node->[prev]->[tail]
# 2 here means: don't compress head or head->next or tail->prev or tail,
# but compress all nodes between them.
# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail]
# etc.
#list压缩策略
list-compress-depth 0
#se使用intset编码的条件
set-max-intset-entries 512
# zet用ziplist编码的条件
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
# HyperLogLog sparse representation bytes limit. The limit includes the
# 16 bytes header. When an HyperLogLog using the sparse representation crosses
# this limit, it is converted into the dense representation.
#
# A value greater than 16000 is totally useless, since at that point the
# dense representation is more memory efficient.
#
# The suggested value is ~ 3000 in order to have the benefits of
# the space efficient encoding without slowing down too much PFADD,
# which is O(N) with the sparse encoding. The value can be raised to
# ~ 10000 when CPU is not a concern, but space is, and the data set is
# composed of many HyperLogLogs with cardinality in the 0 - 15000 range.
#hyperloglog的值长度小于等于该值的时候用稀疏数据结构(sparse),否则用稠密(dense)
#的数据结构
hll-sparse-max-bytes 3000
# 是否开启rehash,hash扩容
activerehashing yes
# The client output buffer limits can be used to force disconnection of clients
# that are not reading data from the server fast enough for some reason (a
# common reason is that a Pub/Sub client can't consume messages as fast as the
# publisher can produce them).
#
# The limit can be set differently for the three different classes of clients:
#
# normal -> normal clients including MONITOR clients
# slave -> slave clients
# pubsub -> clients subscribed to at least one pubsub channel or pattern
#
# The syntax of every client-output-buffer-limit directive is the following:
#
# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
#
# A client is immediately disconnected once the hard limit is reached, or if
# the soft limit is reached and remains reached for the specified number of
# seconds (continuously).
# So for instance if the hard limit is 32 megabytes and the soft limit is
# 16 megabytes / 10 seconds, the client will get disconnected immediately
# if the size of the output buffers reach 32 megabytes, but will also get
# disconnected if the client reaches 16 megabytes and continuously overcomes
# the limit for 10 seconds.
#
# By default normal clients are not limited because they don't receive data
# without asking (in a push way), but just after a request, so only
# asynchronous clients may create a scenario where data is requested faster
# than it can read.
#
# Instead there is a default limit for pubsub and slave clients, since
# subscribers and slaves receive data in a push fashion.
#
# Both the hard or the soft limit can be disabled by setting them to zero.
#限制分配缓冲区的大小,防止内存无节制的分配
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# redis执行任务的频率为1s除以hz。当redis空闲时,提高该值将使用更多的cpu,但同时,
#当有许多**同时到期时,将使redis响应更敏捷,而超时可以更精确地处理。
hz 10
# AOF重写的时候,如果打开该开关,系统会每32MB执行一次fsync。可以避免过大的延迟峰值
aof-rewrite-incremental-fsync yes