Grokking System Design Fundamentals
Ask Author
Back to course home

0% completed

Vote For New Content
Messaging patterns
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

In distributed systems, messaging patterns define how components communicate via asynchronous messages. Understanding these patterns is crucial for designing scalable and reliable systems. Below, we cover five key messaging patterns, each with its core idea, characteristics, and practical examples.

1. Point-to-Point (Direct Messaging)

In a point-to-point pattern, each message is delivered to exactly one consumer. A producer sends messages into a queue (or similar channel), and one of the consumers reading from that queue will receive each message. This ensures no two consumers process the same message.

  • How it works: The message broker (e.g., RabbitMQ, Amazon SQS, JMS) routes each message to a single target queue. Consumers (possibly many) compete for messages from that queue, but each message is consumed by only one of them (often called competing consumers model).
  • Core characteristics: Ensures one-consumer-per-message, enabling load balancing across workers. The queue can buffer messages if consumers are busy, providing back-pressure and reliability. Order is usually FIFO (first-in-first-out) per queue, unless priority or other ordering is configured.
  • Use case: Useful for task distribution and work queues. For example, an image processing service might place jobs on a queue so that each job is picked up by one processing worker. This way, multiple workers can parallelize tasks without duplicating work.
  • Technologies: Implemented by nearly all message queue systems. RabbitMQ and ActiveMQ have queues for this pattern; Kafka achieves a similar one-consumer-per-message effect using a single consumer group (each message in a topic partition goes to one member of the group).

2. Publish-Subscribe (Pub/Sub)

The publish-subscribe pattern delivers each message to all interested subscribers. A publisher sends messages to a topic (or exchange), and multiple subscribers receive a copy of each message. Publishers and subscribers are decoupled — the publisher doesn’t know who receives the message.

  • How it works: The message broker (e.g., Kafka, RabbitMQ, Google Pub/Sub) broadcasts published messages to all queues or subscribers that have subscribed to the topic. In RabbitMQ, a fanout or topic exchange will forward messages to multiple queues (one per subscriber). In Kafka, each subscriber group that reads a topic gets its own copy of the message.
  • Core characteristics: One-to-many distribution. Every subscriber processes the message independently. This is great for event-driven architectures where an event (message) triggers different actions in different services. Publishers and consumers are loosely coupled – you can add new consumers without changing the publisher.
  • Use case: Useful when the same event needs to be acted on in different ways. For example, when a new user registers on a platform, a “User Registered” event might be published. Multiple subscribers can react: one service sends a welcome email, another logs the signup for analytics, and another updates a recommendation model. All get the event in parallel.
  • Technologies: Kafka is inherently a pub/sub system (topics with multiple consumer groups). RabbitMQ supports pub/sub via exchanges + queues (e.g., a fanout exchange sends to all bound queues). Cloud services like AWS SNS + SQS or Azure Service Bus Topics/Subscriptions are designed for pub/sub. Ensure subscribers are configured to handle the message flow (or else messages might be dropped if a subscriber isn’t listening and the broker doesn’t persist them).

3. Request-Reply (Request-Response)

The request-reply pattern is a two-part message exchange: a requester sends a message and expects a response message in return. It’s analogous to a function call or API request, but implemented asynchronously through messaging. This pattern allows a form of synchronous interaction on top of an asynchronous system.

  • How it works: The requester sends a request message (often into a point-to-point queue or directly to a specific service). Along with the request, it provides a return address (reply queue/topic) or a callback mechanism. The consuming service (replier) processes the request and sends back a reply message to the specified reply address. A correlation ID is usually included in both messages so the requester can match the reply to its original request.
  • Core characteristics: Enables two-way communication using messaging. Decouples the request and response in time — the requester can continue other work or handle other messages while waiting for the response. Requires correlation handling in application logic to match responses. Typically involves timeouts or retries in case a reply doesn’t arrive.
  • Use case: Useful when a service needs data or action from another service but you want to avoid tight coupling of direct calls. For example, a web service might enqueue a “generate report” request to a reporting microservice and continue. The reporting service eventually sends back a report ready message. The client receives the reply and then, say, notifies the user. In system design interviews, this pattern often comes up when designing async APIs or RPC over messaging.
  • Technologies: No single broker primitive does full request-reply automatically; it’s usually implemented at the application level. RabbitMQ supports a reply-to pattern (temporary reply queues) and correlation IDs via message headers. JMS messaging has request/reply helpers. With Kafka, one could use a dedicated reply topic partition and include a correlation key. Tools like RPC frameworks (gRPC) are direct request-response (not message-based), but in messaging systems you implement this pattern explicitly.

