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. Pub/Sub & Events

Pub/Sub & Events

পাব/সাব ও ইভেন্ট

Topic 23 of 41Story · visual · design practice

In this lesson

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

0/0 solved

topic, fan-out, at-least-once, ordering, DLQ আর outbox।

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.

কেন দরকার

চলো-তে 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।

প্রশ্নQueuePub/Sub
Consumerএক job এক consumerএক event অনেক subscriber
Fan-outনেইহ্যাঁ
Orderingqueue/partitiontopic partition
Useemail, invoiceride.completed, notifications
Failureretry/DLQper-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?

সাধারণ ভুল

  • Pub/sub আর queue গুলিয়ে ফেলা। এক event অনেক subscriber চাইলে pub/sub, এক job এক consumer চাইলে queue।
  • Global ordering চাওয়া। Partition key ছাড়া order রক্ষা কঠিন; per-key order যথেষ্ট।
  • Consumer idempotent না হওয়া। At-least-once-এ duplicate স্বাভাবিক; event_id unique রাখো।
  • DB commit আর publish একসাথে ভাবা। Publish ব্যর্থ হলে event হারাবে; outbox ব্যবহার করো।
  • Poison event চিরকাল retry। ৩–৫ বারের পরে DLQ-তে পাঠাও।
  • Event-এ PII/secret। Log/replay-তে leak; reference id পাঠাও।

ল্যাব: দুই subscriber সাজাও

একটা 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 দুইবার যাবে।

02

Step 2: Visual story

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

Concept visual

এক ঘটনা, অনেক শ্রোতা

এক event publish হলে অনেক consumer group আলাদা করে পড়ে, তাই email আর analytics একসাথে চলে।

tier 0

ride API
সার্ভিস

publish

tier 1

ride topic
কিউ

৩ partition

tier 2

email group
ওয়ার্কার

offset আলাদা

analytics group
ওয়ার্কার

offset আলাদা

tier 3

DLQ
কিউDLQ

৫ retry পরে

retry ×3 → DLQ
  1. 1topic-এ event পাঠাওproducer জানে না কে শুনছে
  2. 2consumer group আলাদা করোপ্রতি group নিজের offset রাখে
  3. 3at-least-once ধরে নাওconsumer idempotent না হলে ডাবল হয়
  4. 4ordering দরকার হলে key দাওএক key এক partition-এ থাকে
  5. 5retry শেষে DLQবারবার fail হলে আলাদা করে রাখো
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

Ride completed fan-out

20 min

ride.completed-এ billing, analytics, loyalty, notification।

তোমার কাজ

  1. 1topic/partition key বাছো।
  2. 2at-least-once duplicate কীভাবে আটকাবে?
  3. 3poison event/DLQ plan লেখো।

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

  • per-key order
  • event_id unique
  • DLQ/outbox আছে
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 23 of 41

PreviousMessage Queuesপরে করো, হারিও নাTopic 22 of 41
NextReal-time Updatesরিয়েল-টাইম আপডেটTopic 24 of 41
Back to track: System Design MasterclassAll learning tracks
প্রশ্নউত্তর
Fan-out?pub/sub topic
One job?queue
Duplicate?event_id unique
Publish lost?outbox
Poison?DLQ
Order?partition key