Lesson 15 of 30Modern AWS
Queues and Events
Decouple services with SQS, SNS, EventBridge, Kinesis and Step Functions, and pick the right one for each job.
When a user places an order, a lot has to happen: charge the card, reserve stock, send an email, update analytics. Doing all of it inside one HTTP request makes the request slow and fragile: if the email provider is down, the order fails. Queues and events let the request do the one essential thing and hand the rest off reliably. On AWS that means five services, and interviewers love asking which one you would use.
SQS: a queue of work
Amazon SQS (Simple Queue Service) is a fully managed message queue. Producers SendMessage; consumers ReceiveMessage, process, then DeleteMessage. Messages wait in the queue (up to 14 days) until someone handles them, so a slow or crashed consumer never loses work.
Two queue types:
| Standard | FIFO | |
|---|---|---|
| Ordering | Best effort | Strict, per message group ID |
| Delivery | At least once (rare duplicates) | Exactly-once processing within a 5-minute dedup window |
| Throughput | Nearly unlimited | High, but limited per group (higher with high-throughput mode) |
| Name | orders | Must end in .fifo: orders.fifo |
Because standard queues can deliver a message twice, consumers should be idempotent: processing the same message twice has the same effect as once (for example, "insert payment if this order ID has not been charged").
Visibility timeout and dead-letter queues
When a consumer receives a message, SQS does not delete it. It hides it for the visibility timeout (default 30 seconds). If the consumer deletes it in time, done. If the consumer crashes, the timeout expires and the message reappears for another consumer. That is how SQS guarantees work is not lost.
If a message keeps failing (a bug, bad data), it would loop forever. A redrive policy moves it to a dead-letter queue (DLQ) after maxReceiveCount attempts, where you can inspect it, alarm on it, and redrive it once fixed.
Press Run (⌘ Enter) to see what happens.Set the visibility timeout longer than your worst-case processing time (AWS suggests six times the Lambda timeout when Lambda consumes the queue). Too short, and a slow-but-working consumer's message reappears and gets processed twice.
SNS: fan out to many subscribers
Amazon SNS (Simple Notification Service) is pub/sub: a publisher sends one message to a topic, and SNS pushes a copy to every subscriber: SQS queues, Lambda functions, HTTPS endpoints, email, SMS, mobile push.
The classic pattern is SNS to SQS fan-out: one OrderPlaced topic, with a separate queue for each consumer (billing, email, analytics). Each consumer gets its own copy, processes at its own pace, and retries independently. Subscription filter policies let a queue receive only matching messages, like {"country": ["GB"]}.
EventBridge: route events by content
Amazon EventBridge is an event bus with content-based routing. Producers put events onto a bus; rules match events by pattern and send them to targets (Lambda, SQS, Step Functions, another bus, an API destination). AWS services also emit events to the default bus ("EC2 instance stopped", "ECS task failed"), and SaaS partners can send events in.
{
"source": ["com.acme.shop"],
"detail-type": ["OrderPlaced"],
"detail": {
"total": [{ "numeric": [">=", 500] }],
"country": ["GB", "IE"]
}
}import json, boto3
events = boto3.client("events")
events.put_events(Entries=[{
"EventBusName": "shop",
"Source": "com.acme.shop",
"DetailType": "OrderPlaced",
"Detail": json.dumps({"orderId": "A17", "total": 640, "country": "GB"}),
}])EventBridge Scheduler is the modern way to run things on a schedule (cron(0 3 * * ? *) or one-off at a time), with time zones and retries, replacing cron boxes.
The difference from SNS: SNS is "send this to everyone subscribed to this topic"; EventBridge is "look at what this event says and route it to whoever cares", with many AWS integrations, archives and replay.
Kinesis: ordered streams
Amazon Kinesis Data Streams is for streams: a high-volume, ordered log of records that many consumers can read and re-read (retention from 24 hours up to a year). Records with the same partition key go to the same shard and stay in order. Think clickstreams, IoT telemetry, app logs feeding analytics. Amazon Data Firehose (formerly Kinesis Data Firehose) loads streaming data into S3, Redshift or OpenSearch with no consumer code. Amazon MSK is the managed Kafka alternative.
A queue message is work that disappears once done. A stream record is history that stays for every reader.
Step Functions: workflows with steps
AWS Step Functions runs state machines: a workflow of steps with branching, retries, timeouts, parallel branches and waits, defined in JSON (Amazon States Language) or drawn in Workflow Studio. Use it when a process has several steps that must happen in order with error handling, like "charge card, then reserve stock, and if reserving fails, refund".
{
"StartAt": "ChargeCard",
"States": {
"ChargeCard": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "charge-card", "Payload.$": "$" },
"Retry": [{ "ErrorEquals": ["States.TaskFailed"], "MaxAttempts": 3, "BackoffRate": 2 }],
"Next": "ReserveStock"
},
"ReserveStock": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "reserve-stock", "Payload.$": "$" },
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "Refund" }],
"End": true
},
"Refund": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "FunctionName": "refund", "Payload.$": "$" }, "End": true }
}
}An order pipeline, end to end
The API saves the order and returns 202 Accepted quickly. That is the only synchronous work.
Choosing between them
| SQS | SNS | EventBridge | Kinesis Data Streams | |
|---|---|---|---|---|
| Model | Queue (pull) | Pub/sub topic (push) | Event bus with rules (push) | Ordered stream (pull, replayable) |
| Consumers per message | One | Every subscriber | Every matching rule | Every reader, independently |
| Ordering | FIFO queues only | FIFO topics only | No | Per shard |
| Keeps messages | Up to 14 days, until deleted | No (delivers and forgets) | Optional archive and replay | 24 hours to 1 year |
| Best for | Buffering work, smoothing load | Simple fan-out, notifications | Routing by content, AWS/SaaS events, schedules | High-volume telemetry, analytics |
Match the need to the service.
deposit, withdraw) must be applied in order for each account, but different accounts can be processed in parallel. What do you use?- visibility timeout
- How long a received SQS message stays hidden. If not deleted in time, it reappears.
- dead-letter queue
- Where messages go after failing
maxReceiveCounttimes, for inspection and redrive. - idempotent consumer
- Processing the same message twice has the same effect as once. Needed for at-least-once delivery.
- fan-out
- One published message delivered to many subscribers, like SNS to several SQS queues.
- event bus
- EventBridge's router: rules match events by content and send them to targets.
- shard
- A Kinesis partition. Records with the same partition key land on one shard, in order.