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)
Event Sourcing
কেন 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
- Command arrives: "Withdraw 1000 from account 1"।
- Aggregate loads events: Account 1-এর সব past events।
- Replay to current state: Sum = balance।
- Validate command: Sufficient funds? Yes।
- Emit new event: "MoneyWithdrawn(1000)"।
- Append to event store: Persistent।
- Update projections: Read models update।
Snapshot — Performance Optimization
Million events থাকলে — replay slow। সমাধান: periodic snapshot।
- 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 পরিপক্কতা যাচাই।
সাধারণ ভুল ধারণা
- "Event Sourcing = Pub-Sub": Pub-Sub = communication pattern; ES = persistence pattern।
- "Always pair with CQRS": Often, কিন্তু বাধ্যতামূলক না।
- "Easy to implement": না — significant complexity।
- "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।