StackPractices
intermediate By Mathias Paulenko

Serverless Fanout Pattern

Broadcast a single event to multiple independent consumers via SNS, EventBridge, or SQS so each consumer processes the event without coupling.

Overview

The fanout pattern broadcasts one event to every subscribed consumer. A producer publishes one message to a topic (SNS, EventBridge). The topic hands a copy to each subscriber (SQS queue, Lambda, HTTP endpoint). Every consumer works through the event at its own pace; a slow one doesn’t drag down the rest.

This decouples producers from consumers. The producer doesn’t know how many consumers exist or what they do. Adding a new consumer requires only subscribing to the topic, not modifying the producer. If one consumer fails, others continue processing.

When to Use

  • One event must trigger several independent actions (order placed: update inventory, send email, generate invoice)
  • You want to add or remove consumers without modifying the producer
  • Consumers process at different speeds and shouldn’t block each other
  • You need reliable delivery: if a consumer is down, the event should wait and retry
  • Different teams own different consumers

Skip it when there’s only one consumer — a direct queue does the same job with less infrastructure. Also skip it for synchronous request/response flows (fanout is fire-and-forget) and when consumers need a strict global order, since SNS and EventBridge don’t guarantee ordering across subscribers.

Solution

SNS + SQS Fanout (Python CDK)

from aws_cdk import (
    Stack,
    aws_sns as sns,
    aws_sns_subscriptions as subs,
    aws_sqs as sqs,
    aws_lambda as lambda_,
    aws_lambda_event_sources as events,
    Duration,
)
from constructs import Construct

class FanoutStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs):
        super().__init__(scope, construct_id, **kwargs)

        # SNS topic — the fanout point
        topic = sns.Topic(self, "OrderEventsTopic",
            topic_name="order-events",
            display_name="Order Events",
        )

        # Consumer 1: Email notification
        email_queue = sqs.Queue(self, "EmailQueue",
            visibility_timeout=Duration.seconds(300),
            retention_period=Duration.days(7),
        )
        topic.add_subscription(subs.SqsSubscription(email_queue))

        email_lambda = lambda_.Function(self, "EmailConsumer",
            runtime=lambda_.Runtime.PYTHON_3_12,
            handler="email.handler",
            code=lambda_.Code.from_asset("lambda/email"),
            timeout=Duration.seconds(60),
        )
        email_lambda.add_event_source(events.SqsEventSource(email_queue, batch_size=10))

        # Consumer 2: Inventory update
        inventory_queue = sqs.Queue(self, "InventoryQueue",
            visibility_timeout=Duration.seconds(300),
            retention_period=Duration.days(7),
        )
        topic.add_subscription(subs.SqsSubscription(inventory_queue))

        inventory_lambda = lambda_.Function(self, "InventoryConsumer",
            runtime=lambda_.Runtime.PYTHON_3_12,
            handler="inventory.handler",
            code=lambda_.Code.from_asset("lambda/inventory"),
            timeout=Duration.seconds(60),
        )
        inventory_lambda.add_event_source(events.SqsEventSource(inventory_queue, batch_size=10))

        # Consumer 3: Analytics
        analytics_queue = sqs.Queue(self, "AnalyticsQueue",
            visibility_timeout=Duration.seconds(300),
            retention_period=Duration.days(7),
        )
        topic.add_subscription(subs.SqsSubscription(analytics_queue))

        analytics_lambda = lambda_.Function(self, "AnalyticsConsumer",
            runtime=lambda_.Runtime.PYTHON_3_12,
            handler="analytics.handler",
            code=lambda_.Code.from_asset("lambda/analytics"),
            timeout=Duration.seconds(60),
        )
        analytics_lambda.add_event_source(events.SqsEventSource(analytics_queue, batch_size=100))

Publishing Events (Python)

import boto3
import json
import time

sns = boto3.client('sns')

def publish_order_event(order_id: str, event_type: str, data: dict):
    message = {
        "eventType": event_type,
        "orderId": order_id,
        "data": data,
        "timestamp": int(time.time() * 1000),
    }

    response = sns.publish(
        TopicArn="arn:aws:sns:us-east-1:123456789012:order-events",
        Message=json.dumps(message),
        Subject=f"Order {event_type}",
        MessageAttributes={
            "eventType": {"DataType": "String", "StringValue": event_type},
        },
    )

    return response["MessageId"]

