一致性模型的从强弱上分为强一致性和弱一致性,而不同的强弱程度有衍生出许多的一致性。
强一致性模型
strict consistency
| Sequence | Strict model | Non-strict model | ||
|---|---|---|---|---|
P1 | P2 | P1 | P2 | |
| 1 | W(x)1 | W(x)1 | ||
| 2 | R(x)1 | R(x)0 | ||
| 3 | R(x)1 |
该模型是最强一致性模型,在global clock上是一致的,但是无实际意义,它是建立在无严格并发写的条件下成立的,但我个人理解还是有助于加深对一致性的理解的。而且,实际工程中也是从这种理想化的东西根据实际场景变通的。
sequential consistency:
叫做顺序一致性,在单个cpu上的处理顺序和程序中的处理顺序是一致的,什么意思呢,就是假如有执行顺序a->b->c,那么无论在哪个处理器上看到的一定是a->b->c。
但是,顺序一致性并不保证多处理之间的顺序一定一致,举个🌰,两个处理任务a1->b1->c1, a2->b2->c2,这里的a2不一定比a1晚执行。
linearizable consistency:
图-1 线性一致性示意图
linearizability consistency是一个stric model,如果存在网络分区,服务将不可用,它通过在全局使用原子操作保证操作顺序在任何节点上执行的效果都是完全相同的。而且同时只会在一个节点上执行,典型的单一对象模型。这种模型其实在实际生产环境中用的是比较多的,例如一主多从模式,对于写操作来说,就是linearizability consistency模型。而说到的出现分区之后不可用了,可以通过仲裁协议去处理。例如raft。
使用场景:
When you need linearizability across multiple objects, try strict serializability. When real-time constraints are not important, but you still want every process to observe the same total order, try sequential consistency
弱一致性
client-centric consistency:
客户端为中心的一致性模型主要针对一些没有通过同步逻辑保障数据一致性的情况,一致性由客户端去把控,具体的可分为以下几种模型:
Monotonic read consistency:
客户端读取某个值之后,后续的读取相同数据项的操作不会读取到比该值更早的数据。
Monotonic write consistency:
同一个写进程在写同一个数据项的时候,一定是串行的,且下一个写操作开始之前,必须等到前一个写操作结束。
Read-your-writes consistency:
一个进程对数据项写入的值对该进程在该数据项上的后续读操作可见。
Writes-follows-reads consistency:
A write operation by a process on a data item x following a previous read operation on x by the same process is guaranteed to take place on the same or a more recent value of x that was read
暂时不是很理解这种场景。。。学习中。。。
causal consistency
is a weakening model of sequential consistency by categorizing events into those causally related and those that are not. It defines that only write operations that are causally related, need to be seen in the same order by all processes.
只有因果相关的一致性,才需要在所有执行进程上保持顺序一致性。相当于把一致性分类了,弱化了。
| Sequence | P1 | P2 |
|---|---|---|
| 1 | W1(x)3 | |
| 2 | W2(x)5 | |
| 3 | R1(x)3 |
W1 is not causally related to W2. R1 would be sequentially inconsistent but is causally consistent.[clarification needed][5]
| Sequence | P1 | P2 | P3 | P4 |
|---|---|---|---|---|
| 1 | W(x)1 | R(x)1 | R(x)1 | R(x)1 |
| 2 | W(x)2 | |||
| 3 | W(x)3 | R(x)3 | R(x)2 | |
| 4 | R(x)2 | R(x)3 |
W(x)1 and W(x)2 are causally related due to the read made by P2 to x before W(x)2.[5]
Process P2 observes, reads, the earlier write W(x)1 that is done by process P1. Therefore, the two writes W(x)1 and W(x)2 are causally related. Under causal consistency, every process observes W(x)1 first, before observing W(x)2. Notice that the two write operations W(x)2 and W(x)3, with no intervening read operations, are concurrent, and processes P3 and P4 observe (read) them in different orders.
我理解,这里的意思就是说,因为R(x)1和W(x)1相关的,而W(x)2和R(x)1相关的,所以,W(x)2和W(x)1不能并发,而W(x)3可以和W(x)2并发,所以,P3,P4看到的sequence 3和4的顺序就可能不一样了。
processor consistency
保证在多处理器系统中,写的执行顺序在每个处理器上相同。
eventual consistency
最终一致性,是最弱的一种一致性,它是依赖时间足够长,各个节点总能在某个状态或数值上达成一致。
参考资料:
一致性模型wiki:en.wikipedia.org/wiki/Consis…
sequential consistency wiki:en.wikipedia.org/wiki/Sequen…
linearizability consistency wiki:en.wikipedia.org/wiki/Linear…
client-centric consistency wiki:en.wikipedia.org/wiki/Consis…
causal consistency wiki:en.wikipedia.org/wiki/Causal…
processor consistency wiki: en.wikipedia.org/wiki/Proces…
线性一致性示意图:upload.wikimedia.org/wikipedia/c…