简体   繁体   English

CouchDB与关系数据库中的分布式一致性?

[英]Distributed consistency in CouchDB vs relational databases?

From CouchDB guide : CouchDB指南

Maintaining consistency within a single database node is relatively easy for most databases. 对于大多数数据库而言,在单个数据库节点内维护一致性相对容易。 The real problems start to surface when you try to maintain consistency between multiple database servers. 当您尝试维护多个数据库服务器之间的一致性时,真正的问题开始浮出水面。 If a client makes a write operation on server A, how do we make sure that this is consistent with server B, or C, or D? 如果客户端在服务器A上执行写操作,我们如何确保它与服务器B或C或D一致? For relational databases, this is a very complex problem with entire books devoted to its solution. 对于关系数据库而言,这是一个非常复杂的问题,整本书都专门针对其解决方案。 You could use multi-master, master/slave, partitioning, sharding, write-through caches, and all sorts of other complex techniques. 您可以使用多主服务器,主/从服务器,分区,分片,直写式缓存以及各种其他复杂技术。

Why is it hard to maintain consistency between the database server in a relational model? 为什么在关系模型中很难维护数据库服务器之间的一致性? And why is CouchDB approach simpler and easier? 为什么CouchDB方法变得越来越简单?

Couch simplifies it in two ways. Couch通过两种方式简化了这一过程。

First, it has a higher level replication model built in and enforced by the system. 首先,它具有系统内置并由系统强制执行的更高级别的复制模型。

Second, it's data elements are coarser, giving the optimistic locking and conflict resolutions models less to work with. 其次,它的数据元素更粗糙,从而减少了乐观锁定和冲突解决方案模型的使用。

As a general rule, RDBMSs do not natively support optimistic locking. 通常,RDBMS本身不支持乐观锁定。 Many frameworks built on top of them do, but not the DBMSs themselves. 建立在它们之上的许多框架都可以,但是DBMS本身却不能。 Some may support it internally, but if they do, it's not exposed to the end users. 有些人可能会在内部支持它,但如果这样做,最终用户就不会知道。

Couch supports optimistic locking/versioning intrinsically, and relies up this for its replication. Couch固有地支持开放式锁定/版本控制,并依赖其进行复制。

In a RDBMS, most larger order data items are broken up in to their normalized, relational components. 在RDBMS中,大多数较大顺序的数据项被分解为它们的标准化关系组件。 A simple order may well be composed of a half dozen tables, each with their own row structure. 一个简单的订单很可能由六个表组成,每个表都有自己的行结构。 But the combination of tables and their relationships are what make up "an order". 但是表及其关系的组合才构成“订单”。 Given this finer grain representation of the order, it's difficult for the database to capture the concept of "change" at the higher level. 考虑到订单的这种细粒度表示,数据库很难在更高级别上捕获“更改”的概念。 What does "an order changed" mean? “订单已更改”是什么意思? The database sees a collection of nodes and relationships, not higher order meta objects like "orders". 数据库看到的是节点和关系的集合,而不是诸如“ orders”之类的高阶元对象。

The application can define change, but not as readily the database. 应用程序可以定义更改,但不能轻易定义数据库。

Now this is not so much an issue if you're replicating the entire database, but it's significantly more complicated if you're replicating a portion of the database. 现在,如果您要复制整个数据库,那么就不成问题了,但是如果要复制数据库的一部分,则要复杂得多。

In Couch, an order, for example, is an entire document. 例如,在Couch中,订单是整个文档。 Change the document, and the entire order "changes", and thus the entire order is replicated. 更改文档,整个订单“更改”,因此整个订单被复制。 In a RDBMS, if a line item changed, then it's easy enough to detect that one line changed, but does that mean the "order" changed? 在RDBMS中,如果订单项发生更改,那么很容易检测到某行发生了更改,但这是否意味着“订单”已更改? What if an item that the order is referring to changed, does that change the order? 如果订单所指的项目发生更改怎么办? You can see how this gets more complicated. 您会看到情况变得越来越复杂。

All of this can be built on top of the RDBMS, but then it's the application doing change management and facilitating replication, not the database. 所有这些都可以在RDBMS之上构建,但是,应用程序是在进行变更管理并促进复制,而不是数据库。

However, no matter what support CouchDB offers, it can only goes so far, and that caveat is highlighted in the quote: 但是,无论CouchDB提供什么支持,它都只能走得那么远,并且在引号中突出了警告:

When two versions of a document conflict during replication, the winning version is saved as the most recent version in the document's history. 如果在复制过程中两个版本的文档发生冲突,则胜出版本将另存为文档历史记录中的最新版本。 Instead of throwing the losing version away, as you might expect, CouchDB saves this as a previous version in the document's history, so that you can access it if you need to. CouchDB不会像您期望的那样丢掉丢失的版本,而是将其保存为文档历史记录中的先前版本,以便您可以在需要时访问它。 This happens automatically and consistently, so both databases will make exactly the same choice. 这是自动且一致地发生的,因此两个数据库将做出完全相同的选择。

It is up to you to handle conflicts in a way that makes sense for your application. 由您决定以对您的应用程序有意义的方式来处理冲突 You can leave the chosen document versions in place, revert to the older version, or try to merge the two versions and save the result. 您可以将选定的文档版本保留在原位,还原为较旧的版本,或者尝试合并两个版本并保存结果。

During replication, Couch simply has deterministic rules to make two systems consistent. 在复制过程中,Couch仅具有确定性的规则即可使两个系统保持一致。 But consistent doesn't make them correct. 但是一致并不能使它们正确。 When Couch detects two documents in conflict, it picks one, deterministically, and the winner stomps on the loser. 当Couch检测到两个有冲突的文档时,它将确定地选择一个,获胜者将在失败者身上脚。 But as far as your application is concerned, the loser may have been "right", or the correct document is the fusion of the two documents. 但是就您的申请而言,失败者可能是“正确的”,或者正确的文档是两个文档的融合。

You have to write that logic handle those merges. 您必须编写该逻辑来处理那些合并。 And this is a fundamental issue with all master-master replication schemes. 这是所有主-主复制方案的基本问题。 The technique to determine "who wins". 确定“谁获胜”的技术。 The "now what" problem when to different opinions on what the data should look like arrive at the same crossroads. 当对数据的外观有不同的意见时,“现在是什么”问题到达了相同的十字路口。

No system can handle that for you. 没有系统可以为您解决这个问题。 All a system can do is pick some set of rules it follows, or lets you configure to handle the problem, because the problem is almost always application dependent. 系统所能做的就是选择遵循的规则集,或者让您配置以处理问题,因为问题几乎总是取决于应用程序。

If the simpler model that Couch supports and projects for you works, then that's great. 如果Couch支持并为您设计的更简单的模型能够正常工作,那太好了。 If it doesn't, then you're kind of stuck. 如果不是这样,那么您就有点卡住了。 Many RDBMSs have solid support for Master-Slave replication, as it's a simpler model, and with that support it's pretty much transparent to the end user application. 许多RDBMS都是主从复制的可靠支持,因为它是一个更简单的模型,有了这种支持,它对最终用户应用程序几乎是透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM