ReviseAlgo Logo
Beginner8 min readNetworking & Communication

Pub/Sub

Broadcasting transactions to topics where multiple subscribers consume duplicates concurrently.

What you'll learn

  • Topic-Based vs Content-Based Routing
  • Fan-Out Pattern (SNS → SQS)
  • Message Durability
  • At-Least-Once Delivery & Idempotency
  • Push vs Pull Model
  • Dead Letter Topic (DLT)

TL;DR

Broadcasting transactions to topics where multiple subscribers consume duplicates concurrently.

Visual System Topology

Pub/Sub Network Handshake Flow

Client Node Initiates Request
Multiplexed
Pub/Sub Gateway Routes Traffic
Fast Payload
Backend Server Executes Logic

Concept Overview

Publish-Subscribe (Pub/Sub) is a messaging pattern where publishers send messages to named channels (topics) without knowing which subscribers will receive them, and subscribers express interest in topics without knowing which publishers produce messages. This decouples producers and consumers both in time and space.

Pub/Sub differs from a point-to-point message queue in a critical way: in a queue, one consumer processes each message; in pub/sub, every active subscriber receives a copy of every message. This enables fan-out patterns where a single event (e.g., "order shipped") simultaneously triggers multiple downstream workflows (send email, update inventory, push mobile notification, generate invoice).

Key implementations: Amazon SNS (serverless fan-out to SQS, Lambda, HTTP), Google Pub/Sub (at-least-once, scalable), Apache Kafka (log-based, durable, replay), Redis Pub/Sub (in-memory, no durability, extremely fast), MQTT (IoT protocol for constrained devices).

Key Architectural Pillars

1

Topic-Based vs Content-Based Routing

Topic-based: subscribers subscribe to named topics; all messages on a topic are delivered to all subscribers. Content-based: the broker filters messages based on message attributes/content; subscribers declare predicates, not topics. More flexible but more complex.

Example: SNS topic-based: subscriber A subscribes to "orders" topic and receives all order events. Content-based: subscriber A only receives order events where order.value > $1000.
2

Fan-Out Pattern (SNS → SQS)

A single SNS topic fans out to multiple SQS queues. Each downstream service has its own SQS queue, processed independently. If one service is slow or down, it doesn't affect others. Each service processes at its own pace from its own queue.

Example: Order placed → SNS topic → [email-queue, inventory-queue, shipping-queue, analytics-queue]. Email service being slow doesn't delay inventory or shipping.
3

Message Durability

Redis Pub/Sub: zero durability — if no subscriber is connected when a message is published, the message is lost. SNS: messages are buffered if the subscriber is an SQS queue or Lambda. Kafka: messages are durably stored in a replicated log for a configurable retention period.

Example: For a payment event, use SNS→SQS (not Redis Pub/Sub) so messages aren't lost if a downstream service restarts.
4

At-Least-Once Delivery & Idempotency

Pub/Sub systems typically guarantee at-least-once delivery — a message may be delivered more than once (on retries). Subscribers must be idempotent: processing the same message twice must produce the same result as processing it once.

Example: Email notification subscriber: check if a "order_shipped" notification has already been sent for order_id before sending. Use a deduplication table with (order_id, event_type) as unique key.
5

Push vs Pull Model

Push: the broker delivers messages to subscribers automatically (SNS, Google Pub/Sub HTTP). Pull: subscribers explicitly poll the broker for messages (SQS, Kafka consumer groups). Push provides lower latency; pull provides better backpressure control.

Example: Google Pub/Sub supports both: push subscriptions (broker POSTs to a webhook URL) and pull subscriptions (subscriber calls Pull API).
6

Dead Letter Topic (DLT)

Messages that fail delivery after N retries are published to a dead letter topic. Enables isolation of poison messages and debugging without blocking the main topic.

Example: Google Pub/Sub: configure a dead letter topic on a subscription. After 5 failed delivery attempts, the message goes to orders-dlq topic.

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Pub/Sub - Module 2: Networking & Communication | System Design | Revise Algo