Ravindra BagaleCourses & study guides English Track your progress

Lambda आणि DynamoDB ने CQRS आणि Event Sourcing Patterns

क्रिकेट स्कोरर प्रत्येक चेंडू वहीत लिहितो (event log). स्कोअरबोर्ड = projection. CQRS = write commands वेगळे, read models वेगळे. Event sourcing = latest row नव्हे — events. AWS: DynamoDB writes + Streams → Lambda projections.

या chapter मध्ये तुम्ही काय शिकाल

  • CQRS scoreboard
  • Command vs query
  • Event sourcing when
  • Streams → projection
  • Link CDC/ecom

1. रोजचे उदाहरण: वही vs स्कोअरबोर्ड

Kenji (Osaka) notebook append-only; board = projection. Luis (Mexico City, MXN) OrderPlaced events; read table = list screen.

Hands-on: CDC Streams project, ecommerce cart project; depth DynamoDB Streams chapter.

2. CQRS का?

हे का लागतं?

एकच table write+dashboard = scans/contention. Spiky reads writes block करू नयेत.

हे काय आहे?

CQRS = commands vs queries. Event sourcing = events = source of truth.

3. Vertical diagram

Command -> Write -> Stream -> Projection -> ReadQueries = read model
Projection loop टाळा
Client command / query API Gateway + command Lambda Write model (DynamoDB) DynamoDB Streams Projection Lambda(s) Read models (DDB / OpenSearch) Query path skips the write Lambda
CQRS + Streams projections.

4. CQRS without full ES

CQRS light: orders + inbox projection. Full ES: प्रत्येक event कायम — शक्तिशाली आणि जड.

Approachठेवाटाळा
CQRS lightRead वेगळेदोन models नको
Full ESAudit/time-travelSimple CRUD

5. How: projection

कसं करायचं (step by step)

  1. Commands नाम.
  2. Events define.
  3. Invariants write side.
  4. Projection idempotent.
  5. Query ≠ command.
  6. Rebuild plan.

6. Commands + meaning

Stream describe

aws dynamodb describe-table --region ap-south-1 --table-name orders-write \
  --query 'Table.StreamSpecification' 

अर्थ: Projection साठी stream view confirm.

ESM list

aws lambda list-event-source-mappings --region ap-south-1 \
  --query 'EventSourceMappings[?contains(EventSourceArn, `dynamodb`)].{F:FunctionArn,S:State,U:UUID}' 

अर्थ: कुठली Lambda कुठल्या stream वर.

Logs filter

aws logs filter-log-events --region ap-south-1 \
  --log-group-name /aws/lambda/orders-projector \
  --filter-pattern 'ERROR' --limit 20 

अर्थ: Projection fail = stale reads.

Read table count

aws dynamodb scan --region ap-south-1 --table-name orders-inbox \
  --select COUNT --max-items 1 

अर्थ: Lab only; prod = metrics.

7. Industries

  • Fintech ledger.
  • Ecommerce dashboard.
  • EdTech streaks.
  • Media moderation.
  • SaaS seats.

8. Lab

9. Lab - CQRS sketch

Org block नसेल तर ap-south-1 (Mumbai). आधी budget alert.

कसं करायचं (step by step)

  1. Paper: loan command/events/read.
  2. StreamSpecification.
  3. Iterator age alarm.
  4. Rebuild plan.

Cleanup (billing थांबवा)

  1. ESM disable.
  2. Lab cleanup.

10. एकाच pattern — वेगवेगळ्या industries

  • Fintech (Aisha, Lagos): payment webhooks → SQS → Lambda.
  • Ecommerce (Rohan, Pune): checkout API; inventory SNS fan-out.
  • Healthcare (Mei, Singapore): SNS SMS; PHI + fine-grained IAM.
  • EdTech (Diego, São Paulo): EventBridge nightly cleanup.
  • Logistics (Fatima, Casablanca): EventBridge + saga.
  • Media (Kenji, Osaka): S3 → Lambda thumbs + fan-out.
  • SaaS (Priya, Bengaluru): Cognito + per-tenant partition.
  • Gov (Omar, Amman): strangler fig per form route.

11. Troubleshooting checklist

  1. Region confirm करा.
  2. CloudWatch Logs + request id.
  3. Execution role permissions.
  4. DLQ + oldest message age.
  5. curl -v + CORS.
  6. SFN event history.
  7. Linked chapter पुन्हा वाचा.
  8. Cost: concurrency/retries.

