CodeMosa

Master LeetCode Patterns

Pub/Sub Pattern

Messaging & Async

Event-driven architecture with publishers and subscribers decoupled via topics

Core Idea

#

Publishers emit events to topics; subscribers consume independently. Enables fan-out, selective subscriptions, and loose coupling across services.

When to Use

#

When multiple services react to the same event, or you want to decouple producers from diverse consumers.

Recognition Cues

#
Indicators that this pattern might be the right solution
  • Need to fan-out a domain event
  • Multiple teams interested in the same signal
  • Desire to add consumers without changing producers

Pattern Variants & Approaches

#

Overview

#
Publishers emit events to a topic; subscribers independently consume with filters and consumer groups.

Overview Architecture

PublishDeliverDeliver⚙️Publisher📬Topic⚙️Subscriber 1⚙️Subscriber 2

When to Use This Variant

  • Fan-out to multiple systems
  • Team autonomy
  • Selective subscriptions

Use Case

Domain events across microservices and near-real-time pipelines.

Advantages

  • Loose coupling
  • Easy fan-out
  • Independent scaling

Implementation Example

# Pub/Sub sketch
topic.publish(event)
sub1.on(event)
sub2.on(event)

Tradeoffs

#

Pros

  • Loose coupling and team autonomy
  • Easy fan-out and independent scaling
  • Flexible real-time processing

Cons

  • Eventual consistency and debugging complexity
  • Operational overhead (schemas, lag, replay)
  • Hard to guarantee ordering globally

Common Pitfalls

#
  • Assuming strict ordering across partitions
  • No schema/versioning control causing breakages
  • Backpressure not handled leading to lag
  • Duplicate/out-of-order event handling missing

Design Considerations

#
  • Push vs pull delivery; durable vs ephemeral subs
  • Topic partitioning and per-subscription filters
  • Schema registry and evolution policies
  • Consumer groups for horizontal scale
  • DLQ and replay policies

Real-World Examples

#
Apache Kafka

Topic-based pub/sub with consumer groups

Thousands of topics, millions TPS
Google Pub/Sub

Global pub/sub with push/pull

Global multi-region service
AWS SNS

Fan-out to SQS, Lambda, HTTP

Massive event fan-out

Complexity Analysis

#
Scalability

High - Partitioned topics

Implementation Complexity

Medium - Contracts and lag mgmt

Cost

Low to Medium - Broker/service fees