PAU OJ Online Judge
ProblemsLearnStudy plansContestsSubmissionsRank

Updates

Announcements and upcoming contests

Loading announcements…
Sign in for personal alertsBrowse contests
Log inSign up

PAU OJ

PrimeAsia University Online Judge

A real online judge for programming practice, university contests and coding assessments. Write code in your browser, get sandboxed verdicts, and track your progress.

Platform

  • Problems
  • Contests
  • Submissions
  • Ranking
  • Reviewers

Resources

  • User guide
  • Changelog
  • How judging works
  • System health
  • Create an account

© 2026 PrimeAsia University Online Judge. All rights reserved.

v0.9.0

  1. All tracks
  2. Master class
  3. Consensus & Leaders

Consensus & Leaders

কনসেনসাস ও লিডার

Topic 18 of 41Story · visual · design practice

In this lesson

  • Story & concept
    • কেন দরকার
    • মূল ধারণা
    • কোডে দেখি
    • ধাপে ধাপে
    • সাধারণ ভুল
      • ল্যাব: quorum গোনো
      • চিট শিট
      • প্রোডাকশন নোট
  • Visual story
  • Design practice
  • Checkpoint
  • Discussion
Back to track

0/0 solved

Raft/Paxos, quorum, leader election আর split brain—কেন majority-ই সত্য।

In this lesson

  • Story & concept
  • Visual story
  • Design practice
  • Checkpoint
  • Discussion
01

Step 1: Story & concept

Start with the production incident, then the design decision that fixes it.

কেন দরকার

চলো-র payment ledger তিনটা replica-তে চলে। একদিন network partition-এ দুই replica আলাদা হয়ে গেল, আর দুজনই নিজেকে primary দাবি করে payment লিখল। পরে merge-এ দেখা গেল একই invoice দুইবার—একটা replica-র write আরেকটা হারিয়েছে। মাহিন বলল, "আমাদের leader election দরকার, কিন্তু ভোটের সংখ্যা গোনার নিয়মটাও দরকার।"

Consensus মানে একদল সার্ভার একটা সিদ্ধান্তে একমত হবে—কে leader, কোন log entry committed, কোন configuration। Raft/Paxos এর ভিত্তি: majority quorum। ৫টা node-এ ৩টার সম্মতি ছাড়া কোনো সিদ্ধান্ত নেই; partition-এ যার majority নেই, সে leader হতে পারে না। এটাই split brain আটকায়।

এই টপিকের লক্ষ্য: quorum, term, log replication আর leader election-এর intuition—যাতে তুমি বুঝতে পারো etcd/ZooKeeper কেন লাগে, আর কখন লাগে না।

মূল ধারণা

Quorum = ⌊N/2⌋+1। ৩ node-এ ২, ৫-এ ৩। যেকোনো দুই quorum-এর intersection থাকবেই, তাই একই term-এ দুই leader সম্ভব নয়। ২ node-এ quorum ২—একটা মরলেই write বন্ধ; তাই consensus-এর জন্য ৩/৫ বিজোড় সংখ্যা ভালো।

Term আর log। প্রতিটা election-এ term বাড়ে। Leader log entry propose করে, majority replicate করলে committed। follower-এর log leader-এর সাথে মিলে গেলে তবেই সে vote দেয়; এতে committed entry হারায় না।

Leader election। Follower-এর election timeout (১৫০–৩০০ms random) শেষ হলে candidate হয়, term বাড়িয়ে vote চায়। majority পেলে leader। Split vote হলে আবার random timeout—জitter-ই liveness দেয়।

Linearizable read। Leader শুধু committed state পড়ে; follower read দরকার হলে read-index/lease লাগে, নইলে stale। Consensus মানে প্রতিটা read-ও free নয়।

কোথায় লাগে, কোথায় নয়। Leader election, config, distributed lock, metadata, payment ledger—হ্যাঁ। প্রতি user post বা high-throughput counter-এ consensus নয়; সেখানে CRDT/eventual বা single writer ভালো।

NQuorumএকটি node downদুটি down
32চলেবন্ধ
53চলেচলে
74চলেচলে
22বন্ধবন্ধ

কোডে দেখি

Raft-ধাঁচের vote আর commit-এর সরল pseudo-code:

class Node:
    def __init__(self, peers):
        self.term = 0
        self.voted_for = None
        self.log = []
        self.commit_index = -1
        self.peers = peers

    def request_vote(self, term, candidate_id, last_index, last_term):
        if term < self.term:
            return False
        if term > self.term:
            self.term, self.voted_for = term, None
        up_to_date = (last_term, last_index) >= (self.log[-1].term if self.log else 0, len(self.log) - 1)
        if self.voted_for in (None, candidate_id) and up_to_date:
            self.voted_for = candidate_id
            return True
        return False

    def append(self, entry):
        self.log.append(entry.txt)
        # leader replicates to peers; commit when majority acks
        acks = 1 + sum(peer.append(entry) for peer in self.peers)
        if acks >= len(self.peers) // 2 + 1:
            self.commit_index = len(self.log) - 1
