Part 3 · আর্কিটেকচার 📖 ১৩ মিনিট পড়া 📝 ২০টি কুইজ

Event Sourcing

শুধু current state না — সব change-এর full history।

📝 কুইজে যান

আপনার ব্যাংক account-এ এখন ৫,০০০ টাকা। কীভাবে এসেছে? Traditional DB শুধু বলে: "Balance = 5000"। কিন্তু banking-এ সব deposit, withdrawal-এর history থাকা চাই। Event Sourcing-এ data store হয়: "+10,000 deposit", "-3,000 withdraw", "-2,000 fee"। যোগ করলে = 5,000। শুধু current না — সব change record।

Event Sourcing কী?

Event Sourcing = একটি pattern যেখানে আমরা current state store করি না; বরং সব state-changing event-কে immutable, append-only log-এ store করি। Current state derive করা হয় events replay করে।

Traditional vs Event Sourcing

Traditional (CRUD)

accounts table: | id | balance | | 1 | 5000 | -- পুরাতন value আর নেই

Event Sourcing

events log: | id | type | amount | timestamp | | 1 | Deposited | 10000 | 2026-01-01 | | 2 | Withdrawn | 3000 | 2026-01-15 | | 3 | Fee | 2000 | 2026-02-01 | -- Current balance = sum = 5000

কেন Event Sourcing?

  • Complete audit trail: কে, কখন, কী করেছে — সব পাওয়া যায়।
  • Time travel: Past-এর যেকোনো point-এর state।
  • Debugging: Bug কোন event-এ হয়েছিল trace।
  • Replay capability: New service event replay করে state build।
  • Multiple projections: একই event থেকে multiple read model।
  • Compliance: Banking, healthcare, legal — audit mandatory।
  • Event-driven friendly: Event-গুলোই source — natural fit।

মূল Components

  • Event: Past tense fact (UserRegistered, MoneyDeposited)।
  • Event Store: Append-only log (Kafka, EventStoreDB)।
  • Aggregate: Domain object (Account, Order)।
  • Projection (Read Model): Event থেকে derived view।
  • Snapshot: Performance optimization।

Flow

  1. Command arrives: "Withdraw 1000 from account 1"।
  2. Aggregate loads events: Account 1-এর সব past events।
  3. Replay to current state: Sum = balance।
  4. Validate command: Sufficient funds? Yes।
  5. Emit new event: "MoneyWithdrawn(1000)"।
  6. Append to event store: Persistent।
  7. Update projections: Read models update।

Snapshot — Performance Optimization

Million events থাকলে — replay slow। সমাধান: periodic snapshot।

Events: 1...1000 Snapshot at event 1000 (state captured) Events: 1001...1500 Current state = Snapshot + replay 1001-1500
  • Snapshot frequency tune করতে হবে।
  • Snapshot rebuilt possible (event-গুলোই source)।

CQRS-এর সাথে সম্পর্ক

Event Sourcing প্রায়ই CQRS-এর সাথে আসে:

  • Write side: Events।
  • Read side: Projections (events থেকে denormalized read model)।
  • পরের chapter-এ CQRS বিস্তারিত।

সুবিধা

  • Full history available।
  • Audit & compliance natural।
  • Temporal query (yesterday-এর state কী ছিল)।
  • Bug investigation easy — replay করে recreate।
  • Multiple read models — different projections।
  • Event-driven architecture-এর backbone।

Challenges

Eventual Consistency

Read model events থেকে derived — write-এর সাথে সাথে updated না।

Event Schema Evolution

পুরাতন event format — code-এ এখনও handle করতে হবে। Versioning critical।

Debugging Initial

Mental shift — "current state কোথায়?" → "events কী ছিল?"।

Storage Growth

Event log কখনো delete করা যায় না। Storage বাড়ে।

Complexity

CRUD-এর চেয়ে কঠিন — operational overhead।

Replay Time

Long history → snapshot ছাড়া slow rebuild।

কখন Event Sourcing?

  • Audit trail mandatory (banking, healthcare, legal)।
  • Temporal query দরকার (historical state)।
  • Event-driven business domain।
  • Multiple read views একই data থেকে।
  • Domain expert event-এর কথা বলে (DDD)।
  • Debugging-এ historical replay গুরুত্বপূর্ণ।

কখন Event Sourcing না?

  • Simple CRUD app।
  • Audit trail unnecessary।
  • Team-এর experience কম।
  • Strong real-time consistency দরকার everywhere।
  • Schema rapidly changing।

Event Store Tools

  • EventStoreDB: Purpose-built event store।
  • Apache Kafka: With long retention।
  • AWS DynamoDB Streams: Serverless option।
  • Axon Framework: Java/Spring।
  • EventStore (CockroachDB): SQL-based।

বাস্তব উদাহরণ

  • Banking: Account ledger — সব transaction history।
  • Git: Commits = events; HEAD = current state।
  • Accounting: Double-entry bookkeeping — natural event sourcing।
  • Workflow systems: Order lifecycle states।
  • Healthcare records: Patient timeline immutable।

Best Practices

  • Events past tense, immutable, business-meaningful name।
  • Event versioning — backward compatible।
  • Snapshot strategy — frequency tune।
  • Idempotent event handlers।
  • Don't put PII directly in events — GDPR right-to-erasure সমস্যা।
  • Start with bounded context — full system-এ Event Sourcing overkill।
  • Tooling, framework পরিপক্কতা যাচাই।

সাধারণ ভুল ধারণা

  1. "Event Sourcing = Pub-Sub": Pub-Sub = communication pattern; ES = persistence pattern।
  2. "Always pair with CQRS": Often, কিন্তু বাধ্যতামূলক না।
  3. "Easy to implement": না — significant complexity।
  4. "GDPR compliant out of box": Right-to-erasure কঠিন — design care।

📌 চ্যাপ্টার সারমর্ম

  • Event Sourcing = state derived from event log।
  • Append-only, immutable events; replay current state।
  • Snapshot performance optimization।
  • Audit trail, time travel, debugging — main benefits।
  • Banking, accounting, workflow — natural fit।