Lambda Consumer Handler (Python)

import json

def handler(event, context):
    for record in event["Records"]:
        # SQS wraps SNS message
        sns_message = json.loads(record["body"])
        message = json.loads(sns_message["Message"])

        event_type = message["eventType"]
        order_id = message["orderId"]
        data = message["data"]

        if event_type == "ORDER_PLACED":
            send_order_email(order_id, data)
        elif event_type == "ORDER_SHIPPED":
            send_shipping_notification(order_id, data)

def send_order_email(order_id, data):
    # Email sending logic
    pass

EventBridge Fanout (TypeScript)

import { EventBridgeClient, PutEventsCommand } from '@aws-sdk/client-eventbridge';

const eventBridge = new EventBridgeClient({ region: 'us-east-1' });

interface OrderEvent {
  eventType: string;
  orderId: string;
  customerId: string;
  total: number;
  items: any[];
}

async function publishOrderEvent(event: OrderEvent): Promise<void> {
  await eventBridge.send(new PutEventsCommand({
    Entries: [{
      EventBusName: 'default',
      Source: 'order.service',
      DetailType: event.eventType,
      Detail: JSON.stringify(event),
    }],
  }));
}

// Consumer Lambda with event pattern filtering
// EventBridge rule: { "source": ["order.service"], "detail-type": ["ORDER_PLACED"] }
export const emailConsumerHandler = async (event: any): Promise<void> => {
  const orderEvent: OrderEvent = JSON.parse(event.detail);

  console.log(`Sending email for order ${orderEvent.orderId}`);
  // Email logic here
};

// EventBridge rule: { "source": ["order.service"], "detail-type": ["ORDER_PLACED", "ORDER_SHIPPED"] }
export const analyticsConsumerHandler = async (event: any): Promise<void> => {
  const orderEvent: OrderEvent = JSON.parse(event.detail);

  console.log(`Recording analytics for ${orderEvent.eventType}`);
  // Analytics logic here
};

CDK for EventBridge Fanout (TypeScript)

import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as lambda from 'aws-cdk-lib/aws-lambda';

export class FanoutStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string) {
    super(scope, id);

    // Consumer functions
    const emailConsumer = new lambda.Function(this, 'EmailConsumer', {
      runtime: lambda.Runtime.NODEJS_20_X,
      handler: 'email.handler',
      code: lambda.Code.fromAsset('lambda/email'),
    });

    const inventoryConsumer = new lambda.Function(this, 'InventoryConsumer', {
      runtime: lambda.Runtime.NODEJS_20_X,
      handler: 'inventory.handler',
      code: lambda.Code.fromAsset('lambda/inventory'),
    });

    const analyticsConsumer = new lambda.Function(this, 'AnalyticsConsumer', {
      runtime: lambda.Runtime.NODEJS_20_X,
      handler: 'analytics.handler',
      code: lambda.Code.fromAsset('lambda/analytics'),
    });

    // Rules with event pattern filtering
    const orderPlacedRule = new events.Rule(this, 'OrderPlacedRule', {
      eventPattern: {
        source: ['order.service'],
        detailType: ['ORDER_PLACED'],
      },
    });

    // Fanout: one event triggers multiple targets
    orderPlacedRule.addTarget(new targets.LambdaFunction(emailConsumer));
    orderPlacedRule.addTarget(new targets.LambdaFunction(inventoryConsumer));
    orderPlacedRule.addTarget(new targets.LambdaFunction(analyticsConsumer));
  }
}

Explanation

The fanout pattern works by inserting a topic between the producer and consumers:

  1. Producer publishes one message to the topic (SNS or EventBridge). The producer doesn’t know who subscribes.

  2. Topic gives every subscriber its own copy of the message. SNS pushes to SQS queues, Lambda functions, or HTTP endpoints. EventBridge evaluates rules and invokes matching targets.

  3. SQS queue (optional, but you’ll want one) buffers messages per consumer. If the consumer Lambda is down or rate-limited, the message just waits in the queue, so the producer isn’t affected by the consumer’s state.

  4. Consumer reads the message from its queue. When processing fails, the message goes back to the queue and SQS retries it; after max retries it lands in a dead-letter queue.

The key benefit is isolation: if the email consumer is slow or fails, the inventory and analytics consumers are unaffected. Each consumer has its own queue, retry policy, and scaling.

