事件溯源
事件溯源将应用状态的每项变更存为一系列事件,以便重建过去的状态。
结构与工作原理
按步骤查看箭头所示的流程。
将状态变更保留为事件并回放。应考虑事件版本管理和回放成本。
预期收益
在复杂系统中可改善可审计性和写入性能。
权衡
模式本身较复杂,并对存储、并发、模式演进和查询方式带来显著取舍。
如果系统像简单 CRUD 一样主要查询当前状态,且变更历史并不重要,则难以证明采用事件溯源的必要性。
运维与应用指南
综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。
何时使用
适用于订单处理、客户入驻、合同谈判和多级审批等重视变更与决策历史的领域,也常用于需要应对审计、合规或重建历史状态的金融和商业系统。
应用场景
一家中型商业企业重建涵盖订单、支付和配送状态的数百万量级订单系统。要求保留每次状态变更的不可变审计日志,支持重现指定时间点、故障后重建读模型,并满足高峰写入吞吐量。按订单创建仅追加事件流,用快照和异步投影维护查询模型,将有毒事件隔离到死信队列(DLQ)。
部署原则
- 将事件存储部署在带持久卷(PVC)的有状态集群
- 将投影与读模型拆为独立服务和数据库
- 用耐久存储保存快照与检查点,以支持重启恢复
- 将事务发件箱与消息代理配置在领域数据库相关的组件中,以保障原子发布
- 将监控、死信队列和模式注册表放在运维层。
本地部署
本地环境自行运行有状态事件存储集群、持久存储、备份和恢复以及安全和密钥管理,并用声明式部署工具实现标准化。
云环境
在云端使用托管消息和存储、多可用区持久卷、CI/CD 与集中监控,并根据区域和所有权模型部署。
Kubernetes
在 Kubernetes 中使用 StatefulSet、PVC、Service、Secret 或 ConfigMap 部署事件存储,利用 HPA 或 KEDA 调节消费者负载,并运行就绪、存活探针以及备份恢复任务。
故障应对
常见故障包括有毒事件导致投影停滞、快照或模式变更后回放失败、复制延迟、重复处理和重试风暴。按顺序隔离到 DLQ、从检查点回放、重建读模型、确保幂等消费、启用背压并限制重试,最后检查复制延迟。
14 个维度的证据
反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。
定义与概览有证据
事件溯源将应用状态的每项变更存为一系列事件,以便重建过去的状态。
Event Sourcing ensures that all changes to application state are stored as a sequence of events.解决的问题有证据
不只保存当前状态,而是在仅追加的存储中保留对象的完整操作序列,以改善可审计性和写入性能。
store the full series of actions taken on an object in an append-only store.关键组件与资源有证据
核心组件包括事件存储、消息代理、带持久卷(PVC)的有状态集群,以及密钥和配置等运维资源。
configuration uses a PersistentVolumeClaim for durable storage and passes secrets and configuration via arguments数据与资产放置原则有证据
按顺序将事件追加为不可修改或删除的记录,并通过快照和耐久存储提高回放速度。
New events must be added at the end of the sequence. - Immutability. Previous events cannot be updated.代表性使用场景有证据
适合客户入驻、订单处理、合同谈判、多级审批等重视变更过程和决策历史的用例。
customer onboarding, order fulfillment, contract negotiation, or multi-step approvals应用场景示例有证据
有一家荷兰软件供应商为企业 Java 团队提供事件溯源和 CQRS 平台。
Company: a software vendor in the Netherlands, selling an event-sourcing and CQRS platform to enterprise Java teams.不适用情形与注意事项有证据
如果系统像简单 CRUD 一样主要查询当前状态,且变更历史并不重要,则难以证明采用事件溯源的必要性。
For more simple cases where the focus is more on the current state of the application than on histories of changes本地部署:开发、发布与运维有证据
在本地环境,可以自行运行私有基础设施,并通过基于声明式 API 的部署和运维工具管理事件流平台。
self-managing their private infrastructure in on-premises environments云环境:开发、发布与运维有证据
云端可在 GCP、AWS 等多云基础设施上结合 Kubernetes 平台、CI/CD 和监控来运行。
Clouds: Google Cloud as the primary platform, AWS alongside it.Kubernetes:开发、发布与运维有证据
在 Kubernetes 中,通过包含持久卷、密钥和配置的生产级配置部署有状态事件存储。
This guide shows how to run EventSourcingDB in a Kubernetes cluster using a production-ready configuration.故障应对有证据
有毒事件每次重试都失败时,会出现队首阻塞,使事件流和读模型停滞,因此需要死信队列(DLQ)、隔离和重建流程。
Without a release valve, that one event blocks the stream forever and the read model freezes at that point (head-of-line blocking).优势有证据
在复杂系统中可改善可审计性和写入性能。
This approach can improve auditability and write performance in complex systems.不足与权衡有证据
模式本身较复杂,并对存储、并发、模式演进和查询方式带来显著取舍。
Event sourcing is a complex pattern that introduces significant trade-offs.代表性技术与工具未确认
未记录已确认的原文证据,这不代表模式不适用。
更多参考资料 (23)
- Event Sourcing ↗
- Event Sourcing | Architectural Patterns ↗
- Use Cases – Concepts – Event Foundation ↗
- Event Sourcing Pattern - Azure Architecture Center | Microsoft Learn ↗
- Event sourcing: an introduction ↗
- Pattern: Event sourcing ↗
- Event Sourcing: An Introduction ↗
- Event Sourcing: When Is It Right to Use? ↗
- Deploying on Kubernetes – EventSourcingDB – Event Foundation ↗
- Cloud platform and Kubernetes operator for an event-sourcing software vendor ↗
- EventSourcingDB Documentation – Event Foundation ↗
- Optimising Kafka Consumers with Kubernetes and KEDA: Lessons from the Trenches ↗
- Event sourcing after page 1: How we built a cloud native… ↗
- Confluent Launches Confluent for Kubernetes, the Fastest Path to Building a Private Cloud Apache Kafka Service for Data in Motion ↗
- Optimally Scaling Kafka Consumer Applications ↗
- Deployment Options ↗
- Projection Operations: Poison Events & Rebuilds | Relay ↗
- A guide to event sourcing for teams building durable backends ↗
- Optimizing Event Replays – Concepts – Event Foundation ↗
- Resilient Design Guidance for Event Hubs and Functions - Azure Architecture Center | Microsoft Learn ↗
- A Practitioner's Guide to Event Sourcing Families and Replay-Based Recovery | Scixa ↗
- How to handle duplicates, retry storms and bad timestamps from log shippers such as Vector or Cribl ↗
- Migrating a Live System to Event Sourcing ↗