logo
Architecture Patterns

Event Sourcing

事件溯源

Event sourcing 的核心思想是:不要只存当前状态,而是用 append-only store 记录所有变更事件。这个 store 作为 system of record,可用于重建 domain objects。

event-sourcing

这种方式能简化复杂域的任务,避免数据模型与业务域的同步难题,同时提升 performance、scalability 与 responsiveness。它还能为 transactional data 提供一致性,并保留完整审计轨迹,支持 compensating actions。

Event sourcing vs Event-Driven Architecture (EDA)

Event sourcing 常被误认为 Event-driven Architecture (EDA)。EDA 是用事件在服务边界之间通信,通常借助 message broker 异步发布/消费。

Event sourcing 则是把事件作为状态存储方式:不是保存当前状态,而是保存事件流。它是实现 EDA 的多种模式之一。

Advantages

  • 适合 real-time data reporting
  • 可从 event store 重建数据,fail-safety 好
  • 非常灵活,可存任何类型的消息
  • 对高合规系统的 audit logs 很友好

Disadvantages

  • 需要高效的网络基础设施
  • 需要可靠的消息格式治理(如 schema registry)
  • 不同 events payload 不一致,处理复杂

相关练习题

Event Sourcing

暂无相关练习题