---
title: "Election algorithm need and example"
subject: "Distributed System"
chapter: 6
key: "semester_7/distributed_system:election-scenario"
source: https://www.bctengineer.com/q/semester_7/distributed_system/election-scenario
site: BCT Engineering
---

# Election algorithm need and example

Distributed System · Chapter 6: Coordination and Agreement

- Asked in: 2069 Chaitra, 2+4 marks; Model question (asked 2 times)
- Video: [Bully and Ring Election Algorithm Explained  | Distributed System & Computing Lectures ✅](https://www.youtube.com/watch?v=vKpc0y_Ik3E) — Last moment tuitions
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/distributed_system/election-scenario
- Whole chapter: https://www.bctengineer.com/md/semester_7/distributed_system/chapter-6

## Question

Present a practical scenario where you need an election algorithm. Explain an election algorithm with example that is suitable to your scenario. (What is the need of an election algorithm; explain with few practical examples.)

## Short answer

In a replicated database, if the primary server fails, an election algorithm quickly selects the highest-id backup as the new primary to maintain write availability. Bully algorithm: the detecting backup sends ELECTION to higher ids; receiving no reply, it becomes coordinator and broadcasts COORDINATOR. Example: servers 1–5; when 5 crashes, 4 wins and becomes primary after holding its own election.

## Answer

#### Practical scenario
**A distributed database with a primary server.** Five replicated database servers (ids 1 to 5) serve a bank. All writes go to the primary (coordinator), which copies them to backups. If the primary crashes, the backups must choose a new primary quickly, otherwise no write is possible. Other examples: choosing a new master node in a Hadoop/Kafka/ZooKeeper cluster, a new time daemon in the Berkeley algorithm, and regenerating a lost token in a token ring.

#### Need of an election algorithm
- Many algorithms need one coordinator (centralised mutual exclusion, time server, sequencer, primary of a replica group).
- If the coordinator fails, the rest must agree on a single replacement automatically.
- Election ensures exactly one coordinator, avoiding conflicts (two primaries = split brain) and giving high availability.

#### Bully algorithm for the scenario
The highest server id which is alive becomes the primary.
1. A backup notices that the primary (5) does not answer heartbeat or request, and starts an election by sending ELECTION to servers with higher id.
2. A higher server that is alive replies OK and takes over the election.
3. If no one replies within the timeout, the initiator declares itself the primary and sends COORDINATOR to all.

Example: server 5 fails. Server 2 detects and sends ELECTION to 3, 4, 5. Servers 3 and 4 reply OK. Server 3 sends ELECTION to 4, 5; server 4 sends to 5. 4 receives no answer from 5, so 4 broadcasts COORDINATOR(4). Servers 1, 2, 3 now send writes to server 4. When server 5 is repaired, it starts an election, as it has the highest id, and becomes the primary again after getting the latest data.

```text
 1  2  3  4  5(X)
    |--ELECTION-->3,4,5
    |<--OK-- 3,4
          4 --ELECTION--> 5 (no reply)
          4 --COORDINATOR(4)--> 1,2,3
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. In the primary database scenario, why is an election needed when the primary fails?
   A. Writes cannot proceed until the backups agree on a single new primary
   B. To delete the old data
   C. To reduce disk space
   D. To synchronise clocks
   Answer: A. One new primary must be chosen quickly for writes; two primaries would cause split brain. (Key point: Scenario need)

2. What problem does election avoid?
   A. Slow disks
   B. Two primaries (split brain) causing conflicts
   C. Duplicate IP addresses
   D. Token duplication only
   Answer: B. Election ensures exactly one coordinator. (Key point: Split brain)

3. Server 5 fails; server 2 detects it. Which server becomes the primary?
   A. Server 2
   B. Server 3
   C. Server 4, as the highest live id
   D. Server 1
   Answer: C. 4 gets no answer from 5 and broadcasts COORDINATOR(4). (Key point: Bully example)

4. What happens when server 5 is repaired?
   A. It stays idle forever
   B. It joins as lowest priority
   C. It forces all to restart
   D. It starts an election and, with the highest id, becomes primary after getting the latest data
   Answer: D. A recovered highest-id process bullies its way back to coordinator. (Key point: Recovered server)

## Related in Distributed System

- [Need of consensus; Bully algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aconsensus-bully) — Distributed System, chapter 6
- [Central coordinator election; consensus](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aelection-central-consensus) — Distributed System, chapter 6
- [Bully algorithm vs ring algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aelection-bully-ring) — Distributed System, chapter 6
- [Cristian's algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Acristian-algorithm) — Distributed System, chapter 5, asked 3×
- [Need of logical clock synchronization](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alogical-clock-sync-need) — Distributed System, chapter 5
- [Clock synchronization need; Lamport](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aclock-sync-lamport-need) — Distributed System, chapter 5

## Related in other subjects

- [Nepalese electricity sector scenario](https://www.bctengineer.com/question.md?key=semester_8%2Fpower_plant_design%3Anepal-electricity-scenario) — Power Plant Design, chapter 1
- [World renewable energy scenario](https://www.bctengineer.com/question.md?key=semester_8%2Fpower_plant_design%3Aworld-renewable-scenario) — Power Plant Design, chapter 1
- [Need for replacement algorithm in cache](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Areplacement-why) — Computer Organization and Architecture, chapter 6
- [Practical considerations in sampling; natural sampling](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Asampling-practical-natural) — Communication Systems, chapter 4