All writes go through the leader; followers only redirect or read with read-index.
A leader without majority cannot commit, so it steps down.

ধাপে ধাপে

১. Consensus দরকার কি না ঠিক করো। শুধু leader election/lock/metadata/ledger—হ্যাঁ। প্রতি high-volume write-এ নয়।

২. Cluster size বাছো। ৩ node dev/small, ৫ production; even number নয়। Cross-AZ latency quorum-এ যোগ হয়।

৩. Timeout টিউন করো। Heartbeat ৫০ms, election timeout ১৫০–৩০০ms random; cross-region হলে বাড়াও, নইলে অকারণ election।

৪. Read path ঠিক করো। Leader read, read-index, বা lease; follower stale read চাইলে explicit।

৫. Failure drill। Leader crash, minority partition, follower lag, disk slow—কতক্ষণ write বন্ধ, কে leader হবে, client retry করবে কি না।

সাধারণ ভুল

  • ২ node-এর cluster-এ consensus ভাবা। একটা মরলেই quorum শেষ; ৩/৫ বিজোড় সংখ্যা ব্যবহার করো।
  • Follower থেকে সরাসরি read করে stale data সার্ভ করা। read-index/lease ছাড়া follower read consistent নয়।
  • Leader-এর clock-এর ওপর election নির্ভর করা। Term আর quorum-ই সিদ্ধান্ত দেয়।
  • Cross-region ৫ node দিয়ে low-latency write আশা করা। Quorum round-trip latency বাজেটে ধরো।
  • Consensus দিয়ে সব write করা। Metadata/lock-এ দরকার, high-volume post/counter-এ নয়।

ল্যাব: quorum গোনো

৫ node-এর cluster-এ ২টা node dead। কতগুলো alive থাকলে write চলবে? ৩ alive থাকলে quorum ৩, তাই চলবে; ২ alive হলে বন্ধ। এখন ৪ node-এর cluster-এ ২ dead—কেন ২ alive দিয়ে চলবে না? কাগজে quorum হিসাব লেখো।

চিট শিট

প্রোডাকশন নোট

Consensus latency মানে quorum round-trip; cross-region ৫ node দিয়ে প্রতি write-এ ১০০ms+ যেতে পারে। Metadata-র জন্য etcd, application data-র জন্য নয়। Leader election-এ clock-এর ওপর ভরসা করো না; term/quorum-ই সত্য। Backup/restore-এ consensus state আলাদা করে রাখো।

02

Step 2: Visual story

See the mental model first — the animation and the steps below it say the same thing.

Concept visual

মেজরিটি রাজি হলে সিদ্ধান্ত

Raft-এ leader term বাড়িয়ে ভোট চায়, আর ৩/৫ quorum পেলেই সিদ্ধান্ত পাকা হয়।

L1L2L3F1F2
unvisitedvisitedactive edge

quorum 3/5 · term 2

  1. 1follower ভোট দেয়term বাড়লে পুরোনো leader বাদ
  2. 2quorum লাগে৫ node-এ ৩টে রাজি হলেই commit
  3. 3log replicate হয়প্রতি entry term আর index বহন করে
  4. 4split brain আটকায়দুই পক্ষই একসাথে ৩ ভোট পায় না
  5. 5etcd আর ZooKeeper এটাই চালায়lock আর config-এর ভিত্তি
5টি ধাপে দেখা হচ্ছেগতি কমালে স্থির
03

Step 3: Design practice

Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.

Design practice

Leader election

25 min

৫ node cluster; leader crash + minority partition।

তোমার কাজ

  1. 1quorum ও election timeout design করো।
  2. 2split brain কীভাবে আটকাবে?
  3. 3read consistency কীভাবে রাখবে?

ভালো উত্তরে কী থাকবে

  • quorum ⌊N/2⌋+1
  • term/vote mechanism
  • read-index/lease
04

Step 4: Checkpoint

One timed attempt. Solve every problem to earn the module badge.

Final checkpoint

Ready to prove this module?

45 minutes3 problemsScale Architect badge
Go to checkpoint
05

Step 5: Discussion

Ask questions, share an approach or leave a study tip for this lesson.

Was this lesson helpful?

Your reaction helps other learners judge these notes.

Sign in to react

Discussion

0 comments

Sign in to ask a question or share an approach.

Sign in to comment

No comments yet

Be the first to ask a question or leave a study tip.

Topic 18 of 41

PreviousCAP & ConsistencyCAP ও কনসিস্টেন্সিTopic 17 of 41
NextTransactions & Isolationট্রানজেকশন ও আইসোলেশনTopic 19 of 41
Back to track: System Design MasterclassAll learning tracks
প্রশ্নউত্তর
Quorum?⌊N/2⌋+1
Cluster size?৩/৫, বিজোড়
Split brain?majority ছাড়া leader নয়
Read?leader/read-index/lease
কোথায়?lock, config, ledger