Paxos Consensus Algorithm
Transcript
[MUSIC PLAYING] LUIS QUESADA TORRES: Hello, my name is Luis Quesada Torres. I work as Site Reliability Engineer at Google. As Site Reliability Engineers, we often work on large-scale systems, some of which rely on algorithms to reach consensus so that changes to a certain entity are eventually perceived by the distributed fleet in a certain order, consistently. One of these algorithms is the Paxos algorithm, which is what I'm going to talk about today. So first I will present what is the Paxos algorithm
and why we need it. Then, I will introduce the Paxos algorithm itself. And finally, I will discuss a practical case, which is a distributed storage system based on Paxos. What is Paxos? Paxos is a family of distributed algorithms used to reach consensus. A family means there are many variants of the algorithm. We will only talk about the vanilla Paxos algorithm. Distributed means it runs in a set of computers or nodes, and consensus, we will discuss in one minute what reaching consensus means.
The Paxos algorithm was proposed in the paper that is listed at the bottom of the slide. What is to reach consensus with Paxos? Imagine you are together with some friends, and you want to agree on what to do this afternoon. You really don't care about exactly what you do. You want to do something together. So someone could start by proposing going to the cinema. Someone else could back it up, like, OK, let's go to a cinema. In the meanwhile, someone that hasn't been listening may propose something different, like, why don't we go have dinner.
And even some other people may agree with them and say, yes, I am in. Let's go have dinner. Eventually, in order to reach consensus, some of these people will have to change their mind. In this case, let's say someone switches from going to the cinema to going to have dinner. And eventually the last person will go for that, too, like, fine, let's go have dinner. Consensus is agreeing on one result. Once a majority agrees on a proposal in the Paxos world, that would be considered consensus.
So once three people know they want to go for dinner, everyone will go for dinner. It only may take some time for them to figure out. Any reached consensus can be eventually known by everyone. They could ask around whether a consensus was reached, and what the agreement was. In Paxos, the involved parties want to agree on any result, not on their proposal, so they will contribute towards reaching consensus.
And finally, communication channels may be faulty, that is messages can get lost. In this case you observe that someone wasn't listening, they proposed a different thing to do, in the end an agreement was reached. Why do systems need to reach consensus? Imagine you have a workstation at your home. You write a server, you make it public, you start getting visits from people, and it's kind of becoming a success. So more and more people are using it, and at some point your computer is too slow. You're running out of resources. So you upgrade the RAM, upgrade the CPU, and it happens again. It's still-- more and more people using it. You upgrade it again. But that has a limit.
You can't have an infinitely powerful and scalable single computer. You have to go for something different. You either go for a leader-replicas schema, in which one node is the leader and the other nodes are replicas. And whenever someone wants to propose writing a value, they would talk to the leader, and the leader would serialize the different proposals and send them to all the replicas. All the data would be written in all these nodes.
Or you go for a peer-to-peer schema, in which all nodes are the same and behave the same. And whenever they want to propose a new value, they send the proposals to each other to agree on what is the next proposal to be accepted, to be performed by everyone, so that everything is applied in the same order everywhere eventually. In the first case, if the leader becomes unavailable, nodes must reach consensus to elect a new one. Otherwise, the whole system would be stalled.
In the second one, the nodes must reach consensus continuously, so as to guarantee consistency so that operations are applied in the same order everywhere. In both cases, you need to reach consensus. That said, let's move on to some Paxos basics. Paxos defines three roles-- proposers, who propose values to reach consensus on; acceptors, who contribute to reaching the consensus itself; and learners, who learn the agreed upon value. They can be later queried for what their consensus value was.
In practice, Paxos nodes can take multiple roles, even all of them. So a single node-- a single server-- can send proposals to other nodes, they can contribute to reaching consensus, and they learn the final agreed upon value so that you can query them for it later. Paxos nodes must know how many acceptors a majority is. This is important because of a property which is two majorities will always overlap in at least one node. We will see these being needed several times
through the algorithm in order to guarantee a set of properties. Paxos nodes must be persistent. So even though the communications channels may be faulty, the Paxos nodes cannot forget what they accept. There are variants of Paxos that can deal with that, but vanilla Paxos won't. A Paxos run aims at reaching a single consensus. This is usually a source of confusion. So you can only agree on one value. Once a consensus has been reached on a certain value,
it won't progress to any other consensus-- any other different value, which means if you want a value to mutate over time, a different Paxos run must happen. We will see this in depth in the practical case by the end of the presentation. Now we can move to introducing the actual Paxos algorithm. Let's say we have a Proposer and three acceptors, so the majority of acceptors would be two. And let's say that Proposer wants to propose a certain value. It will send a PREPARE ID message
to a majority or all of the acceptors. The ID must be unique. It could be, for example, the timestamp in nanoseconds slotted by the Proposer. So that Proposer number 1 can choose IDs 1, 3, 5, and so on. And Proposer 2 can choose IDs 2, 4, 6, et cetera. And the point is that no ID is used twice, ever. In the case of a time out, the Proposer we'll retry with a new, higher ID. So this Proposer wants to propose a certain value, it sends PREPARE 5 to all the acceptors.
Now whenever an Acceptor receives a PREPARE message for a certain ID, it checks if it promised to ignore requests with this ID. If it did promise, it will ignore them. If it didn't promise, it promises to ignore any request lower than that ID. It will reply with PROMISE ID. You can observe there's an interrogation mark in there. I will have to add something here in the future. This Acceptor got a PREPARE 5 message, and we'll reply a PROMISE 5 message to the Proposer.
Once a majority of Acceptors promise, no ID lower than that ID can make it through. So if you get the majority of PROMISE 5, there is no way a majority of Acceptors will promise 4, because the two majorities would overlap. A majority of them would ignore their request with ID 4. Whenever Proposers get a majority of PROMISE messages for a specific ID, it will send ACCEPT REQUEST with that ID and the value to a majority or all of the acceptors.
For now, let's say the Proposer will pick any value it wants, so whatever they wanted to propose at the beginning of the Paxos run. In this case, this Proposer gets a majority of PROMISE 5 messages, and it sends ACCEPT REQUEST 5, 'cat' to all the acceptors. If an acceptor receives an ACCEPT REQUEST message for a certain ID and value, they check, did it promise to ignore requests with this ID? If they promised-- and please note this is the promise that we talked about before--
if they promised, they ignore it. This request won't make it through this Acceptor. If they didn't promise, they send ACCEPT with that ID and that value. And will also send it to all the Learners. So these acceptors that get ACCEPT REQUEST 5, 'cat,' they didn't promise to ignore request 5. They promised to ignore anything lower than 5. They send ACCEPT 5, 'cat' to the Proposer and to all the Learners. If a majority of Acceptors accept a certain ID and value, consensus is reached.
And that means consensus is and will always be on value-- not necessarily the ID. The ID is some Paxos internal artifact that doesn't show up outside of Paxos. So let's assume that a majority of Acceptors replied ACCEPT 5, 'cat.' A consensus has been reached on 'cat'. And then, the Proposers or Learners get ACCEPT messages for ID and value. If a Proposer or a Learner gets a majority of ACCEPT messages for a certain ID, they know that consensus has been reached on that value, not on the ID. Again, the ID may change. So if the Proposer gets two ACCEPT 5, 'cat,' it knows that its proposal made it to consensus. And the Learners can figure out the value the same way. There are three milestones that always happen sequentially in a Paxos run.
I will mark them with stars in this slide. First, a majority of Acceptors promise on a certain ID, so no ID lower than that can make it through, ever. Second, a majority of Acceptors accept a certain ID and value. Consensus is reached on that value. And third, whenever a Proposer or Learner gets a majority of ACCEPT messages for a certain ID, they know that the consensus has been reached on the value. And now let's see the couple of cases that we are missing.
Let's introduce a second Proposer who is unaware of any consensus and wants to propose something different. Let's say it sends a PREPARE 4 message to all the Acceptors, and these Acceptors get the message. A majority of them promised 5, so maybe one of them would reply PROMISE 4, but there is no way that a majority of Acceptors will promise 4. So the Proposer times out, retries again with a new higher ID, which would be, let's say, 6, and sends a new PREPARE 6 message to all the Acceptors.
The Acceptors check whether they promised to ignore requests with this ID. They didn't promise to ignore any request with that ID, so they will have to reply with PROMISE ID. But I'm going to add a little piece of logic there. They check, did I ever accept any value? If they didn't-- which was the case before, with PROMISE 5-- if they didn't ever accept any value, they reply with PROMISE ID like before. They haven't sent out any ACCEPT message, so PROMISE 5 is fine.
But right now, they have already accepted a proposal, so they are forced to reply with PROMISE 6, which is the ID of the current proposal, but I have accepted 5, 'cat' in the past. They send it to the Proposer, and the Proposer gets a majority of PROMISE messages for ID 6. Before we said it would pick any value it wanted, but let's complete that with some extra piece of logic. It checks if it has got any already accepted value from promises, so any piggybacked value.
If they haven't got any value, they could pick any value they wanted-- anything. But given that they got a piggybacked value, they are forced to pick the value with the highest accepted ID that they got. In this case it was 5, 'cat.' So they are forced to send ACCEPT REQUEST 6, which is this new proposal ID, but the piggybacked value, which was 'cat.' The rest is the same. Acceptors receive ACCEPT REQUEST messages for this ID and value. They didn't promise to ignore it. So they send ACCEPT 6, 'cat' to the Proposer and the Learners. The Proposer, the Learners get these ACCEPT messages and figure out what the consensus was. This is the whole Paxos algorithm. Let's review some of the key points here in a little bit more detail.
First of all, let's talk about majority of promises. Let's say we have two Proposers, and we have three Acceptors. Proposer 1 sends a PREPARE 5 message to the three Acceptors, and even gets one promise already. In the meanwhile, Proposer two sends a PREPARE 4 message to the three Acceptors, which please note can get there in a different order, because the different nodes may be located in different places. The latencies may be different. At the point Proposer 1 gets a PROMISE 5,
it has got a majority of promises. That means no IDs lower than that can make it through. The PREPARE 4 message will be ignored by Acceptor 1, will be ignored by Acceptor 2, even though it is possible that Proposer 2 gets a PROMISE message from a different Acceptor, just due to latency. Proposer 1 can continue with ACCEPT REQUEST 5, whatever value they want to propose, but REQUEST 4 will never make it through. Indeed, you may have observed that this may cause contention.
For example, Proposer 1 may send a PREPARE 5 message to a majority of Acceptors who may promise on it. Proposer 2, in the meanwhile, sends a PREPARE 6 message, and gets a majority of promises too. That means request number 5 will never make it through. It will be ignored by at least a majority of Acceptors. So maybe Proposer 1 times out and retries with a new higher ID, like PREPARE 7, gets a majority of promises, which steps over the proposal with ID 6
from Proposer 2, which will be ignored by a majority of nodes. So Proposer 2 may want to retry with a higher ID. Several proposals on the same Paxos run can cause hotspots that yield contention. It can really stall the Paxos run. A potential solution is to set an exponential backoff in place so that in that case this is happening, the exponential backoff will leave enough time for any of the proposals to go through entirely. At some point, after some iterations
on the exponential backoff, maybe PREPARE 8 will get the PROMISE 8, Proposer 1` will be waiting because of the backoff, and Proposer 2 will have enough time to send ACCEPT REQUEST and get it accepted, at which point consensus has been reached and the Paxos run is over. The other case is the majority of accepts. So let's say Proposer 1 sends PREPARE 5, gets a majority of promises, sends ACCEPT REQUEST 5, 'cat,' gets a majority of accepts,
and at that point consensus has been reached and is that value, 'cat.' No accept requests with lower ID will be accepted by the majority, because that would require a majority of promises for the lower ID, but we got it for the higher one, so they would ignore the lower one. Also, no accept requests with higher ID than the different value will be accepted by a majority, because at least an Acceptor will piggyback this ID and value in their promise, which will propagate to the accept request.
So while you can still have higher IDs, they will be on the same value. In this case, let's say Proposer 2 sends a PREPARE 6 message to a majority of Acceptors. It is guaranteed that at least one of them will piggyback the accepted 5, 'cat' in their promise, which means Proposer 2 can only send an ACCEPT REQUEST 6, their ID but value, 'cat,' which will be-- or can be-- accepted, and this is actually how Proposers and Learners can learn about Paxos consensus
if they missed a majority of ACCEPT messages in the past. A Proposer will try to propose a value, and the Acceptors will piggyback the consensus value, and the Proposer will have to propose it, and will get it accepted. Finally, let's briefly discuss a practical case, which is a distributed storage system based on the Paxos algorithm. This is a fictional storage system which is an extreme simplification of some parts of Megastore. The original Megastore paper is listed
at the bottom of the slide in case you want to check it. Let's say we have a user, and we have a bank account for this user, and we have it replicated. We have replica A, replica B, and replica C. So they are in different places, and we want to have the balance for a bank account of the user. We discussed before that Paxos can only reach consensus on one value that will never mutate. That doesn't work for a bank account, because we want to transfer money in, we want to withdraw money, and so on.
The solution to this is we are going to have a replicated log. This would be the log position 0, which is the initial state of the bank account, the state at the opening date of the bank account. Anytime we want the value to mutate, we will start a new consensus that will be on the next log position. If we want to transfer $50 in, we have to propose that log position 1 for User Luis will be plus $50, and maybe you can even piggyback that the balance is $150 just for efficiency reasons.
Then if the user wants to withdraw money, this has to be recorded in a different log position. A new consensus will start on log position 2 for User Luis so that the amount of money that is in their account is $130, because the user has withdrawn $20. The client would interact with this storage system as follows. Let's say we have storage servers in each of the replicas that act as Paxos nodes, let's say Proposer, Acceptor, and Learner all together.
The user talks to the storage server, can do a read or can propose a write. Let's say the user wants to write a withdrawal of $30. The storage server will act as a Paxos Proposer, and will send this proposal to all the Paxos Acceptors, which are three-- itself and the other two. With its Acceptors hat, it will accept the request, and maybe some other node accepts the request, so consensus has been reached. And at the point the Proposer knows that consensus
has been reached, the value can be written, and starts to be propagated. It's fine if only two of the replicas write the value. The third one will eventually figure out the value by some mechanism to catch it up, or when trying to propose a new value, they will try to propose it for the log position three, User Luis, which is the same Paxos run, which will get the $30 withdrawal piggybacked in the ACCEPT message. So eventually, the third replica will figure out what the right value is.
This is actually a very simplified storage system, but this is the way to use Paxos to implement a distributed storage system. With that we came to the end. I hope this helped you learn Paxos, and I hope you enjoyed the presentation. I certainly did. Thanks a lot. [MUSIC PLAYING]

Ask anything
We've read this whole study set, so questions about it get the best answers.