রিয়েল-টাইম আপডেট
polling, long polling, SSE, WebSocket, fan-out আর reconnect/backfill।
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।
| পদ্ধতি | Direction | Reconnect | Use |
|---|---|---|---|
| Polling | pull | trivial | slow status |
| Long poll | pull | per event | legacy |
| SSE | server→client | built-in | dashboard, feed |
| WebSocket | both | manual | chat, 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।
since/Last-Event-ID দিয়ে gap-only backfill করো।২০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।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
২০ মিলিয়ন connection-এ polling টেকে না, WebSocket gateway-ই connection ধরে রাখে।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
২ লাখ concurrent rider প্রতি ৫s poll; WebSocket ভাবছে।
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.
| প্রশ্ন | উত্তর |
|---|
| Server→client? | SSE |
| Both way? | WebSocket |
| Slow status? | polling |
| Resume? | event id/seq |
| Presence? | heartbeat TTL |
| Scale? | gateway pool |