Lesson 4 of 8 High Availability and Resilient Design Patterns

Decoupling Components with Queues and Events

This lesson explains how loose coupling through queues and events stops failures cascading between tightly coupled components and lets each component scale independently.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish the decoupling services.
  • Explain how a queue changes behaviour under load and failure.
  • Identify the settings that make queues reliable.
  • Explain design rules such as idempotent consumers.

Decoupling services

Four kinds of service decouple components, each in a different way. Amazon SQS is a queue between producers and consumers. Consumers work at their own pace, and bursts are absorbed. Standard queues offer nearly unlimited throughput with at-least-once delivery and best-effort ordering. FIFO queues process each message exactly once, in order within each message group. Amazon SNS fans one message out to many subscribers, such as SQS queues, Lambda functions, HTTP endpoints and email, and filter policies send each subscriber only the messages it wants; FIFO topics preserve order.

Amazon EventBridge routes events by matching their content against patterns, across services and accounts. It can archive and replay events, and it includes a schema registry, Pipes for point-to-point integrations and Scheduler. Kinesis and MSK provide ordered, replayable, high-throughput streams that many consumers can read independently. The following table summarises the services.

ServiceRole
Amazon SQSDecouples producers from consumers and absorbs bursts. Standard: nearly unlimited throughput, at-least-once, best-effort order. FIFO: exactly-once processing, ordered per message group
Amazon SNSFans out to many subscribers (SQS, Lambda, HTTP, email) with filter policies; FIFO topics preserve order
Amazon EventBridgeRoutes events by pattern across services and accounts; archive and replay, schema registry, Pipes, Scheduler
Kinesis, MSKOrdered, replayable, high-throughput streams

What a queue changes

A queue between two components changes how the system behaves under load and failure. It provides load levelling: producers can write at any rate, while consumers process at the rate the back end can sustain, so a burst becomes a backlog rather than an overload. It provides failure isolation: if consumers fail, messages wait in the queue instead of being lost or making the producer fail. It allows independent scaling: consumers scale on queue depth, for example with target tracking on backlog per instance.

The cost is latency and eventual consistency. The producer no longer knows when the work is done, so the user experience must allow for it, for example by confirming that an order was received rather than that it was fulfilled, and by notifying the user later.

The following diagram shows a burst absorbed by a queue.

Load levelling with a queue. A burst of requests arrives at up to 14 per second, but consumers keep processing at the 6 per second the back end can sustain. The excess becomes a backlog in the queue, which peaks at 25 messages and drains once arrivals fall below the processing rate, so the back end is never overloaded.
The rates are illustrative. The cost of load levelling is latency: messages that arrive during the burst wait in the backlog.
Select all that apply

What a queue adds

Exam skills 1.1.1 · 4.3.3

Identify what placing a queue between two services changes.

Log in to try this activity Interactive activities are free with an account.

Operating queues reliably

A few settings decide whether a queue behaves reliably. The visibility timeout must be longer than the processing time, or a message becomes visible again while it is still being processed and is processed twice. A dead-letter queue, with a sensible maxReceiveCount, moves poison messages aside after repeated failures so they do not block processing, and an alarm on its depth tells the team that something needs attention.

Long polling lets consumers wait for messages rather than repeatedly receiving empty responses, which reduces cost. An alarm on ApproximateAgeOfOldestMessage detects a growing backlog, for example because consumers have stopped, which queue depth alone may not reveal quickly.

Design rules

Asynchronous designs follow a few rules. Consumers must be idempotent, because most messaging delivers at least once, so processing the same message twice must have the same effect as processing it once, for example by recording processed message IDs. Ordering is expensive, so the design decides where it really matters and uses FIFO queues or partitioned streams only there, keyed by the entity whose events must stay in order.

Publishing events that describe what happened, such as order placed, is preferred to sending commands that tell another service what to do, because new consumers can subscribe to an event without any change to the producer, which keeps services loosely coupled as the system grows.

Worked example

Putting a queue between the web tier and a fragile back end

An order system writes synchronously from its web tier to a legacy fulfilment service that can process 50 orders a second. During promotions, traffic reaches 400 orders a second, the fulfilment service times out, and customers see errors. The architect inserts an SQS queue: the web tier sends the order and returns immediately, and workers consume at a controlled rate. The queue is configured so that slow or failing messages are handled safely:

aws sqs create-queue --queue-name orders-dlq --attributes MessageRetentionPeriod=1209600
aws sqs create-queue --queue-name orders --attributes '{
  "VisibilityTimeout": "120",
  "ReceiveMessageWaitTimeSeconds": "20",
  "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:eu-west-1:111111111111:orders-dlq\",\"maxReceiveCount\":\"5\"}"
}'

The visibility timeout of 120 seconds exceeds the worst-case processing time, so a message is not delivered twice while it is still being processed. Long polling reduces empty receives. After five failed attempts a message moves to the dead-letter queue, where an alarm alerts the team and the message can be redriven after a fix. Workers are idempotent, keyed on the order ID, because standard queues deliver at least once. During promotions, the backlog grows and drains over the following minutes instead of producing errors.

Check yourself

What does a dead-letter queue protect against?

Show answer

Poison messages that fail repeatedly. After the maximum number of receives they are moved aside, so they stop blocking processing and can be investigated.

Exam Tips

Remember for the exam

  • To absorb traffic spikes in front of a database, use an SQS queue.
  • To fan one event out to many consumers, use SNS with filter policies, or EventBridge.
  • For event routing across accounts with filtering, use EventBridge cross-account event buses.
  • For ordered processing per entity, use SQS FIFO message groups or Kinesis partition keys.
  • Because messages are delivered at least once, make consumers idempotent.
  • Set the SQS visibility timeout longer than processing time and add a dead-letter queue.