4. Fan-Out/Fan-In (Scatter-Gather)

Fan-Out/Fan-In (also known as Scatter-Gather) is a composite pattern where a message is scattered to multiple recipients in parallel (fan-out), and then the results are gathered back (fan-in). Essentially, one request triggers multiple parallel processes and an aggregator collects all the responses or outcomes.

  • How it works (Fan-Out): A component (dispatcher) takes an incoming message or request and duplicates or distributes it to multiple consumers or services. This could be done by publishing to a topic that multiple services subscribe to, or by sending individual point-to-point messages to several target queues. Each target processes the message independently.
  • How it works (Fan-In): Another component (aggregator) waits for responses from all the scattered requests (or a certain number of them, or a timeout). Once the responses arrive, the aggregator combines the results or otherwise processes them to form a single output. The aggregated result might be sent back to the original requester or trigger the next step.
  • Core characteristics: Achieves parallelism for potentially faster overall processing time when multiple independent tasks can run concurrently. Requires handling of partial failures or slow responders (e.g., using timeouts or default values if some responses don’t come). The aggregator needs logic to correlate multiple responses to the original request (similar to correlation ID, but tracking multiple sub-responses).
  • Use case: Common in scenarios like search or aggregation services. For example, a search query in a distributed search system can be fanned-out to multiple shard servers; each shard returns results which are then merged (fan-in) before returning the final combined result to the user. Another example is an analytics job that splits work into sub-tasks (map-reduce style) across many workers and then collects the results.
  • Technologies: This pattern is implemented at the architecture level. Message brokers help with the fan-out (e.g., using pub/sub topics to broadcast requests to multiple workers). The fan-in typically requires an aggregator service or function. Some frameworks (like Apache Camel or Azure Durable Functions) provide built-in support for scatter-gather, handling the collection of responses. When using raw messaging, you’d implement a coordinating service that sends out messages and listens for replies on a dedicated channel for aggregation.

5. Dead Letter Queue (DLQ)

A Dead Letter Queue is a safety net for messages that cannot be processed successfully. When a message continually fails processing (due to errors, format issues, or exceeding retry limits), it is moved to a special queue called the Dead Letter Queue instead of being lost or blocking the main queue. This pattern ensures problematic messages are isolated for later inspection or remediation.

  • How it works: Most message systems allow configuring a DLQ for each queue or topic. If a message is rejected by a consumer, or if it exceeds a maximum number of processing attempts, the broker reroutes it to the DLQ. The DLQ is essentially a holding area for “poison messages” (messages that consistently cause failures). They remain there until they are manually reviewed or automatically processed by some error-handling service.
  • Core characteristics: Improves system resilience and reliability by not discarding failed messages immediately. Prevents blocked queues – one bad message won’t jam the processing pipeline since it gets shunted to the DLQ. Typically accompanied by monitoring/alerts, because the presence of messages in a DLQ indicates issues that need attention. It’s a passive pattern (only used on failure) but critical for robust messaging setups.
  • Use case: Any critical system with asynchronous processing should use DLQs as a best practice. For instance, in an order processing system, if an order message has corrupt data and the order service throws exceptions every time it tries that message, after a few retries the message goes to the Dead Letter Queue. This allows the system to continue with other orders, and later the faulty message can be examined or fixed without data loss.
  • Technologies: Nearly all enterprise messaging systems support DLQs. In RabbitMQ, you set a Dead Letter Exchange on a queue to route failed messages to a dead-letter queue. Kafka doesn’t have a built-in DLQ mechanism at the broker level, but patterns using separate “error topics” are used (for example, consumers or Kafka Streams can send bad records to a designated dead-letter topic). AWS SQS and Azure Service Bus both natively support DLQs with configurable retry thresholds. The key is configuring the threshold for moving to DLQ (e.g., after 5 failed attempts) and ensuring someone or something processes or monitors the DLQ.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible