CAP ও কনসিস্টেন্সি
partition-এ consistency নাকি availability—CAP/PACELC আর quorum-এর আসল প্রশ্ন।
Start with the production incident, then the design decision that fixes it.
রাত ২টায় দুই ডেটাসেন্টারের নেটওয়ার্ক কেটে গেল। চলো-র ঢাকা region আলাদা হয়ে গেল সিঙ্গাপুর থেকে। একজন rider ঢাকায় ride বুক করল, সিঙ্গাপুরের replica এখনো জানে না। মাহিন জিজ্ঞেস করল, "এই মুহূর্তে বুকিং নেব, নাকি 'এখনই সম্ভব নয়' বলব?" নুসরাত বলল, "availability দরকার।" তাসনিম বলল, "double booking হলে তো আরও খারাপ।" দুজনেই ঠিক, কিন্তু একই সময়ে দুটোই পাওয়া যাবে না।
এটাই CAP: network partition হলে consistency আর availability-র মধ্যে একটা বেছে নিতে হয়। অনেকেই ভাবে CAP মানে "তিনটার মধ্যে দুইটা"—ভুল। Partition বাস্তবে ঘটে, তাই P ধরে নিয়ে C বা A বাছতে হয়। আধুনিক প্রশ্ন PACELC: partition না থাকলেও latency আর consistency-র trade-off আছে।
C (Consistency)। সব read সর্বশেষ write দেখবে, linearizable। Bank balance-এ দরকার; ভুল balance মানে টাকা হারানো।
A (Availability)। প্রতিটা request উত্তর পাবে, হয়তো stale data দিয়ে। Feed, presence, recommendations-এ চলে।
P (Partition tolerance)। নেটওয়ার্ক ভাঙলেও সিস্টেম চলবে। Distributed system-এ P বাদ দেওয়া যায় না; প্রশ্ন শুধু partition-এর সময় C নাকি A।
CP বনাম AP। CP: partition-এ minority side লিখতে দেয় না, quorum দরকার; ব্যাংক, inventory, booking lock। AP: দুই side-ই চলে, পরে merge; social feed, likes, presence। AP-তে conflict resolution (last-write-wins, version vector, CRDT) লাগে।
PACELC। Partition ছাড়াও (Else) latency বনাম consistency: synchronous replication strong কিন্তু ধীর; asynchronous দ্রুত কিন্তু stale। ৩ replica-তে quorum write W=2, read R=2 হলে R+W>3, তাই strong-ish; W=1, R=1 হলে দ্রুত কিন্তু stale।
Consistency মডেল। Linearizable > sequential > causal > read-your-write > eventual। যত শক্ত, তত দামি। সব ডেটার একই দরকার নেই—booking strong, view count eventual।
| চাহিদা | বেছে নাও | উদাহরণ |
|---|---|---|
| টাকা/booking | CP, strong | ledger, seat lock |
| Feed/likes | AP, eventual | news feed, reactions |
| Session | read-your-write | login, cart |
| Analytics | eventual | counters, dashboards |
| Config | quorum/consensus | feature flags |
Quorum read/write-এর ছোট simulation—W+R>N হলে সর্বশেষ write পড়া যায়:
def write(key, value, replicas, w=2):
acks = 0
for replica in replicas[:w]:
replica[key] = (value, version := replica.get(key, (None, 0))[1] + 1)
acks += 1
return acks >= w
def read(key, replicas, r=2):
values = [replica.get(key) for replica in replicas[:r]]
values = [item for item in values if item]
return max(values, key=lambda item: item[1]) if values else None
N=3, W=2, R=2 -> R+W=4 > 3: read sees latest acknowledged write
N=3, W=1, R=1 -> R+W=2 < 3: fast, but stale read possible
Partition-এ minority side W=2 পূরণ করতে পারবে না, তাই write fail—CP। AP চাইলে W=1 দিয়ে local write নাও, পরে conflict merge করো।
১. ডেটা ভাগ করো। কোন write হারালে টাকা যায়, কোনটা শুধু count—প্রতিটা entity-র consistency চাহিদা লেখো।
২. Partition scenario আঁকো। দুই region-এর মধ্যে link কাটলে কোন side write নেবে? Minority কী করবে—fail, queue, নাকি local?
৩. Quorum বাছো। CP-তে W+R>N; write latency বাড়লে W=1/R=2 বা regional quorum ভাবো। AP-তে conflict resolution লিখো।
৪. Conflict merge ঠিক করো। Last-write-wins timestamp ভুল হতে পারে; version vector/CRDT ব্যবহার করো। Booking-এ merge নয়, unique constraint।
৫. Failure drill। Partition ৩০ সেকেন্ড, ৫ মিনিট, ১ ঘণ্টা—প্রতিটায় user-facing behavior কী? Recovery-তে stale read কতক্ষণ?
কাগজে ঢাকা আর সিঙ্গাপুর দুই replica আঁকো। Link কাটার পর একজন ঢাকায় seat বুক করল, আরেকজন সিঙ্গাপুরে same seat। CP-তে কোনটা fail করবে, AP-তে কোনটা পরে merge হবে? দুই উত্তর লেখো, তারপর ঠিক করো চলো-র booking কোনটা হওয়া উচিত।
"আমরা CP" বলে দিলেই হয় না—কোন operation CP, কোনটা AP আলাদা করে লিখো। Multi-region write-এ quorum cross-region latency বাড়ায়; regional quorum + async replication ভাবো। Partition recovery-তে stale replica থেকে read না হয়, read-your-write routing রাখো।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
CAP আসলে partition-এর সময়ের প্রশ্ন—consistency ছাড়বে, না availability ছাড়বে।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
ঢাকা-সিঙ্গাপুর link কাটল; booking আর feed একই DB।
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.
| প্রশ্ন | উত্তর |
|---|
| CAP মানে? | partition-এ C নাকি A |
| PACELC? | else latency vs consistency |
| Strong read? | R+W>N |
| AP conflict? | LWW/version/CRDT |
| Booking? | CP, unique constraint |