Volume 10 · Chapter 3 — Messaging and Event-Driven Architecture
Publish/Subscribe with SNS
3.1 Introduction
A queue delivers each message to one consumer, but often you want one event to reach many independent consumers - that’s publish/subscribe, and on AWS it’s SNS (Simple Notification Service). Pub/sub is the fan-out mechanism at the heart of event-driven architecture, and the SNS-plus-SQS combination is one of the most important and frequently-asked messaging patterns on AWS. This chapter covers how pub/sub works, the fan-out pattern, and why combining SNS with SQS gives the best of both.
3.2 Publish/Subscribe and Fan-Out
In publish/subscribe, a publisher sends a message to a topic, and every subscriber to that topic receives a copy - so one published message fans out to many consumers at once. This is the opposite of a queue’s point-to-point delivery (one message, one consumer); with pub/sub, one message reaches all interested subscribers. SNS provides this: publishers send to an SNS topic, and all its subscribers - which can be queues, functions, HTTP endpoints, email - each get the message. Fan-out is exactly what event-driven architecture wants: when something happens, a single event notifies every service that cares, independently, and the publisher neither knows nor depends on the subscribers, so you add new subscribers without changing the publisher.
flowchart TB Pub[Publisher: 'PaymentMade'] --> Topic[(SNS Topic)] Topic --> Q1[(SQS: Notifications)] Topic --> Q2[(SQS: Fraud)] Topic --> Q3[(SQS: Ledger)] Q1 --> C1[Notification consumers] Q2 --> C2[Fraud consumers] Q3 --> C3[Ledger consumers] Note[One event fans out to many;\neach queue buffers for its consumers] -.-> Topic
3.3 SNS Plus SQS: The Fan-Out Pattern
The most powerful and common pattern combines SNS and SQS: a publisher sends an event to an SNS topic, and each interested service subscribes an SQS queue to that topic, so the event fans out to every service’s own queue, where that service’s consumers process it at their own pace. This combines the strengths of both: SNS gives the one-to-many fan-out, and each SQS queue gives that consumer durable buffering, independent processing rate, retries with a dead-letter queue, and resilience if the consumer is temporarily down. Without the queue, a subscriber that’s down when the event is published might miss it; with an SQS queue per subscriber, the event waits durably in each queue until that service processes it. This SNS-to-SQS fan-out is a cornerstone pattern for reliable event distribution on AWS.
3.4 Filtering and Delivery
SNS supports message filtering, so a subscriber can receive only the messages that match criteria it cares about rather than every message on the topic - letting you publish varied events to one topic while each subscriber gets only its relevant subset, which reduces noise and unnecessary processing. SNS handles delivery to each subscriber with retries, and pairing subscribers with SQS queues adds the durability and dead-letter handling above. The design consideration is topic granularity: you organize events into topics at a sensible granularity and use filtering so subscribers get what’s relevant. Together, fan-out with filtering lets one event distribution mechanism serve many consumers efficiently, each getting exactly the events it needs.
3.5 Choosing Queue vs Pub/Sub
The choice between a plain queue and pub/sub follows the delivery need. Use a queue (SQS alone) when a message should be processed by exactly one consumer from a pool - distributing work, like processing each payment once. Use pub/sub (SNS, usually with SQS queues) when an event should reach multiple independent consumers - fan-out, like one “PaymentMade” event notifying notifications, fraud, and ledger services. Many systems use both: SNS fans an event out to several SQS queues, and each queue then uses competing consumers to scale that service’s processing. So it’s not either/or - it’s “one message to one worker” (queue) versus “one event to many services” (pub/sub), and the SNS-plus-SQS combination delivers fan-out with per-consumer buffering and scaling.
3.6 Banking Production Scenario
A bank distributes its “PaymentMade” event using SNS-to-SQS fan-out. The payment service publishes the event to an SNS topic; the notification, fraud, ledger, and analytics services each subscribe their own SQS queue to the topic. The event fans out to all four queues instantly, and each service’s consumers process from their own queue at their own pace, with durable buffering so a service that’s briefly down doesn’t miss the event, plus retries and a dead-letter queue per service. Message filtering ensures each service receives only the event types it cares about. A new rewards service is added later simply by subscribing another queue - the payment service is untouched. This fan-out pattern gives the bank reliable, extensible, independently-scaled distribution of events to many consumers.
3.7 Troubleshooting: Pub/Sub Problems
Pub/sub issues usually involve delivery reliability or the wrong pattern. A subscriber missing events that were published while it was down means it subscribed directly (not via an SQS queue) so there was no durable buffer - use the SNS-to-SQS pattern so events wait in each subscriber’s queue. Needing to notify multiple services but using a single queue means only one consumer gets each message - use SNS to fan out. Subscribers overwhelmed by irrelevant messages means no filtering - add SNS message filtering so each gets only its relevant subset. Adding a consumer requiring changes to the producer means coupling crept in - fan-out should let you add subscribers without touching the publisher. Failures with no investigation path mean missing dead-letter queues on the subscriber queues. The discipline is SNS for one-to-many fan-out, an SQS queue per subscriber for durability and independent processing, filtering for relevance, and DLQs for failures.
What's the difference between SQS and SNS, and why combine them in a fan-out pattern?
SQS and SNS provide two different messaging models - point-to-point queuing versus publish/subscribe fan-out - and combining them gives the strengths of both. SQS is a message queue where each message is processed by exactly one consumer from a pool: a producer sends messages to the queue, and consumers poll and take different messages, so it’s used to distribute work where each item should be handled once, like processing each payment a single time, with competing consumers scaling throughput. SNS is publish/subscribe: a publisher sends a message to a topic, and every subscriber to that topic receives a copy, so one message fans out to many consumers at once - the opposite of the queue’s one-message-one-consumer model. SNS is used when an event should reach multiple independent services, like a single “PaymentMade” event notifying the notification, fraud, and ledger services, and because the publisher doesn’t know or depend on the subscribers, you can add new subscribers without changing the publisher. So the core difference is delivery: SQS delivers each message to one consumer for distributing work, while SNS delivers each message to all subscribers for fan-out. The reason to combine them is the powerful and common fan-out pattern: the publisher sends an event to an SNS topic, and each interested service subscribes its own SQS queue to that topic, so the event fans out to every service’s queue, where that service’s consumers process it at their own pace. This gives the best of both - SNS provides the one-to-many fan-out, and each SQS queue provides that consumer durable buffering, an independent processing rate, retries, a dead-letter queue for failures, and resilience if the consumer is temporarily down. The crucial benefit is durability per subscriber: if a service subscribed directly to SNS and happened to be down when an event was published, it could miss the event, but with an SQS queue per subscriber the event waits durably in each queue until that service processes it, so nothing is missed. Each service can also then use competing consumers on its own queue to scale its processing, and SNS message filtering can ensure each subscriber only receives the event types it cares about. So I use SQS alone when one message should go to one worker from a pool, SNS when one event should reach many services, and the SNS-to-SQS fan-out pattern - one of the cornerstone AWS messaging patterns - when I want reliable, extensible distribution of an event to multiple independent consumers, each with its own durable, independently-scaled, failure-handling queue.
Chapter Revision Notes
- SQS delivers each message to one consumer (point-to-point, distribute work); SNS delivers each message to all subscribers (pub/sub, fan-out).
- SNS: publishers send to a topic, all subscribers get a copy - one event notifies many services; add subscribers without touching the publisher.
- The SNS-to-SQS fan-out pattern subscribes an SQS queue per service, combining fan-out with durable per-consumer buffering, independent rate, retries, and DLQs.
- SNS message filtering delivers each subscriber only its relevant subset of events.
- Use a queue for one-message-one-worker; pub/sub for one-event-many-services; often both together.