SQS, SNS & EventBridge
Messaging and event-driven architecture on AWS — SQS standard vs FIFO queues, visibility timeout, long polling, dead-letter queues and redrive; SNS topics, fan-out and filtering; EventBridge event buses, rules, schemas, Pipes and Scheduler; Kinesis and MSK for streams; and choosing the right service.
Decoupling services with messages lets each one scale, fail and deploy independently: the producer doesn't wait for, or even know about, the consumers. AWS offers several building blocks that differ in delivery model (queue, pub/sub, event routing, stream). The theory is in Message queues and event streaming.
| Service | Model | Think of it as |
|---|---|---|
| SQS | Queue — each message processed by one consumer | A to-do list for workers |
| SNS | Pub/sub — push each message to every subscriber | A broadcast |
| EventBridge | Event bus — route events by content with rules | A smart router for events across services, accounts and SaaS |
| Kinesis Data Streams / MSK | Ordered, replayable log with shards/partitions | Kafka-style streaming (Stream processing) |
SQS: Simple Queue Service
The protocol is pull, process, delete:
- Visibility timeout (default 30 s, max 12 h): after a consumer receives a message, it's hidden from others. If the consumer doesn't delete it in time — it crashed, or it's slow — the message reappears and is delivered again. Set it comfortably above your processing time (six times the Lambda timeout for Lambda triggers), or extend it with
ChangeMessageVisibilityfor long jobs. - Long polling (
WaitTimeSecondsup to 20) waits for messages instead of returning empty responses — cheaper and lower-latency than short polling. - Retention: 4 days by default, up to 14 days.
- Message size up to 1 MiB; for larger payloads store the body in S3 and send a pointer (the "claim check" pattern).
- Delay queues and per-message timers postpone delivery up to 15 minutes.
Standard vs FIFO
| Standard | FIFO | |
|---|---|---|
| Throughput | Nearly unlimited | High (thousands per second per queue in high-throughput mode), scaled by message groups |
| Ordering | Best effort | Strict within a message group (MessageGroupId, e.g. per customer) |
| Delivery | At least once — occasional duplicates | Exactly-once processing within a 5-minute deduplication window |
| Use | Most background work | Order-sensitive flows — ledger entries, per-entity state changes |
Even with FIFO, design consumers to be idempotent — side effects outside SQS can still repeat if a consumer crashes after acting but before deleting (Lambda).
Dead-letter queues
A message that keeps failing would loop forever. A redrive policy with maxReceiveCount (say 5) moves it to a dead-letter queue after N receives. Alarm on DLQ depth, inspect the poison messages, fix the bug, then redrive them back to the source queue.
Scaling consumers
Queue depth (ApproximateNumberOfMessagesVisible) and the age of the oldest message are the signals: scale ECS services or Auto Scaling groups on backlog per worker, or let Lambda's SQS integration scale pollers automatically (cap it with maximum concurrency to protect downstream systems).
SNS: Simple Notification Service
A topic pushes each published message to all its subscriptions: SQS queues, Lambda functions, HTTPS endpoints, Kinesis Data Firehose, email, SMS and mobile push.
Fan-out
SNS + SQS fan-out is a classic pattern: each consuming service gets its own queue, so it can process at its own pace, retry independently and survive downtime — SNS alone just pushes and retries for a while. Subscription filter policies on message attributes or body let each subscriber receive only the messages it cares about. SNS FIFO topics pair with FIFO queues to preserve ordering.
EventBridge
EventBridge is a serverless event bus: producers put events; rules match event patterns and send them to targets (Lambda, SQS, Step Functions, API destinations, other buses in other accounts — 20+ target types).
{
"source": ["com.shop.orders"],
"detail-type": ["OrderPlaced"],
"detail": {
"total": [{ "numeric": [">", 10000] }],
"country": ["IN", "SG"]
}
}- The default bus receives events from AWS services themselves (an EC2 instance changed state, a CodePipeline failed, a GuardDuty finding) — the backbone of automation.
- Custom buses for your application events; partner buses for SaaS sources (Stripe, Shopify, Datadog and others).
- Schema registry discovers event structures and generates code bindings.
- Archive and replay for re-processing past events.
- Pipes: point-to-point integration from a source (SQS, Kinesis, DynamoDB Streams) with optional filtering, enrichment and transformation to a target, without glue code.
- Scheduler: one-off and recurring schedules (cron or rate) at massive scale — replacing "cron on an EC2 box".
Event design matters more than the service: publish facts that happened (OrderPlaced) with stable schemas and versioning, not commands to specific consumers.
Streams: Kinesis and MSK
When you need ordering per key, high throughput and replay — clickstreams, telemetry, change data capture — use a log:
- Kinesis Data Streams: shards (or on-demand mode); ordering per partition key; retention from 24 hours up to 365 days; multiple consumers read independently, with enhanced fan-out for dedicated throughput.
- Amazon Data Firehose: managed delivery of streams into S3, Redshift, OpenSearch or HTTP endpoints, with batching and format conversion.
- Amazon MSK: managed Apache Kafka (provisioned or serverless) when you want the Kafka ecosystem.
Choosing
| Need | Use |
|---|---|
| Buffer work for a pool of workers, smooth spikes | SQS |
| Strict per-entity ordering | SQS FIFO (or Kinesis by partition key) |
| Notify several services of the same message | SNS → SQS fan-out |
| Route events by content across many services and accounts; react to AWS events; SaaS integrations | EventBridge |
| High-volume ordered streams with replay and multiple readers | Kinesis or MSK |
| Multi-step workflows with state, retries and compensation | Step Functions (API Gateway and Step Functions) |
QWhat is the SQS visibility timeout and what happens if it's too short?easy
When a consumer receives a message, SQS hides it from other consumers for the visibility timeout. The consumer must delete the message before the timeout expires; otherwise it becomes visible again and is redelivered. If the timeout is shorter than the processing time, messages get processed concurrently by multiple consumers, causing duplicates and wasted work. Set it above the worst-case processing time or extend it during long processing.
QWhy use SNS with SQS instead of SNS alone?medium
SNS pushes messages to subscribers and retries for a limited period, but it doesn't store messages for consumers that are down or slow. Putting an SQS queue per consumer behind the topic gives each consumer durable buffering, independent scaling and retries, a dead-letter queue, and the ability to process at its own pace — while the publisher still sends a single message.
QSNS vs EventBridge — when would you choose each?medium
SNS is for high-throughput, low-latency pub/sub fan-out to many subscribers, including SMS, email and mobile push, with attribute or body filtering. EventBridge is an event router: richer content-based pattern matching, native integration with AWS service events and SaaS partners, cross-account buses, schema registry, archive and replay, and many target types — at somewhat higher latency and per-event cost. Use EventBridge as the backbone for domain events between services and SNS when you need massive fan-out or notification channels.
- SQS queues work for consumers that pull, process and delete; tune the visibility timeout and always configure a dead-letter queue.
- Standard queues are at-least-once with best-effort ordering; FIFO queues order within message groups and deduplicate — consumers should still be idempotent.
- SNS pushes to every subscriber; SNS → SQS fan-out gives each consumer durable, independent processing.
- EventBridge routes events by content across services, accounts and SaaS, and provides Pipes and Scheduler.
- Kinesis and MSK provide ordered, replayable streams for high-volume data.
- Publish facts as events with stable schemas; pick the service by delivery model.