Decoupling with SQS, SNS and EventBridge

Connect services asynchronously with queues, pub/sub topics and event buses, and handle retries, ordering and dead-letter queues.

Intermediate · about 5 hours · difficulty 3/5

Learn first: AWS Lambda Functions

Tightly coupled systems fail together. Putting a queue or event bus between services lets each one scale, fail and deploy independently.

Amazon SQS (queues)

  • A producer sends messages; consumers poll and delete them after processing.
  • Standard queues: nearly unlimited throughput, at-least-once delivery, best-effort ordering.
  • FIFO queues: strict ordering per message group and exactly-once processing within a window, with lower throughput.
  • Visibility timeout hides a message while it is being processed. Dead-letter queues (DLQs) catch messages that keep failing.

Amazon SNS (pub/sub)

  • Publish once to a topic; it fans out to many subscribers (SQS queues, Lambda, HTTP endpoints, email, SMS).
  • The SNS to multiple SQS fan-out pattern lets several services each process every event independently.

Amazon EventBridge (event bus)

  • Routes events using rules that match on event content, to targets like Lambda, Step Functions or SQS.
  • Receives events from AWS services (EC2 state changes, S3 uploads) and SaaS partners.
  • Scheduler replaces cron jobs; Pipes connect sources to targets with filtering.

Azure Service Bus, Event Grid and Google Pub/Sub cover the same ground.

Pitfalls

  • Non-idempotent consumers, since at-least-once means duplicates will happen.
  • Visibility timeout shorter than processing time, causing double processing.
  • No DLQ, so poison messages retry forever.

You are ready to move on when you can explain when to use a queue, a topic or an event bus, and build a fan-out with a DLQ.

Key concepts

  • SQS standard vs FIFO
  • Visibility timeout
  • Dead-letter queues
  • SNS topics and fan-out
  • EventBridge rules and buses
  • EventBridge Scheduler
  • Idempotent consumers
  • Kinesis Data Streams: For ordered, high-volume streaming

Mini project

Build an order-placed flow: an API publishes to SNS, which fans out to two SQS queues (email service and inventory service), each consumed by a Lambda with a DLQ. Send a deliberately bad message and watch it land in the DLQ. Low volumes stay within free tier; delete everything when finished.

Check yourself

Why must an SQS consumer be idempotent even when you use a standard queue correctly?

Free resources