目的
为了解决单机 Redis 容量有限的问题,将数据按一定的规则分配到多台机器,内存/QPS 不受限于单机,可受益于分布式集群高扩展性。
哨兵作用于高可用,集群提高并发量
cluster
Redis 集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
安装测试
最好所有的 redis 示例在不通的节点上。
本次使用 一个主机不同端口
配置文件 如下(节选), 修改端口
1 | port 6379 |
启动命令
--replicas 1 : 采用最简单的方式配置集群,一台主机,一台从机,正好三组。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
52redis-cli --cluster create --cluster-replicas 1 172.20.40.249:6379 172.20.40.249:6380 172.20.40.249:6381 172.20.40.249:6389 172.20.40.249:6390 172.20.40.249:6391
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.20.40.249:6390 to 172.20.40.249:6379
Adding replica 172.20.40.249:6391 to 172.20.40.249:6380
Adding replica 172.20.40.249:6389 to 172.20.40.249:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 33991073bfbd3a9445bb2033d31da05e58d2d7c6 172.20.40.249:6379
slots:[0-5460] (5461 slots) master
M: b169cd10dc0946371134263dd12159cec492c376 172.20.40.249:6380
slots:[5461-10922] (5462 slots) master
M: ce64c931b780453a86c8f79a10119e606bbce365 172.20.40.249:6381
slots:[10923-16383] (5461 slots) master
S: ebe1d7f9abf5bc987aa797a24fa3401d3c303e2a 172.20.40.249:6389
replicates 33991073bfbd3a9445bb2033d31da05e58d2d7c6
S: b6c3f47aa83084210d366dee583314bc400d8514 172.20.40.249:6390
replicates b169cd10dc0946371134263dd12159cec492c376
S: 031740e8edf7e35f60e82461bcb06175d0967c7e 172.20.40.249:6391
replicates ce64c931b780453a86c8f79a10119e606bbce365
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 172.20.40.249:6379)
M: 33991073bfbd3a9445bb2033d31da05e58d2d7c6 172.20.40.249:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: ce64c931b780453a86c8f79a10119e606bbce365 172.20.40.249:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: ebe1d7f9abf5bc987aa797a24fa3401d3c303e2a 172.20.40.249:6389
slots: (0 slots) slave
replicates 33991073bfbd3a9445bb2033d31da05e58d2d7c6
S: b6c3f47aa83084210d366dee583314bc400d8514 172.20.40.249:6390
slots: (0 slots) slave
replicates b169cd10dc0946371134263dd12159cec492c376
M: b169cd10dc0946371134263dd12159cec492c376 172.20.40.249:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 031740e8edf7e35f60e82461bcb06175d0967c7e 172.20.40.249:6391
slots: (0 slots) slave
replicates ce64c931b780453a86c8f79a10119e606bbce365
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看信息
1 | [root@test-249 redis-home]# redis-cli -p 6390 |
solts
一个 Redis 集群包含 16384 个插槽(hash slot), 数据库中的每个键都属于这 16384 个插槽的其中一个,
集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽, 其中 CRC16(key) 语句用于计算键 key 的 CRC16 校验和 。
集群中的每个节点负责处理一部分插槽。 举个例子, 如果一个集群可以有主节点, 其中:
节点 A 负责处理 0 号至 5460 号插槽。
节点 B 负责处理 5461 号至 10922 号插槽。
节点 C 负责处理 10923 号至 16383 号插槽。
不足
多键操作是不被支持的
多键的Redis事务是不被支持的。lua脚本不被支持
由于集群方案出现较晚,很多公司已经采用了其他的集群方案,而代理或者客户端分片的方案想要迁移至redis cluster,需要整体迁移而不是逐步过渡,复杂度较大。