Architectural Patterns, Coupling and State
Overview
Before writing code you choose a shape for the application. A monolith ships as one unit, microservices split it into small independently deployed services, and event-driven designs let components react to events instead of calling each other directly. DVA-C02 expects you to pick the AWS building blocks that fit each shape.
Picture a restaurant. In a tightly coupled kitchen the waiter stands next to the chef until the dish is ready (synchronous). In a loosely coupled one the waiter drops a ticket on a rail (a queue) and carries on, and several stations (fanout) pick up the parts they need. Choreography means every station knows its own job and reacts to tickets; orchestration means a head chef (Step Functions) tells each station what to do and in what order.
The recurring exam theme is: stateless compute, state in a managed store, asynchronous messaging between components, and retries that are safe because operations are idempotent.
Key Concepts
- Monolith is simple to start but scales and deploys as a whole; microservices scale and deploy independently at the cost of distributed-system complexity.
- Event-driven architecture uses events (EventBridge, SNS, Kinesis, DynamoDB Streams) so producers do not know their consumers.
- Choreography = services react to events with no central coordinator; orchestration = a coordinator such as Step Functions controls the sequence.
- Stateless services keep no session data locally so any instance can serve any request; state goes to DynamoDB, ElastiCache or S3.
- Tight coupling shares failure and release cycles; loose coupling uses queues, topics and APIs with versioned contracts.
- Synchronous calls (API Gateway to Lambda) give an immediate answer; asynchronous calls (SQS, SNS, async Lambda invoke) decouple timing and absorb spikes.
- Fanout publishes one message to many consumers, typically SNS to several SQS queues.
- Idempotency lets a retried message produce the same result as the first delivery.
Detailed Notes
Pattern comparison
| Pattern | Typical AWS services | Strength | Trade-off |
|---|---|---|---|
| Monolith | Elastic Beanstalk, EC2, ECS | Simple deployment and debugging | Scales as one unit |
| Microservices | ECS/EKS, Lambda, API Gateway | Independent scaling and releases | Operational and data-consistency complexity |
| Event-driven | EventBridge, SNS, SQS, Kinesis | Loose coupling, easy extension | Harder to trace end-to-end |
| Orchestration | Step Functions | Visible state, retries, error handling | Central dependency on the coordinator |
| Choreography | EventBridge rules, SNS fanout | No single point of control | Flow is implicit across services |
Stateful versus stateless
A stateful web server that keeps the shopping cart in memory forces sticky sessions and loses data when the instance dies. Make it stateless: keep the cart in DynamoDB or ElastiCache, put a signed token (JWT) in the cookie, and store uploads in S3. Now an Auto Scaling group or Lambda can add and remove capacity freely. Some state is unavoidable (database engines, Kafka brokers); keep it in managed services built for it.
Synchronous versus asynchronous
Use synchronous calls when the client needs the result now and the work is short. Use asynchronous when work is slow, spiky or can be retried later.
import json, boto3
sqs = boto3.client("sqs")
def handler(event, context):
# API Gateway returns 202 immediately; a worker processes the queue
sqs.send_message(QueueUrl=QUEUE_URL, MessageBody=json.dumps(json.loads(event["body"])))
return {"statusCode": 202, "body": json.dumps({"status": "queued"})}
Fanout with SNS and SQS
OrderTopic:
Type: AWS::SNS::Topic
BillingQueue:
Type: AWS::SQS::Queue
BillingSub:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref OrderTopic
Protocol: sqs
Endpoint: !GetAtt BillingQueue.Arn
RawMessageDelivery: true
Each subscriber gets its own durable queue, so a slow shipping service never blocks billing. Add the queue policy that lets the topic send messages.
Choreography versus orchestration
Choose choreography when services are owned by different teams and the flow is simple. Choose orchestration when you need ordering, branching, compensation (saga rollback), human approval or a clear audit trail. A Step Functions state machine records every transition, which makes debugging far easier than reconstructing a chain of events from logs.
Idempotency
At-least-once delivery (SQS standard, SNS, EventBridge, Lambda async) means duplicates happen. Make handlers idempotent: use a conditional write with a request ID, natural keys (upsert) or the Lambda Powertools idempotency utility.
table.put_item(
Item={"pk": f"ORDER#{order_id}", "status": "PAID"},
ConditionExpression="attribute_not_exists(pk)",
) # a second delivery fails the condition and is safely ignored
Contracts and versioning
Loose coupling only works when message schemas are managed. Add fields without removing old ones, version event types (order.created.v2), and register schemas in the EventBridge Schema Registry so producers and consumers can generate typed bindings.
Fault-tolerant code
Design for failure: set timeouts, bound retries, use dead-letter queues, validate input at the edge and fail fast on non-retryable errors. Keep Lambda functions small and single-purpose, and avoid long synchronous chains because latency and failure probability multiply.
Real World Example
An e-commerce site had a monolith where checkout called payment, inventory and email in one request; a slow email provider made checkout time out. The team published an OrderPlaced event to EventBridge and let each capability subscribe through its own SQS queue. Checkout now returns in under 200 ms and a failure in email only grows that queue. Fulfilment uses a Step Functions workflow because it needs ordered steps and compensation if the carrier booking fails.
Architecture Diagram
A producer publishes once; independent consumers process at their own pace.
- 1The client calls the API and receives an immediate 202.
- 2The order event is published to the event bus.
- 3Rules route copies to each consumer queue.
- 4Workers process messages idempotently and update the data store.
Best Practices
- ✓Keep compute stateless and store session data in DynamoDB, ElastiCache or tokens.
- ✓Prefer asynchronous messaging between services and use queues to absorb bursts.
- ✓Make every consumer idempotent because delivery is at-least-once.
- ✓Use Step Functions for multi-step flows that need retries, branching or compensation.
- ✓Version event schemas and register them so contracts stay compatible.
Common Interview Questions
Exam Tips
DVA-C02 focus
- 'Decouple' and 'absorb traffic spikes' point to SQS; 'one event to many consumers' points to SNS or EventBridge.
- Ordered, multi-step workflow with retries and visual tracking means Step Functions.
- Sticky sessions are a smell; externalise state to ElastiCache or DynamoDB.
- At-least-once delivery requires idempotent processing; the exam rewards conditional writes.
Finished this lesson?
0 of 46 lessons done · 0% of the course

