【Redis运维】Redis 6.x配置文件示例与说明

TangLu Redis 2021-05-28 2032 0
bind 172.20.1.136
protected-mode yes
port 6379
tcp-backlog 511
timeout 300              #客户端连接最大空闲时间,超出这个空闲时长的连接将被自动关闭
tcp-keepalive 300        #通过 TCP 层面保持连接有效性,防止网络不稳定导致连接中断
lua-time-limit 5000
daemonize yes
databases 16
maxclients 10000
pidfile /data/redis/redis_6379.pid
loglevel notice
logfile "/data/redis/logs/redis.log"
slowlog-log-slower-than 100000
slowlog-max-len 1000
maxmemory 4G
maxmemory-policy volatile-lfu        #指定内存淘汰策略
maxmemory-samples 5                  #内存淘汰策略的精度,5为中间值,10的话是最高值,会消耗更多的CPU以取得更精确的LRU算法结果
maxmemory-eviction-tenacity 10
requirepass a5791E2a3043T 

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 0 0 0  
client-output-buffer-limit pubsub 32mb 8mb 60
client-query-buffer-limit 1gb

lazyfree-lazy-eviction yes      #内存达到maxmemory并设置了淘汰策略时是否尝试异步释放内存
lazyfree-lazy-expire yes        #key在过期删除时是否尝试异步释放内存
lazyfree-lazy-server-del yes    #执行RENAME/MOVE等命令或需要覆盖一个key时,删除旧key是否尝试异步释放内存
replica-lazy-flush yes          #主从执行全量同步时,从库在一开始清空当前数据库时是否异步释放内存
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no    

jemalloc-bg-thread yes
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
disable-thp yes
hz 10
dynamic-hz yes
io-threads 6                  #线程个数,建议小于CPU个数,比如8C的机器配置为6
io-threads-do-reads yes       #多线程默认为no关闭,如果CPU开销不大建议开启,通过加速网络处理性能从而提升吞吐量

# notify-keyspace-events ""  #通过订阅实现事件通知,比如有key过期、有key淘汰等
# syslog-enabled no
# syslog-ident redis
# syslog-facility local0
# crash-log-enabled no
# crash-memcheck-enabled no

############################### ADVANCED CONFIG ###############################
activerehashing yes
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
proto-max-bulk-len 512mb


################################ RDB  ################################
# save ""
# save 3600 1
# save 300 100
# save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
rdb-save-incremental-fsync yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /data/redis/rdb_file/


############################## AOF ###############################
# appendonly no
# appendfilename "appendonly.aof"
# appendfsync always
# appendfsync everysec
# appendfsync no
# no-appendfsync-on-rewrite no
# auto-aof-rewrite-percentage 100
# auto-aof-rewrite-min-size 64mb
# aof-rewrite-incremental-fsync yes
# aof-load-truncated yes
# aof-use-rdb-preamble yes  #开启混合持久化


################################# 主从复制 #################################
replicaof 172.20.1.135 6379
masterauth a5791E2a3043T 
# masteruser <username>

replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes         #开启无盘复制,主从全量同步时主库并不会在本地创建RDB文件,而是创建一个子进程通过Socket将RDB文件写入到从服务器,节约IO资源
repl-diskless-sync-delay 5     #开启无盘复制时,主节点等待秒数,如果超过这个时间没有更多从节点提交连接申请就开始传输数据,后续请求需要排队。好比公交车发车前等待一会儿,后续人得等下一班
repl-diskless-load on-empty-db
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no    #是否用nodelay方式传输数据,no可以降低数据传输到从库的延迟,但使用更多的带宽
repl-backlog-size 100mb        #增量复制时数据积压缓冲区大小,主从发生中断后,主库会把离线之后产生的数存储在该区域,避免短时间断开服务却进行全量同步的问题
repl-backlog-ttl 3600          #当所有从库都与主库断开连接后达到多少秒释放backlog
replica-priority 100           #从库优先级,数字越小优先级越高,0代表不会被哨兵选为主
replica-ignore-maxmemory yes   #从Redis5开始,从节点默认忽略自身maxmemory配置,除非发生故障转移后升级为主服务器。即只有主节点才会执行过期淘汰策略,并且master执行del操作后从节点也会执行相同操作,保证主从数据一致性
#min-replicas-to-write 3       #如果从库少于N个,主库就停止写入,需配合min-slaves-max-lag一起使用。比如至少需要3个从库、并且延时小于等于10秒的,主库才能写入
#min-replicas-max-lag 10       #如果从库延迟小于N秒,主库才能写入数据,需配合min-slaves-to-write一起使用。比如至少需要3个从库、并且延时小于等于10秒的,主库才能写入

# 从库上报信息给主库时用到的IP和端口,若启用了端口转发或者NAT才需要设置
# replica-announce-ip 5.5.5.5  
# replica-announce-port 1234


################################# 活动碎片整理 #################################
# activedefrag yes                    #开启动态碎片整理(该功能可以动态开启,无需重启服务),当服务运行过程中同时满足下面两个条件才会进行清理
# active-defrag-ignore-bytes 100mb    #条件1,碎片达到100M就开始清理
# active-defrag-threshold-lower 10    #条件2,碎片空间占操作系统分配给 Redis 的总空间比例达到 10% 时,开始清理
# active-defrag-threshold-upper 100   #内存碎片率超过 100%,尽最大努力碎片整理
# active-defrag-cycle-min 1           #清理过程所用CPU时间比例不低于1%,保证清理能正常开展
# active-defrag-cycle-max 25          #清理过程所用CPU时间比例不高于25%,一旦超过就停止清理,避免清理时发生大量内存拷贝阻塞Redis
# active-defrag-max-scan-fields 1000  #碎片整理期间,对于 List/Set/Hash/ZSet 类型元素一次 Scan 的数量


################################ REDIS CLUSTER  ###############################
# cluster-enabled yes
# cluster-config-file nodes-6379.conf
# cluster-node-timeout 15000
# cluster-replica-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
# cluster-replica-no-failover no


################################# ACL #################################
# Redis ACL users are defined in the following format:
#   user <username> ... acl rules ...

# For example:
#   user worker +@list +@connection ~jobs:* on >ffa9203c493aa99
#   user alice on +@all -DEBUG ~* >somepassword
#   user alice on -DEBUG +@all ~* >somepassword
acllog-max-len 128
# aclfile /etc/redis/users.acl


通过命令 memory purge 手动清理内存碎片

 redis-cli -p 6389 memory purge

异步FLUSH清理整个实例或DB

127.0.0.1:6379> flushall async


评论