CQRS and Event Sourcing Patterns with Lambda and DynamoDB
A cricket scorer writes every ball in a notebook (the event log). The scoreboard is a projection you can rebuild by replaying balls. CQRS splits write commands from read models. Event sourcing stores facts as events, not only the latest row. On AWS this often means DynamoDB for writes plus Streams to Lambda that update read tables or OpenSearch.
What you'll learn in this chapter
- Explain CQRS with a scoreboard story
- Separate command side from query side
- Describe event sourcing and when to skip it
- Wire DynamoDB Streams to projection Lambdas
- Link CDC and ecommerce project chapters
1. Everyday example: match notebook vs scoreboard
Kenji in Osaka scores a local match. The notebook is append-only: ball 1, ball 2. The public board shows 142/3. If the board glitches, he rebuilds from the notebook. Luis in Mexico City (MXN POS) can model orders the same way: OrderPlaced events append; a read table serves the app list screen fast.
Hands-on pieces: CDC Streams project, ecommerce cart project, depth in DynamoDB Streams chapter. This chapter names the pattern and the traps.
2. Why CQRS?
Why do we need this?
One fat table serving writes and every dashboard query forces ugly scans and contention. Spiky reads (festival tickets) should not block writes. CQRS lets you optimize each side.
What is it?
CQRS (Command Query Responsibility Segregation) = commands change state; queries read models shaped for screens. Event sourcing = store the stream of domain events as source of truth; derive state by folding events.
3. Vertical diagram
4. CQRS without full event sourcing
Many teams do CQRS light: write to orders table; stream updates a per-user inbox table. You still have a current row as source of truth. Full event sourcing keeps every OrderItemAdded forever — powerful and heavier.
| Approach | Keep when | Avoid when |
|---|---|---|
| CQRS light | Read shapes differ; rebuild rare | Team cannot own two models |
| Full event sourcing | Audit/rebuild/time-travel needed | Simple CRUD blog |
5. How: projection checklist
How to do it (step by step)
- Name commands: PlaceOrder, CancelOrder — not UpdateRow.
- Define events: OrderPlaced{orderId,amountINR,...}.
- Write model enforces invariants (stock, totals).
- Projection is idempotent on eventId / sequence.
- Queries never call command Lambda.
- Plan rebuild: replay stream or snapshot + replay.
6. Per-concept commands
Describe stream on a table
aws dynamodb describe-table --region ap-south-1 --table-name orders-write \
--query 'Table.StreamSpecification'
Meaning: Confirms NEW_IMAGE or NEW_AND_OLD_IMAGES for projections.
List event source mappings
aws lambda list-event-source-mappings --region ap-south-1 \
--query 'EventSourceMappings[?contains(EventSourceArn, `dynamodb`)].{F:FunctionArn,S:State,U:UUID}'
Meaning: Shows which Lambdas consume which streams and if they are enabled.
Sample CloudWatch log filter for projection errors
aws logs filter-log-events --region ap-south-1 \
--log-group-name /aws/lambda/orders-projector \
--filter-pattern 'ERROR' --limit 20
Meaning: Projection failures cause stale read models — watch ERROR early.
Count items in a read table (careful)
aws dynamodb scan --region ap-south-1 --table-name orders-inbox \
--select COUNT --max-items 1
Meaning: Rough health check in labs only; prefer metrics in prod.
7. Industries
- Fintech (Aisha, Lagos, NGN): ledger append events; balance projection per wallet.
- Ecommerce (Rohan, Pune, INR): OrderPlaced; seller dashboard read model.
- EdTech (Diego, São Paulo, BRL): LessonCompleted events; streak board projection.
- Media (Priya, Bengaluru, INR): VideoUploaded; moderation queue read model.
- SaaS (Sofia, Sofia, EUR): SeatAssigned; per-tenant admin list.
8. Lab: sketch models then inspect a stream
9. Lab - CQRS sketch + stream check
Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.
How to do it (step by step)
- On paper: one command, two events, one read model for a library loan (Omar, Amman, JOD).
- Enable or inspect StreamSpecification on a lab table in ap-south-1.
- Note iterator age metric name you would alarm on.
- Write how you would rebuild the read model after a bug.
Cleanup (stop billing)
- Disable lab event source mappings you created.
- Delete lab tables if empty and tagged.
10. Same pattern across industries
- Fintech (Aisha, Lagos): payment webhooks → SQS → Lambda; never block the bank callback thread.
- Ecommerce (Rohan, Pune): checkout API on API Gateway+Lambda; inventory fan-out on SNS.
- Healthcare (Mei, Singapore): appointment SMS via SNS; PHI stays in DynamoDB with fine-grained IAM.
- EdTech (Diego, São Paulo): nightly cleanup EventBridge → Lambda on student drafts.
- Logistics (Fatima, Casablanca): package scan events on EventBridge bus; Step Functions for customs saga.
- Media (Kenji, Osaka): S3 upload → Lambda thumbnails; fan-out to moderation + CDN invalidate.
- SaaS multi-tenant (Priya, Bengaluru): Cognito groups + DynamoDB partition per tenant.
- Gov portal (Omar, Amman): strangler fig — peel one PDF form route per sprint to Lambda.
11. Troubleshooting checklist
- Confirm region (ap-south-1 vs us-east-1 for CloudFront/ACM quirks).
- Check CloudWatch Logs for the function name and request id.
- Verify IAM execution role allows the API you call.
- For async: check DLQ depth and SQS age of oldest message.
- For APIs: curl -v and compare status, CORS headers, body.
- For workflows: open Step Functions execution event history.
- Re-read the linked deep chapter before reinventing a service.
- If cost spikes: check concurrency, retries, and provisioned capacity.
12. Common errors and fixes
Each row is a real mistake students and juniors hit. Read the fix before you panic.
| Error / symptom | Fix |
|---|---|
| Read model hours behind | Check iterator age, Lambda concurrency, batch size, DLQ. |
| Projection double-applies event | Idempotency on eventId; conditional put. |
| Infinite loop write→stream→write | Never write back to same watched keys without guard. |
| Query still scans write table | Route API to read table; fix IAM. |
| NEW_IMAGE missing old stock | Use NEW_AND_OLD_IMAGES when diff needed. |
| OpenSearch mapping explode | Version projections; recreate index carefully. |
| Team debates every field as event | Event storm — start CQRS light. |
| PII in every event forever | Minimize payload; encrypt; retention policy. |
| Currency missing on OrderPlaced | Include currency code (INR/USD/EUR) in event. |
13. FAQ
Q. Must CQRS use event sourcing?
No. CQRS light is common and enough.
Q. Is DynamoDB Streams event sourcing?
It is a change feed; full ES is a design choice on top.
Q. How do UIs stay consistent?
Read-your-writes via short sync or wait on projection.
Q. Link to CDC project?
dynamodb-project-cdc-streams-lambda-opensearch.
Q. When to avoid?
Simple CRUD with one screen shape.
14. Interview Q&A (Beginner / Mid / Senior + MNC)
Q. Beginner: CQRS?
Split commands that write from queries that read.
Q. Beginner: projection?
Read model updated from events/stream.
Q. Mid: idempotent projector?
Same event twice → same read state.
Q. Mid: when full ES?
Audit, rebuild, temporal queries justify cost.
Q. Senior: poison event?
DLQ, fix, replay with version; do not block whole shard forever.
Q. MNC: compliance?
Immutable event store + access logs + retention.
15. Tips in English, Marathi and Hindi
Ravindra Bagale's Tip (EN)
Notebook of balls rebuilds any scoreboard. Events rebuild read models.
Ravindra Bagale's Tip (MR)
चेंडू वही → scoreboard. Events → read models.
Tip (HI)
बॉल की कॉपी से स्कोअरबोर्ड: events से read models।
Try at home
- Name 3 events for a food order.
- Draw write vs read tables.
In short
- Start from a real story, then name the AWS services.
- Link existing chapters — do not copy their labs.
- Design for failure: retries, DLQ, idempotency, rollback.
- Measure before you buy provisioned concurrency or multi-region.
16. Architecture decision checklist
- Write the user journey in five bullets with no AWS names.
- Mark each step sync (user waits) or async.
- Name failure mode: retry, DLQ, compensate, or alert-only.
- Name the idempotency key for every write.
- Pick data store from access patterns (link DynamoDB modeling).
- Estimate monthly cost at 1x and 10x traffic (USD and INR).
- Confirm account concurrency and service quotas in ap-south-1.
- Decide auth: Cognito, IAM, Lambda authorizer, or none.
- Name on-call and which CloudWatch alarm pages them.
- Link the deep chapter you will not rewrite.
- Only then open console or IaC.
17. Copy-paste quick commands (read-only first)
# Region and identity
aws configure get region
aws sts get-caller-identity
# Lambda landscape
aws lambda list-functions --region ap-south-1 --max-items 20 \
--query 'Functions[].{N:FunctionName,R:Runtime,T:Timeout,M:MemorySize}' --output table
aws lambda get-account-settings --region ap-south-1
# API Gateway (HTTP APIs)
aws apigatewayv2 get-apis --region ap-south-1 --query 'Items[].{N:Name,Id:ApiId,Ep:ProtocolType}' --output table
# Queues and topics
aws sqs list-queues --region ap-south-1
aws sns list-topics --region ap-south-1 --query 'Topics[].TopicArn'
# Event buses and state machines
aws events list-event-buses --region ap-south-1
aws stepfunctions list-state-machines --region ap-south-1 --query 'stateMachines[].name'
# DynamoDB tables (names only)
aws dynamodb list-tables --region ap-south-1
Run these in a sandbox account. Prefer read-only IAM while learning.
18. More industry and stack stories
- Fintech (Aisha, Lagos, NGN): bank webhooks must return 200 fast — SQS absorbs spikes; ledger Lambda is idempotent on paymentId.
- Ecommerce (Rohan, Pune, INR): Black Friday — API Gateway throttle + SQS order intake; SNS fans inventory, email, analytics.
- Healthcare (Mei, Singapore, SGD): appointment reminders via SNS SMS; PHI only in DynamoDB with condition keys.
- EdTech (Diego, São Paulo, BRL): EventBridge nightly cleanup of draft quizzes; Lambda timeout 30s, not 15 min.
- Logistics (Fatima, Casablanca, MAD): package scans on custom EventBridge bus; Step Functions customs saga.
- Media (Kenji, Osaka, JPY): S3 → Lambda thumbnails; never write thumbs to the watched prefix.
- SaaS (Priya, Bengaluru, INR): Cognito groups + DynamoDB PK tenantId; one Lambda authorizer for all routes.
- Gov (Omar, Amman, JOD): strangler — peel one PDF form route per sprint from the monolith.
- Startup (Sofia, Sofia, EUR): start with Function URL for demos; move to HTTP API when partners need JWT.
- Retail (Luis, Mexico City, MXN): store POS sync via SQS FIFO when order matters; Standard when it does not.
- Python: handlers thin; put domain logic in plain modules for unit tests without Lambda.
- Node: prefer async/await; never block the event loop on sync DNS.
- Java: SnapStart for cold starts; keep dependencies lean.
- .NET: AspNetCoreServer for low-traffic tenants; watch package size.
- Go: single static binary; great for SQS workers.
- PHP: keep Laravel queues on ECS; Lambda for S3/webhooks glue.
19. Glossary (short)
| Term | Meaning |
|---|---|
| Cold start | First invoke pays init; later invokes reuse the env |
| Idempotency | Same request twice → same effect |
| DLQ | Dead-letter queue for failed messages |
| Choreography | Services react to events; no central conductor |
| Orchestration | Central workflow (Step Functions) directs steps |
| Fan-out | One event → many consumers |
| Fan-in | Many branches join before next step |
| Strangler fig | Peel routes from monolith gradually |
| Saga | Distributed tx via steps + compensations |
| Provisioned concurrency | Keep N envs warm; you pay for ready capacity |
20. Practice: explain out loud
- 60 seconds: everyday story for CQRS and event sourcing.
- 60 seconds: sync vs async with a checkout example.
- 60 seconds: when you refuse Lambda.
- 60 seconds: DLQ + idempotency in one breath.
- Record once; cut fillers; record again.
21. Safe lab hygiene
- Tag every lab resource with owner and expiry date.
- Set a monthly AWS Budget alarm in INR or USD before experiments.
- Prefer read-only commands first; mutate in a sandbox account.
- Delete queues, APIs, tables you created — DynamoDB and APIGW leave footprints.
- Never put production secrets in env vars or screenshots.
- Do not enable provisioned concurrency for a hello-world.
22. How this chapter links others
- Compute depth: Lambda.
- HTTP edge: API Gateway.
- Data modeling: DynamoDB.
- Messaging: SQS/SNS.
- Events: EventBridge.
- Workflows: Step Functions.
- Project chapters (build-*): worked assemblies — link, do not paste.
23. Myths to drop
| Myth | Reality |
|---|---|
| Serverless means zero servers | Servers exist; you do not manage them |
| Always cheaper than EC2 | Steady high load can favor EC2/Savings Plans |
| No Ops needed | You own IAM, data, alarms, cost, incidents |
| One Lambda per app is fine | Bounded contexts scale better |
| Queues are optional decoration | Queues are the load leveler and blast-radius wall |
24. Error budget thinking for serverless
If your monthly error budget is 0.1% on checkout, a fan-out that retries aggressively can burn the budget with duplicate emails while money stays correct. Separate business correctness SLOs from notification SLOs.
- Write two SLOs: money-correct and notify-delivered.
- Retries on money path: bounded + idempotent.
- Retries on notify path: best-effort + DLQ.
- Page humans on money DLQ; ticket on notify DLQ.
25. Regional notes (ap-south-1)
- Build labs in Mumbai (ap-south-1) unless your org mandates elsewhere.
- Some services launch first in us-east-1 — check feature availability.
- Data residency for INR fintech may require in-region only.
- Latency to end users in India usually favors ap-south-1.
26. Naming conventions that save incidents
# Suggested prefixes
# Lambda: pay-intake-prod, pay-worker-prod
# SQS: pay-orders.fifo / pay-orders-dlq
# SNS: pay-order-events
# EB bus: pay.domain
# SFN: pay-refund-saga
# DDB: pay-orders-prod
Include env (dev/stage/prod) and domain. Future you grepping CloudWatch will thank present you.
27. Pre-mortems (ten minutes)
- Imagine tomorrow headline: double charge in AED. What broke?
- Imagine DLQ depth 50,000 after a bad deploy. How do you drain?
- Imagine Cognito outage. Do APIs fail closed or serve read-only?
- Write answers in the runbook before go-live.
28. Currency and locale in examples
Rotate currencies in designs: INR, USD, EUR, AED, NGN, BRL, JPY, SGD. Rotate names: Aisha, Rohan, Mei, Diego, Fatima, Kenji, Priya, Omar, Sofia, Luis. This keeps mental models global — not one-country samples.
29. Worked story: Mei appointment board (SGD)
- Command BookSlot writes AppointmentBooked to write table.
- Stream wakes projector; updates doctor-day read table.
- Clinic TV queries read table only.
- Cancel emits AppointmentCancelled; projector removes slot.
30. Anti-patterns
- One mega-event GodUpdated with 80 fields.
- Projections in the command Lambda synchronously always.
- No replay story when projection code has a bug.
Revision, interview and comparison cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.








