redis集群-说明

集群可以做的
1.可以帮你自动在多个节点拆分数据
2.可以在节点不可用时继续操作数据直到节点恢复。
端口数值第二高的当作集群的bus,bus用做发现节点错误,更新配置,故障切换授权,客户端不会尝试连接bus节点。
分片的实现
Redis Cluster 是 Redis的集群实现,内置数据自动分片机制,集群内部将所有的key映射到16384个Slot中,集群中的每个Redis Instance负责其中的一部分的Slot的读写。集群客户端连接集群中任一Redis Instance即可发送命令,当Redis Instance收到自己不负责的Slot的请求时,会将负责请求Key所在Slot的Redis Instance地址返回给客户端,客户端收到后自动将原请求重新发往这个地址,对外部透明。一个Key到底属于哪个Slot由crc16(key) % 16384 决定。

Redis Cluster supports multiple key operations as long as all the keys involved into a single command execution (or whole transaction, or Lua script execution) all belong to the same hash slot. The user can force multiple keys to be part of the same hash slot by using a concept called hash tags.

Hash tags are documented in the Redis Cluster specification, but the gist is that if there is a substring between {} brackets in a key, only what is inside the string is hashed, so for example this{foo}key and another{foo}key are guaranteed to be in the same hash slot, and can be used together in a command with multiple keys as arguments.

关于负载均衡,集群的Redis Instance之间可以迁移数据,以Slot为单位,但不是自动的,需要外部命令触发。

关于集群成员管理,集群的节点(Redis Instance)和节点之间两两定期交换集群内节点信息并且更新,从发送节点的角度看,这些信息包括:集群内有哪些节点,IP和PORT是什么,节点名 字是什么,节点的状态(比如OK,PFAIL,FAIL,后面详述)是什么,包括节点角色(master 或者 slave)等。

关于可用性,集群由N组主从Redis Instance组成。主可以没有从,但是没有从 意味着主宕机后主负责的Slot读写服务不可用。一个主可以有多个从,主宕机时,某个从会被提升为主,具体哪个从被提升为主,协议类似于Raft,参见这里。 如何检测主宕机?Redis Cluster采用quorum+心跳的机制。从节点的角度看,节点会定期给其他所有的节点发送Ping,cluster-node-timeout(可 配置,秒级)时间内没有收到对方的回复,则单方面认为对端节点宕机,将该节点标为PFAIL状态。通过节点之间交换信息收集到quorum个节点都认为这 个节点为PFAIL,则将该节点标记为FAIL,并且将其发送给其他所有节点,其他所有节点收到后立即认为该节点宕机。从这里可以看出,主宕机后,至少 cluster-node-timeout时间内该主所负责的Slot的读写服务不可用。
高可用的实现(主备)

In order to remain available when a subset of master nodes are failing or are not able to communicate with the majority of nodes, Redis Cluster uses a master-slave model where every hash slot has from 1 (the master itself) to N replicas (N-1 additional slaves nodes).

In our example cluster with nodes A, B, C, if node B fails the cluster is not able to continue, since we no longer have a way to serve hash slots in the range 5501-11000.

However when the cluster is created (or at a latter time) we add a slave node to every master, so that the final cluster is composed of A, B, C that are masters nodes, and A1, B1, C1 that are slaves nodes, the system is able to continue if node B fails.

Node B1 replicates B, and B fails, the cluster will promote node B1 as the new master and will continue to operate correctly.

However note that if nodes B and B1 fail at the same time Redis Cluster is not able to continue to operate.
一致性的实现
redis不保证强一致性,由于采用异步写导致主备的数据出现不一致。redis同样支持同步写,但也可能在更复杂的场景会在未写入从节点时,从节点被当选为主节点,造成数据不同步。
还有一种可能导致不一致性,在partition时,当从节点B1被选为主节点,此时客户端依然连接的是partition前的主节点B,此时仍会向B写入数据,B1数据丢失。
集群配置
配置参数在$redis_home/redis.conf上。
cluster-enabled<yes/no>:是否以集群方式启动
cluster-config-file<filename> 集群模式下,每个redis节点生成一个自己的集群配置文件,这个文件不需要人工修改,由redis自己维护,文件内容类似其他几点的状态,持久变量等等,通常他们在每次接收到消息时会刷新这个文件。
cluster-node-timeout<miliseconds>:
参考:http://redis.io/topics/cluster-tutorial

©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!

发表评论

电子邮件地址不会被公开。