কনসেনসাস ও লিডার
Raft/Paxos, quorum, leader election আর split brain—কেন majority-ই সত্য।
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 ভালো।
| N | Quorum | একটি node down | দুটি down |
|---|---|---|---|
| 3 | 2 | চলে | বন্ধ |
| 5 | 3 | চলে | চলে |
| 7 | 4 | চলে | চলে |
| 2 | 2 | বন্ধ | বন্ধ |
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-এ ২টা 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 আলাদা করে রাখো।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
Raft-এ leader term বাড়িয়ে ভোট চায়, আর ৩/৫ quorum পেলেই সিদ্ধান্ত পাকা হয়।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
৫ node cluster; leader crash + minority partition।
One timed attempt. Solve every problem to earn the module badge.
Final checkpoint
Ready to prove this module?
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.
0 comments
Sign in to ask a question or share an approach.
Sign in to commentNo comments yet
Be the first to ask a question or leave a study tip.
| প্রশ্ন | উত্তর |
|---|
| Quorum? | ⌊N/2⌋+1 |
| Cluster size? | ৩/৫, বিজোড় |
| Split brain? | majority ছাড়া leader নয় |
| Read? | leader/read-index/lease |
| কোথায়? | lock, config, ledger |