More cards: Ravi Sir's Cards.
Practice Tasks
Vidarbha Cotton Mills is a textiles business in Nagpur. Ganesh Wankhede looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 16
Easy CQRS splits read write — 1
Why: Idea.
What to do: Enter 1 if CQRS separates the write model from the read model.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy ES stores events — 1
Why: Idea.
What to do: Enter 1 if event sourcing stores state as a sequence of events.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy Commands change state — 1
Why: Write.
What to do: Enter 1 if commands are the write-side intents that change state.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy Queries hit projections — 1
Why: Read.
What to do: Enter 1 if queries usually hit denormalized projections.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Easy List DynamoDB tables
Why: CLI.
What to do: List DynamoDB tables
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy CQRS sides YAML
Why: Map.
What to do: YAML: write: commands, read: projections.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Easy ES helps audit — 1
Why: Why.
What to do: Enter 1 if an event log helps audit and replay.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium DDB Streams → projection — 1
Why: Pipe.
What to do: Enter 1 if DynamoDB Streams can feed CQRS projections.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium Replay rebuilds views — 1
Why: Rebuild.
What to do: Enter 1 if replaying the event log can rebuild read models.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium Read lag is normal — 1
Why: Tradeoff.
What to do: Enter 1 if eventual consistency between write and read is expected.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium Projector idempotent — 1
Why: Safe.
What to do: Enter 1 if projection handlers must tolerate duplicate events.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Medium Pipe YAML
Why: Flow.
What to do: YAML: source: stream, target: projection.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Fill in each property with the value from the task.
- Click Check answer. Key order does not matter.
Hard Snapshots speed fold — 1
Why: Perf.
What to do: Enter 1 if snapshots reduce fold time for long event streams.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Hard Version event schemas — 1
Why: Evolve.
What to do: Enter 1 if event schemas need versioning as the domain evolves.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Hard Not every CRUD needs ES — 1
Why: Fit.
What to do: Enter 1 if not every CRUD app needs full event sourcing.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.
Hard Delete lab same day — 1
Why: Hygiene.
What to do: Enter 1 if lab-spg-cqrs should be deleted same day.
- Write down the numbers the task gives you.
- Work it out step by step on paper.
- Type only the final answer in the box.
- Click Check answer.