Coupling

A direct call ties two services’ speed and uptime together

Direct calls: checkout waits for everyoneCheckoutPaymentEmailInventoryAnalyticsemail is slow: every checkout is slowemail is down: every checkout failsThrough a queue: checkout records and moves onCheckoutPaymentone messageEmailInventoryAnalyticsemail is down: its messages wait in the queuecheckout is as fast and as available as beforeA direct call ties two services’ speed and uptime together. A queue between them cuts the tie.With synchronous calls, a request is as slow as its slowest call and as available as the product of all of them (lesson 2).Only the payment needs an answer before the order is confirmed. Everything else can be told and done a moment later.That is loose coupling: each part can fail, slow down, scale or be replaced without the others noticing.

In synchronous calls the slowest dependency sets the pace; a queue lets each part work and fail on its own.

Queues

A queue absorbs a spike and lets workers catch up at their pace

0 min10 min20 min30 min40 minorders arriving: 5,000 a minute during the saleworkers process 2,500 a minute25,000 waiting in the queueempty again at 32 minA queue absorbs a spike and lets the workers catch up at their own pace. Nothing is dropped.For ten minutes orders arrive twice as fast as they can be processed. Without a queue, 25,000 of them would be errors ortimeouts. With one, they wait, and the workers drain the backlog after the sale. The queue is also the scaling signal:scale the workers on messages waiting per worker (lesson 3) and the backlog clears sooner. Users get “order received”.

A queue buffers work: here 25,000 orders wait at the peak, and none are lost.

Standard and FIFO · Going deeper

Standard queues trade order for scale; FIFO keeps order per group

Standard queueFIFO queueOrderbest effort: may arrive out of orderstrict order within a message groupDeliveryat least once: duplicates possibleexactly-once processing (5-minute dedup)Throughputnearly unlimited300 calls/s, 3,000 msg/s batched; more in high-throughput modeUse it forjobs that can run in any orderbank transfers, commands that must not reorderMessage groups: order holds within each group, and different groups run in parallelaccount AA1A2A3account BB1B2Standard queues trade order for scale. FIFO queues keep order, per group, at a lower rate.Most work does not care about order, so the standard queue is the default. When order matters, use a FIFO queue andput each customer, account or device in its own message group: order is kept where it matters and groups still runin parallel. The same choice exists as Azure Service Bus sessions and Google Pub/Sub ordering keys.

A FIFO queue keeps strict order within each message group and removes duplicates within five minutes.

Message lifecycle

A message hides while it is worked on, and returns if the worker fails

0 s30 s60 s90 s120 sreceive 1hidden 30 s; worker crashesreceive 2hidden 30 s; worker crashesreceive 3hidden 30 s; worker crashesdead-letter queueafter 3 receivesThe happy path: receive, process, delete before the timeout.If the worker never deletes it, the message reappears for another try.Defaults: visibility 30 s (up to 12 h), kept 4 days (up to 14), up to 1 MiB a message, long polling waits up to 20 s.A message is hidden while it is worked on, and returns if the worker fails. Poison messages go aside.Receiving does not remove a message: it hides it for the visibility timeout. Only an explicit delete, after the workis done, removes it. A crashed worker therefore loses nothing; the message comes back. A message that fails every timewould loop forever, so after a set number of receives it moves to a dead-letter queue to be inspected and replayed.

Receiving hides a message for the visibility timeout; only a delete removes it. After 3 failed tries it goes to a dead-letter queue.

Queues on three clouds · Going deeper · 1 of 2

SQS, Service Bus and Pub/Sub: one lifecycle, different defaults

Amazon SQSAzure Service Bus queueGoogle Pub/Sub subscriptionLargest message1 MiB256 KB; 100 MB on Premium10 MBKept for4 days by default, 14 at mostits time to live, which you set7 days by default, 31 at mostHidden while in workvisibility timeout: 30 s, up to 12 hpeek-lock: 1 min, up to 5, renewableack deadline: 10 s, up to 600 sRemoved byDeleteMessageCompletean acknowledgementDead-lettered aftermaxReceiveCount receives, if setMaxDeliveryCount: 10 by default5 to 100 attempts, if setDead letters go toa second queue you createa built-in sub-queuea topic you nameStrict orderFIFO queue, per message groupsessionsordering keysDuplicates removedFIFO: 5-minute deduplicationdetection window: 10 min, up to 7 daysexactly-once delivery, pull onlyThe same lifecycle on every cloud: receive, hide, finish or return, set aside after too many tries.All three deliver at least once by default, so the handler must be idempotent whichever you use. The trap is the hidetime: a job that outlasts it is delivered again while the first copy still runs, so extend the timeout or renew the lock.Service Bus dead-letters after 10 tries without being asked; SQS and Pub/Sub retry until the message expires unless you configure it.

A visibility timeout, a peek-lock and an ack deadline are the same idea with defaults of 30 s, 1 minute and 10 s.

Redrive policy · Going deeper · 2 of 2

A dead-letter queue as written, and the four numbers beside it

