Decoupling Components with Queues and Asynchronous Communication
This lesson explains three pairs of ideas that run through the whole exam: tight and loose coupling, stateful and stateless components, and synchronous and asynchronous communication. Queues show all three clearly.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish tightly and loosely coupled components and explain how a queue decouples them.
- Explain why stateless components scale more easily and where state should be kept.
- Choose between synchronous and asynchronous communication for a given requirement.
Tight vs loose coupling
Tightly coupled components call each other directly and fail together: if the order service calls the invoice service synchronously and the invoice service is slow or down, orders fail too, and both must scale in step. Loosely coupled components communicate through an intermediary, so each can fail, scale and change independently. The sender only needs to reach the intermediary, which keeps the message until the receiver can process it.
Each kind of intermediary suits a different communication pattern. A queue holds work for a pool of consumers that share it, and each message is processed by one consumer; it absorbs spikes and keeps work safe while consumers are unavailable. A topic pushes a copy of each message to every subscriber, so several systems can react to the same event. An event bus routes events to targets by matching their content, so new consumers can be added with a rule and without changing the producer. A stream keeps an ordered log of records that several consumers read at their own pace and can replay, which suits high-volume data such as clickstreams. The following table summarises the choice.
| Intermediary | Use for |
|---|---|
| Queue (SQS) | A pool of consumers sharing work; absorbing spikes; keeping work safe while consumers are down |
| Topic (SNS) | Pushing one message to many subscribers |
| Event bus (EventBridge) | Routing events by their content across many services |
| Stream (Kinesis) | Ordered, replayable, high-volume data |
Stateful vs stateless
Stateless components keep no client data between requests, so any instance can serve any request and scaling is simple. State that must persist, such as sessions or shopping carts, belongs in an external store like ElastiCache or DynamoDB, not in instance memory or Lambda's /tmp.
Synchronous vs asynchronous
In synchronous communication, the caller sends a request and waits for the result before continuing, as with a direct API call or a synchronous Lambda invocation. It is simple to reason about, because success or failure is known immediately, but the caller is only as fast and as available as everything it calls. The caller must set timeouts so that a slow dependency does not hold its threads, connections or Lambda concurrency.
In asynchronous communication, the caller hands the work to an intermediary, such as an SQS queue or an asynchronous Lambda invocation, and continues at once. The work is done later, possibly after retries, which makes the system more resilient to slow or failed components and smooths bursts of load. In return, the receiver must cope with retries and duplicate messages, and the caller does not receive the result directly. The following table summarises the differences.
When a caller needs the result of asynchronous work later, the following techniques apply.
- Correlation ID: the request is given an ID that the caller keeps and that the worker records with the result.
- Polling: the caller checks a status endpoint, backed by a store such as DynamoDB, using that ID.
- Push: the result is sent to the caller through API Gateway WebSockets or AppSync subscriptions.
Surviving a slow downstream service
An order API calls an invoicing service synchronously. When invoicing slows down at month end, order requests time out and customers cannot buy.
The API now returns as soon as the message is queued. Because state such as the order lives in DynamoDB rather than in the API's memory, any instance can handle any request, which keeps the service stateless and easy to scale.
Why is a stateless service easier to scale than one that keeps session data in memory?
Show answer
Any instance can handle any request, so instances can be added, removed or replaced freely. Session data kept in an external store such as DynamoDB or ElastiCache is not lost when an instance goes away.
Exam Tips
Remember for the exam
- To decouple components or absorb traffic spikes, put an SQS queue between them.
- If sessions are lost when instances are replaced, use an external session store rather than sticky sessions.
- For long-running work behind an API, accept the request, queue the work and return immediately.
- To return the results of asynchronous work, use a correlation ID with a status store or a push channel.
- Content-based routing of events to many services points to EventBridge.