Lesson 255 of 296
SQS, SNS and EventBridge
What you will learn
- SQS: queues, visibility timeout, dead-letter queues, FIFO
- Processing SQS messages with Lambda, including partial batch failures
- SNS: publish-subscribe and fan-out
- EventBridge: event buses, rules and the scheduler
- Choosing between them
On this page
SQS (Simple Queue Service)
A fully managed queue (Lesson 236).
- A producer sends messages of up to 1 MiB (the limit was raised from 256 KiB in August 2025, so older material still says 256 KB); larger payloads go to S3 with a reference in the message.
- A consumer receives messages, processes them, then deletes them.
- Visibility timeout: while a consumer is processing a message, SQS hides it from other consumers for a set time (default 30 seconds). If the consumer deletes it in time, it is gone; if the consumer crashes, the message becomes visible again and is retried. Set the visibility timeout longer than the processing time (for Lambda, AWS recommends at least six times the function timeout).
- Long polling (
WaitTimeSecondsup to 20) waits for messages instead of returning empty, reducing cost and latency. - Retention: messages are kept from 1 minute up to 14 days (default 4 days).
- Dead-letter queue (DLQ): after
maxReceiveCountfailed attempts, SQS moves the message to a DLQ for inspection; redrive sends it back later (Lesson 204). - Delay queues and per-message delays postpone delivery (up to 15 minutes).
| Standard queue | FIFO queue | |
|---|---|---|
| Throughput | Nearly unlimited | Lower (high-throughput mode raises it) |
| Ordering | Best effort | Strict order within a message group ID |
| Delivery | At least once (occasional duplicates) | Exactly-once processing within a 5-minute deduplication window |
| Name | Any | Must end in .fifo |
Even with FIFO, design consumers to be idempotent.
Lambda processing SQS, with partial batch failures
Lambda receives batches of messages. If the handler throws, the whole batch returns to the queue and successful messages are processed again. With partial batch responses enabled (ReportBatchItemFailures), the function reports only the failed message IDs:
import type { SQSEvent, SQSBatchResponse } from "aws-lambda";
type EmailJob = { to: string; template: string };
export function makeHandler(sendEmail: (job: EmailJob) => Promise<void>) {
return async (event: SQSEvent): Promise<SQSBatchResponse> => {
const batchItemFailures: SQSBatchResponse["batchItemFailures"] = [];
for (const record of event.Records) {
try {
const job = JSON.parse(record.body) as EmailJob;
await sendEmail(job);
} catch {
batchItemFailures.push({ itemIdentifier: record.messageId });
}
}
return { batchItemFailures };
};
}Only the failed messages become visible again and are retried; after maxReceiveCount they go to the DLQ. makeHandler receives sendEmail as a parameter (dependency injection, Lesson 208) so it can be tested without AWS.
SNS (Simple Notification Service)
A managed publish-subscribe service: publishers send a message to a topic; SNS pushes a copy to every subscriber: SQS queues, Lambda functions, HTTPS endpoints, email, SMS and mobile push.
- Fan-out pattern: SNS topic → several SQS queues, one per consuming service. Each service gets its own durable copy and processes at its own pace (Lesson 236).
- Message filtering: subscription filter policies deliver only matching messages (for example only
eventType = "OrderPaid"to the invoicing queue). - FIFO topics exist for ordered fan-out to FIFO queues.
EventBridge
Amazon EventBridge is a serverless event bus: services put events (JSON with source, detail-type, detail) on a bus; rules with event patterns match events and send them to targets (Lambda, SQS, Step Functions, another bus, API destinations for external HTTP APIs).
{
"source": ["jobprep.orders"],
"detail-type": ["OrderPlaced"],
"detail": { "totalPence": [{ "numeric": [">", 10000] }] }
}This rule matches only orders over £100. Features:
- Receives events from AWS services automatically (an EC2 instance changed state, a CodePipeline run failed) and from SaaS partners (for example Shopify and Auth0 integrations).
- Schema registry, archive and replay of events.
- EventBridge Scheduler: one-off and recurring schedules (cron or rate expressions, with time zones) that trigger targets: reminder emails, nightly jobs.
- Pipes: connect a source (SQS, DynamoDB Streams, Kinesis) to a target with filtering and enrichment.
Choosing
| Need | Use |
|---|---|
| Distribute work to workers, buffering and retries | SQS |
| Send the same message to several subscribers quickly, including push to email, SMS or HTTP | SNS (often SNS → SQS fan-out) |
| Route events by content between many producers and consumers; AWS and SaaS events; schedules | EventBridge |
| Very high-volume, ordered, replayable streams | Kinesis or Amazon MSK (managed Kafka) |
| Multi-step workflows with retries, waits and compensation | Step Functions (orchestration, Lesson 236) |
How to say it in an interview
"SQS is a managed queue: consumers receive messages, which stay hidden for the visibility timeout and must be deleted after processing, otherwise they reappear and retry, ending in a dead-letter queue after max receives. Standard queues are at-least-once with best-effort order; FIFO queues order per message group and de-duplicate. With Lambda I enable partial batch responses so only failed messages are retried. SNS is pub/sub, typically fanning out to one SQS queue per service with filter policies. EventBridge is an event bus with content-based rules, AWS and partner events, archive and replay, and a scheduler for cron-style jobs."
Key words
| Word | Meaning |
|---|---|
| Visibility timeout | How long a received message stays hidden from other consumers |
| Long polling | Waiting for messages instead of returning immediately |
| maxReceiveCount | Attempts before a message goes to the DLQ |
| FIFO queue / message group | Ordered queue / the unit of ordering |
| Partial batch response | Reporting only the failed messages of a batch |
| SNS topic / subscription | Pub/sub channel / a delivery target |
| Fan-out | One message delivered to many queues |
| EventBridge rule / event pattern | Routes matching events to targets |
| EventBridge Scheduler | Managed cron and one-off schedules |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.