ProgramGuru

Consensus and Leader Election

Agree on one order of important changes

Follow the responsibility

ClientProposes a change
LeaderCoordinates a replicated log
Voting majorityConfirms according to the protocol
Committed stateApplied in the agreed order
Client: Proposes a change → Leader: Coordinates a replicated log → Voting majority: Confirms according to the protocol → Committed state: Applied in the agreed order

When several machines coordinate a shared decision, a failed response does not tell them whether a peer is dead or merely unreachable. Consensus protocols help a defined group agree on a sequence of decisions under their stated assumptions.

A leader coordinates that work, but declaring yourself leader is not sufficient. The protocol must prevent conflicting committed decisions when communication fails and leadership changes.

What you’ll learn

  • Explain majorities without equating them with uptime.
  • Distinguish leader election from safe application effects.
  • Plan quorum placement and safe membership changes.

Overlapping majorities support a shared history

In a three-voter group, a majority is two; in a five-voter group it is three. Any two majorities overlap. Protocol rules about terms, votes and log history use that overlap to preserve agreement across leadership changes. Counting acknowledgements by itself is not a consensus protocol.

If a three-voter group splits into two connected voters and one isolated voter, only the majority side can make progress under an appropriate quorum protocol. The isolated node cannot promise a new committed change. This protects agreement while sacrificing progress on that side.

A new leader must respect committed history

A replicated log records ordered decisions. Raft, for example, uses election terms and log rules to choose a leader and preserve committed entries. A minority partition cannot safely turn itself into an independent replacement simply because its own timer expired.

Leader election also does not stop a paused worker from later writing to an external system. Use fencing tokens or equivalent enforcement at the resource for protected effects. Design application idempotency separately from agreement on the log.

Choose what the disconnected group may promise

Try the design choices

One voter is isolated from the other two.
A replacement worker gets leadership while the old worker is paused.

Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.

This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.

Select one option per situation. Every consequence remains visible; the record can be changed as you learn.

Use a three-voter coordination group and separate its coordination contract from the application database.

Use proven implementations and plan the voting topology

etcd uses Raft for distributed coordination and is used by Kubernetes for cluster state. ZooKeeper provides coordination through its own protocol. Database products have different leader and quorum mechanisms; consult their documentation rather than applying one generic majority rule to all reads and writes.

Place voters to match the failures you intend to tolerate. A five-voter group with three voters in one failed location loses its majority. More voters can add write coordination and network cost; they are not extra application request workers.

Read etcd’s learner and membership design before changing membership. A joining node needs to catch up; unsafe membership changes can destroy progress. Choose established software rather than implementing a new consensus protocol for an ordinary application.

Check your reasoning

A five-voter group loses contact with three voters. Can the remaining two alone form a majority?
Choose one answer. You can try again.

Answer and explanation

No; a majority of five is three.

The remaining two lack the required voting majority.

Check your reasoning

Does electing a leader automatically prevent an old worker writing to an external resource?
Choose one answer. You can try again.

Answer and explanation

No; the resource needs an appropriate fencing or ownership rule.

Coordination and protected external effects must connect.

Bring the decisions together

Consensus preserves agreement under a defined protocol; quorum loss can stop progress. Connect leadership to resource enforcement and plan membership changes.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: CAP Theorem Explained for Beginners →