Seven Core Messaging Patterns in Systems

Seven Core Messaging Patterns in Systems

An architectural deep dive into seven essential messaging patterns for distributed systems and microservices, exploring Competing Consumers, Pub/Sub, Request-Reply, Scatter-Gather, and Dead-Letter Channels.

ArchitectureMessagingMicroservicesEvent-DrivenDistributed Systems

The Foundations of Enterprise Messaging

Modern Microservices နဲ့ Distributed Systems တွေမှာ Services အချင်းချင်း Synchronous HTTP/REST Calls တွေနဲ့သာ တင်းကျပ်စွာ ချိတ်ဆက်ထားမယ်ဆိုရင် Service တစ်ခု နှေးကွေးသွားရုံနဲ့ စနစ်တစ်ခုလုံး ပြိုလဲသွားတဲ့ Cascading Failure အန္တရာယ် ကြုံတွေ့ရတတ်ပါတယ်။ ဒါကြောင့် အကြီးစား စနစ်တွေမှာ စနစ်အချင်းချင်း အမှီအခိုကင်းစေဖို့ (Loose Temporal Coupling) နဲ့ Traffic ရုတ်တရက် တက်လာချိန်မှာ ဝန်အားကို ထိန်းညှိပေးဖို့ (Load Leveling) အတွက် Asynchronous Messaging စနစ်တွေကို မဖြစ်မနေ အသုံးပြုကြပါတယ်။

Enterprise Integration Patterns (EIP) မှာ ဖော်ပြထားတဲ့ Messaging Patterns များစွာ ရှိတဲ့အနက်၊ လက်တွေ့ Cloud Architecture တွေမှာ အသုံးအများဆုံးနဲ့ အခြေခံအကျဆုံး Core Messaging Patterns ၇ ခု ရှိပါတယ်။ ဒီ Patterns တွေကို မှန်ကန်စွာ ပေါင်းစပ် အသုံးချနိုင်မှသာ Fault-tolerant ဖြစ်ပြီး Scale လုပ်ရ လွယ်ကူတဲ့ Distributed Event-Driven Architecture တစ်ခုအဖြစ် စိတ်ချလက်ချ ရပ်တည်နိုင်မှာ ဖြစ်ပါတယ်။

Pattern 1 and 2: Point-to-Point vs Publish-Subscribe

Distributed Messaging ရဲ့ အခြေခံအကျဆုံး ပုံစံနှစ်မျိုးဟာ Messages တွေကို လက်ခံမယ့် Consumers အရေအတွက်ပေါ် မူတည်ပြီး ကွဲပြားပါတယ်။

  1. Pattern 1: Point-to-Point (Competing Consumers): ဒီ Pattern မှာ Producer က Message တစ်ခုကို Queue ထဲ ထည့်သွင်းလိုက်ချိန်မှာ နောက်ကွယ်က Worker Pool ထဲက အားလပ်နေတဲ့ Consumer တစ်ခုတည်းကသာ အဲဒီ Message ကို ရယူ လုပ်ဆောင်ပါတယ်။ Worker တစ်ခုက Message ကို ACK ပေးလိုက်တာနဲ့ Broker က Queue ထဲကနေ ချက်ချင်း ဖျက်ပစ်ပါတယ်။

    • Worker တွေ အများကြီး ထပ်တိုးပြီး Background Jobs တွေကို ဝန်မျှဝေ စေခိုင်းချင်တဲ့အခါ (ဥပမာ PDF Generation, Image Resizing, Video Transcoding) မှာ သုံးပါတယ်။
    • အဓိက အားသာချက်ကတော့ Worker တစ်ခု သေသွားရင်တောင် အခြား Worker တစ်ခုက အလိုအလျောက် တာဝန်လွှဲယူ လုပ်ဆောင်နိုင်ခြင်း ဖြစ်ပါတယ်။
  2. Pattern 2: Publish-Subscribe (Topic Fan-Out): Producer က သီးခြား Worker တစ်ခုဆီကို ရည်ရွယ်ပို့တာ မဟုတ်ဘဲ ဖြစ်ပျက်သွားတဲ့ Domain Event (ဥပမာ OrderPlaced) ကို Topic Exchange ပေါ်သို့ Broadcast ပုံစံ ထုတ်လွှင့်တာ ဖြစ်ပါတယ်။

    • အဆိုပါ Topic ကို Subscribe လုပ်ထားတဲ့ Independent Consumer Groups တွေ (Payment Service, Inventory Service, Email Service) အားလုံးဟာ Message ရဲ့ သီးခြား Copy တစ်ခုစီကို တစ်ပြိုင်နက်တည်း လက်ခံရရှိကြပါတယ်။
    • စနစ်အသစ်တစ်ခု (ဥပမာ Fraud Detection Service) ထပ်မံ ထည့်သွင်းလိုတဲ့အခါ မူလ Producer Code ကို ပြင်စရာမလိုဘဲ Topic ကို အသစ် လှမ်းနားထောင်ရုံဖြင့် Decoupled Architecture ကို ရရှိစေပါတယ်။

