SNS topics, filtering and fan-out
One event can have several independent consumers
Amazon SNS is a publish/subscribe service. A topic is a named destination to which a producer publishes messages. Subscriptions identify destinations that receive matching messages. In a bookstore, an OrderPlaced event may feed packing and analytics independently. Sending the event to one shared SQS queue makes workers compete; publishing to an SNS topic with two subscribed queues gives each workflow its own backlog. This pattern is called fan-out. A queue buffers work; a topic distributes events. Neither is the application database.
Filter only the events a subscriber needs
Use message attributes or a JSON body filter policy with the matching FilterPolicyScope. For example, publish an event whose attribute eventType is OrderPlaced and another whose attribute is OrderCancelled. A packing subscription with attribute filter {"eventType":["OrderPlaced"]} receives only the first; an analytics subscription without a filter receives both. This is a simplified exact-match example, not the whole SNS filtering language. Raw message delivery changes the envelope; consumers must know whether they receive the SNS wrapper or the original body. Filter changes can take time to propagate, so do not treat them as immediate authorization controls.
Grant access at both boundaries
For an illustrative same-account commercial AWS setup in us-east-1, a topic ARN is arn:aws:sns:us-east-1:123456789012:orders and the packing queue ARN is arn:aws:sqs:us-east-1:123456789012:packing. The publisher needs appropriate sns:Publish permission; the queue resource policy must allow the SNS service principal to perform sqs:SendMessage on that queue, restricted with aws:SourceArn to the intended topic. The packing worker needs only its queue receive/delete/change-visibility actions as appropriate. Customer-managed encryption keys may require additional key policy permissions. This is a permission sketch, not a deployable complete policy. Do not include secrets in messages, expose them in logs, or subscribe real recipients in the offline lab.
Distinguish two delivery failures
Publishing successfully is not proof every subscriber processed the event. SNS retries delivery failures according to the endpoint protocol and error class; a subscription DLQ can capture failed deliveries. Once the message reaches SQS, repeated processing failures belong to the queue redrive policy and its own DLQ. If analytics is slow, its queue may grow while packing continues. Monitor subscription delivery failures, queue age and DLQ counts separately. Recovery must preserve the business event ID and account for duplicate side effects.
Practice: trace an order cancellation
A cancellation must reach analytics and a refund workflow, but never start packing. Draw the topic and three queues; assign filters to packing and refund and leave analytics unfiltered. For the two example events, packing receives OrderPlaced, refund receives OrderCancelled and analytics receives both. Use the offline fixture to check this exact-match routing; it does not validate IAM, SNS delivery or policy propagation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SNS topics, filtering and fan-out — knowledge check
Reason about fictional messaging workflows without AWS access.
Packing and analytics each need every order event independently. Which design fits?
An attribute filter is {"eventType":["OrderPlaced"]}. Which event matches?
What should authorize SNS delivery to a packing queue?
SNS cannot deliver to the queue at all. Which failure store addresses that boundary?
Analytics workers are temporarily slow. What is a benefit of separate queues?