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. Real-time Updates

Real-time Updates

রিয়েল-টাইম আপডেট

Topic 24 of 41Story · visual · design practice

In this lesson

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

0/0 solved

polling, long polling, SSE, WebSocket, fan-out আর reconnect/backfill।

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.

কেন দরকার

রাইড বুক করার পরে rider প্রতি ৫ সেকেন্ডে "driver কোথায়" poll করছে। ২ লাখ concurrent rider মানে সেকেন্ডে ৪০ হাজার API call, বেশিরভাগই "এখনো একই জায়গা"। DB/API load বাড়ল, তবু update ৫ সেকেন্ড দেরিতে—live feeling নেই। নুসরাত WebSocket যোগ করল, কিন্তু ২ কোটি connection-এ gateway memory ফুরাল, reconnect-এ পুরো history আসতে লাগল।

Realtime মানে server থেকে client-এ update push; polling নয়। চারটাবিকল্প: polling, long polling, SSE, WebSocket। প্রতিটার connection cost, direction, reconnect, infrastructure আলাদা। সঠিক choice না করলে scale-এ connection explosion বা unnecessary complexity।

মূল ধারণা

Polling। Client প্রতি N সেকেন্ডে GET; সহজ, stateless, কিন্তু waste + delay। Status page-এ ৫–১৫s ঠিক; live location-এ নয়।

Long polling। Server request ধরে রাখে update এলে বা timeout পর্যন্ত; near-realtime, কিন্তু প্রতি update-এ new request, connection churn।

SSE (Server-Sent Events)। HTTP connection-এ one-way server→client stream; browser auto-reconnect, Last-Event-ID দিয়ে resume। Dashboard/notification/live score-এর জন্য দুর্দান্ত; client→server message দরকার হলে WebSocket।

WebSocket। Bi-directional, persistent; chat/collab/live location। Connection stateful—gateway, heartbeat, fan-out, backpressure লাগে। ২০M connection = many gateways + Redis/pubsub.

Connection math। প্রতিটা gateway ৫০k connection, memory ২০–৫০KB/connection; ২০M → ৪০০ gateway। Heartbeat ৫s, presence TTL ১৫s; DB-তে heartbeat নয়।

Reconnect/backfill। Client last_event_id/since_seq পাঠায়; server gap-only fetch। Full history reconnect = thundering herd।

পদ্ধতিDirectionReconnectUse
Pollingpulltrivialslow status
Long pollpullper eventlegacy
SSEserver→clientbuilt-indashboard, feed
WebSocketbothmanualchat, live location

কোডে দেখি

SSE endpoint-এর কাঠামো—event id আর heartbeat:

def sse_stream(user_id, last_event_id):
    yield "retry: 3000\n\n"
    for event in subscribe(f"user:{user_id}", since=last_event_id):
        yield f"id: {event.id}\n"
        yield f"event: {event.type}\n"
        yield f"data: {json.dumps(event.data)}\n\n"
const source = new EventSource("/api/rides/stream");
source.onmessage = (event) => updateRide(JSON.parse(event.data));
source.onerror = () => { /* EventSource reconnects with Last-Event-ID */ };

WebSocket-এ heartbeat আর backpressure:

async def ws_handler(ws, user_id):
    await ws.send_json({"type": "ready"})
    while True:
        msg = await ws.receive_json()
        if msg["type"] == "ping":
            await ws.send_json({"type": "pong"})
        else:
            await bus.publish(f"user:{user_id}", msg)

ধাপে ধাপে

১. Direction ঠিক করো। শুধু server→client হলে SSE; client→server দরকার হলে WebSocket; rare update হলে polling।

২. Connection layer। Gateway pool, LB, heartbeat/timeout, per-connection memory budget; ৫০k/gateway estimate।

৩. Fan-out। Redis pub/sub/queue; user/channel key; offline হলে push/notification।

৪. Resume। Event id/seq; Last-Event-ID/since; gap-only backfill; full reload নয়।

৫. Failure drill। Gateway crash → reconnect storm; Redis down → fallback; slow client → drop/close; 20M reconnect wave।

সাধারণ ভুল

  • সব realtime-এ WebSocket। শুধু server→client হলে SSE সহজ ও কম খরচ।
  • Reconnect-এ full history। since/Last-Event-ID দিয়ে gap-only backfill করো।
  • Slow consumer-কে unbounded buffer। Memory explode; drop/close with resume।
  • Presence DB-তে heartbeat। ২০M connection-এ DB মরে যাবে; Redis TTL।
  • Proxy buffering না বন্ধ করা। SSE/WS data আটকে থেকে "realtime" ভাঙে।
  • Reconnect jitter না দেওয়া। Gateway restart-এ সব client একসাথে ঝাঁপ দেবে।

ল্যাব: connection হিসাব

২০M concurrent, প্রতি gateway ৫০k, memory ৩০KB/connection। কত gateway, মোট memory? ৫s heartbeat-এ Redis-এ কত write/s? কাগজে হিসাব করো; তারপর ঠিক করো SSE নাকি WebSocket।

চিট শিট

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

SSE/WebSocket-এ proxy buffering বন্ধ করো; idle timeout heartbeat-এর চেয়ে বড় রাখো। Connection storm এড়াতে reconnect-এ jitter/backoff। Slow consumer-কে unbounded buffer দিও না; drop/close with resume। 20M connection-এ presence DB-তে নয়—Redis TTL।

02

Step 2: Visual story

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

Concept visual

polling থেকে WebSocket

২০ মিলিয়ন connection-এ polling টেকে না, WebSocket gateway-ই connection ধরে রাখে।

poll ২s1/5
long-poll2/5
SSE3/5
WS gateway4/5
presence5/5
ধাপগুলো ক্রমে চলেশেষ ধাপেই ফল
  1. 1আগে polling মাপো২s interval-এ ২০M user = ১০M রিকোয়েস্ট/s
  2. 2SSE দিয়ে একমুখী streamসহজ, কিন্তু ক্লায়েন্ট থেকে পাঠানো যায় না
  3. 3WebSocket-এ দুইমুখীgateway-তে connection ধরে রাখো
  4. 4presence আলাদা রাখোRedis-এ online set, TTL ৩০s
  5. 5reconnect-এ backfill দাওlast message id থেকে আবার
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

Live ride status

20 min

২ লাখ concurrent rider প্রতি ৫s poll; WebSocket ভাবছে।

তোমার কাজ

  1. 1SSE vs WebSocket বাছো।
  2. 2gateway/connection/heartbeat estimate করো।
  3. 3reconnect/backfill design করো।

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

  • direction/connection math
  • since/event-id resume
  • backpressure আছে
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 24 of 41

PreviousPub/Sub & Eventsপাব/সাব ও ইভেন্টTopic 23 of 41
NextRedundancy & Failoverরিডান্ডেন্সি ও ফেইলওভারTopic 25 of 41
Back to track: System Design MasterclassAll learning tracks
প্রশ্নউত্তর
Server→client?SSE
Both way?WebSocket
Slow status?polling
Resume?event id/seq
Presence?heartbeat TTL
Scale?gateway pool