Pattern 3: Request-Reply via Correlation IDs

Asynchronous Message Queue တွေဟာ အခြေခံအားဖြင့် One-Way (Fire-and-Forget) ပုံစံ ဖြစ်ပါတယ်။ ဒါပေမဲ့ Client က Async Queue သုံးပြီး အလုပ်တစ်ခု ခိုင်းစေပြီးနောက် ထွက်ပေါ်လာတဲ့ ရလဒ်ကို ပြန်လည် စောင့်ဆိုင်း ရယူလိုတဲ့အခါ Request-Reply Pattern ကို အသုံးပြုပါတယ်။

ဒီ Pattern မှာ အဓိက သော့ချက်နှစ်ခု ပါဝင်ပါတယ်။

  • Correlation ID: Request တစ်ခုချင်းစီအတွက် မထပ်တူညီတဲ့ Unique UUID တစ်ခု ဖြစ်ပါတယ်။
  • Reply-To Queue: Worker က အလုပ်ပြီးမြောက်ချိန်မှာ ရလဒ် ပြန်ပို့ပေးရမယ့် Callback Queue လိပ်စာ ဖြစ်ပါတယ်။

အောက်ပါ TypeScript နမူနာမှာ Correlation ID ပါဝင်တဲ့ Message Envelope တည်ဆောက်ပုံကို ကြည့်ရှုနိုင်ပါတယ်။

import { randomUUID } from 'node:crypto'

export interface MessageEnvelope<T> {
  id: string
  correlationId: string
  replyTo?: string
  timestamp: number
  payload: T
}

export function createRequestMessage<T>(payload: T, replyQueue: string): MessageEnvelope<T> {
  return {
    id: randomUUID(),
    correlationId: randomUUID(),
    replyTo: replyQueue,
    timestamp: Date.now(),
    payload,
  }
}

Client က Request Message ကို Main Queue ထဲ ထည့်သွင်းပြီး၊ သူ့ရဲ့ ကိုယ်ပိုင် replyQueue ကို စောင့်ကြည့်နေပါတယ်။ Worker က အလုပ်ပြီးတဲ့အခါ မူရင်း correlationId ကို ပြန်လည် ထည့်သွင်းပြီး Response ပို့ပေးပါတယ်။ Client က အဆိုပါ Correlation ID ကို တိုက်ဆိုင်စစ်ဆေးခြင်းအားဖြင့် ပြိုင်တူ ဝင်ရောက်လာတဲ့ အဖြေများစွာထဲက သက်ဆိုင်ရာ Request ရဲ့ အဖြေကို အတိအကျ ပြန်လည် ဖော်ထုတ်နိုင်ပါတယ်။

Pattern 4 and 5: Content-Based Routing and Envelope Wrapper

