Pub/Sub Explained | Interview Guide

Pub/Sub Explained | Interview Guide
Pub/Sub

Pub/Sub Explained: The Foundation of Event-Driven Systems

A practical, interview-focused guide to publish/subscribe messaging: how it works, when to use it, and the trade-offs engineers must consider.

Focus: explain publishers, topics, subscribers, delivery models, common technologies, and interview-ready best practices and patterns.

Table of Contents

Introduction

Publish/Subscribe (Pub/Sub) is a messaging pattern that enables loosely coupled communication between components by decoupling producers (publishers) from consumers (subscribers) using named channels called topics.

Instead of directly invoking other services, a publisher emits messages to a topic. Subscribers register their interest in one or more topics and receive copies of messages the moment they are published (or shortly thereafter) depending on delivery guarantees.

This pattern is foundational for event-driven architectures, distributed systems, and real-time data pipelines. In interviews, you should be able to explain the basic flow, the common delivery models, and the trade-offs of choosing pub/sub over point-to-point messaging or synchronous APIs.

Below you'll find a comprehensive explanation suitable for interview prep, including concrete examples, comparison to other messaging styles, and operational considerations that senior engineers will expect you to know.

How Pub/Sub Works

At its simplest, pub/sub consists of three roles: the publisher, the broker (or topic), and the subscriber.

The publisher creates messages and sends them to a named topic on the broker. The broker is responsible for persisting or routing the message and delivering it to the set of subscribers that have expressed interest in that topic.

Subscribers can be many and varied: background workers, analytics pipelines, notification services, or other microservices. Each subscriber receives its own copy of the message (fan-out), enabling independent processing and scaling.

Unlike request-response, where a client waits for a reply, pub/sub is inherently asynchronous. The publisher does not wait for subscribers to act; it only ensures the message is published, and the broker coordinates delivery.

Message Flow Example

Consider a simple e-commerce flow: when an order is placed, the Order Service publishes an order.placed message. The Payments Service, Inventory Service, and Notification Service each subscribe to order.placed. When the broker receives the message, it delivers a copy to each subscriber so they can process the event independently.

Key point: Pub/Sub allows many subscribers to consume the same event without the publisher knowing about them. This supports extensibility and loose coupling.

Message Delivery Models

Publish/Subscribe (Fan-out)

In the classic pub/sub model, one message is delivered to many subscribers. This is useful when multiple systems need to react to the same information, such as analytics, auditing, and downstream workflows.

Queue (Point-to-Point)

Some brokers support point-to-point queues where a message is consumed by a single consumer from the queue. This is useful for load balancing work across multiple workers where each message should be processed only once.

Delivery Guarantees

Delivery semantics matter in production systems. Common models include:

  • At most once: Messages may be lost but are never duplicated. Use when occasional loss is tolerable.
  • At least once: Messages are retried until acknowledged, which can lead to duplicates — consumers must handle idempotency.
  • Exactly once: Achieving true exactly-once across distributed components is hard, but some systems offer transactional semantics or deduplication helpers to approximate it.

Ordering and Partitions

Brokers often partition topics to increase throughput. While partitioning improves scalability, it can make global ordering harder: ordering is typically guaranteed within a partition but not across partitions. Choose keys and partitions thoughtfully when ordering matters.

Common Pub/Sub Patterns

Fan-out

One event is broadcast to many subscribers. Useful for notifications, logging, and analytics.

Event Notification

Publish a small message to state that something happened; subscribers then decide whether to fetch additional data. This keeps events lightweight but may require additional reads.

Event-Carried State Transfer

Include enough state in the message payload so subscribers can act without fetching extra data. This reduces latency but increases message size.

Competing Consumers

Multiple consumers consume from the same queue to parallelize processing. Works well for background jobs and asynchronous tasks where only one consumer should handle each message.

Dead-letter Queues

Unprocessable messages are routed to a dead-letter queue for inspection or manual handling. This prevents a single malformed message from blocking processing.

Popular Technologies

Apache Kafka

High-throughput distributed streaming platform with partitioned logs and strong ordering guarantees within partitions. Often used for event streaming and durable message storage.

RabbitMQ

