Sub topic 1
Why asynchronous messaging: decoupling producers and consumers
Prerequisites
This chapter assumes you know basic AWS concepts (accounts, regions, IAM users and roles), can read JSON, and can follow short Python examples that use boto3. You also need a general idea of what a distributed application is. You do not need any SQS or SNS experience.
The problem with direct calls
In a synchronous design, a producer calls a consumer and waits for the answer. A checkout service might call the payment, inventory, email, and analytics services one after another. This creates three problems:
- Shared failure. If the email service is down, checkout fails or hangs, even though the customer's order could have been accepted.
- Shared speed. The total response time is the sum of every downstream call.
- Shared load. A traffic spike hits every downstream service at the same moment, at the rate the producer sends.
What asynchronous messaging changes
With asynchronous messaging, the producer hands a message to an intermediary and continues. A message is a small piece of data, such as a JSON document, that describes something that happened or something to do. The consumer reads it later, at its own pace.
The producer no longer needs to know who the consumer is, whether it is running, or how fast it works. This separation is called decoupling. It has three practical effects:
- Failure isolation. Messages wait in the intermediary while a consumer is down.
- Load leveling. The intermediary absorbs a spike, and consumers drain it at a steady rate.
- Independent scaling. You can add consumers without changing the producer.
Two AWS services, two roles
AWS provides two building blocks for this, and the rest of the tutorial covers both.
| Service | Model | Who receives a message |
|---|---|---|
| Amazon SQS | Queue | One consumer processes each message. Consumers pull messages. |
| Amazon SNS | Topic (publish/subscribe) | Every subscriber gets a copy. SNS pushes messages. |
SQS stores messages until a consumer asks for them. SNS does not store messages for later reading. It delivers each published message to all current subscribers right away. Combined, they let one event reach many independent queues, a pattern called fan-out that appears later in the tutorial.
A first look in code
The producer below sends a message and does not wait for any consumer. It assumes a queue named orders already exists, which a later chapter shows how to create.
import json
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="orders")["QueueUrl"]
response = sqs.send_message(
QueueUrl=queue_url,
MessageBody=json.dumps({"orderId": "1001", "total": 25}),
)
print("Accepted message", response["MessageId"])The call returns once SQS has stored the message. Whether anything has processed it yet is not the producer's concern.
Trade-offs to accept
Decoupling is not free.
- Eventual results. The producer cannot return the consumer's result in the same call. If a user needs an immediate answer, a synchronous call may fit better.
- Duplicates and ordering. Delivery can repeat, and order may not be preserved. Later chapters cover idempotent consumers and FIFO queues.
- More to observe. A backlog or a failing consumer is invisible to the producer, so you must monitor queue depth and message age.
Exercise
A checkout service calls an email service directly, and checkouts fail whenever the email service is down. Name two benefits of placing an SQS queue between them.
Show answer
Failure isolation: checkout succeeds because it only needs the queue to accept the message, and emails wait in the queue until the email service recovers. Load leveling: during a traffic spike the queue absorbs the messages and the email service processes them at its own steady rate. Independent scaling is also an acceptable answer.