Redis Cluster简介

Since Redis is mostly single-threaded, Redis cannot make use of the multiple cores of your server’s CPU for command processing. But if we split the data between two Redis servers, our system can process requests in parallel, increasing the throughput by almost 200%. In fact, performance will scale close to linearly by adding more Redis servers to the system. This database architectural pattern of splitting data between multiple servers for the purpose of scaling is called sharding. The resulting servers that hold chunks of the data are called shards.

ref: https://redis.io/learn/operate/redis-at-scale/scalability/clustering-in-redis

Redis Cluster是Redis的分布式实现,通过更多的节点水平扩展,具有以下特性:

  • 无中心节点或代理节点,没有统一入口。client连接任意节点即可获取Cluster的全部数据。

  • 使用一致性哈希确保数据在节点间均匀分布,共有 $2^14=16384$ 个哈希槽,所有节点被映射到这些slot上。

  • 通过维护从节点提供容错能力,每个主节点有至少一个从节点。如果主节点故障,Cluster将一个从节点提升为主节点。

  • 整个Cluster会对节点进行投票,如果超过半数的节点投票认为某个节点故障,就认为节点故障。

  • 如果集群中任意一个节点故障,且该节点没有从节点,则集群故障。

配置要求

  • Redis支持cluster,可通cluster nodes 命令检查

  • 至少三个节点,因为投票容错机制需要超过半数节点拥有相同结果才生效,因此两个节点无法构成cluster

  • 每个节点至少有一个从节点

因此搭建至少需要6台支持cluster模式的redis实例。

Cluster配置过程

修改redis.conf ,启用cluster并配置cluster配置文件。

cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000

启动搭建集群的全部redis实例,之后使redis-cli 配置集群(127.0.0.1:7000-7005为6台redis实例的监听地址)。

redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1