Feature-rich message broker supporting many messaging patterns, routing keys, and delivery guarantees. Works well for traditional messaging and complex routing.

Google Pub/Sub

Fully managed pub/sub service with auto-scaling, at-least-once delivery, and integration with cloud services.

AWS SNS / SQS

SNS is a pub/sub topic service for fan-out; SQS provides queues for point-to-point processing. They are commonly used together for decoupled architectures on AWS.

Real-World Use Cases

Pub/Sub is widely applicable. Common use cases include:

  • Notifications: Send email, SMS, or push notifications when events occur.
  • Analytics pipelines: Stream user events to analytics systems for metrics and ML training.
  • Microservices integration: Propagate domain events across bounded contexts.
  • IoT telemetry: Collect and process device events at scale.
  • Audit and compliance: Persist event streams for traceability and replay.

Use these examples in interviews to show practical thinking: mention concrete systems (e.g., "OrderPlaced topic feeds Payments, Inventory, and Email services") and explain why pub/sub is a good fit.

Key Benefits

Loose coupling

Producers don't need to know about consumers, enabling independent evolution.

Scalability

Partitioning and fan-out let systems scale horizontally.

Flexibility

Add new subscribers without modifying publishers.

Resilience

Broker buffering and retries help absorb spikes and transient failures.

Real-time communication

Subscribers can react to changes immediately for near-real-time experiences.

Challenges

Operational Complexity

Managing brokers, partitions, retention, and scaling adds operational overhead.

Observability

Tracing events across many services requires correlation IDs and robust observability tooling.

Consistency and Ordering

Global ordering is difficult; eventual consistency requires careful design of user-visible behavior.

Idempotency and Duplicates

At-least-once delivery means consumers must be idempotent or deduplicate messages.

Schema Evolution

Changing message formats is hard; consumers must maintain backward compatibility.

Best Practices

  • Pick meaningful topic names that reflect business intent (e.g., order.created, user.signup).
  • Keep messages small and include essential context only.
  • Version your schemas and prefer additive, optional fields for backward compatibility.
  • Use correlation IDs for tracing and debugging across services.
  • Design consumers to be idempotent and to handle duplicates safely.
  • Instrument consumer lag, retry rates, and dead-letter activity to monitor health.
  • Use dead-letter queues and alerting for poison messages to avoid processing blocks.
  • Partition topics using keys that preserve ordering where needed.
  • Encrypt messages in transit and secure access to topics and brokers.
  • Document event contracts and publish them for consumers to consume confidently.

Interview Strategy

When asked about pub/sub in an interview, structure your answer: define the pattern, explain roles, give an example, and discuss trade-offs.

Start with a concise definition: "Pub/Sub is a messaging pattern where publishers send messages to a topic and subscribers receive copies of those messages asynchronously."

Then describe a short example flow, mention delivery guarantees and ordering, and show awareness of operational concerns like monitoring and schema evolution. Finish with a recommendation about when to use pub/sub versus request-response.

Interviewers look for practical reasoning: show you understand failure modes and how to mitigate them (idempotency, dead-letter queues, correlation IDs), not just the happy path.

10 Question Quiz

Test your pub/sub knowledge with these interview-style questions.

1. What does Pub/Sub primarily decouple?
2. Which model delivers one message to many subscribers?
3. What is a common guarantee offered by brokers?
4. Which technology is commonly used for high-throughput pub/sub?
5. Why is idempotency important for subscribers?
6. What pattern stores events and replays them to rebuild state?
7. What is a dead-letter queue used for?
8. Which is a drawback of partitioning topics?
9. What should you include in messages for observability?
10. When is Pub/Sub a better choice than synchronous HTTP?

Final Thoughts

Pub/Sub is a versatile and powerful messaging model that powers many modern systems. For interviews, focus on clear explanations, concrete examples, and operational considerations.

Explain the flow, name the common tools, discuss delivery semantics and ordering, and show how you would mitigate failure modes using idempotency, dead-letter queues, and observability.

When possible, tie your answers to real systems you know or have designed — that demonstrates practical experience and makes your answers memorable.

Comments

Popular posts from this blog

RabbitMQ Explained | Interview Guide

Vulkan vs DirectX Explained | Interview Guide

Indecision at Key Levels (Reversal Signal)