1. Overview
1.概述
Apache Cassandra is an open-source, NoSQL, highly available, and scalable distributed database. To achieve high availability, Cassandra relies on the replication of data across clusters.
Apache Cassandra是一个开源、NoSQL、高可用和可扩展的分布式数据库。为了实现高可用性,Cassandra 依赖于跨集群的数据复制。
In this tutorial, we will learn how Cassandra provides us the control to manage the consistency of data while replicating data for high availability.
在本教程中,我们将学习Cassandra如何为我们提供控制来管理数据的一致性,同时复制数据以获得高可用性。
2. Data Replication
2.数据复制
Data replication refers to storing copies of each row in multiple nodes. The reason for data replication is to ensure reliability and fault tolerance. Consequently, if any node fails for any reason, the replication strategy makes sure that the same data is available in other nodes.
数据复制是指在多个节点中存储每一行的副本。数据复制的原因是为了确保可靠性和容错。因此,如果任何节点因任何原因发生故障,复制策略将确保相同的数据在其他节点上可用。
The replication factor (RF) specifies how many nodes across the cluster would store the replicas.
复制因子(RF)指定整个集群的多少个节点将存储复制。。
There are two available replication strategies:
有两种可用的复制策略。
The SimpleStrategy is used for a single data center and one rack topology. First, Cassandra uses partitioner logic to determine the node to place the row. Then it puts additional replicas on the next nodes clockwise in the ring.
SimpleStrategy用于单个数据中心和一个机架拓扑结构。首先,Cassandra使用分区器逻辑来确定放置行的节点。然后,它将额外的副本放在环形结构中顺时针方向的下一个节点上。
The NetworkTopologyStrategy is generally used for multiple datacenters and multiple racks. Additionally, it allows you to specify a different replication factor for each data center. Within a data center, it allocates replicas to different racks to maximize availability.
NetworkTopologyStrategy通常用于多个数据中心和多个机架。此外,它允许你为每个数据中心指定不同的复制因子。在一个数据中心内,它将复制分配到不同的机架上,以最大限度地提高可用性。
3. Consistency Level
3.一致性水平
Consistency indicates how recent and in-sync all replicas of a row of data are. With the replication of data across the distributed system, achieving data consistency is a very complicated task.
一致性表明一行数据的所有副本的最新和同步程度。随着数据在分布式系统中的复制,实现数据的一致性是一项非常复杂的任务。
Cassandra prefers availability over consistency. It doesn’t optimize for consistency. Instead, it gives you the flexibility to tune the consistency depending on your use case. In most use cases, Cassandra relies on eventual consistency.
相对于一致性,Cassandra 更倾向于可用性。它并没有对一致性进行优化。相反,它让你可以根据你的用例灵活调整一致性。在大多数用例中,Cassandra依赖于最终的一致性。
Let’s look at consistency level impact during the write and read of data.
让我们来看看数据写入和读取过程中的一致性水平影响。
4. Consistency Level (CL) on Write
4.写入时的一致性水平(CL)
For write operations, the consistency level specifies how many replica nodes must acknowledge back before the coordinator successfully reports back to the client. More importantly, the number of nodes that acknowledge (for a given consistency level) and the number of nodes storing replicas (for a given RF) are mostly different.
对于写操作,一致性级别指定了在协调器成功报告给客户之前,必须确认回馈的复制节点数量。更重要的是,确认的节点数量(对于给定的一致性级别)和存储复制的节点数量(对于给定的RF)大多是不同的。
For example, with the consistency level ONE and RF = 3, even though only one replica node acknowledges back for a successful write operation, Cassandra asynchronously replicates the data to 2 other nodes in the background.
例如,在一致性级别为ONE,RF=3的情况下,即使只有一个复制节点确认回写操作成功,Cassandra也会在后台异步复制数据到其他两个节点。
Let’s look at some of the consistency level options available for the write operation to be successful.
让我们看一下写操作成功的一些一致性级别的选项。
The consistency level ONE means it needs acknowledgment from only one replica node. Since only one replica needs to acknowledge, the write operation is fastest in this case.
一致性级别ONE意味着它只需要一个副本节点的确认。由于只有一个副本需要确认,在这种情况下,写操作是最快的。
The consistency level QUORUM means it needs acknowledgment from 51% or a majority of replica nodes across all datacenters.
一致性级别QUORUM意味着它需要来自所有数据中心的51%或大多数复制节点的确认。
The consistency level of LOCAL_QUORUM means it needs acknowledgment from 51% or a majority of replica nodes just within the same datacenter as the coordinator. Thus, it avoids the latency of inter-datacenter communication.
LOCAL_QUORUM的一致性水平意味着它需要来自51%或大多数复制节点的确认,而这些复制节点就在与协调者相同的数据中心内。因此,它避免了数据中心间通信的延迟。
The consistency level of ALL means it needs acknowledgment from all the replica nodes. Since all replica nodes need to acknowledge, the write operation is the slowest in this case. Moreover, if one of the replica nodes is down during the write operation, it fails, and availability suffers. Therefore, the best practice is not to use this option in production deployment.
ALL的一致性级别意味着它需要所有复制节点的确认。由于所有的复制节点都需要确认,在这种情况下,写操作是最慢的。此外,如果其中一个复制节点在写操作过程中发生故障,它就会失败,可用性就会受到影响。因此,最好的做法是不在生产部署中使用这个选项。
We can configure the consistency level for each write query or at the global query level.
The diagram below shows a couple of examples of CL on write:
下图显示了几个关于CL写的例子。
5. Consistency Level (CL) on Read
5.读取时的一致性水平(CL)
For read operations, the consistency level specifies how many replica nodes must respond with the latest consistent data before the coordinator successfully sends the data back to the client.
对于读取操作,一致性级别规定了在协调器成功地将数据送回客户端之前,必须有多少个复制节点响应最新的一致性数据。
Let’s look at some of the consistency level options available for the read operation where Cassandra successfully returns data.
让我们来看看Cassandra成功返回数据的读取操作的一些一致性级别选项。
The consistency level ONE means only one replica node returns the data. The data retrieval is fastest in this case.
一致性级别ONE意味着只有一个复制节点返回数据。在这种情况下,数据的检索是最快的。
The consistency level QUORUM means 51% or a majority of replica nodes across all datacenters responds. Then the coordinator returns the data to the client. In the case of multiple data centers, the latency of inter-data center communication results in a slow read.
一致性级别QUORUM意味着51%或所有数据中心的大多数复制节点都会做出响应。然后协调器将数据返回给客户端。在多个数据中心的情况下,数据中心间通信的延迟会导致缓慢的读取。
The consistency level of LOCAL_QUORUM means 51% or a majority of replica nodes within the same datacenter. As the coordinator responds, then the coordinator returns the data to the client. Thus, it avoids the latency of inter-datacenter communication.
LOCAL_QUORUM的一致性级别意味着51%或同一数据中心内的大多数复制节点。随着协调器的响应,然后协调器将数据返回给客户端。因此,它避免了数据中心间通信的延迟。
The consistency level of ALL means all the replica nodes respond, then the coordinator returns the data to the client. Since all replica nodes need to acknowledge, the read operation is the slowest in this case. Moreover, if one of the replica nodes is down during the read operation, it fails, and availability suffers. The best practice is not to use this option in production deployment.
ALL 的一致性级别意味着所有的复制节点都会响应,然后协调器将数据返回给客户端。由于所有复制节点都需要确认,在这种情况下,读取操作是最慢的。此外,如果一个复制节点在读取操作期间发生故障,它就会失败,可用性也会受到影响。最好的做法是不在生产部署中使用这个选项。
We can configure the consistency level for each write query or at the global query level.
The diagram below shows a couple of examples of CL on read:
下图显示了几个CL在读取时的例子。
6. Strong Consistency
6.强烈的连贯性
Strong consistency means you are reading the latest written data into the cluster no matter how much time between the latest write and subsequent read.
强一致性意味着你正在将最新写入的数据读入集群,无论最新写入和随后的读取之间有多长的时间。
We saw in the earlier sections how we could specify desired consistency level (CL) for writes and reads.
我们在前面的章节中看到,我们可以为写和读指定所需的一致性级别(CL)。
Strong consistency can be achieved if W + R > RF, where R – read CL replica count, W – write CL replica count, RF – replication factor.
如果W + R > RF,可以实现强一致性,其中R–读CL复制数,W–写CL复制数,RF–复制系数。
In this scenario, you get a strong consistency since all client reads always fetches the most recent written data.
在这种情况下,你会得到一个强大的一致性,因为所有的客户端读取总是获取最新的写入数据。
Let’s look at a couple of examples of strong consistency levels:
让我们来看看几个强一致性水平的例子。
6.1. Write CL = QUORUM and Read CL = QUORUM
6.1.写CL=QUORUM和读CL=QUORUM
If RF = 3, W = QUORUM or LOCAL_QUORUM, R = QUORUM or LOCAL_QUORUM, then W (2) + R (2) > RF (3)
如果RF=3,W=QUORUM或LOCAL_QUORUM,R=QUORUM或LOCAL_QUORUM,那么W(2)+R(2)> RF(3)
In this case, the write operation makes sure two replicas have the latest data. Then the read operation also makes sure it receives the data successfully only if at least two replicas respond with consistent latest data.
在这种情况下,写操作确保两个复制体拥有最新的数据。然后,读操作也确保只有在至少两个副本响应一致的最新数据时,它才能成功接收到数据。
6.2. Write CL = ALL and Read CL = ONE
6.2.写CL = ALL和读CL = ONE
If RF = 3, W = ALL, R = ONE, then W (3) + R (1) > RF (3)
如果RF=3,W=ALL,R=ONE,那么W(3)+R(1)> RF(3)
In this case, once the coordinator writes data to all the replicas, the write operation is successful. Then it’s enough to read the data from one of those replicas to make sure we read the latest written data.
在这种情况下,一旦协调器将数据写入所有的副本,那么写入操作就成功了。然后从其中一个副本中读取数据就可以了,以确保我们读取的是最新写入的数据。
But as we learned earlier, write CL of ALL is not fault-tolerant, and the availability suffers.
但正如我们之前所了解的,ALL的写入CL是不容错的,可用性受到影响。
7. Conclusion
7.结语
In this article, we looked at data replication in Cassandra. We also learned about the different consistency level options available on data write and read. Additionally, we looked at a couple of examples to achieve strong consistency.
在这篇文章中,我们研究了Cassandra的数据复制。我们还了解了在数据写入和读取时可用的不同一致性级别选项。此外,我们看了几个例子来实现强一致性。