12. सामान्य errors आणि fixes

प्रत्येक row = खरी चूक. Panic करण्याआधी fix वाचा.

Error / लक्षणFix
Stale readIterator age.
Double applyeventId.
LoopGuard.
Wrong tableRead route.
Need oldOLD images.
OS mappingVersion.
Event stormCQRS light.
PII foreverMinimize.
No currencycurrency field.

13. FAQ

प्र. CQRS=ES?

नाही.

प्र. Streams=ES?

Change feed.

प्र. UI consistent?

Read-your-writes.

प्र. CDC?

cdc project.

प्र. Avoid?

Simple CRUD.

14. Interview Q&A (Beginner / Mid / Senior + MNC)

प्र. Beginner CQRS

Write/read split.

प्र. Projection

Read from events.

प्र. Mid idempotent

Same state.

प्र. Full ES

Audit/rebuild.

प्र. Senior poison

DLQ+replay.

प्र. MNC

Immutable+retention.

15. Tips — English, मराठी आणि हिंदी

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।

घरी करून बघा

  1. 3 events.
  2. Write vs read.

थोडक्यात सांगायचं तर

  1. गोष्टी पासून सुरू, मग services.
  2. Existing chapters link — labs copy नको.
  3. Failure साठी design.
  4. Measure → मग पैसे.

16. Architecture decision checklist

  1. User journey 5 bullets, no AWS names.
  2. Sync vs async mark.
  3. Failure mode.
  4. Idempotency key.
  5. Data from access patterns.
  6. Cost 1x and 10x.
  7. Quotas ap-south-1.
  8. Auth decision.
  9. On-call + alarm.
  10. Deep chapter link.
  11. मग console/IaC.

17. Copy-paste quick commands

# 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

Sandbox + read-only IAM.

18. अजून industry आणि stack stories

  • Fintech Lagos: webhook → SQS → idempotent ledger.
  • Ecommerce Pune: throttle + fan-out.
  • Healthcare SG: SNS + PHI IAM.
  • EdTech: EventBridge cleanup.
  • Logistics: bus + saga.
  • Media: avoid recursive S3.
  • SaaS: tenant partition.
  • Gov: strangler peel.
  • Startup: Function URL → HTTP API.
  • Retail: FIFO vs Standard.
  • Python: thin handlers.
  • Node: async/await.
  • Java: SnapStart.
  • .NET: package size.
  • Go: static binary.
  • PHP: hybrid.

19. Glossary

Termअर्थ
Cold startपहिल्या invoke ची init
Idempotencyदोनदा = तोच effect
DLQFail झालेले messages
ChoreographyEvents; conductor नाही
OrchestrationSFN central
Fan-outएक → अनेक
Fan-inअनेक → join
Strangler figMonolith peel
SagaSteps + compensate
Provisioned concurrencyWarm N; pay

20. Practice: मोठ्याने explain

  1. 60s: CQRS आणि event sourcing गोष्ट.
  2. 60s: sync vs async checkout.
  3. 60s: Lambda नको कधी.
  4. 60s: DLQ + idempotency.
  5. Record ×2.

21. Safe lab hygiene

  • Tag owner+expiry.
  • Budget alarm आधी.
  • Read-only first.
  • Cleanup.
  • No secrets in env/screenshots.
  • No provisioned on hello.

23. Myths

Mythखरं
Servers नाहीतServers आहेत; manage नको
नेहमी स्वस्तSteady वर EC2
No OpsIAM/data/alarms
एक LambdaBounded contexts
Queues optionalLoad leveler

24. Serverless error budget

Checkout error budget 0.1% असेल तर aggressive retries email duplicates देऊन budget जाळू शकतात. Money SLO ≠ notify SLO.

  1. दोन SLOs.
  2. Money retries bounded.
  3. Notify best-effort.
  4. Money page; notify ticket.

25. Regional notes (ap-south-1)

  • Labs Mumbai.
  • Feature availability.
  • Data residency.
  • Latency India.

26. Naming conventions

# 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

env + domain नावात ठेवा. CloudWatch grep सोपा.

27. Pre-mortems

  1. Double charge headline.
  2. DLQ drain plan.
  3. Cognito outage mode.
  4. Runbook.

28. Currency / locale

Currencies आणि names rotate करा — global mental model.

29. Mei appointments (SGD)

  1. BookSlot event.
  2. Projector.
  3. TV = read.
  4. Cancel projection.

30. Anti-patterns

  • Fat events.
  • Always sync proj.
  • No replay.

