多智能体一致性、编队 入门
NOTE: This was initially posted at https://blog.bulloak.io/post/20200828-eventual-consistency-primer/ Visit my blog for the latest updates on this article and more articles on the subject.
注意:最初发布在https://blog.bulloak.io/post/20200828-eventual-consistency-primer/。请访问我的博客,以获取有关本文的最新更新以及有关该主题的更多文章。
This is the first blog in a short series about Consistency
这是有关一致性的简短系列文章中的第一个博客
When I decided to start my blog again, I created a poll about what should I write first. The reason was that I have gathered material about 2–3 subjects which I liked and wanted to speak about, but I wasn’t sure which one to start with. More people voted than I expected, but the decision was a tie between what different types of events would be helpful to recognise in an Event Sourced system, and Distributed consistency.
当我决定再次开始自己的博客时,我就应该首先写的内容进行了调查。 原因是我收集了我喜欢并想谈论的关于2-3个主题的资料,但是我不确定该从哪一个开始。 投票人数超过了我的预期,但是决定是在事件源系统中哪些不同类型的事件将有助于识别以及分布式一致性之间的联系。
I decided to write a bit about consistency first, which would also serve to establish some terminology before continuing on other (potentially more controversial) topics.
我决定先写一些关于一致性的文章,这还将有助于建立一些术语,然后再继续讨论其他(可能会引起更多争议的)主题。
最终的一致性是什么? (What is eventual consistency?)
Eventual consistency is a consistency model which guarantees that given some requests for change, then, in the absence of new changes, you would be able to see the result of those operations only after some indeterminate amount of time has passed. While it is a consistency model, it is probably easy to understand that it can co-exist with other consistency models in a system and, importantly, at the same time. One such example is the “read-your-writes” consistency model, which allows for eventual consistency across writes other than your own.
最终一致性是一种一致性模型,可以确保在给定某些更改请求的情况下,然后在没有新更改的情况下,只有在不确定的时间量过去之后,您才能看到这些操作的结果。 尽管它是一个一致性模型,但可能很容易理解它可以与系统中的其他一致性模型并存,而且很重要的是可以同时存在。 一个这样的例子就是“ read-your-writes”一致性模型,该模型可以实现除您自己以外的其他写入之间的最终一致性。
It is also important to mention that eventual consistency is a consistency model which paves the way for high availability, by making the tradeoff for availability over consistency from CAP (see note 1).
同样重要的是要提到,最终一致性是一种一致性模型,它通过在CAP的可用性与一致性之间进行权衡,从而为高可用性铺平了道路(请参见注释1)。
I’m not sure if this would help you, but I personally find it easier to think of eventual consistency as being a family of consistency models, or a characteristic rather than an actual, independent consistency model. The reason I say this is that as an independent consistency model, it makes the absolute minimum set of guarantees for distributed state, and it’s really permissive what can happen. This makes it quite challenging to use in practical terms in the absence of stricter guarantees which can enforce a minimum of expectations for our users to rely on. In fact, most of the times we use the term ‘eventual consistency’, we are referring to a consistency model which is indeed not immediately consistent, but does exhibit stricter guarantees than the eventual consistency model.
我不确定这是否对您有帮助,但我个人认为将最终一致性视为一致性模型家族或特性而非实际的独立一致性模型会更容易。 我之所以这样说是因为,作为一个独立的一致性模型,它为分布式状态提供了绝对最小的保证集,并且发生的事情真的是允许的。 这使得在缺乏更严格的保证的情况下在实践中使用具有很大的挑战性,这些保证可能会使我们的用户所依赖的期望达到最低限度。 实际上,在大多数情况下,我们使用术语“最终一致性”来表示一致性模型,该模型的确不是立即一致的,但是比最终一致性模型具有更严格的保证。
The below example illustrates how eventual consistency behaves:
以下示例说明了最终一致性的行为方式:

