Loose Coupling and Scalability
Module 3 · Design Resilient Architectures

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.

Tightly coupledWeb tierWorkersync callWorker slow or down → web requests failQueue (work distribution)Web tierSQSWorkersASG on queue depthDLQFailed messages land in a dead-letter queueFan-out (one event, many consumers)OrderSNSBillingShippingAnalyticsEach consumer gets its own SQS queue

The Messaging Toolkit

ServicePatternKey facts
Amazon SQSQueue: one message is processed by one consumerStandard (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 SNSPub/sub topic: one message is pushed to many subscribersPush to SQS, Lambda, HTTP, email, SMS. Combine with SQS for fan-out
Amazon EventBridgeEvent bus with content-based routing rulesRoutes AWS service events, SaaS events, and your own events to targets; schedules; archive and replay
AWS Step FunctionsWorkflow orchestration as a state machineRetries, branching, parallel steps, waits, human approval; Standard (long-running) and Express (high-volume, short)
Amazon Kinesis Data StreamsOrdered, replayable stream with multiple readersReal-time analytics, clickstreams; covered in Module 4
Amazon MQManaged ActiveMQ or RabbitMQMigrating 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 Pitfall: Visibility Timeout Shorter Than the Job

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:

ProducerVisiblewaiting in the queueIn flighthidden until the timeoutDeletedafter DeleteMessageDead-letter queueafter maxReceiveCount triesSendReceiveSuccessTimeout or failure: visible again,receive count + 1Too many tries

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.

🧪 Try It: Build a Fan-Out in 15 Minutes

In a sandbox account:

  1. Create an SNS topic called orders.
  2. Create two SQS queues, billing and shipping, and subscribe both to the topic.
  3. Publish a message to the topic from the console.
  4. Poll each queue. Both received a copy.
  5. Add a subscription filter policy to shipping so it only receives messages with the attribute type = 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.

🧠 Knowledge Check

1. An order system must process messages in the exact order they are sent, with no duplicates. Which service fits?

2. Each new order must be delivered to the billing, shipping, and analytics systems, which process it independently. What is the BEST design?

3. Workers sometimes process the same SQS message twice. Processing takes about 2 minutes. What is the MOST likely cause?

4. Which approach makes a web tier easiest to scale horizontally?