何时使用 Paxos(真实的实际用例)?
有人能给我一份 Paxos 真实用例的列表吗?这是真正的问题,需要作为更大问题的一部分达成共识。
以下是 Paxos 的用例吗?
假设有两个客户端在扑克服务器上互相玩扑克。扑克服务器被复制。我对 Paxos 的理解是,它可以用来维护代表当前扑克牌的内存数据结构的一致性。也就是说,确保所有副本都具有完全相同的手的内存状态。
但为什么需要 Paxos 呢?假设需要发一张新牌。如果一切顺利,运行相同代码的每个副本将生成相同的卡。为什么客户端不能只从所有复制的服务器请求最新状态并选择出现次数最多的卡。因此,如果一台服务器出现错误,客户端仍然可以通过选择多数来获得正确的状态。
Could someone give me a list of real use cases of Paxos. That is real problems that require consensus as part of a bigger problem.
Is the following a use case of Paxos?
Suppose there are two clients playing poker against each other on a poker server. The poker server is replicated. My understanding of Paxos is that it could be used to maintain consistency of the inmemory data structures that represent the current hand of poker. That is, ensure that all replicas have the exact same inmemory state of the hand.
But why is Paxos necessary? Suppose a new card needs to be dealt. Each replica running the same code will generate the same card if everything went correct. Why can't the clients just request the latest state from all the replicated servers and choose the card that appears the most. So if one server had an error the client will still get the correct state from just choosing the majority.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您假设所有服务器彼此同步(即具有相同的状态),因此当服务器需要选择下一张卡时,每个服务器将选择完全相同的卡(假设您的代码是确定性的)。
但是,服务器的状态还取决于用户的操作。例如,如果用户决定筹集 50 美元 - 您的服务器需要将该信息存储在某处。现在,假设您的服务器向网络客户端回复“ok”(我假设是基于网络的扑克游戏),然后服务器崩溃了。您的其他服务器可能没有有关 50 美元加薪的信息,并且您的系统将不一致(从某种意义上说,客户端认为已加薪 50 美元,而幸存的服务器则不知道这一点)。
请注意,大多数在这里没有帮助,因为数据丢失了。此外,假设主服务器没有崩溃,而是主服务器加上另一台服务器获得了 50 美元的加薪数据。在这种情况下,使用多数可能会更糟:如果您从两台服务器收到包含数据的响应,您会认为 50 美元的加薪已执行。但如果其中之一失败,那么你就不会获得多数票,并且你会认为加注没有执行。
一般来说,Paxos 可用于以容错方式复制状态机。其中“状态机”可以被认为是具有某种初始状态的算法,并且它根据从外部(即网络客户端)接收到的消息确定性地推进状态。
更正确地说,Paxos 应该被视为分布式日志,您可以在这里阅读更多相关信息:了解 Paxos – 第 1 部分
You assume all the servers are in sync with each other (i.e., have the same state), so when a server needs to select the next card, each of the servers will select the exact same card (assuming your code is deterministic).
However, your servers' state also depends on the the user's actions. For example, if a user decided to raise by 50$ - your server needs to store that info somewhere. Now, suppose that your server replied "ok" to the web-client (I'm assuming a web-based poker game), and then the server crashed. Your other servers might not have the information regarding the 50$ raise, and your system will be inconsistent (in the sense that the client thinks that the 50$ raise was made, while the surviving servers are oblivious of it).
Notice that majority won't help here, since the data is lost. Moreover, suppose that instead of the main server crashing, the main server plus another one got the 50$ raise data. In this case, using majority could even be worse: if you get a response from the two servers with the data, you'll think the 50$ raise was performed. But if one of them fails, then you won't have majority, and you'll think that the raise wasn't performed.
In general, Paxos can be used to replicate a state machine, in a fault tolerant manner. Where "state machine" can be thought of as an algorithm that has some initial state, and it advances the state deterministically according to messages received from the outside (i.e., the web-client).
More properly, Paxos should be considered as a distributed log, you can read more about it here: Understanding Paxos – Part 1
更新2018:
Mysql高可用性使用paxos:https://mysqlhighavailability.com/the-king-is-dead-long-live-the-king-our-homegrown-paxos-based-consensus/
真实世界示例:
Cassandra 使用 Paxos通过在写操作中添加“IF NOT EXISTS”来保证连接到不同集群节点的客户端能够安全地执行写操作。 Cassandra 没有主节点,因此可以在多个节点上同时发出两个冲突的操作。当使用 if-not-exists 语法时,paxos 算法用于在机器之间排序操作以确保只有一台成功。然后,客户端可以使用它存储具有到期租约的权威数据。只要大多数 Cassandra 节点正常运行,它就能工作。因此,如果您将键空间的复制因子定义为 3,则 1 个节点可能会失败,如果为 5,则 2 个节点可能会失败,等等。
对于正常写入,Caassandra 允许不同节点接受多个冲突的写入,这些节点可能暂时无法通信。在这种情况下,不使用 Paxos,因此可能会丢失数据对于同一个键,两次写入同时发生。 Cassandra 内置了特殊的数据结构,不会丢失仅插入的数据。
扑克和 Paxos:
正如其他答案所指出的,扑克是回合制的并且有规则。如果您允许一个主服务器和多个副本,则主服务器将仲裁下一步操作。假设用户首先单击“检查”按钮,然后改变主意并单击“折叠”。这些命令是相互冲突的,只有第一个命令应该被接受。浏览器不应该让他们按下第二个按钮,当他们按下第一个按钮时,浏览器会禁用它。由于涉及到金钱,主服务器也应该执行规则,并且每回合只允许每个玩家执行一个操作。当主控在游戏过程中崩溃时就会出现问题。哪个副本可以成为主副本以及如何强制只有一个副本成为主副本?
选择新主机的一种方法是使用外部强大的一致服务。我们可以使用 Cassandra 为主节点创建租约。副本可能会在主服务器上超时并尝试获取租约。由于 Cassandra 使用 Paxos,因此它具有容错能力;即使 Cassandra 节点崩溃,您仍然可以读取或更新租约。
在上面的示例中,扑克主机和副本最终是一致的。主服务器可以发送心跳,以便副本知道它们仍然连接到主服务器。当消息朝一个方向流动时,速度很快。当主服务器崩溃时,尝试成为主服务器的副本可能会出现竞争条件。此时使用 Paxos 可以让您对哪个节点现在是主节点的结果保持一致。这需要节点之间的额外消息以确保单个主节点的共识结果。
Update 2018:
Mysql High Availability uses paxos: https://mysqlhighavailability.com/the-king-is-dead-long-live-the-king-our-homegrown-paxos-based-consensus/
Real world example:
Cassandra uses Paxos to ensure that clients connected to different cluster nodes can safely perform write operations by adding "IF NOT EXISTS" to write operations. Cassandra has no master node so two conflicting operations can to be issued concurrently at multiple nodes. When using the if-not-exists syntax the paxos algorithm is used order operations between machines to ensure only one succeeds. This can then be used by clients to store authoritative data with an expiration lease. As long as a majority of Cassandra nodes are up it will work. So if you define the replication factor of your keyspace to be 3 then 1 node can fail, of 5 then 2 can fail, etc.
For normal writes Caassandra allows multiple conflicting writes to be accepted by different nodes which may be temporary unable to communicate. In that case doesn't use Paxos so can loose data when two Writes occur at the same time for the same key. There are special data structures built into Cassandra that won't loose data which are insert-only.
Poker and Paxos:
As other answers note poker is turn based and has rules. If you allow one master and many replicas then the master arbitrates the next action. Let's say a user first clicks the "check" button then changes their mind and clicks "fold". Those are conflicting commands only the first should be accepted. The browser should not let them press the second button it will disable it when they pressed the first button. Since money is involved the master server should also enforce the rules and only allow one action per player per turn. The problem comes when the master crashes during the game. Which replica can become master and how do you enforce that only one replica becomes master?
One way to handle choosing a new master is to use an external strong consistently service. We can use Cassandra to create a lease for the master node. The replicas can timeout on the master and attempt to take the lease. As Cassandra is using Paxos it is fault tolerant; you can still read or update the lease even if Cassandra nodes crash.
In the above example the poker master and replicas are eventually consistent. The master can send heartbeats so the replicas know that they are still connected to the master. That is fast as messages flow in one direction. When the master crashes there may be race conditions in replicas trying to be the master. Using Paxos at that point gives you strong consistently on the outcome of which node is now the master. This requires additional messages between nodes to ensure a consensus outcome of a single master.
现实生活中的用例:
松散耦合分布式系统的 Chubby 锁服务
Apache ZooKeeper
Real life use cases:
The Chubby lock service for loosely-coupled distributed systems
Apache ZooKeeper
我工作的公司 (WANdisco plc) 使用 Paxos 进行基于 WAN 的 Subversion 存储库复制和 Hadoop NameNode 的高可用性。
Paxos is used for WAN-based replication of Subversion repositories and high availability of the Hadoop NameNode by the company I work for (WANdisco plc.)
在你描述的情况下,你是对的,Paxos 并不是真正必要的:一个单一的中央权威可以为牌组生成一个排列,并在牌局开始时将其分发给每个人。事实上,对于一般的扑克游戏来说,像扑克一样有严格的回合顺序和单个活跃玩家,我看不出您可能需要使用 Paxos 的明智情况,除了可能选举中央权威机构洗牌。
一个更好的例子可能是同时进行动作的游戏,例如 Jeopardy。在这种情况下,Paxos 将允许所有服务器共同决定一系列时间紧密的事件(例如蜂鸣器按下)发生的顺序,以便所有服务器得出相同的结论。
In the case you describe, you're right, Paxos isn't really necessary: A single central authority can generate a permutation for the deck and distribute it to everyone at the beginning of the hand. In fact, for a poker game in general, where there's a strict turn order and a single active player as in poker, I can't see a sensible situation in which you might need to use Paxos, except perhaps to elect the central authority that shuffles decks.
A better example might be a game with simultaneous moves, such as Jeopardy. Paxos in this situation would allow all the servers to decide together what sequence a series of closely timed events (such as buzzer presses) occurred in, such that all the servers come to the same conclusion.