စနစ်တစ်ခု ကြီးထွားလာတဲ့အခါ Messages တွေရဲ့ အကြောင်းအရာပေါ် မူတည်ပြီး လမ်းကြောင်းခွဲပေးခြင်းနဲ့ စံသတ်မှတ်ခြင်းတွေ လိုအပ်လာပါတယ်။

  1. Pattern 4: Content-Based Router: Queue ထဲကို ရောက်ရှိလာတဲ့ Message ရဲ့ Header သို့မဟုတ် Payload Data (ဥပမာ customerTier: "VIP", region: "EU") ကို Router က စစ်ဆေးပြီး သင့်တော်တဲ့ သီးသန့် Destination Queue ဆီသို့ လမ်းကြောင်း လွှဲပြောင်းပေးပါတယ်။

    • Standard Customers တွေရဲ့ အလုပ်များပြားနေချိန်မှာ VIP Customers တွေရဲ့ အရေးကြီး အလုပ်တွေ ပိတ်မိ မနေစေဖို့အတွက် Dedicated High-priority Queues တွေဆီ လမ်းခွဲပေးရာမှာ အလွန် အသုံးဝင်ပါတယ်။
  2. Pattern 5: Envelope Wrapper (Message Normalizer): မတူညီတဲ့ Services တွေဆီက လာတဲ့ Payload ပုံစံ မတူညီမှုတွေကို ဖြေရှင်းဖို့အတွက် Message တိုင်းကို ဘုံ Metadata စံနှုန်းတစ်ခု သတ်မှတ်ပေးတဲ့ Pattern ဖြစ်ပါတယ်။

    • Payload အပြင် traceId (Distributed Tracing အတွက်), schemaVersion, producerService နဲ့ idempotencyKey စတဲ့ Headers တွေကို Wrapper ထဲ ထည့်သွင်းပေးပါတယ်။ ဒါမှသာ downstream Consumers တွေက စနစ်တကျ Observability နဲ့ Validation ကို စံတစ်ခုတည်းအဖြစ် ကိုင်တွယ်နိုင်မှာ ဖြစ်ပါတယ်။

Pattern 6: Scatter-Gather Parallel Aggregation

အသုံးပြုသူတစ်ယောက်က လေယာဉ်လက်မှတ် ရှာဖွေတဲ့အခါ သို့မဟုတ် ကုန်ပစ္စည်း ဈေးနှုန်း နှိုင်းယှဉ်တဲ့အခါ ပြင်ပ Vendor စနစ် ၅ ခုဆီကို တစ်ပြိုင်နက် မေးမြန်းပြီး ရလဒ်အားလုံးကို ပြန်လည် စုစည်းပေးဖို့ လိုအပ်ပါတယ်။ ဒါကို Scatter-Gather Pattern လို့ ခေါ်ဆိုပါတယ်။

Loading flowchart...
  • Scatter (ဖြန့်ကြက်ခြင်း): Root Request ကို Splitter ကနေ သက်ဆိုင်ရာ Worker/Partner Queues အားလုံးဆီသို့ တစ်ပြိုင်နက် Fan-Out ပို့ဆောင်ပါတယ်။
  • Gather (စုစည်းခြင်း): Aggregator Component က ပြန်လည် ရောက်ရှိလာတဲ့ Responses တွေကို correlationId ဖြင့် စုဆောင်းပါတယ်။
  • Timeout Window: စနစ်တစ်ခု နှေးကွေးနေရုံနဲ့ အလုံးစုံ ရပ်တန့်မသွားစေဖို့ Aggregator မှာ Strict Timeout (ဥပမာ ၂ စက္ကန့်) သတ်မှတ်ထားရပါမယ်။ သတ်မှတ်ချိန်အတွင်း ရရှိသမျှ Responses တွေကိုသာ ပေါင်းစည်းပြီး Client ဆီ အကောင်းဆုံး အဖြေ ပြန်ထုတ်ပေးပါတယ်။

Pattern 7: Dead-Letter Channel and Poison Messages

Asynchronous Systems တွေမှာ အန္တရာယ်အရှိဆုံး အခြေအနေတစ်ခုကတော့ Poison Message ဖြစ်ပါတယ်။ မမှန်ကန်တဲ့ JSON Syntax ပါဝင်နေတာ၊ သို့မဟုတ် Code Bug ကြောင့် Consumer က Message တစ်ခုကို Parse လုပ်တိုင်း Crash ဖြစ်ပြီး Unhandled Exception တက်သွားတဲ့အခါ Message ဟာ Queue ထဲ အကြိမ်ကြိမ် ပြန်ရောက်လာပြီး Worker Pool တစ်ခုလုံးကို ပိတ်ဆို့သွားစေနိုင်ပါတယ် (CrashLoop).

