Redis cluster Specification 笔记

  ref: http://redis.io/topics/cluster-spec

1. 设计目标: 高性能;线性扩展;不支持合并操作;写操作安全:小概率丢弃;(对于每个key)只要有一个slave工作,就可用;

Redis Cluster is a distributed implementation of Redis with the following goals, in order of importance in the design:

  • High performance and linear scalability up to 1000 nodes.
  • No merge operations in order to play well with values size and semantics typical of the Redis data model.
  • Write safety: the system tries to retain all the writes originating from clients connected with the majority of the nodes. However there are small windows where acknowledged writes can be lost.
  • Availability: Redis Cluster is able to survive to partitions where the majority of the master nodes are reachable and there is at least a reachable slave for every master node that is no longer reachable.

What is described in this document is implemented in the unstable branch of the Github Redis repository. Redis Cluster has now entered the beta stage, so new betas are released every month and can be found in the download page of the Redis web site.

2. 使用hash tag实现将一个key总是路由到固定的节点;

3. 通信协议:

  • 通过一个“cluster bus”的二进制协议通信;
  • 每个几点都和其他所有节点建立tcp链接(在这点就不是线性扩展的);
  • client发起链接给任何请求都是允许的,但是节点不做proxy功能,而是像http那样,返回一个重定向的错误信息;

4. 安全写: 存在两个丢数据的可能:

Redis Cluster tries hard to retain all the writes that are performed by clients connected to the majority of masters, with two exceptions:

1) A write may reach a master, but while the master may be able to reply to the client, the write may not be propagated to slaves via the asynchronous replication used between master and slave nodes. If the master dies without the write reaching the slaves, the write is lost forever in case the master is unreachable for a long enough period that one of its slaves is promoted.

2) Another theoretically possible failure mode where writes are lost is the following:

  • A master is unreachable because of a partition.
  • It gets failed over by one of its slaves.
  • After some time it may be reachable again.
  • A client with a not updated routing table may write to it before the master is converted to a slave (of the new master) by the cluster.

5 可用性: 当网络分裂时,包含多数server的一侧可以正常使用,另一侧不可以使用; 不适用于大规模网络故障的场景; 对任何一个key,只要有一个master或slave存在,就能正常访问;

6. 性能: (每个节点)与单个redis基本相同(这就是所谓的性能线性增长);

7. 为什么不支持merge操作: 性能考虑;

8. key的分布:先CRC然后取模分配从16k的分片(slot),然后在分配到各个node上;

HASH_SLOT = CRC16(key) mod 16384

The CRC16 is specified as follows:

  • Name: XMODEM (also known as ZMODEM or CRC-16/ACORN)
  • Width: 16 bit
  • Poly: 1021 (That is actually x16 + x12 + x5 + 1)
  • Initialization: 0000
  • Reflect Input byte: False
  • Reflect Output CRC: False
  • Xor constant to output CRC: 0000
  • Output for "123456789": 31C3

9 。 keys hash tag: 在key “{tag}otherString”中,tag就是hash tags,用于计算这个key的slot位置,为了实现先同tag的key映射到相同的slot中。10。 node属性:node的标识是一个随机数,第一次运行时写入到配置文件,并保持不变;

Every node has other associated information that all the other nodes know:

  • The IP address and TCP port where the node is located.
  • A set of flags.
  • A set of hash slots served by the node.
  • Last time we sent a ping packet using the cluster bus.
  • Last time we received a pong packet in reply.
  • The time at which we flagged the node as failing.
  • The number of slaves of this node.
  • The master node ID, if this node is a slave (or 0000000... if it is a master).

11. Cluster topology 拓扑: 全连接且长tcp链接。

Redis cluster is a full mesh where every node is connected with every other node using a TCP connection.

In a cluster of N nodes, every node has N-1 outgoing TCP connections, and N-1 incoming connections.

These TCP connections are kept alive all the time and are not created on demand.

12. 节点间通信: 新节点加入时,只有管理员才能发起MEET消息;MEET消息会在cluster中传播。

13. 重定向策略

A Redis client is free to send queries to every node in the cluster, including slave nodes. The node will analyze the query, and if it is acceptable (that is, only a single key is mentioned in the query) it will see what node is responsible for the hash slot where the key belongs.

If the hash slot is served by the node, the query is simply processed, otherwise the node will check its internal hash slot -> node ID map and will reply to the client with a MOVED error.

A MOVED error is like the following:

GET x
-MOVED 3999 127.0.0.1:6381

14. key迁移:

The following subcommands are available:

  • CLUSTER ADDSLOTS slot1 [slot2] ... [slotN]
  • CLUSTER DELSLOTS slot1 [slot2] ... [slotN]
  • CLUSTER SETSLOT slot NODE node
  • CLUSTER SETSLOT slot MIGRATING node
  • CLUSTER SETSLOT slot IMPORTING node
  • CLUSTER GETKEYSINSLOT slot count
  • MIGRATE target_host target_port key target_database id timeout

15. Ask redirection: 查询一个key的位置

16. Client处理重定向: client应该适当记录key与slot的关系(减少redirect的次数),并且处理redirect的错误信息;  

17 . 多key。Multiple keys operations

Using hash tags clients are free to use multiple-keys operations. For example the following operation is valid:

MSET {user:1000}.name Angela {user:1000}.surname White

18 容错:

  1. 节点间心跳检测:随机发给随机数量的节点,使得整个cluster的总心跳数在N的规模;
  2. 心跳报文内容:

The common header has the following information:

  • Node ID, that is a 160 bit pseudorandom string that is assigned the first time a node is created and remains the same for all the life of a Redis Cluster node.
  • The currentEpoch and configEpoch field, that are used in order to mount the distributed algorithms used by Redis Cluster (this is explained in details in the next sections). If the node is a slave the configEpoch is the last known configEpoch of the master.
  • The node flags, indicating if the node is a slave, a master, and other single-bit node information.
  • A bitmap of the hash slots served by a given node, or if the node is a slave, a bitmap of the slots served by its master.
  • Port: the sender TCP base port (that is, the port used by Redis to accept client commands, add 10000 to this to obtain the cluster port).
  • State: the state of the cluster from the point of view of the sender (down or ok).
  • The master node ID, if this is a slave.

19 失效节点检测: PFAIL/FAIL标志。当A心跳检测B失败,A标志为B为PFAIL;然后A询问其他的node,如果多数节点返回B为PFAIL,则A标志B为FAIL,并通知其他所有的node B为FAIL。

This mechanism is used in order to escalate a PFAIL condition to a FAIL condition, when the following set of conditions are met:

  • Some node, that we‘ll call A, has another node B flagged as PFAIL.
  • Node A collected, via gossip sections, information about the state of B from the point of view of the majority of masters in the cluster.
  • The majority of masters signaled the PFAIL or PFAIL condition within NODE_TIMEOUT * FAIL_REPORT_VALIDITY_MULT time.

If all the above conditions are true, Node A will:

  • Mark the node as FAIL.
  • Send a FAIL message to all the reachable nodes.

The FAIL message will force every receiving node to mark the node in FAIL state.

20. 逻辑时钟:Cluster epoch

21. Slave提升为Master: 检测到master失效-》slave发起选举-》赢得选举的slave将自己变为master. 选举过程:

  • slave A发送FAILOVER_AUTH_REQUEST给其他所有的master,并等待回应(至少NODE_TIMEOUT*2时长);
  • 其他master收到FAILOVER_AUTH_REQUEST请求后,决定如果同意就回应FAILOVER_AUTH_ACK消息,并且在2*NODE_TIMEOUT时间内不在同意其他请求(和zk的类似)
  • slave A 如果收到大多少(超过半数)master的ack回应,则赢得选举,广播自己赢得选举的消息。 然后就可以升为master了。

22. key slot分配与信息传播。

Rule 1: If an hash slot is unassigned, and a known node claims it, I‘ll modify my hash slot table to associate the hash slot to this node.

Rule 2: If an hash slot is already assigned, and a known node is advertising it using a configEpoch that is greater than theconfigEpoch advertised by the current owner of the slot, I‘ll rebind the hash slot to the new node.

22. publish and subscribe: 可以向任何一个节点publish或subscribe,cluter内部会通知到正确的节点;

地方

Redis cluster Specification 笔记

时间: 2024-10-09 10:44:33

Redis cluster Specification 笔记的相关文章

Redis Cluster(Redis 3.X)设计要点

Redis 3.0.0 RC1版本10.9号发布,Release Note这个版本支持Redis Cluster,相信很多同学期待已久,不过这个版本只是RC版本,要应用到生产环境,还得等等 Redis Cluster设计要点: 架构:无中心 Redis Cluster采用无中心结构,每个节点都保存数据和整个集群的状态每个节点都和其他所有节点连接,这些连接保持活跃使用gossip协议传播信息以及发现新节点node不作为client请求的代理,client根据node返回的错误信息重定向请求 数据分

Redis Essentials 读书笔记 - 第九章: Redis Cluster and Redis Sentinel (Collective Intelligence)

Chapter 9. Redis Cluster and Redis Sentinel (Collective Intelligence) 上一章介绍了复制,一个master可以对应一个或多个slave(replica), 在以下的情况下是够用的: 1. master有足够内存容纳所有key 2. 通过slave可以扩展读,解决网络吞吐量的问题 3. 允许停止master的维护窗口时间 4. 通过slave做数据冗余 但复制解决不了自动failover和自动resharding的问题,在以下的情

redis cluster官方集群的搭建笔记

参考文档: https://www.zybuluo.com/phper/note/195558 http://www.cnblogs.com/shihaiming/p/5949772.html http://redis.io/topics/cluster-tutorial http://xiaorui.cc 系统环境: CentOS6.7 X86_64 双机6节点: node1: 192.168.2.11 node2: 192.168.2.12 node3:192.168.2.13 redis

Redis Protocol specification

Redis Protocol specification Redis clients communicate with the Redis server using a protocol called RESP (REdis Serialization Protocol). While the protocol was designed specifically for Redis, it can be used for other client-server software projects

第十一课——codis-server的高可用,对比codis和redis cluster的优缺点

[作业描述] 1.配置codis-ha 2.总结对比codis的集群方式和redis的cluster集群的优缺点 ================================================================================= 一.codis-ha的部署配置 ##codis-ha要独立于codis集群,单独配置,也是基于go环境的 1.go方式下载codis-ha: go get github.com/ngaut/codis-ha 2.进入cod

Redis Cluster集群搭建与应用

1.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper,但从redis 3.0之后版本支持redis-cluster集群,redis-cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接.其redis-cluster架构图如下: 其结构特点 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. 节点的fail是通过集群中超过半数的节点检测失效时才生效. 客户端与redis节点直

Redis Cluster集群部署搭建

在Oracle的路上走了许多年,换换感觉,尝试一下新的知识,也是一个不错的感觉.Redis,一个超轻量化的内存数据库,只做一小块数据库功能实现,却非常优秀的一个产品.今天,就分享一下安装Redis集群的过程. 搭建redis集群,建议至少需要准备3台服务器,共搭建6个节点,3个master,3个slave,并且要求3个master节点不能全部跑到同一台服务器上,保证节点安全,3台服务器的配置相同,使用redistest账号搭建,对应的端口是7000/7001/7002端口 我的集群分配如下,每个

Windows 配置Reids集群 Redis Cluster

1. 下载安装Redis Redis官方不支持Windows,但是Microsoft Open Tech group在 GitHub上开发了一个Win64的版本,下载地址为: 下载Redis 启动服务 redis-server redis.windows.conf 客户端连接 redis-cli.exe -h 127.0.0.1 -p 6379 2. 安装Ruby并配置环境 安装Ruby,Windows可以安装RubyInstaller,下载地址: http://railsinstaller.o

redis cluster

Redis cluster 介绍 Redis在3.0版正式引入了集群特性.Redis集群是一个分布式(distributed).容错(fault-tolerant)的 Redis内存K/V服务, 普通单机 Redis 使用的功能 仅是集群中的功能的一个子集(subset).Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误. Redis集群的几个重要特征: (1).Redis 集群的分片特征在