Event Sourcing là pattern lưu trữ mọi thay đổi trạng thái của application dưới dạng chuỗi immutable events thay vì chỉ lưu trạng thái hiện tại – giống như transaction log của bank hơn là số dư hiện tại.
Ví dụ: thay vì lưu account.balance = 1000, lưu [Deposited(500), Deposited(700), Withdrew(200)] – số dư là kết quả replay của events.
Lợi ích: complete audit trail miễn phí, có thể rebuild state tại bất kỳ điểm nào trong lịch sử, time-travel debugging, dễ tích hợp với CQRS và projections, event stream là nguồn sự thật duy nhất. Hạn chế: querying current state cần replay (giải quyết bằng snapshots), event schema evolution phức tạp khi domain thay đổi, storage tốn hơn, learning curve cao. Phù hợp cho: banking/fintech (audit trail bắt buộc), booking systems, domain-driven design với complex business rules. EventStoreDB là database chuyên biệt cho Event Sourcing; có thể implement trên PostgreSQL, Kafka cũng được.
Event Sourcing is a pattern that stores every state change in an application as a sequence of immutable events, rather than storing only the current state — more like a bank's transaction log than just the current balance. For example, instead of storing account.balance = 1000, you store [Deposited(500), Deposited(700), Withdrew(200)] — the balance is derived by replaying the events.
Benefits: a complete audit trail for free, the ability to rebuild state at any point in history, time-travel debugging, easy integration with CQRS and projections, and the event stream as a single source of truth. Limitations: querying current state requires replaying events (addressed with snapshots); event schema evolution is complex when the domain changes; higher storage requirements; steep learning curve. Best suited for: banking/fintech (where audit trails are mandatory), booking systems, and domain-driven design with complex business rules. EventStoreDB is a purpose-built database for Event Sourcing; it can also be implemented on PostgreSQL or Kafka.