ဒီပြဿနာကို ဖြေရှင်းဖို့အတွက် Dead-Letter Channel (DLQ) Pattern ကို မဖြစ်မနေ ထည့်သွင်းရပါမယ်။

  1. Max Retry Count: Message တစ်ခုကို Worker က Process လုပ်လို့ မအောင်မြင်ပါက Retry အကြိမ်ရေ (ဥပမာ ၃ ကြိမ် သို့မဟုတ် ၅ ကြိမ်) သတ်မှတ်ထားပါတယ်။
  2. Quarantine (သီးခြား ခွဲထုတ်ခြင်း): Retry အကြိမ်ရေ ပြည့်သွားတဲ့အထိ Fail နေဆဲ Message ကို Main Queue ထဲကနေ ဖယ်ရှားပြီး သီးသန့် Dead-Letter Queue ထဲသို့ ရွှေ့ပြောင်း ထိန်းသိမ်းလိုက်ပါတယ်။
  3. Alerting and Redrive: DLQ ထဲ Message ရောက်ရှိလာတာနဲ့ အင်ဂျင်နီယာများဆီ Alert ပို့ဆောင်ပေးပြီး၊ Bug ကို Fix လုပ်ပြီးနောက် DLQ ထဲက Messages တွေကို Main Queue ထဲသို့ ဘေးကင်းစွာ ပြန်လည် ထည့်သွင်း (Redrive) စေနိုင်ပါတယ်။

Comparing the Seven Messaging Patterns

Distributed Systems တွေမှာ အသုံးပြုနိုင်တဲ့ အဓိက Messaging Patterns ၇ ခုရဲ့ အင်္ဂါရပ်တွေကို အောက်ပါအတိုင်း နှိုင်းယှဉ် သုံးသပ်နိုင်ပါတယ်။

PatternPrimary ObjectiveCommunication StyleFault ToleranceBest Use Case
Competing ConsumersLoad balance task processing across workersAsynchronous Point-to-PointHigh (Worker crash reassigns message)Background jobs, video encoding, order invoices
Publish-SubscribeBroadcast state changes to multiple listenersAsynchronous Fan-OutHigh (Subscribers decoupled from publisher)Domain events (OrderPlaced), audit logging
Request-ReplyAsynchronous two-way query/response flowBidirectional via Correlation IDModerate (Requires reply timeout handling)Cross-service RPC over queues, synchronous bridges
Content-Based RouterRoute messages based on headers or payload rulesDynamic conditional forwardingHigh (Prevents unroutable message drops)Tiered VIP order routing, multi-tenant partitioning
Envelope WrapperStandardize headers (TraceId, Version) across servicesStructural message transformationHigh (Maintains cross-cutting telemetry)Distributed tracing, schema evolution wrappers
Scatter-GatherFan out parallel tasks and aggregate responsesParallel broadcast and gatherHigh (Timeout window prevents hang)Flight ticket aggregators, multi-vendor price comparison
Dead-Letter ChannelQuarantine poison pills and repeatedly failing tasksAsynchronous failure isolationHighest (Protects main queue from blocking)Malformed JSON payloads, unrecoverable 4xx/5xx errors

Final Thoughts

Microservices နဲ့ Event-Driven Architecture တွေမှာ Messaging ဆိုတာ Data တွေကို Queue တစ်ခုထဲ ထည့်လိုက်ရုံနဲ့ အလိုအလျောက် အဆင်ပြေသွားတဲ့ အရာ မဟုတ်ပါဘူး။ စနစ်ရဲ့ Business Requirements ပေါ် မူတည်ပြီး သင့်တော်တဲ့ Messaging Pattern ကို စနစ်တကျ ရွေးချယ် အသုံးချတတ်ဖို့ လိုအပ်ပါတယ်။

Point-to-Point နဲ့ Pub/Sub ဖြင့် message throughput ကို မျှဝေခြင်း၊ Content-Based Router နဲ့ Envelope Wrappers သုံးပြီး payload consistency ထိန်းသိမ်းခြင်း၊ ပြီးတော့ Dead-Letter Queue ဖြင့် poison messages တွေကို isolate လုပ်ထားခြင်းက enterprise event-driven systems တွေမှာ data loss မဖြစ်ဘဲ ရေရှည်တည်ငြိမ်စွာ run နိုင်စေဖို့ အဓိက အထောက်အကူပြုပေးပါတယ်။