本文介绍了了解 cassandra 复制因子与一致性级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清 Cassandra 中复制因子和一致性级别的非常基本的概念.如果有人可以回答以下问题,我们将不胜感激.

I want to clarify very basic concept of replication factor and consistency level in Cassandra. Highly appreciate if someone can provide answer to below questions.

RF-复制因子RC-读取一致性WC- 写一致性

RF- Replication FactorRC- Read ConsistencyWC- Write Consistency

2 个 cassandra 节点 (Ex: A, B) RF=1, RC=ONE, WC=ONE or ANY

2 cassandra nodes (Ex: A, B) RF=1, RC=ONE, WC=ONE or ANY

  • 我可以将数据写入节点 A 并从节点 B 读取吗?
  • 如果 A 下降会发生什么?

3 个 cassandra 节点 (Ex: A, B, C) RF=2, RC=QUORUM, WC=QUORUM

3 cassandra nodes (Ex: A, B, C) RF=2, RC=QUORUM, WC=QUORUM

  • 我可以向节点 A 写入数据并从节点 C 读取数据吗?
  • 如果节点 A 出现故障会发生什么?

3 个 cassandra 节点 (Ex: A, B, C) RF=3, RC=QUORUM, WC=QUORUM

3 cassandra nodes (Ex: A, B, C) RF=3, RC=QUORUM, WC=QUORUM

  • 我可以向节点 A 写入数据并从节点 C 读取数据吗?
  • 如果节点 A 出现故障会发生什么?

推荐答案

简短摘要:复制因子描述了数据的副本数量.一致性级别描述了客户端看到的行为.也许有更好的方法来对这些进行分类.

Short summary: Replication factor describes how many copies of your data exist. Consistency level describes the behavior seen by the client. Perhaps there's a better way to categorize these.

例如,您可以将复制因子设为 2.当您写入时,将始终存储两个副本,假设有足够的节点已启动.当一个节点宕机时,该节点的写入会被隐藏起来,并在它恢复时写入,除非它宕机的时间足够长,以至于 Cassandra 决定它永远消失了.

As an example, you can have a replication factor of 2. When you write, two copies will always be stored, assuming enough nodes are up. When a node is down, writes for that node are stashed away and written when it comes back up, unless it's down long enough that Cassandra decides it's gone for good.

现在假设在该示例中,您以 ONE 的一致性级别进行编写.在对一个节点进行写入后,客户端将收到成功确认,而无需等待第二次写入.如果您使用 ALL 的 CL 进行写入,则对客户端的确认将等到两个副本都被写入.还有很多其他的一致性级别选项,太多了,无法涵盖这里的所有变体.不过,请阅读 Datastax 文档很好地解释了它们.

Now say in that example you write with a consistency level of ONE. The client will receive a success acknowledgement after a write is done to one node, without waiting for the second write. If you did a write with a CL of ALL, the acknowledgement to the client will wait until both copies are written. There are very many other consistency level options, too many to cover all the variants here. Read the Datastax doc, though, it does a good job of explaining them.

在同一个例子中,如果你以 ONE 的一致性级别读取,则在单个副本响应后将响应发送到客户端.另一个副本可能有更新的数据,在这种情况下,响应将不是最新的.在许多情况下,这已经足够了.在其他情况下,客户端将需要最新的信息,并且您将在读取时使用不同的一致性级别 - 可能是级别 ALL.这样,Cassandra 和其他后关系型数据库的一致性就可以以关系型数据库通常不具备的方式进行调整.

In the same example, if you read with a consistency level of ONE, the response will be sent to the client after a single replica responds. Another replica may have newer data, in which case the response will not be up-to-date. In many contexts, that's quite sufficient. In others, the client will need the most up-to-date information, and you'll use a different consistency level on the read - perhaps a level ALL. In that way, the consistency of Cassandra and other post-relational databases is tunable in ways that relational databases typically are not.

现在回到你的例子.

示例一:是的,您可以写入 A 并从 B 读取,即使 B 没有自己的副本.B 将代表您的客户向 A 索取.对于节点全部启动的其他情况也是如此.当它们全部完成后,您可以写入一个并读取另一个.

Example one: Yes, you can write to A and read from B, even if B doesn't have its own replica. B will ask A for it on your client's behalf. This is also true for your other cases where the nodes are all up. When they're all up, you can write to one and read from another.

对于写入,WC=ONE,如果单个副本的节点已启动并且是您要连接的节点,则写入将成功.如果是其他节点,写入将失败.如果您使用 ANY,则写入将成功,假设您正在与已启动的节点通话.我认为您还必须为此启用提示切换.宕机节点稍后会获取数据,直到发生该数据之后,您才能读取数据,甚至无法从启动节点读取.

For writes, with WC=ONE, if the node for the single replica is up and is the one you're connect to, the write will succeed. If it's for the other node, the write will fail. If you use ANY, the write will succeed, assuming you're talking to the node that's up. I think you also have to have hinted handoff enabled for that. The down node will get the data later, and you won't be able to read it until after that occurs, not even from the node that's up.

在其他两个示例中,复制因子将影响最终写入的副本数量,但不会影响超出我上面描述的客户端行为.QUORUM 将影响客户端行为,因为您必须有足够数量的节点启动并响应写入和读取.如果你很幸运并且至少 (nodes/2) + 1 个节点超出了你需要的节点,那么写入和读取将成功.如果没有足够的节点和副本,读取和写入将失败.总体而言,如果某个节点出现故障,则某些 QUORUM 读取和写入可能会成功,假设该节点不需要存储您的副本,或者它的中断仍然留下足够的副本节点可用.

In the other two examples, replication factor will affect how many copies are eventually written, but doesn't affect client behavior beyond what I've described above. The QUORUM will affect client behavior in that you will have to have a sufficient number of nodes up and responding for writes and reads. If you get lucky and at least (nodes/2) + 1 nodes are up out of the nodes you need, then writes and reads will succeed. If you don't have enough nodes with replicas up, reads and writes will fail. Overall some QUORUM reads and writes can succeed if a node is down, assuming that that node is either not needed to store your replica, or if its outage still leaves enough replica nodes available.

这篇关于了解 cassandra 复制因子与一致性级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 15:53