flowchart diagram: Producer Lambda

Variants

ApproachTopicBest For
SNS + SQSSNS topic, SQS per consumerReliable delivery, buffering
EventBridgeEvent bus with rulesContent-based filtering, many event types
SNS direct to LambdaSNS topic, Lambda subscriptionSimple, no buffering needed
Kinesis fanoutKinesis stream, consumersHigh-throughput streaming
SNS + SQS + DLQSNS, SQS, dead-letter queueProduction-grade with error handling

Best Practices

  • For a deeper guide, see Serverless Event Sourcing Pattern.

  • Use SQS between SNS and Lambda — direct SNS-to-Lambda invocations fail if Lambda is throttled. SQS buffers messages and retries. This is the recommended production pattern; see the SQS to Lambda event-driven recipe for the consumer side, batch handling, and DLQ setup.

  • Set visibility timeout to 6x Lambda timeout — if Lambda takes 60s, set SQS visibility timeout to 360s. This keeps one long-running invocation from processing the same batch twice.

  • Use dead-letter queues — after max receive count, move messages to a DLQ for inspection. Don’t lose events silently.

  • Filter events at the topic — SNS supports message attribute filtering. EventBridge supports content-based filtering. Only deliver relevant events to each consumer.

  • Make consumers idempotent — SQS may deliver a message twice (at-least-once). Consumers should treat every message as a possible duplicate and dedupe with an idempotency key.

Common Mistakes

  • Direct SNS to Lambda without SQS — if Lambda is throttled, SNS retries with backoff but may drop messages after max retries. Put an SQS queue in front of the consumer for reliable delivery.
  • No dead-letter queue — failed messages disappear after max retries. Always attach a DLQ so you can inspect and reprocess failures.
  • Visibility timeout too short — if Lambda takes longer than the visibility timeout, SQS redelivers the message, causing duplicate processing. Set it to 6x the Lambda timeout.
  • Coupling producer to consumers — if the producer checks consumer status or sends different messages to different consumers, the fanout isn’t truly decoupled. The producer should publish one event and let the topic handle delivery.
  • No idempotency in consumers — SQS at-least-once delivery means consumers may process the same message twice. Use an idempotency key so a repeated message doesn’t run the side effect twice.

Troubleshooting

  • One consumer doesn’t get the event: check its subscription filter policy first. A FilterPolicy mismatch quietly drops the message — SNS doesn’t count it as an error.
  • Messages redelivered while Lambda still runs: the visibility timeout is shorter than the function timeout. Set it to 6x the Lambda timeout and consider a shorter batch_size for slow consumers.
  • Seeing the same side effect twice: SQS is at-least-once. Store the MessageId or your own idempotency key and skip work that’s already done.
  • DLQ filling up: something in the message or the consumer is consistently broken. Inspect the DLQ, fix the cause, then redrive — don’t just purge it.
  • EventBridge target never fires: the rule’s event pattern doesn’t match the event. Test the pattern against a sample event before blaming the target.

Further Reading

Frequently Asked Questions

What is the difference between SNS fanout and EventBridge fanout?

SNS sends every message to every subscriber. EventBridge evaluates rules and only invokes the targets that match the event pattern. EventBridge is better for content-based filtering; SNS is simpler for unconditional fanout.

Should I use SQS between SNS and Lambda?

Yes, for production. SQS buffers messages when Lambda is throttled or down. Without SQS, SNS retries with backoff but may drop messages after the retry policy is exhausted. SQS gives you reliable delivery plus retries and a DLQ.

How do I filter events in SNS?

Use message attributes. Set a FilterPolicy on the subscription so the queue only gets messages whose attributes match. An email consumer, for example, can subscribe with {"eventType": ["ORDER_PLACED"]} and get just the order-placed events.

How do I handle poison messages?

Attach a dead-letter queue to the SQS queue. Once a message hits maxReceiveCount failed receives (say 5), it moves to the DLQ. Monitor the DLQ and investigate why the message couldn't be processed. Fix the consumer or the message format, then redrive from the DLQ.

SNS fanout or Kinesis — which scales better?

Different tools. SNS + SQS scales to very high message rates and each consumer works at its own pace; Kinesis keeps a single ordered stream that consumers share with enhanced fan-out. Pick SNS + SQS for independent consumers reacting to business events; pick Kinesis when you're streaming high-volume data that consumers read in order.