本文目录
1 INCLUDES
多实例的情况可以把公用的配置文件提取出来,如果配置文件有相同值,后面的会覆盖前面的:
# Notice option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf
2 NETWORK 网络配置相关
2.1 bind
默认情况bind=127.0.0.1只能接受本机的访问请求不写的情况下,无限制接受任何ip地址的访问。
生产环境肯定要写你应用服务器的地址;服务器是需要远程访问的,所以需要将其注释掉。
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
如果开启了protected-mode,那么在没有设定bind ip且没有设密码的情况下,Redis只允许接受本机的响应
2.2 protected-mode yes
保护模式,如果保护模式开了,而且redis既没有bind ip,也没设置密码,那redis只接收127.0.0.1的连接。 默认都开
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes
2.3 port
默认端口号为3306
2.4 tcp-backlog
设置tcp的backlog,backlog其实是一个连接队列,backlog队列总和=未完成三次握手队列 + 已经完成三次握手队列。
在高并发环境下你需要一个高backlog值来避免慢客户端连接问题。
# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511
2.5 timeout
一个空闲的客户端维持多少秒会关闭,0表示关闭该功能。即永不关闭
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
2.6 tcp-keepalive
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300
对访问客户端的一种心跳检测,每个n秒检测一次。单位为秒,如果设置为0,则不会进行Keepalive检测
3 GENERAL通用
3.1 daemonize
是否为后台进程,设置为yes,守护进程,后台启动
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
3.2 pidfile
存放pid文件的位置,每个实例会产生一个不同的pid文件
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid
3.3 loglevel
指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为notice
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
3.4 logfile
日志文件名称,默认为空
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""
3.5 databases 16
设定库的数量,默认16个,默认数据库为0,可以使用SELECT 命令在连接上指定数据库id
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16
4 SECURITY安全
4.1 requirepass
设置密码。
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
5 CLIENTS 客户端
5.1 maxclients 10000
-
设置redis同时可以与多少个客户端进行连接。
-
默认情况下为10000个客户端。
-
如果达到了此限制,redis则会拒绝新的连接请求,并且向这些连接请求方发出“max number of clients reached”以作回应。
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# maxclients 10000
6 MEMORY MANAGEMENT内存管理
6.1 maxmemory
-
建议必须设置,否则,将内存占满,造成服务器宕机
-
设置redis可以使用的内存量。一旦到达内存使用上限,redis将会试图移除内部数据,移除规则可以通过maxmemory-policy来指定。
-
如果redis无法根据移除规则来移除内存中的数据,或者设置了“不允许移除”,那么redis则会针对那些需要申请内存的指令返回错误信息,比如SET、LPUSH等。
-
但是对于无内存申请的指令,仍然会正常响应,比如GET等。如果你的redis是主redis(说明你的redis有从redis),那么在设置内存使用上限时,需要在系统中留出一些内存空间给同步队列缓存,只有在你设置的是“不移除”的情况下,才不用考虑这个因素。
# In short... if you have replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
6.2 maxmemory-policy
内存淘汰策略,决定了当redis内存满时如何删除key。 默认值是noeviction。
-
volatile-lru:使用LRU算法移除key,只对设置了过期时间的键;(最近最少使用)
-
allkeys-lru:在所有集合key中,使用LRU算法移除key
-
volatile-random:在过期集合中移除随机的key,只对设置了过期时间的键
-
allkeys-random:在所有集合key中,移除随机的key
-
volatile-ttl:移除那些TTL值最小的key,即那些最近要过期的key
-
noeviction:不进行移除。针对写操作,只是返回错误信息
# The default is:
#
# maxmemory-policy noeviction
6.3 maxmemory-samples
-
设置样本数量,LRU算法和最小TTL算法都并非是精确的算法,而是估算值,所以你可以设置样本的大小,redis默认会检查这么多个key并选择其中LRU的那个。
-
一般设置3到7的数字,数值越小样本越不准确,但性能消耗越小
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs more CPU. 3 is faster but not very accurate.
#
# maxmemory-samples 5
7 Redis持久化之RDB
在指定的时间间隔内将内存中的数据集快照写入磁盘, 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里。
RDB持久化流程
Redis会单独创建(fork)一个子进程来进行持久化,会先将数据写入到 一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。 整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能 如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失**。
Redis默认持久化策略是RDB
7.1 Save (快照配置)
RDB是整个内存的压缩过的Snapshot,RDB的数据结构,可以配置复合的快照触发条件。
默认是1分钟内改了1万次,或5分钟内改了10次,或15分钟内改了1次
# Save the DB on disk:
#
# save <seconds> <changes>
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# save ""
# 表示15分钟(900秒钟)内至少1个键被更改则进行快照。
save 900 1
# 表示5分钟(300秒)内至少10个键被更改则进行快照。
save 300 10
# 表示1分钟内至少10000个键被更改则进行快照。
save 60 10000
命令显式触发快照备份:
127.0.0.1:6379> bgsave
7.2 stop-writes-on-bgsave-error
当Redis无法写入磁盘的话,直接关掉Redis的写操作。推荐yes.
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes
7.3 rdbcompression 压缩文件
对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。
如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能。推荐yes.
# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes
7.4 rdbchecksum 检查完整性
在存储快照后,还可以让redis使用CRC64算法来进行数据校验,
但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能,推荐yes。
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes
7.5 dbfilename 存储名称
默认为dump.rdb
# The filename where to dump the DB
dbfilename dump.rdb
7.6 存储位置
rdb文件的保存路径,也可以修改。默认为Redis启动时命令行所在的目录下
dir ./
7.7 RDB的备份与恢复
先通过config get dir
查询rdb文件的目录 ,将*.rdb的文件拷贝到别的地方
rdb的恢复
-
关闭Redis
-
先把备份的文件拷贝到工作目录下
-
启动Redis, 备份数据会直接加载
7.8 RDB优势与劣势
RDB优势
-
适合大规模的数据恢复
-
对数据完整性和一致性要求不高更适合使用
-
节省磁盘空间
-
恢复速度快
RDB劣势
-
Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
-
虽然Redis在fork时使用了写时拷贝技术,但是如果数据庞大时还是比较消耗性能。
-
在备份周期在一定间隔时间做一次备份,所以如果Redis意外down掉的话,就会丢失最后一次快照后的所有修改。
8 Redis持久化之AOF
8.1 AOF持久化流程
AOF文件中存储的是redis的命令,同步命令到 AOF 文件的整个过程可以分为三个阶段:
- 命令传播:Redis 将执行完的命令、命令的参数、命令的参数个数等信息发送到 AOF 程序中。
- 缓存追加:AOF 程序根据接收到的命令数据,将命令转换为网络通讯协议的格式,然后将协议内容追加 到服务器的 AOF 缓存中。
- 文件写入和保存:AOF 缓存中的内容被写入到 AOF 文件末尾,如果设定的 AOF 保存条件被满足的话, fsync 函数或者 fdatasync 函数会被调用,将写入的内容真正地保存到磁盘中。
AOF和RDB同时开启,系统默认取AOF的数据
8.2 AOF 重写
AOF记录数据的变化过程,越来越大,需要重写“瘦身”。 Redis可以在 AOF体积变得过大时,自动地在后台(Fork子进程)对 AOF进行重写。重写后的新 AOF文 件包含了恢复当前数据集所需的最小命令集合。 所谓的“重写”其实是一个有歧义的词语, 实际上, AOF 重写并不需要对原有的 AOF 文件进行任何写入和读取, 它针对的是数据库中键的当前值。
举例如下:
set s1 11
set s1 22
set s1 33
没有优化的:
set s1 11
set s1 22
set s1 33
优化后:
set s1 33
lpush list1 1 2 3
lpush list1 4 5 6 -------- > list1 1 2 3 4 5 6
优化后
lpush list1 1 2 3 4 5 6
8.3 开启AOF
AOF默认不开启,开启AOF则修改默认的appendonly no,改为yes
appendonly yes
8.4 AOF同步频率设置
-
appendfsync always
始终同步,每次Redis的写入都会立刻记入日志;性能较差但数据完整性比较好
-
appendfsync everysec
默认方式每秒同步,每秒记入日志一次,如果宕机,本秒的数据可能丢失。
-
appendfsync no
redis不主动进行同步,把同步时机交给操作系统
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec
# appendfsync no
8.5 no-appendfsync-on-rewrite 重写方式
-
no-appendfsync-on-rewrite no
默认方式如果
no-appendfsync-on-rewrite=no
, 还是会把数据往磁盘里刷,但是遇到重写操作,可能会发生阻塞。(数据安全,但是性能降低) -
no-appendfsync-on-rewrite yes
如果
no-appendfsync-on-rewrite=yes
,不写入aof文件只写入缓存,用户请求不会阻塞,但是在这段时间如果宕机会丢失这段时间的缓存数据。(降低数据安全性,提高性能)
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.
no-appendfsync-on-rewrite no
8.6 触发机制,何时重写?
Redis会记录上次重写时的AOF大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发。
重写虽然可以节约大量磁盘空间,减少恢复时间。但是每次重写还是有一定的负担的,因此设定Redis要满足一定条件才会进行重写。
参数介绍:
-
auto-aof-rewrite-percentage 100
表示当前aof文件大小超过上一次aof文件大小的百分之多少的时候会进行重写。如果之前没有重写过,以 启动时aof文件大小为准
-
auto-aof-rewrite-min-size
限制允许重写最小aof文件大小,也就是文件大小,小于64mb的时候,不需要进行优化
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature.
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
执行bgrewriteaof
命令 手动重写
127.0.0.1:6379> bgrewriteaof
8.7 aof-use-rdb-preamble 混合持久化
RDB和AOF各有优缺点,Redis 4.0 开始支持 rdb 和 aof 的混合持久化。如果把混合持久化打开,aof rewrite 的时候就直接把 rdb 的内容写到 aof 文件开头。 RDB的头+AOF的身体---->appendonly.aof 开启混合持久
# When loading Redis recognizes that the AOF file starts with the "REDIS"
# string and loads the prefixed RDB file, and continues loading the AOF
# tail.
aof-use-rdb-preamble yes
8.6 AOF 修复/恢复
-
AOF的备份机制和性能虽然和RDB不同, 但是备份和恢复的操作同RDB一样,都是拷贝备份文件,需要恢复时再拷贝到Redis工作目录下,启动系统即加载。
-
正常恢复
- 修改默认的appendonly no,改为yes
- 将有数据的aof文件复制一份保存到对应目录(查看目录:config get dir)
- 恢复:重启redis然后重新加载
-
异常恢复
- 修改默认的appendonly no,改为yes
- 如遇到AOF文件损坏,通过/usr/local/bin/redis-check-aof–fix appendonly.aof进行恢复
- 备份被写坏的AOF文件
- 恢复:重启redis,然后重新加载
8.7 AOF优劣势
优势:
-
备份机制更稳健,丢失数据概率更低。
-
可读的日志文本,通过操作AOF稳健,可以处理误操作。
劣势
-
比起RDB占用更多的磁盘空间。
-
恢复备份速度要慢。
-
每次读写都同步的话,有一定的性能压力。
-
存在个别Bug,造成不能恢复。
8.8 AOF与RDB选择哪一个
- 内存数据库 rdb+aof 数据不容易丢
- 缓存服务器 rdb 性能高
- 不建议 只使用 aof (性能差)
- 在数据还原时 有rdb+aof 则还原aof,因为RDB会造成文件的丢失,AOF相对数据要完整。
- 只有rdb,则还原rdb