Cassandra的consistency level

在将cassandra的节点配置为互备以后,某天其中一个节点突然down机了,这个时候发现通过cassandra的thrift api往另外的节点写数据也没法成功了。研究了下thrift的api以后,推测估计是跟client的consistency level有关系。

 

consistency level即一致性级别,用来根据配置的副本因子(ReplicationFactor )来控制cassandra的读写行为,在读或写操作下,不同的一致性级别有不同的含义。所以读写的一致性级别定义是不完全一样的。

 

Write

 

Level

Behavior

ANY

Ensure that the write has been written to at least 1 node, including HintedHandoff recipients.

ONE

Ensure that the write has been written to at least 1 replica's commit log and memory table before responding to the client.

QUORUM

Ensure that the write has been written to N / 2 + 1 replicas before responding to the client.

LOCAL_QUORUM

Ensure that the write has been written to <ReplicationFactor> / 2 + 1 nodes, within the local datacenter (requires NetworkTopologyStrategy )

EACH_QUORUM

Ensure that the write has been written to <ReplicationFactor> / 2 + 1 nodes in each datacenter (requires NetworkTopologyStrategy )

ALL

Ensure that the write is written to all N replicas before responding to the client. Any unresponsive replicas will fail the operation.

 

 

Read

 

Level

Behavior

ANY

Not supported. You probably want ONE instead.

ONE

Will return the record returned by the first replica to respond. A consistency check is always done in a background thread to fix any consistency issues when ConsistencyLevel.ONE is used. This means subsequent calls will have correct data even if the initial read gets an older value. (This is called ReadRepair )

QUORUM

Will query all replicas and return the record with the most recent timestamp once it has at least a majority of replicas (N / 2 + 1 ) reported. Again, the remaining replicas will be checked in the background.

LOCAL_QUORUM

Returns the record with the most recent timestamp once a majority of replicas within the local datacenter have replied.

EACH_QUORUM

Returns the record with the most recent timestamp once a majority of replicas within each datacenter have replied.

ALL

Will query all replicas and return the record with the most recent timestamp once all replicas have replied. Any unresponsive replicas will fail the operation.

 

 

在这些级别中,为了保证读写的一致性,我们基本都采用Quorum这级,但是在我们这个场景中,如果在一个节点down的情况下,还有保证另一个节点写入的话,只能使用写入ONE这个级别了。读取的时候还是Quorum,如果也用ONE的话,会存在不一致的情况,不过在这种情况下,会有一个后台线程来做同步

 

参考

http://wiki.apache.org/cassandra/API

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章