关于最终一致性的观察; 它的采用 (Observations about Eventual Consistency; and it’s adoption)
The most important thing to mention is that by adopting eventual consistency, some aspects of our users experience will be negatively affected. However, eventual consistency still is quite interesting, because while it may have its negatives, it does allow for high availability, which (depending on the domain) may be vastly more important than any inconvenience which may be introduced when we adopt eventual consistency. In fact, its support towards high availability makes it more than just an option, but an actual requirement for some classes of problems and systems.
要提到的最重要的事情是,如果采用最终的一致性,我们的用户体验的某些方面将受到负面影响。 但是,最终一致性仍然非常有趣,因为尽管它可能有负面影响,但它确实允许高可用性,这(取决于域)可能比采用最终一致性时可能带来的任何不便更为重要。 实际上,它对高可用性的支持使其不仅仅是一个选择,而是对某些类型的问题和系统的实际要求。
On the subject, there is one thing that I was just dying to say, and that is about the mentality and disposition which some people have against eventual consistency. If you read y-combinator posts (and even worse, some of the comments there), or Medium articles you’ll undoubtedly come across quite a large number of people advising against adopting eventual consistency. The popular rhetoric starts with teams adopting Event Sourcing and CQRS (some patterns which we’ll visit in this blog in due time) and failing in no small part due to Eventual Consistency.
关于这个问题,有一两件事,我只是渴望说,那是关于心态和性格其中有些人对最终一致性。 如果您阅读y-combinator的文章(甚至更糟糕的是,其中的一些评论)或Medium文章,那么无疑会遇到很多人建议不要采用最终的一致性。 流行的言论始于采用Event Sourcing和CQRS(我们将在适当的时候在此博客中访问的某些模式)的团队,但由于最终的一致性而在很大程度上失败了。
Now I won’t go into CQRS-specific options\comments, but what I will say is that from personal experience, a lot (but not all) of these problems stem from other aspects of each systems design, and are not inherent to Eventual Consistency. Instead they are mostly introduced by wrong system and transactional boundaries which exacerbate the negative tradeoffs of eventual consistency (most often by compounding those) to such an extent that they become a problem. In other words, I’m saying that systems which benefit from being distributed are inherently complex and exhibit complex problems. Unless we purposely design, and do that well, we run an increased risk of introducing complex bugs, which may involve eventual consistency in some parts. And this has been most of those peoples experience: they took a simple, synchronous, well-designed, non-distributed system, and tried to replicate it as an asynchronous, distributed one without going through the learning process required of the product team. Fundamentally, they’re facing a learning and mentality problem, not a technical one.
现在,我不再讨论CQRS特定的选项\注释,但是我要说的是,根据个人经验,很多(但不是全部)这些问题都来自每个系统设计的其他方面,并且不是最终的固有问题。一致性。 相反,它们大多是由错误的系统和交易边界引入的,它们加剧了最终一致性的负面权衡(大多数情况下是通过将它们加重),以至于它们成为问题。 换句话说,我要说的是,受益于分布式的系统本质上是复杂的,并且显示出复杂的问题。 除非我们有意识地进行设计并做得好,否则引入复杂错误的风险会增加,这可能会最终导致某些部分的一致性。 这些都是大多数人的经验:他们采用了一个简单,同步,设计良好的非分布式系统,并尝试将其复制为异步,分布式系统,而无需经历产品团队要求的学习过程。 从根本上讲,他们面临的是学习和心态问题,而不是技术问题。
In summary, I strongly believe, and it has been my personal experience as well, that eventual consistency allows for a significantly simpler systems from implementation and maintenance perspective, assuming the system needs to be distributed.
总而言之,我坚信并且根据我个人的经验,最终的一致性允许从实施和维护的角度出发,使系统大大简化,并假设系统需要分布式。
常见误解←“保证”位 (Common misunderstandings ← the “ranty” bits)
Now that we’ve seen the definition, a small analysis, and an example of eventual consistency, I’d like to go over some common misconceptions about eventual consistency.
既然我们已经看到了定义,简要分析和最终一致性示例,那么我想对一些关于最终一致性的常见误解进行回顾。
1.“即使电子也需要时间才能移动,所以即使分配变量也最终是一致的”,以及其他暗示所有操作都需要时间的示例 (1. “Even electrons need time to travel, so even assigning a variable is eventually consistent”, and other examples which imply all operations take time)
I’ve seen countless permutations of this argument. “Even if you write on paper, light takes time to travel to your eyes”, for example, and other similar ones, some specific to the domain at hand.
我见过无数的这种说法的排列。 例如,“即使您在纸上书写,光也需要时间才能到达您的眼睛”,以及其他类似的光,其中一些特定于手边的领域。
NOTE: There exists some truth, and relevance, to some of these arguments, but not in the obvious way. For example, once I’ve heard that “even writing to RAM is eventually consistent”. Well… That may be technically true because, for soft-write memory models, writes will stay in cache. This doesn’t happen in x86 architecture, (although it may still exhibit eventual consistency due to defaulting to soft reads) but it does happen in some other CPU architectures which employ a soft memory model.
注意: 对于其中一些论点 存在一些真理和相关性,但不是显而易见的。 例如,一旦我听说“即使写入RAM最终也是一致的”。 嗯……这在技术上可能是正确的,因为对于软写内存模型,写操作将保留在缓存中。 这 在x86架构 中不会 发生(尽管由于默认为软读取,它可能仍会显示出最终的一致性),但在采用软内存模型的其他一些CPU架构中,确实会发生这种情况。
Why isn’t this true then? Well, it’s not true because the time taken for the information to travel between the system and you is unimportant to the consistency model. What is of importance, is the ordering of operations, and how long they can take before completion.
那为什么不正确呢? 嗯,这是不正确的,因为信息在系统和您之间传播所需的时间对一致性模型并不重要。 重要的是操作的顺序以及完成之前需要花费的时间。
The time taken by light to reach your eyes, or a query to reach your PC across half the globe, are not part of the definition here. So long as you can query the value and see what you wrote, immediately after you wrote it, and at any time later (in the absence of further operations) then you aren’t eventually consistent, but have a stronger form of consistency.
光到达您的眼睛所需的时间,或查询到达全球一半的PC所花费的时间,不属于此处的定义。 只要您可以查询值并在编写之后立即查看值,然后在任何时候(在没有进一步操作的情况下)就可以看到最终写入的值,则最终并不一致,而是具有更强的一致性形式。
2.“一切最终都是一致的” (2. “Everything is eventually consistent anyway”)
No, not everything is eventually consistent. Linearizability is achievable, and by definition it is a different consistency model than eventual consistency. It does require locking over data and synchronisation across servers, it does require preferring unavailability in the face of partitions, and it does include the risk of unavailability due to locking required by the transactions mentioned earlier, but it is achievable.
不,并非所有事情最终都是一致的。 线性化是可以实现的,并且根据定义,它是与最终一致性不同的一致性模型。 它确实需要锁定数据并跨服务器进行同步,它确实更希望面对分区时不可用,并且确实存在由于前面提到的事务所需的锁定而导致不可用的风险,但是这是可以实现的。
That means that while it may not be a good option for systems which need to exhibit high availability, but it is doable and much stricter than eventual consistency.
这意味着,尽管对于需要表现出高可用性的系统来说,这可能不是一个好的选择,但它是可行的,并且比最终的一致性要严格得多。
3.“严格的一致性是不可能的” (3. “Strict consistency is impossible”)
This statement is true, but at the same time, it’s in my list of misconceptions and rants because of what is implied when people use it in support of Eventual Consistency. I may go over why strict consistency isn’t possible in another article (see note 2), but for now, I’ll ask you please to just accept the word of Wikipedia on the matter.
这个说法是正确的,但同时,在我的误解和错误列表中,这是因为人们使用它来支持最终一致性时所隐含的含义。 我可能会再说一遍,为什么在另一篇文章中无法做到严格的一致性(请参阅注释2),但是现在,我请您仅接受Wikipedia一词 。
The implicit thing which bothers me here is that there are only two forms of consistency: strict (sometimes people use the term “immediate”), and eventual. And because strict consistency is impossible, you’re left with just eventual consistency, so you better accept it! There are multiple consistency levels, which are identified, have been documented and exhibit different trade offs, each useful in different circumstances. It makes a lot of sense to know their guarantees, recognise which one we need, and how to apply and implement each correctly.
这里困扰我的隐含之处是,只有两种形式的一致性:严格(有时人们使用术语“立即”)和最终的。 而且由于不可能实现严格的一致性,因此最终的一致性将给您留下深刻的印象,因此您最好接受它! 已经确定了多个一致性级别,这些级别已被记录并显示出不同的折衷,每种折衷都在不同的情况下有用。 了解他们的保证,认识到我们需要哪种保证,以及如何正确地应用和实施这些保证,在很大程度上是有意义的。
最终的一致性很有用 (Eventual consistency is useful)
With this article, I hope you have a better understanding of what eventual consistency is and what it’s trade offs are, and I sincerely hope that I piqued your interest on this subject. In future articles, when I refer to eventual consistency, I’ll be referring to it as described above, and probably linking here.
希望通过这篇文章,您可以更好地了解最终的一致性以及需要权衡的问题,并真诚地希望引起您对这一主题的兴趣。 在以后的文章中,当我提到最终的一致性时,将如上所述,并且可能在此处进行链接。
Thank you for reading.
感谢您的阅读。
笔记 (Notes)
1 The CAP Theorem: The CAP theorem states that you cannot have linearizable consistency in the face of partitions. I very strongly suggest you read the paper in the further reading material list below
1 CAP定理:CAP定理指出,面对分区,您不能具有线性化的一致性。 我强烈建议您阅读以下更多阅读材料列表中的文章
2 Strong consistency: Tbh, I do not find a lot of reasons to expand on the strong consistency model because in it’s formal definition is so strict which makes it useful only in entirely theoretical contexts, and what Wikipedia has on the matter seems to be all that need to be said about strong consistency.
2强一致性:Tbh,我发现没有太多理由扩展强一致性模型,因为它的正式定义是如此严格,这使其仅在完全理论上才有用,而维基百科在此问题上所拥有的似乎全部关于强一致性需要说。
进一步阅读材料 (Further reading material)
Originally published at https://blog.bulloak.io on August 27, 2020.
最初于 2020年8月27日 发布在 https://blog.bulloak.io 。
翻译自: https://medium.com/swlh/a-primer-and-a-rant-about-eventual-consistency-ff9839fe0038
多智能体一致性、编队 入门
所有评论(0)