OrdersQueue: Type: AWS::SQS::Queue Properties: VisibilityTimeout: 180 # seconds MessageRetentionPeriod: 345600 # 4 days ReceiveMessageWaitTimeSeconds: 20 # long polling RedrivePolicy: deadLetterTargetArn: !GetAtt OrdersDlq.Arn maxReceiveCount: 5OrdersDlq: Type: AWS::SQS::Queue Properties: MessageRetentionPeriod: 1209600 # 14 daysVisibilityTimeout 1806 × the 30 s function timeout: the rulefor a Lambda consumer, so a throttledretry does not overlap the first trymaxReceiveCount 5at least 5 with Lambda. A poison messageis set aside after 5 × 180 s = 15 minutesDead letters kept 14 dayslonger than the source: in a standardqueue the clock runs from the firstenqueue, not from the moveWait 20 sthe longest long poll: few empty receivesA dead-letter queue is one property of the source queue. The numbers around it decide whether it works.The dead-letter queue is the same type as its source (FIFO for FIFO), in the same account and Region. Alarm when itholds a single message: something fails every time. Fix the consumer, then redrive the messages back to the source.With Lambda, return the failed items of a batch, or one bad message sends the whole batch round again.

A redrive policy names the dead-letter queue and a maxReceiveCount; here a poison message is set aside after 15 minutes.

Idempotency

Messages can arrive twice; make handling safe to repeat

Delivered twicecharge order 42: $80charge order 42: $80a retry after a timeout, or the queuedelivering at least onceNot idempotentcharge card $80charge card $80 againthe customer pays $160Idempotentif seen("order-42"): skipcharge card $80record "order-42"first copy: charged and recordedsecond copy: already seen, skippedthe customer pays $80Messages may arrive twice. Make handling safe to repeat: an idempotent operation done twice equals done once.Standard queues, topics, retries and failovers all deliver at least once, so duplicates are normal, not rare. Give eachmessage a unique key (the order id), record each key when its work is done, and skip keys already recorded, in the sametransaction as the work where possible. Payment APIs accept an idempotency key for exactly this reason.

Delivery is at least once, so make each handler idempotent: record each message’s key and skip ones already done.

Transactional outbox · Going deeper

Save and publish cannot commit together: write the event as a row

Two writes, no transaction across themorders servicedatabase1 save ordertopic2 publishcrash before step 2Order saved, event never sent: no email, no stock update.Publish first? Then an event exists for an order that rolled back.Outbox: the event is a row in the same transactionBEGIN;INSERT INTO orders VALUES (42, 'ana', 80);INSERT INTO outbox VALUES ('evt-42', 'OrderPlaced', 42);COMMIT;relay: reads unsentrows, marks them sentpublishconsumers skipids already seenBoth rows commit or neither does. If the relay crashes after publishing,it publishes evt-42 again: at least once, and the id makes that harmless.A database write and a publish cannot commit together, so store the event beside the data and send it afterwards.The relay is a poller or, better, the database’s own change stream: DynamoDB Streams, or change data capture readingthe PostgreSQL or MySQL log (Debezium). This is the transactional outbox pattern. It turns “saved but never announced”into “announced at least once”, which the idempotent consumer of the main page already handles.

The transactional outbox writes the event in the same transaction as the data; a relay publishes it afterwards, at least once.

Publish and subscribe

A queue gives each message to one worker; a topic gives every subscriber a copy

A queue: each message to one workerproducerworkers share the load; one does each jobA topic: a copy to every subscriber (fan-out)order placedtopicEmailInventoryAnalyticseach service gets its own copy in its own queueA queue gives each message to one worker. A topic gives a copy to every subscriber.Queues spread work; topics spread news. The standard pattern combines them: publish once to a topic (SNS), and giveeach subscribing service its own queue (SQS), so each can be slow, fail, or retry without affecting the rest. Thepublisher does not even know who listens. Add a subscriber tomorrow and nothing else changes. Filters trim what each receives.

A topic fans out a copy to each subscriber; combine it with a queue per subscriber, the fan-out pattern.

Event buses

An event bus routes each event by its content to whoever cares

shoppaymentsSaaS appEvent busEventBridgesource = "shop" and type = "OrderPlaced"Inventory functiontype = "OrderPlaced" and total > 1000Fraud workflowsource = "payments" and type = "Refunded"Email queueevery eventArchive (replay later)An event bus routes each event by its content, so producers and consumers never need to know each other.Services publish facts (“order placed”) as JSON events. Rules match on any field and send matching events to targets:functions, queues, workflows, other buses. The bus also receives events from AWS services and SaaS products, keeps aschema registry, and can archive and replay events. Azure Event Grid and Google Eventarc fill the same role.

An event bus matches rules against each event’s content and routes it to targets; producers and consumers stay unaware of each other.

Choosing the tool · Going deeper

Queue, topic, bus, stream or workflow

ToolServicesChoose it forQueueSQS · Service Bus · Pub/Subone consumer per message; buffer workTopicSNS · Service Bus topics · Pub/Subfan-out of one message to manyEvent busEventBridge · Event Grid · Eventarcroute by content; many sources and targetsStreamKinesis · Event Hubs · Pub/Sub, Kafkaordered log; replay; many readers, high volumeWorkflowStep Functions · Logic Apps · Workflowssteps with state, retries and waitsChoreography: each service reacts to events; no one is in charge.Orchestration: a workflow calls each step in order and handles failure.Five tools for moving work between services. Pick by who should receive each message, and whether order and replay matter.A stream (Kinesis Data Streams) keeps an ordered log for 24 hours by default, up to 365 days, so many readers can read thesame records at their own pace and replay them; queues delete messages once handled. When a process has many steps thatmust happen in order with retries and compensation (a refund if shipping fails), a workflow such as Step Functions makes it visible.

Queues buffer, topics fan out, buses route, streams keep an ordered log for replay, and workflows orchestrate steps.

Lesson complete

You finished “Queues and events”

Nicely done.

Open in a new tab