उजळणी, मुलाखत आणि तुलना cards

हा lesson काही मिनिटांत revise करा. Cards phone वर save करा किंवा PNG download करा. प्रत्येक card वरचा QR हा lesson उघडतो.

उजळणी card: CQRS splits read/write. One model hurts. Write commands; read projections. Cmd → event → projection. Split paths.
उजळणी card: CQRS splits read/write · PNG download करा · Card page उघडा
उजळणी card: Event sourcing log. Lost why. Append events; fold. Append → replay → snapshot. Log = audit.
उजळणी card: Event sourcing log · PNG download करा · Card page उघडा
तुलना: Single CRUD model vs CQRS. Single CRUD model: One model. CQRS: Split models.
तुलना: Single CRUD model vs CQRS · PNG download करा · Card page उघडा
तुलना: Current-state store vs Event-sourced log. Current-state store: Latest row. Event-sourced log: Append facts.
तुलना: Current-state store vs Event-sourced log · PNG download करा · Card page उघडा
मुलाखत card: When CQRS helps. Overkill. Divergent read/write. Fit vs anti.
मुलाखत card: When CQRS helps · PNG download करा · Card page उघडा
मुलाखत card: Streams → projections. Poll fight. Stream → projection. Dup handling.
मुलाखत card: Streams → projections · PNG download करा · Card page उघडा
मुलाखत card: Replay to rebuild. No rebuild. Replay / snapshot. Why snapshots.
मुलाखत card: Replay to rebuild · PNG download करा · Card page उघडा
मुलाखत card: Version event schemas. Silent break. Version + tolerate. Additive vs break.
मुलाखत card: Version event schemas · PNG download करा · Card page उघडा

अजून cards: Ravi Sir's Cards.

सराव Tasks

या scenario बद्दल

Vidarbha Cotton Mills हा नागपूर मधला वस्त्रोद्योग व्यवसाय आहे. Ganesh Wankhede त्यांचं IT व cloud काम पाहतात. त्यांचे पैसे INR मध्ये मोजले जातात आणि AWS resources ap-south-1 Region मध्ये चालतात. खालच्या tasks मध्ये तुम्ही या lesson मध्ये शिकलेलं वापरून त्यांची खरी कामं करता. सगळी नावं, account IDs, IP addresses व numbers सरावासाठी काल्पनिक आहेत.

0 / 16

सोपं CQRS

का: Idea.

काय करायचं: Split तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं ES

का: Idea.

काय करायचं: Events store तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं Cmd

का: Write.

काय करायचं: Commands तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं Query

का: Read.

काय करायचं: Projections तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं List-tables

का: CLI.

काय करायचं: list.

कसं करायचं:
  1. Task वाचा आणि ते कोणत्या AWS service बद्दल आहे ते ठरवा.
  2. aws लिहा, मग service चं नाव, मग operation (action शब्द).
  3. प्रत्येक आवश्यक flag (--name) व त्यापुढे task मधली value लिहा.
  4. Flags कोणत्याही क्रमाने चालतात. Check answer दाबा.

सोपं Sides

का: Map.

काय करायचं: write/read.

कसं करायचं:
  1. Task मधल्या top-level keys पासून सुरू करा.
  2. Child keys 2 spaces ने indent करा (tabs नकोत).
  3. प्रत्येक property मध्ये task मधली value भरा.
  4. Check answer दाबा. Keys चा क्रम महत्त्वाचा नाही.

सोपं Audit

का: Why.

काय करायचं: Audit तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Stream

का: Pipe.

काय करायचं: Streams तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Replay

का: Rebuild.

काय करायचं: Replay तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Lag

का: Tradeoff.

काय करायचं: Eventual तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Idem

का: Safe.

काय करायचं: Idempotent तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Pipe

का: Flow.

काय करायचं: source/target.

कसं करायचं:
  1. Task मधल्या top-level keys पासून सुरू करा.
  2. Child keys 2 spaces ने indent करा (tabs नकोत).
  3. प्रत्येक property मध्ये task मधली value भरा.
  4. Check answer दाबा. Keys चा क्रम महत्त्वाचा नाही.

अवघड Snap

का: Perf.

काय करायचं: Snapshots तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड Schema

का: Evolve.

काय करायचं: Version schemas तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड Scope

का: Fit.

काय करायचं: Not every CRUD तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड Lab

का: Hygiene.

काय करायचं: Delete lab तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.