Coupling
A direct call ties two services’ speed and uptime together
In synchronous calls the slowest dependency sets the pace; a queue lets each part work and fail on its own.
Consider checkout. It calls payment, then email, then inventory, then analytics, and waits for each. If email is slow, every checkout is slow. If email is down, every checkout fails, though the payment went through. Now only the payment stays a direct call, because the customer needs its answer. Everything else is one message on a queue: checkout records the order and moves on. Email, inventory and analytics read the queue at their own pace. When email is down, its messages simply wait. Checkout does not even notice.
Queues
A queue absorbs a spike and lets workers catch up at their pace
A queue buffers work: here 25,000 orders wait at the peak, and none are lost.
Queues also smooth out bursts. Orders normally arrive at a thousand a minute. During a ten-minute sale, five thousand. The workers can process two and a half thousand a minute. Without a queue, half the orders during the sale would fail. With one, they wait. The queue grows to 25,000, then drains once the sale ends, and is empty by minute 32. Every order is processed, a little later. Scale the workers on the backlog, and it drains sooner.
Standard and FIFO · Going deeper
Standard queues trade order for scale; FIFO keeps order per group
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
Receiving hides a message for the visibility timeout; only a delete removes it. After 3 failed tries it goes to a dead-letter queue.
What happens to one message? A worker receives it. The message is not removed, only hidden, for thirty seconds. If the worker finishes, it deletes the message. Here, it crashes. When the thirty seconds are up, the message reappears. A second worker tries. It crashes too. And a third. This message is poison: it will never succeed, and would loop forever. So after three receives the queue moves it to a dead-letter queue, where a person can look at it, fix the bug, and send it back.
Queues on three clouds · Going deeper · 1 of 2
SQS, Service Bus and Pub/Sub: one lifecycle, different defaults
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
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
Delivery is at least once, so make each handler idempotent: record each message’s key and skip ones already done.
Queues and retries come with a promise: at least once. Sometimes, twice. The same charge message arrives two times, perhaps because a worker timed out just after finishing. A naive worker charges the card again. The customer pays a hundred and sixty dollars for an eighty dollar order. An idempotent worker checks first: has order forty-two been done? The first copy is charged and recorded. The second is recognised and skipped. Doing it twice now has the same effect as doing it once, and duplicates become harmless.
Transactional outbox · Going deeper
Save and publish cannot commit together: write the event as a row
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 topic fans out a copy to each subscriber; combine it with a queue per subscriber, the fan-out pattern.
Two shapes of messaging. A queue gives each message to exactly one worker. Workers share the load. A topic gives a copy of each message to every subscriber. Here one “order placed” message reaches email, inventory and analytics. Each subscriber has its own queue, so each can go at its own speed, fail and retry, without the others knowing. The checkout publishes once and does not know who is listening. A new service tomorrow just subscribes.
Event buses
An event bus routes each event by its content to whoever cares
An event bus matches rules against each event’s content and routes it to targets; producers and consumers stay unaware of each other.
As systems grow, a richer router helps. Events arrive from many sources: the shop, payments, even outside software products. They land on an event bus. Each rule looks inside the event. Orders placed go to the inventory function. Orders over a thousand dollars also go to a fraud check. Refunds go to the email queue. And everything is archived, so it can be replayed later. Nobody changes the shop when a new consumer appears; they add a rule.
Choosing the tool · Going deeper
Queue, topic, bus, stream or workflow
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.