Loose Coupling and Scalability
Design scalable, loosely coupled architectures: queues, topics, and events with SQS, SNS, and EventBridge; workflows with Step Functions; Auto Scaling and load balancers; serverless and container compute; and caching.
Why Coupling Breaks Systems
Two components are tightly coupled when one must be up, fast, and correctly sized for the other to work. If the web tier calls the image processor directly, a slow processor makes the website slow, and a crashed processor makes uploads fail.
Loose coupling puts a buffer between them. The producer hands off work and moves on. The consumer works at its own pace and scales on its own. A failure on one side no longer cascades to the other.
The Messaging Toolkit
| Service | Pattern | Key facts |
|---|---|---|
| Amazon SQS | Queue: one message is processed by one consumer | Standard (very high throughput, at-least-once, best-effort order) or FIFO (strict order, exactly-once processing, lower throughput). Visibility timeout, dead-letter queues, long polling, retention up to 14 days |
| Amazon SNS | Pub/sub topic: one message is pushed to many subscribers | Push to SQS, Lambda, HTTP, email, SMS. Combine with SQS for fan-out |
| Amazon EventBridge | Event bus with content-based routing rules | Routes AWS service events, SaaS events, and your own events to targets; schedules; archive and replay |
| AWS Step Functions | Workflow orchestration as a state machine | Retries, branching, parallel steps, waits, human approval; Standard (long-running) and Express (high-volume, short) |
| Amazon Kinesis Data Streams | Ordered, replayable stream with multiple readers | Real-time analytics, clickstreams; covered in Module 4 |
| Amazon MQ | Managed ActiveMQ or RabbitMQ | Migrating apps that already use JMS, AMQP, or MQTT with minimal code changes |
Choosing among them
- “Decouple and buffer work between tiers” → SQS
- “Messages must be processed in order, exactly once” → SQS FIFO
- “Send the same event to several systems” → SNS + SQS fan-out, or EventBridge if routing depends on event content
- “React to an AWS event, like an EC2 state change or an S3 upload” → EventBridge (or S3 event notifications)
- “Coordinate a multi-step process with retries and error handling” → Step Functions
Common mistake: processing a message takes 3 minutes, but the queue’s visibility timeout is the 30-second default.
After 30 seconds the message reappears and another worker picks it up, so the job runs twice (or many times). Set the visibility timeout longer than your longest processing time, make consumers idempotent, and add a dead-letter queue so a poison message doesn’t loop forever.
The life of a single SQS message:
Scaling the Compute
Auto Scaling and load balancing
An Auto Scaling group (ASG) keeps the right number of EC2 instances running across multiple AZs, replaces unhealthy ones, and scales on demand.
- Target tracking: “keep average CPU at 50%” or “keep 100 messages per instance in the queue.” Usually the best default.
- Step scaling: add or remove instances in steps as an alarm crosses thresholds.
- Scheduled scaling: scale up before a known event, such as 8 a.m. on weekdays.
- Predictive scaling: uses history to provision capacity ahead of a recurring pattern.
A load balancer spreads traffic across healthy targets: ALB for HTTP/HTTPS with path and host routing, NLB for TCP/UDP with very high performance and static IPs, and Gateway Load Balancer for inserting third-party network appliances.
Serverless and containers
- AWS Lambda runs functions in response to events and scales automatically. Max 15 minutes per invocation. Great for event-driven and spiky workloads.
- Amazon API Gateway is the managed front door for REST, HTTP, and WebSocket APIs, with throttling, auth, and caching.
- Amazon ECS and Amazon EKS run containers; AWS Fargate runs them without managing servers.
Design compute to be stateless: keep sessions in ElastiCache or DynamoDB, and files in S3 or EFS, so any instance can serve any request and can be terminated safely.
Caching
Caching absorbs load and lowers latency:
- CloudFront at the edge for static and dynamic content
- ElastiCache (Redis OSS, Valkey, or Memcached) in front of a database
- DynamoDB Accelerator (DAX) for microsecond reads from DynamoDB
- API Gateway caching for repeated API responses
Key Takeaway
Put a queue, topic, or event bus between components so each side can fail and scale on its own. Use SQS to distribute work, SNS + SQS to fan out, EventBridge to route events by content, and Step Functions to orchestrate. Keep compute stateless, scale it with ASGs, Lambda, or Fargate, and add caches to absorb load.
In a sandbox account:
- Create an SNS topic called
orders. - Create two SQS queues,
billingandshipping, and subscribe both to the topic. - Publish a message to the topic from the console.
- Poll each queue. Both received a copy.
- Add a subscription filter policy to
shippingso it only receives messages with the attributetype = physical. Publish one physical and one digital order and confirm what each queue gets.
Bonus: connect billing to a Lambda function and watch it process messages automatically. Delete everything when you’re done.
1. An order system must process messages in the exact order they are sent, with no duplicates. Which service fits?
SQS FIFO queues preserve order and provide exactly-once processing.
2. Each new order must be delivered to the billing, shipping, and analytics systems, which process it independently. What is the BEST design?
SNS fan-out delivers a copy to each queue, so each consumer processes independently and failures stay isolated.
3. Workers sometimes process the same SQS message twice. Processing takes about 2 minutes. What is the MOST likely cause?
If the visibility timeout expires before a worker deletes the message, it becomes visible again and another worker picks it up.
4. Which approach makes a web tier easiest to scale horizontally?
Stateless instances can be added or removed freely because no request depends on a particular instance.