1 基础配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 cd /usr/local/redis./bin/redis-server redis.node1.6379.conf include /usr/local/redis/redis-pub.conf
2 网络配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 bind 0.0.0.0port 6379 protected-mode yes timeout 0tcp-keepalive 300 tcp-backlog 511
3 通用配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 daemonize yes supervised no pidfile /usr/local/redis/redis.noed1.6379.pid loglevel notice logfile "/usr/local/redis/logs/redis.node1.6379.log" databases 16 always-show-logo yes
4 快照配置(RDB 持久化) RDB 持久化,即按频率规则配置,定期将数据快照写入磁盘,格式为 rdb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb rdb-del-sync-files no dir /usr/local/redis/data/
5 AOF 持久化 定期将所有指令写到磁盘,默认关闭。作为 rdb 的辅助,最大程度避免数据丢失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes
6 安全配置
7 客户端配置
8 内存管理配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 maxmemory 1gb maxmemory-policy allkeys-lru maxmemory-samples 5 replica-ignore-maxmemory yes active-expire-effort 1
9 惰性释放配置 惰性释放(懒删除),即先摘除 key 的索引,实际删除操作使用异步线程去执行。在惰性删除机制之前,如一个元素非常多的集合,直接删除会很低效,导致其它连接发起的指令阻塞。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 azyfree-lazy-eviction no lazyfree-lazy-expire yes lazyfree-lazy-server-del yes replica-lazy-flush no lazyfree-lazy-user-del no
10 多线程配置
6.0 后,Redis 开始引入多线程配置,但不是真正多线程读写内存数据,执行指令扔是单线程顺序执行。多线程部分只是优化网络 I/O,
1 2 3 4 5 io-threads-do-reads no io-threads 4
11 主从复制 实践中一般不单独使用主从复制,需要配合哨兵模式,便于故障后自动选新 master 节点。
实践中的高可用方式,一般是 主从+哨兵 ,或者 Cluster集群模式 ,以及三方中间件代理,类似数据分片中间件,如 codis。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 replicaof masterIp masterPort masterauth masterPwd replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-diskless-load disabled repl-ping-replica-period 10 repl-timeout 60 repl-disable-tcp-nodelay no repl-backlog-size 1mb repl-backlog-ttl 3600 replica-priority 100 min-replicas-to-write 3 min-replicas-max-lag 10 replica-announce-ip 127.0.0.1 replica-announce-port 6379
12 哨兵配置 哨兵是配合主从复制使用的,动态管理各个节点主从关系。
部署参考: RedisSentinel笔记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 cd /usr/local/redis./bin/redis-sentinel sentinel-node1.conf daemonize yes pidfile /usr/local/redis/cluster/sentinel/node01.pid logfile "/usr/local/redis/cluster/sentinel/log/node01.log" dir "/usr/local/redis/cluster" bind 0.0.0.0port 4001 sentinel monitor masterName masterIp masterPort 2 sentinel down-after-milliseconds 60000 sentinel failover-timeout masterName 180000 sentinel parallel-syncs masterName 3 sentinel auth-pass sineMaster 123123456
13 Cluster 集群 一般情况,除非主从+哨兵无法满足需求,才考虑 Redis Cluster 集群模式(数据分片)
部署参考: RedisCluster笔记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 cluster-enabled yes cluster-config-file /usr/local/redis/cluster/nodes1-6379.conf cluster-node-timeout 15000 cluster-replica-validity-factor 5 cluster-migration-barrier 1 cluster-require-full-coverage yes cluster-allow-reads-when-down no