Paxos Consensus Interview Questions

Paxos consensus algorithm interview questions covering the classic distributed consensus protocol.

Q1: How does Paxos consensus work?

Answer:

Paxos is a consensus algorithm for distributed systems that ensures agreement among nodes even with failures.

Sequence Diagram:

Overall Flow Diagram:

Individual Node Decision Diagram:

Paxos Phases:

Phase 1: Prepare

  1. Proposer sends prepare(n) with proposal number n
  2. Acceptors respond:
    • If n > highest_seen: Promise not to accept proposals < n, return highest accepted value
    • Otherwise: Reject

Phase 2: Accept

  1. If majority promise: Proposer sends accept(n, v) with value v
  2. Acceptors accept if n >= highest_seen
  3. If majority accept: Consensus reached

Key Properties:

  • Safety: Only one value can be chosen
  • Liveness: Eventually reaches consensus (if no failures)
  • Fault Tolerance: Works with up to (n-1)/2 failures

Example:

 1class PaxosNode:
 2    def __init__(self, node_id):
 3        self.node_id = node_id
 4        self.highest_seen = 0
 5        self.accepted_value = None
 6        self.accepted_proposal = 0
 7    
 8    def prepare(self, proposal_num):
 9        if proposal_num > self.highest_seen:
10            self.highest_seen = proposal_num
11            return {
12                'promise': True,
13                'accepted_proposal': self.accepted_proposal,
14                'accepted_value': self.accepted_value
15            }
16        return {'promise': False}
17    
18    def accept(self, proposal_num, value):
19        if proposal_num >= self.highest_seen:
20            self.highest_seen = proposal_num
21            self.accepted_proposal = proposal_num
22            self.accepted_value = value
23            return {'accepted': True}
24        return {'accepted': False}

Use Cases:

  • Distributed databases
  • Configuration management
  • State machine replication

Related Snippets