পাব/সাব ও ইভেন্ট
topic, fan-out, at-least-once, ordering, DLQ আর outbox।
Start with the production incident, then the design decision that fixes it.
চলো-তে ride complete হলে চারটা কাজ হয়: rider-কে receipt, driver-কে payout, analytics-এ trip, আর loyalty point। প্রথমে ride service-এর ভিতরে চারটা function call ছিল। একদিন analytics service ধীর হলে পুরো ride completion ১২ সেকেন্ড আটকে গেল; আর loyalty call fail হলে payout-ও হয়নি, কারণ একই transaction-এ ছিল।
মাহিন বলল, "একটা ঘটনা অনেক জায়গায় যাবে, কিন্তু কেউ কারও জন্য অপেক্ষা করবে না।" এটাই pub/sub: publisher শুধু event publish করে, subscriber-রা নিজের মতো consume করে। Queue-তে এক job এক consumer পায়; pub/sub-এ এক event অনেক subscriber পায়। দুটোকে গুলিয়ে ফেললে delivery/ordering ভাঙে।
Topic ও event। ride.completed topic-এ event; subscriber group: billing, analytics, loyalty, notification। Event immutable, past tense, schema versioned।
Fan-out। এক publish, অনেক subscriber। প্রতিটা subscriber-এর নিজের queue/offset থাকে; একজন ধীর হলে অন্যদের আটকায় না। Queue-তে এক consumer group-এর ভিতরে load-balance; pub/sub-এ group আলাদা।
Delivery। At-least-once সাধারণ; consumer idempotent হতে হবে (event_id unique)। Exactly-once broker-এও end-to-end guarantee নয়; DB unique constraint/outbox লাগে।
Ordering। Global order ব্যয়বহুল; partition key (ride_id/user_id) দিয়ে per-key order। একই key এক partition-এ গেলে order রক্ষা।
Retry/DLQ। Consumer fail হলে backoff retry; বারবার fail হলে DLQ। Poison event পুরো partition আটকে রাখতে পারে—DLQ + alert।
Outbox। DB write আর event publish atomic নয়। Business row + outbox এক transaction-এ; worker outbox পড়ে publish করে; duplicate হলে consumer dedupe।
| প্রশ্ন | Queue | Pub/Sub |
|---|---|---|
| Consumer | এক job এক consumer | এক event অনেক subscriber |
| Fan-out | নেই | হ্যাঁ |
| Ordering | queue/partition | topic partition |
| Use | email, invoice | ride.completed, notifications |
| Failure | retry/DLQ | per-subscriber retry |
Outbox + idempotent consumer-এর কাঠামো:
BEGIN;
UPDATE rides SET status = 'COMPLETED' WHERE id = $1;
INSERT INTO outbox (event_id, topic, payload)
VALUES (gen_random_uuid(), 'ride.completed', $2);
COMMIT;
def handle(event):
if not db.insert_ignore("processed_events", event["id"]):
return # duplicate, already handled
if event["topic"] == "ride.completed":
billing.charge(event["ride_id"])
analytics.track(event)
loyalty.award(event["rider_id"])
insert_ignore unique event_id; duplicate publish হলেও side effect একবার। Outbox worker publish করার পরে sent_at বসায়; crash হলে আবার পাঠাবে, consumer dedupe করবে।
১. Event catalogue। কোন ঘটনা, কে publish, কে subscribe, schema, retention। ride.completed past tense, version।
২. Topic/partition। Key = ride_id/user_id; per-key order; subscriber group আলাদা।
৩. Outbox। Business write + outbox atomic; worker publish; retry; sent_at।
৪. Consumer idempotency। processed_events(event_id) unique; side effect একবার; DLQ।
৫. Failure drill। Subscriber down → backlog; poison event → DLQ; broker partition → publisher retry; replay → duplicate safe?
একটা ride.completed event-এ billing আর analytics subscriber। Billing ২ সেকেন্ড, analytics ১০ মিনিট ধীর। কাগজে দেখাও: queue হলে analytics billing-কে আটকাবে কি? Pub/sub-এ আলাদা group হলে কী হবে? কোনটায় ordering দরকার, লেখো।
Event schema evolve করলে backward-compatible field যোগ করো; subscriber পুরোনো schema পড়তে পারুক। PII event-এ নয়; reference id পাঠাও। Replay করার আগে consumer idempotency যাচাই করো, নইলে payout দুইবার যাবে।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
এক event publish হলে অনেক consumer group আলাদা করে পড়ে, তাই email আর analytics একসাথে চলে।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
ride.completed-এ billing, analytics, loyalty, notification।
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.
| প্রশ্ন | উত্তর |
|---|
| Fan-out? | pub/sub topic |
| One job? | queue |
| Duplicate? | event_id unique |
| Publish lost? | outbox |
| Poison? | DLQ |
| Order? | partition key |