Skip to the content

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 (WaitTimeSeconds up 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 maxReceiveCount failed 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 queueFIFO queue
ThroughputNearly unlimitedLower (high-throughput mode raises it)
OrderingBest effortStrict order within a message group ID
DeliveryAt least once (occasional duplicates)Exactly-once processing within a 5-minute deduplication window
NameAnyMust 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:

TypeScript
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

NeedUse
Distribute work to workers, buffering and retriesSQS
Send the same message to several subscribers quickly, including push to email, SMS or HTTPSNS (often SNS → SQS fan-out)
Route events by content between many producers and consumers; AWS and SaaS events; schedulesEventBridge
Very high-volume, ordered, replayable streamsKinesis or Amazon MSK (managed Kafka)
Multi-step workflows with retries, waits and compensationStep 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

WordMeaning
Visibility timeoutHow long a received message stays hidden from other consumers
Long pollingWaiting for messages instead of returning immediately
maxReceiveCountAttempts before a message goes to the DLQ
FIFO queue / message groupOrdered queue / the unit of ordering
Partial batch responseReporting only the failed messages of a batch
SNS topic / subscriptionPub/sub channel / a delivery target
Fan-outOne message delivered to many queues
EventBridge rule / event patternRoutes matching events to targets
EventBridge SchedulerManaged cron and one-off schedules

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.