
Real-Time APIs: Webhooks Versus Polling
A deep dive into real-time API architecture, comparing Short Polling, Long Polling, and Webhooks, exploring HMAC signature verification, idempotent retry strategies, and delivery guarantees.
The Challenge of Real-Time REST APIs
Traditional REST Architecture ဟာ Request-Driven Model ပေါ်မှာ အခြေခံထားပါတယ်။ Client က မေးမြန်းမှသာ Server က အချက်အလက် ပြန်ပေးတဲ့ Stateless Pull-based Model ဖြစ်ပါတယ်။ ဒါပေမဲ့ Modern Web Applications တွေမှာ Third-party Payment Gateways (Stripe), CI/CD Build Pipelines (GitHub Actions), E-commerce Order Tracking နဲ့ Telemedicine Updates တွေဟာ စနစ်တစ်ခုရဲ့ အခြေအနေ ပြောင်းလဲသွားတာနဲ့ ချက်ချင်း အသိပေးချက် ရရှိဖို့ လိုအပ်လေ့ ရှိပါတယ်။
ဒီလို Real-time Updates တွေကို REST API ဒီဇိုင်းမှာ အကောင်အထည်ဖော်ဖို့အတွက် အဓိက မဟာဗျူဟာ နှစ်မျိုး ရှိပါတယ်။
- Pull Model (Polling): Client က Server ဆီကို Data အသစ် ရောက်မရောက် အချိန်အပိုင်းအခြားအလိုက် ထပ်ခါတလဲလဲ သွားရောက် မေးမြန်းခြင်း ဖြစ်ပါတယ်။
- Push Model (Webhooks): Server ပေါ်မှာ Event အသစ် ဖြစ်ပေါ်လာတဲ့အခါ Client က သတ်မှတ်ပေးထားတဲ့ Callback URL ဆီသို့ HTTP POST Request ဖြင့် တိုက်ရိုက် လှမ်းပို့ပေးခြင်း ဖြစ်ပါတယ်။
ဒီ ချဉ်းကပ်မှု နှစ်ခုဟာ Network Traffic, Server Resource Consumption, Latency နဲ့ System Complexity တွေမှာ အခြေခံကျကျ ကွဲပြားကြပါတယ်။
Polling: Short Polling vs Long Polling
Polling ချဉ်းကပ်မှုမှာ Client က Data ရရှိဖို့အတွက် အသုံးပြုလေ့ရှိတဲ့ နည်းလမ်း နှစ်မျိုး ရှိပါတယ်။
-
Short Polling (ရိုးရိုး ပုံမှန် မေးမြန်းခြင်း): Client က သတ်မှတ်ထားတဲ့ ကြာချိန် (ဥပမာ ၃ စက္ကန့် သို့မဟုတ် ၅ စက္ကန့်) တိုင်းမှာ
GET /api/orders/123/statusကို ဆက်တိုက် ခေါ်ဆိုနေတာ ဖြစ်ပါတယ်။ အများအားဖြင့် Request ပေါင်း ၉၅% ကျော်ဟာ Data အပြောင်းအလဲ မရှိဘဲ Empty Response သာ ပြန်ရလေ့ ရှိပါတယ်။ ဒါဟာ Database Connections တွေ၊ TLS Handshakes တွေနဲ့ Network Bandwidth တွေကို အလဟဿ ကုန်ဆုံးစေပါတယ်။ Client ပေါင်း ထောင်သောင်းချီ တစ်ပြိုင်နက် Poll လုပ်လာတဲ့အခါ Database ပေါ်မှာ မလိုအပ်ဘဲ Read Traffic ဒဏ် ပိလာစေပါတယ်။ -
Long Polling (Comet / Hanging GET): Client က Request ပို့လိုက်ချိန်မှာ Server က ချက်ချင်း မဖြေဘဲ Connection ကို ခေတ္တ ဆွဲထားပါတယ်။ Data အသစ် ရောက်လာမှ (သို့မဟုတ် Timeout မတိုင်ခင်) မှသာ Response ပြန်ထုတ်ပေးပြီး Connection ပိတ်လိုက်ပါတယ်။ Client က ချက်ချင်း Request အသစ် ပြန်ဖွင့်ပါတယ်။ Short Polling ထက် Latency နည်းပေမဲ့ Server ပေါ်မှာ Open TCP Sockets ပေါင်း ထောင်ချီကို ထိန်းသိမ်းထားရတဲ့အတွက် Server Memory Pressure မြင့်တက်စေပါတယ်။ Worker Threads အကန့်အသတ်ရှိတဲ့ Node.js သို့မဟုတ် Python WSGI Servers တွေမှာ Connection Exhaustion ဖြစ်ပေါ်စေနိုင်ပါတယ်။
Webhooks: The Event-Driven Push Model
Webhooks တွေကို Reverse APIs လို့လည်း ခေါ်ဆိုကြပါတယ်။ Client က Request လှမ်းမေးနေစရာ မလိုဘဲ၊ Server ပေါ်မှာ Event ဖြစ်ပေါ်လာချိန်မှာ Client ရဲ့ Webhook Endpoint ဆီသို့ JSON Payload ပါဝင်တဲ့ HTTP POST Request ကို တိုက်ရိုက် ပို့ဆောင်ပေးတာ ဖြစ်ပါတယ်။
Webhooks ရဲ့ အဓိက အားသာချက်တွေကတော့ အောက်ပါအတိုင်း ဖြစ်ပါတယ်။
- Zero Wasted Requests: Event မဖြစ်မချင်း ဘာ Request မှ မသွားတဲ့အတွက် မလိုအပ်တဲ့ Network Overhead လုံးဝ မရှိပါဘူး။
- Instant Delivery: Event ဖြစ်ပေါ်တဲ့ မီလီစက္ကန့်ပိုင်းအတွင်းမှာ Receiver ဆီ ရောက်ရှိတဲ့အတွက် Real-time အချက်အလက် စီးဆင်းမှုကို ရရှိစေပါတယ်။
- Highly Scalable Architecture: Background Workers (BullMQ, Kafka, AWS SQS) တွေကနေ Asynchronous Queue ပုံစံနဲ့ Webhook Dispatch လုပ်နိုင်တဲ့အတွက် Main API Server ရဲ့ Latency ကို မထိခိုက်စေပါဘူး။
Production Webhook System တစ်ခုမှာ Producer က Webhook ကို synchronous inline အနေနဲ့ မပို့သင့်ပါဘူး။ မဟုတ်ပါက Receiver ရဲ့ Endpoint နှေးကွေးနေရင် Producer ရဲ့ Core Transaction ပါ ပိတ်မိသွားနိုင်ပါတယ်။ ဒါကြောင့် Durable Message Queue တစ်ခုထဲ Event ကို Publish လုပ်ပြီး သီးခြား Dispatcher Workers တွေကနေသာ HTTP POST ခေါ်ဆိုစေရပါမယ်။
Webhook Security: HMAC Signature Verification
Webhook စနစ်တစ်ခုမှာ အကြီးမားဆုံး စိန်ခေါ်မှုကတော့ လုံခြုံရေး ဖြစ်ပါတယ်။ Receiver Endpoint ဟာ Public Internet ပေါ်မှာ ဖွင့်ထားရတဲ့အတွက် မသမာသူတွေက Fake Payload တွေ လှမ်းပို့တာ၊ သို့မဟုတ် Request ကို ကြားဖြတ် ခိုးယူ ပြန်လည် ပို့ဆောင်တာ (Replay Attacks) တွေ ဖြစ်ပေါ်နိုင်ပါတယ်။
ဒါကို ကာကွယ်ဖို့အတွက် HMAC (Hash-based Message Authentication Code) ကို အသုံးပြုရပါမယ်။ Provider က Shared Secret Key ကို သုံးပြီး Payload ရဲ့ Cryptographic Signature ကို တွက်ချက်ကာ Header (ဥပမာ X-Signature-SHA256) ထဲမှာ ထည့်သွင်း ပေးပို့ပါတယ်။ အဲဒီ Header ထဲမှာ Unix Timestamp (t=1731600000) ကိုပါ ထည့်သွင်းပေးပြီး၊ Receiver က ၅ မိနစ်ထက် ကျော်လွန်နေတဲ့ Requests တွေကို ပယ်ချခြင်းဖြင့် Replay Attacks တွေကို အပြည့်အဝ တားဆီးနိုင်ပါတယ်။
အောက်ပါ TypeScript နမူနာမှာ Timing Attacks တွေကို ကာကွယ်ပေးတဲ့ လုံခြုံစိတ်ချရသော Webhook Signature စစ်ဆေးပုံကို ကြည့်ရှုနိုင်ပါတယ်။
import { createHmac, timingSafeEqual } from 'node:crypto'
export function verifyWebhookSignature(
rawPayload: string,
signatureHeader: string,
secretKey: string
): boolean {
// Compute expected HMAC SHA-256 signature
const expectedSignature = createHmac('sha256', secretKey)
.update(rawPayload)
.digest('hex')
const expectedBuffer = Buffer.from(expectedSignature, 'utf8')
const actualBuffer = Buffer.from(signatureHeader, 'utf8')
// Prevent timing attacks using constant-time buffer comparison
if (expectedBuffer.length !== actualBuffer.length) return false
return timingSafeEqual(expectedBuffer, actualBuffer)
}
ဒီနေရာမှာ String Equality (===) ဖြင့် တိုက်ရိုက် မစစ်ဆေးဘဲ timingSafeEqual ကို သုံးထားတဲ့အတွက် Hacker တွေက Response Time ကွာခြားချက်ကို ကြည့်ပြီး Signature ကို ခန့်မှန်းဖော်ထုတ်နိုင်တဲ့ Timing Attack အန္တရာယ်ကို လုံးဝ တားဆီးပေးပါတယ်။
Delivery Guarantees: Retries, Idempotency, and DLQs
Distributed Systems တွေမှာ Network Flakiness နဲ့ Server Downtime တွေဟာ အမြဲတမ်း ဖြစ်ပွားနိုင်ပါတယ်။ Webhook ပို့ဆောင်တဲ့အခါ Receiver ဘက်က 500 Error သို့မဟုတ် Timeout ဖြစ်သွားနိုင်တဲ့အတွက် Robust ဖြစ်တဲ့ စနစ်တစ်ခုမှာ အောက်ပါ အစိတ်အပိုင်းများ ပါဝင်ရပါမယ်။
- Exponential Backoff with Jitter: Webhook ပို့ဆောင်မှု ကျရှုံးသွားတဲ့အခါ ချက်ချင်း ထပ်မပို့ဘဲ 1m, 5m, 15m, 1h စသဖြင့် တဖြည်းဖြည်း ကြာမြင့်တဲ့ အချိန်အပိုင်းအခြားဖြင့် Retry လုပ်ဆောင်ရပါမယ်။ Retry တိုင်းမှာ Random Jitter ထည့်သွင်းခြင်းဖြင့် ပြိုင်တူ Request ဒဏ်ကြောင့် Receiver ဒုက္ခရောက်ခြင်းကို ရှောင်ရှားနိုင်ပါတယ်။
- Idempotency Keys:
Network Retries ကြောင့် Receiver ဆီသို့ တူညီတဲ့ Event ဟာ နှစ်ကြိမ် ရောက်ရှိသွားနိုင်ပါတယ်။ Receiver က
event_idကို စစ်ဆေးပြီး ရှိပြီးသား Event ဖြစ်ပါက Duplicate Processing မလုပ်ဘဲ200 OKသာ ပြန်ထုတ်ပေးရပါမယ် (ဥပမာ Customer ဆီက ငွေ နှစ်ခါ ဖြတ်မိခြင်းမှ ကာကွယ်ရန်)။ - Dead-Letter Queue (DLQ): သတ်မှတ်ထားတဲ့ Retry အကြိမ်ရေ ကုန်ဆုံးတဲ့အထိ မအောင်မြင်တဲ့ Webhooks တွေကို DLQ ထဲ ထည့်သွင်းထားပြီး၊ နောက်ပိုင်းမှာ Developer တွေက Manual Replay လုပ်နိုင်အောင် UI Dashboard ပြင်ဆင်ပေးထားရပါမယ်။
Architectural Decision Framework: When to Use Which
စနစ်တစ်ခု တည်ဆောက်တဲ့အခါ ဘယ်နည်းလမ်းကို ရွေးချယ်သင့်သလဲဆိုတဲ့ မူဘောင်ကို သိရှိထားဖို့ လိုအပ်ပါတယ်။
- Polling ကို ဘယ်အချိန်မှာ သုံးမလဲ: Client ဟာ Mobile App သို့မဟုတ် Firewall/NAT နောက်ကွယ်က Client ဖြစ်ပြီး Public IP သို့မဟုတ် Inbound Domain မရှိတဲ့အခါ Polling ကို သုံးရပါမယ်။ ဒါ့အပြင် Export လုပ်နေတဲ့ PDF Report တစ်ခု ပြီးဆုံးမလား စောင့်ဆိုင်းတာမျိုး (ကြာချိန် စက္ကန့် ၃၀ အောက် ခဏတာ စောင့်ဆိုင်းမှုများ) မှာ Polling ဟာ အလွန် ရိုးရှင်းပြီး အလုပ်ဖြစ်ပါတယ်။
- Webhooks ကို ဘယ်အချိန်မှာ မဖြစ်မနေ သုံးမလဲ: Server-to-Server Asynchronous Integrations တွေမှာ Webhooks ဟာ မဖြစ်မနေ စံနှုန်း ဖြစ်ပါတယ်။ ငွေပေးချေမှု အတည်ပြုခြင်း၊ Third-party Webhook Sync လုပ်ခြင်းနဲ့ Microservices အချင်းချင်း Event ပို့ဆောင်ခြင်းတွေမှာ Webhooks ကသာ အကောင်းဆုံး ရွေးချယ်မှု ဖြစ်ပါတယ်။
- WebSockets / SSE (Server-Sent Events): Browser-to-Server Real-time UI ချိတ်ဆက်မှု (ဥပမာ Live Chat, Collaborative Editing, Live Stock Tickers) တွေအတွက်တော့ HTTP Webhooks မဟုတ်ဘဲ WebSockets သို့မဟုတ် SSE ကို ရွေးချယ် အသုံးပြုသင့်ပါတယ်။
Comparing Real-Time API Delivery Strategies
Real-time အချက်အလက် ပေးပို့နိုင်တဲ့ အဓိက Architecture နည်းလမ်းတွေကို အောက်ပါအတိုင်း နှိုင်းယှဉ် သုံးသပ်နိုင်ပါတယ်။
| Strategy | Communication Model | Latency | Server Resource Cost | Client Requirement | Best Use Case |
|---|---|---|---|---|---|
| Short Polling | Pull (Repeated HTTP GET) | High (Interval delay) | Very High (Wasted requests) | Standard HTTP Client | Ephemeral jobs (<30s), simple scripts |
| Long Polling | Pull (Hanging HTTP GET) | Low (Instant on event) | High (Open connections) | Standard HTTP Client | Browser notifications without WebSockets |
| Webhooks | Push (Reverse HTTP POST) | Near Zero (Instant delivery) | Very Low (Event-driven) | Publicly accessible URL | Server-to-server async event sync |
| Server-Sent Events | Push (Unidirectional HTTP stream) | Real-time | Low-Moderate (HTTP/2 stream) | EventSource API | AI LLM streaming, live stock feeds |
| WebSockets | Full-Duplex (Bidirectional TCP) | Ultra-low (<5ms) | High (Persistent socket state) | WebSocket Client | Multiplayer gaming, collaborative docs |
Final Thoughts
Real-time API Architecture ဆိုတာ တစ်ခုတည်းသော ပြီးပြည့်စုံတဲ့ နည်းလမ်း မရှိပါဘူး။ Polling နဲ့ Webhooks ဟာ တစ်ခုနဲ့တစ်ခု ဆန့်ကျင်ဘက် မဟုတ်ဘဲ အခြေအနေအလိုက် ဖြည့်စွက် အသုံးပြုရတဲ့ အင်ဂျင်နီယာဆိုင်ရာ ကိရိယာတွေ ဖြစ်ကြပါတယ်။
Server-to-Server asynchronous event exchange အတွက် HMAC signatures နဲ့ exponential retries ပါဝင်တဲ့ Webhooks ကို သုံးစွဲပြီး၊ ရိုးရှင်းတဲ့ client-side status checks တွေအတွက် short polling ကို လိုအပ်သလို တွဲဖက် အသုံးချခြင်းက system overhead ကို အနည်းဆုံးဖြစ်စေပြီး production API ecosystem တစ်ခုလုံးကို ပိုမိုခိုင်မာစေပါတယ်။