`

redis 配置文件

 
阅读更多
# Redis configuration file example

################################## INCLUDES ###################################


# 引用其它配置文件(例如共用的)如果需要用被引用配置文件覆盖本文件配置,最好把引用行放到本文件最后
# include /path/to/local.conf
# include /path/to/other.conf

################################ GENERAL  #####################################


#是否做为守护进程运行 ,no/yes
daemonize no

#pidfile 保存路径,redis启动时会生成pidfile,可用于检测重复启动,多redis server 时需指定不同的pidfile
pidfile /var/run/redis.pid

# 端口号
port 6379

# TCP listen() backlog.
#在高速请求环境中,为了避免客户端连接缓慢问题,需要设置更大的backlog,linux系统默认为/proc/sys/net/core/somaxconn=128,
#backlog意义为:已经完成三次握手、已经成功建立连接的套接字队列的长度
#本值大于系统默认值时,需要修改扩大linux系统默认为/proc/sys/net/core/somaxconn,否存该值会被系统默认值覆盖。 
#修改/proc/sys/net/core/somaxconn方式:sysctl -w net.core.somaxconn=65535,sysctl -p生效
tcp-backlog 511

# Examples:
# 默认情况下,redis 在 server 上所有有效的网络接口上监听客户端连接。
# 你如果只想让它在一个网络接口上监听,那你就绑定一个IP或者多个IP。
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1

# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 700


# 设置客户端连接空闲多少秒后,断开连接。 0为永不超时
timeout 0

# TCP keepalive.

# 如果值非0,单位是秒,表示将周期性的使用SO_KEEPALIVE检测客户端是否还处于健康状态,避免服务器一直阻塞,官方给出的建议值是60S。
tcp-keepalive 0

# Specify the server verbosity level.

# 日志级别:日志详细程度 degug >verbose>notic>warning 
loglevel notice

# 指定日志文件的位置
logfile ""



#设置数据库数量,可能通过 select <dbid>命令选择数据库, dibi 由0至设定值-1
databases 16

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:

# 保存数据到数据库文件
# 格式:save <seconds> <changes> ,在多少秒内发生了多少个key的改变,就触发写入数据库文件
#   save 900 1  900 秒内至少有 1 个 key 被改变
#   save 300 10  300 秒内至少有 300 个 key 被改变
#   save 60 10000  60 秒内至少有 10000 个 key 被改变
save 900 1
save 300 10
save 60 10000


# 默认情况下,redis写入数据库文件失败时,redis停止接收写操作,这样能使用户知道持久化失败,当redis 可以正常持久化了,也就允许写操作了
stop-writes-on-bgsave-error yes


#持久化时是否压缩数据
rdbcompression yes


# 是否校验数据库rdb文件
rdbchecksum yes

# 数据库文件名
dbfilename dump.rdb

#数据库文件路径
dir ./

################################# REPLICATION #################################


# 配置本redis server 为某一个redis server 实便的从数据库,masterip 主库IP,masterport主库端口
# 从库自动从主库同步数据
# slaveof <masterip> <masterport>


#  当 redis 主库设置了密码保护时配置主库的密码
# masterauth <master-password>


#  当从库同主机失去连接或者复制正在进行,从机库有两种运行方式:
# 1)  如果 slave-serve-stale-data 设置为 yes( 默认设置 ) ,从库会继续响应客户端的请求
# 2)  如果 slave-serve-stale-data 是指为 no ,出去 INFO 和 SLAVOF 命令之外的任何请求都会返回一个
#     错误 "SYNC with master in progress"
slave-serve-stale-data yes

#从库是否为只读
slave-read-only yes

 
#从库会按照一个时间间隔向主库发送PINGs,默认是10秒
# repl-ping-slave-period 10

# The following option sets the replication timeout for:

#
#设置主库批量数据传输时间或者 ping 回复时间间隔,默认值是 60 秒
#一定要确保 repl-timeout 大于 repl-ping-slave-period
# repl-timeout 60


#	在 slave socket 的 SYNC 后禁用 TCP_NODELAY
#  如果选择“ yes ” ,Redis 将使用一个较小的数字 TCP 数据包和更少的带宽将数据发送到 slave , 
#				但是这可能导致数据发送到 slave 端会有延迟 , 如果是 Linux kernel 的默认配置,会达到 40 毫秒 .
#  如果选择 "no" ,则发送数据到 slave 端的延迟会降低,但将使用更多的带宽用于复制 .
repl-disable-tcp-nodelay no

#用于主从间闪断的情况下,从连到主从后,从主缓冲区获取闪断时间内的更新命令,从而同步主从库

# repl_backlog是redis用于存储更新命令的一块buffer,在部分复制的时候Slave会请求Master
#从这块buffer中获取闪断情况下丢失的更新操作。repl_backlog在redis启动的时候初始化为NULL,
#当有Slave连接上来的时候,会被指向创建的buffer,默认为1024*1024(即1Mb)。
#repl_backlog_size表示该buffer的大小(默认1024*1024,即1Mb)。
#该buffer是作为一个环形缓存区使用的,当有数据超过buffer的大小以后就会重新从buffer的头部开始写入。
#repl_backlog_idx表示当前缓存数据的尾部(因为是环形buffer)。repl_backlog_off是全局缓存的偏移量,
#从开始缓存数据起一直在增长。如果Master一个Slave都没有,则超过一段时间以后repl_backlog会被释放,默认超时时间是1小时。
# 
#从库有一个 cach_master 缓存,是主库repl_backlog缓存的镜像, Slave与Master完全同步以后,
#maste的更新命令会被存到repl_backlog中,同时不断更新偏移量等相关变量。这些更新命令不断地被
#发送到Slave端,Slave也随之更改自己记录的偏移量。当期间再次有网络断开的情况,Slave会根据记录的
#runid(哪个主库)和reploff向Master请求部分复制,Master检查Slave请求的偏移量对应的内容是否还在
#repl_backlog中,即比较repl_backlog_off和Slave传递过来的reploff的值的差是否小于等于repl_backlog中
#实际数据的长度,如果满足条件则将这部分内容发送给Slave,部分复制完成。否则让Slave进行全量复制。
# repl-backlog-size 1mb


# 主库的repl_backlog在没有Slave的情况下过多久再释放的时间阈值()
# repl-backlog-ttl 3600

#如果 master 不能再正常工作,那么会在多个 slave 中,
#选择优先值最小的一个 slave 提升为 master ,优先值为 0 表示不能提升为 master 。
slave-priority 100


#  如果少于 N 个 slave 连接,且延迟时间 <=M 秒,则 master 可配置停止接受写操作。
#  例如需要至少 3 个 slave 连接,且延迟 <=10 秒的配置:
# 默认 min-slaves-to-write 为 0 (禁用), min-slaves-max-lag 为 10
# min-slaves-to-write 3
# min-slaves-max-lag 10
 
################################## SECURITY ###################################


#设置客户端连接后进行任何其他指定前需要使用的密码。
#警告:因为 redis 速度相当快,所以在一台比较好的服务器下,
#一个外部的用户可以在一秒钟进行 150K 次的密码尝试,这意味着你需要指定非常非常强大的密码来防止暴力破解
# requirepass foobared

# 命令重命名 .
#  在一个共享环境下可以重命名相对危险的命令。比如把 CONFIG 重名为一个不容易猜测的字符。
#  举例 :
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
#如果想删除一个命令,直接把它重命名为一个空字符 "" 即可,如下:
# rename-command CONFIG ""


################################### LIMITS ####################################

#设置同一时间最大客户端连接数,默认无限制, 
#Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,
#如果设置  maxclients 0 ,表示不作限制。
#当客户端连接数到达限制时, Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息
#
# maxclients 10000

#  指定 Redis 最大内存限制, Redis 在启动时会把数据加载到内存中,达到最大内存后, Redis 会按照清除策略尝试清除已到期的 Key
#  如果 Redis 依照策略清除后无法提供足够空间,或者策略设置为 ”noeviction” ,则使用更多空间的命令将会报错,例如 SET, LPUSH 等。但仍然可以进行读取操作
#  注意: Redis 新的 vm 机制,会把 Key 存放内存, Value 会存放在 swap 区
#  该选项对 LRU 策略很有用。
# maxmemory 的设置比较适合于把 redis 当作于类似 memcached 的缓存来使用,而不适合当做一个真实的 DB 。
#  当把 Redis 当做一个真实的数据库使用的时候,内存使用将是一个很大的开销
# maxmemory <bytes>

#内存策略:如果达到内存限制了,Redis如何删除key。你可以在下面五个策略里面选:
# 
# volatile-lru -> 根据LRU算法生成的过期时间来删除。
# allkeys-lru -> 根据LRU算法删除任何key。
# volatile-random -> 根据过期设置来随机删除key。
# allkeys->random -> 无差别随机删。
# volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
# noeviction -> 谁也不删,直接在写操作时返回错误。
# 
# 注意:对所有策略来说,如果Redis找不到合适的可以删除的key都会在写操作时返回一个错误。
# maxmemory-policy volatile-lru

# LRU和最小TTL算法的实现都不是很精确,但是很接近(为了省内存),所以你可以用样例做测试。
# 例如:默认Redis会检查三个key然后取最旧的那个,你可以通过下面的配置项来设置样本的个数。
# maxmemory-samples 3

############################## APPEND ONLY MODE ###############################

# 默认情况下,Redis是异步的把数据导出到磁盘上。这种情况下,当Redis挂掉的时候,最新的数据就丢了。
# 如果不希望丢掉任何一条数据的话就该用纯累加模式:一旦开启这个模式,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件。
# 每次启动时Redis都会把这个文件的数据读入内存里。
#
# 注意,异步导出的数据库文件和纯累加文件可以并存(你得把上面所有"save"设置都注释掉,关掉导出机制)。
# 如果纯累加模式开启了,那么Redis会在启动时载入日志文件而忽略导出的 dump.rdb 文件。
#
# 重要:查看 BGREWRITEAOF 来了解当累加日志文件太大了之后,怎么在后台重新处理这个日志文件。  
appendonly no

#  APPEND ONLY模式的文件名
appendfilename "appendonly.aof"

# fsync() 请求操作系统马上把数据写到磁盘上,不要再等了。
# 有些操作系统会真的把数据马上刷到磁盘上;有些则要磨蹭一下,但是会尽快去做。
#
# Redis支持三种不同的模式:
#
# no:不要立刻刷,只有在操作系统需要刷的时候再刷。比较快。
# always:每次写操作都立刻写入到aof文件。慢,但是最安全。
# everysec:每秒写一次。折衷方案。
#
# 默认的 "everysec" 通常来说能在速度和数据安全性之间取得比较好的平衡。
# 如果你真的理解了这个意味着什么,那么设置"no"可以获得更好的性能表现(如果丢数据的话,则只能拿到一个不是很新的快照);
# 或者相反的,你选择 "always" 来牺牲速度确保数据安全、完整。
#
# 如果拿不准,就用 "everysec"
# appendfsync always
appendfsync everysec
# appendfsync no


# 如果AOF的同步策略设置成 "always" 或者 "everysec",那么后台的存储进程(后台存储或写入AOF日志)会产生很多磁盘I/O开销。
# 某些Linux的配置下会使Redis因为 fsync() 而阻塞很久。
# 注意,目前对这个情况还没有完美修正,甚至不同线程的 fsync() 会阻塞我们的 write(2) 请求。
#
# 为了缓解这个问题,可以用下面这个选项。它可以在 BGSAVE 或 BGREWRITEAOF 处理时阻止 fsync()。
# 
# 这就意味着如果有子进程在进行保存操作,那么Redis就处于"不可同步"的状态。
# 这实际上是说,在最差的情况下可能会丢掉30秒钟的日志数据。(默认Linux设定)
# 
# 如果你有延迟的问题那就把这个设为 "yes",否则就保持 "no",这是保存持久数据的最安全的方式。

no-appendfsync-on-rewrite no

# 自动重写AOF文件
#
# 如果AOF日志文件大到指定百分比,Redis能够通过 BGREWRITEAOF 自动重写AOF日志文件。
# 
# 工作原理:Redis记住上次重写时AOF日志的大小(或者重启后没有写操作的话,那就直接用此时的AOF文件),
# 基准尺寸和当前尺寸做比较。如果当前尺寸超过指定比例,就会触发重写操作。
#
# 你还需要指定被重写日志的最小尺寸,这样避免了达到约定百分比但尺寸仍然很小的情况还要重写。
#
# 指定百分比为0会禁用AOF自动重写特性

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb



aof-load-truncated yes

################################ LUA SCRIPTING  ###############################

 
# 一个Lua脚本最长的执行时间为5000毫秒(5秒),如果为0或负数表示无限执行时间。
lua-time-limit 5000

################################## SLOW LOG ###################################

# Redis慢查询日志可以记录超过指定时间的查询。运行时间不包括各种I/O时间。
# 例如:连接客户端,发送响应数据等。只计算命令运行的实际时间(这是唯一一种命令运行线程阻塞而无法同时为其他请求服务的场景)
# 
# 你可以为慢查询日志配置两个参数:一个是超标时间,单位为微妙,记录超过个时间的命令。
#
# 下面的时间单位是微秒,所以1000000就是1秒。注意,负数时间会禁用慢查询日志,而0则会强制记录所有命令。
slowlog-log-slower-than 10000

# 慢查询日志长度。当一个新的命令被写进日志的时候,最老的那个记录会被删掉。
slowlog-max-len 128

################################ LATENCY MONITOR ##############################

 
latency-monitor-threshold 0

############################# Event notification ##############################

#因为开启键空间通知功能需要消耗一些 CPU , 所以在默认配置下, 该功能处于关闭状态。
#notify-keyspace-events 的参数可以是以下字符的任意组合, 它指定了服务器该发送哪些类型的通知:

#字符	发送的通知
#K	键空间通知,所有通知以 __keyspace@<db>__ 为前缀
#E	键事件通知,所有通知以 __keyevent@<db>__ 为前缀
#g	DEL 、 EXPIRE 、 RENAME 等类型无关的通用命令的通知
#$	字符串命令的通知
#l	列表命令的通知
#s	集合命令的通知
#h	哈希命令的通知
#z	有序集合命令的通知
#x	过期事件:每当有过期键被删除时发送
#e	驱逐(evict)事件:每当有键因为 maxmemory 政策而被删除时发送
#A	参数 g$lshzxe 的别名
#输入的参数中至少要有一个 K 或者 E , 否则的话, 不管其余的参数是什么, 都不会有任何通知被分发。

#举个例子, 如果只想订阅键空间中和列表相关的通知, 那么参数就应该设为 Kl , 诸如此类。

#将参数设为字符串 "AKE" 表示发送所有类型的通知。
notify-keyspace-events ""

############################### ADVANCED CONFIG ###############################

# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

# Similarly to hashes, small lists are also encoded in a special way in order
# to save a lot of space. The special representation is only used when
# you are under the following limits:
list-max-ziplist-entries 512
list-max-ziplist-value 64

# Sets have a special encoding in just one case: when a set is composed
# of just strings that happens to be integers in radix 10 in the range
# of 64 bit signed integers.
# The following configuration setting sets the limit in the size of the
# set in order to use this special memory saving encoding.
set-max-intset-entries 512

# Similarly to hashes and lists, sorted sets are also specially encoded in
# order to save a lot of space. This encoding is only used when the length and
# elements of a sorted set are below the following limits:
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.
hll-sparse-max-bytes 3000

# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
# order to help rehashing the main Redis hash table (the one mapping top-level
# keys to values). The hash table implementation Redis uses (see dict.c)
# performs a lazy rehashing: the more operation you run into a hash table
# that is rehashing, the more rehashing "steps" are performed, so if the
# server is idle the rehashing is never complete and some more memory is used
# by the hash table.
# 
# The default is to use this millisecond 10 times every second in order to
# active rehashing the main dictionaries, freeing memory when possible.
#
# If unsure:
# use "activerehashing no" if you have hard latency requirements and it is
# not a good thing in your environment that Redis can reply form time to time
# to queries with 2 milliseconds delay.
#
# use "activerehashing yes" if you don't have such hard requirements but
# want to free memory asap when possible.
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 calls an internal function to perform many background tasks, like
# closing connections of clients in timeout, purging expired keys that are
# never requested, and so forth.
#
# Not all tasks are performed with the same frequency, but Redis checks for
# tasks to perform accordingly to the specified "hz" value.
#
# By default "hz" is set to 10. Raising the value will use more CPU when
# Redis is idle, but at the same time will make Redis more responsive when
# there are many keys expiring at the same time, and timeouts may be
# handled with more precision.
#
# The range is between 1 and 500, however a value over 100 is usually not
# a good idea. Most users should use the default of 10 and raise this up to
# 100 only in environments where very low latency is required.
hz 10

# When a child rewrites the AOF file, if the following option is enabled
# the file will be fsync-ed every 32 MB of data generated. This is useful
# in order to commit the file to the disk more incrementally and avoid
# big latency spikes.
aof-rewrite-incremental-fsync yes

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics