[1] ZooKeeper: Wait-free coordination for Internet-scale systems
[2] A simple totally ordered broadcast protocol
分布式系统中的一致性协议(consistency protocol)基本可以分为两类:
1)强一致性:如两阶段提交协议(two phase commit),paxos
2)弱一致性:诸如很多nosql中用到的eventually consisitency,方法有很多,不一一累述
值得一提的是GFS中replicas间的一致性保证是通过串行的写3份数据来获得的,这种方法虽然能够获得比较好的强一致性,但high latency也是必然,所以只适合大数据长连接的场景。
two phase commit则要求参与consensus的所有机器都没有故障才能将处理继续,显然这种协议的scalibity不好。
paxos允许在2f+1台机器挂掉f台,consensus的过程依然可以继续,但由于paxos的约束太松散,每次consensus都需要大量的网络通讯,所以性能也不是很高。
zab协议,也就是Zookeeper使用的协议,是基于工程实践,对paxos协议的简化,它基于如下假设:
1)Reliable delivery If a message, m, is delivered by one server, then it will be eventually delivered by all correct servers.
2)Total order If message a is delivered before message b by one server, then every server that delivers a and b delivers a before b.
3)Causal order If message a causally precedes message b and both messages are delivered, then a must be ordered before b.
4)Prex property: If m is the last message delivered for a leader L, any message proposed before m by L must also be delivered;
基本上通过TCP协议和对客户端请求的FIFO的处理,就可以达到以上的要求。
Zookeeper是一个基于内存的数据库,通过snapshot和write ahead log来恢复数据,所以有比较高的并发性能。
同时zab采用了一种类似two phase commit + quorum的方式提交协议,每次consensus所需的网络通信较少,所以可以做到较低延迟。
但quorum也会带来一个问题,就是followers和leader会存在不同步的情况,一般来说会有milliseconds的延迟,如果是跨IDC的话,估计就得上秒级了,所以client在读操作前可以调用一个sync来同步一下状态。