Event-Driven Architecture
Event-driven design trades tight coupling for asynchronous workflows—and introduces ordering, duplicates, schema evolution, and distributed tracing challenges.
When to Offer This Workflow
Trigger conditions:
- - Replacing long chains of synchronous HTTP calls
- Adopting Kafka, Pub/Sub, EventBridge, NATS, etc.
- Need for sagas, compensating transactions, or cross-service workflows
Initial offer:
Use six stages: (1) identify events, (2) contracts & versioning, (3) delivery semantics, (4) orchestration vs choreography, (5) observability, (6) failure & replay). Assume at-least-once delivery unless proven otherwise.
Stage 1: Identify Events
Goal: Distinguish domain events (facts that happened) from commands (requests). Assign owning bounded context per event type.
Exit condition: Event catalog: name, schema, producers, consumers, SLAs.
Stage 2: Contracts & Versioning
Goal: Schema registry or equivalent; backward-compatible evolution; consumers ignore unknown fields; deprecation policy for old versions.
Stage 3: Delivery Semantics
Goal: Partition keys for per-entity ordering; idempotent consumers; dedupe keys when exactly-once illusion is needed.
Stage 4: Orchestration vs Choreography
Goal: Central orchestrator (saga coordinator) vs decentralized choreography—trade visibility vs coupling.
Practices
- - Transactional outbox when DB write and event publish must align
Stage 5: Observability
Goal: Correlation ids on events; traces spanning HTTP → broker → consumer; lag and DLQ depth metrics.
Stage 6: Failure & Replay
Goal: Dead-letter queues, replay tooling, poison message handling, and idempotent replays.
Final Review Checklist
- - [ ] Event inventory with clear ownership
- [ ] Versioned contracts and compatibility rules
- [ ] Idempotent consumers; partition strategy documented
- [ ] Saga/outbox where transactional consistency required
- [ ] Tracing and replay operationalized
Tips for Effective Guidance
- - Choreography can hide flows—document critical sequences as diagrams.
- Pair with message-queues and idempotency for implementation detail.
Handling Deviations
- - Low volume: start with a simple queue before full Kafka topology.
事件驱动架构
事件驱动设计将紧耦合替换为异步工作流——并引入了排序、重复、模式演进和分布式追踪等挑战。
何时提供此工作流
触发条件:
- - 替换长链同步HTTP调用
- 采用Kafka、Pub/Sub、EventBridge、NATS等
- 需要Saga、补偿事务或跨服务工作流
初始提供:
使用六个阶段:(1) 识别事件,(2) 契约与版本管理,(3) 投递语义,(4) 编排与编排舞,(5) 可观测性,(6) 故障与重放。除非另有证明,否则默认采用至少一次投递。
阶段1:识别事件
目标: 区分领域事件(已发生的事实)与命令(请求)。为每种事件类型分配所属的限界上下文。
退出条件: 事件目录:名称、模式、生产者、消费者、SLA。
阶段2:契约与版本管理
目标: 模式注册表或等效方案;向后兼容的演进;消费者忽略未知字段;旧版本的废弃策略。
阶段3:投递语义
目标: 按实体排序的分区键;幂等消费者;需要精确一次投递幻觉时的去重键。
阶段4:编排与编排舞
目标: 中央编排器(Saga协调器)与去中心化编排舞——权衡可见性与耦合度。
实践
- - 当数据库写入与事件发布必须一致时使用事务性发件箱
阶段5:可观测性
目标: 事件上的关联ID;跨越HTTP→代理→消费者的追踪;延迟和死信队列深度指标。
阶段6:故障与重放
目标: 死信队列、重放工具、毒消息处理以及幂等重放。
最终审查清单
- - [ ] 具有明确所有权的事件清单
- [ ] 版本化契约与兼容性规则
- [ ] 幂等消费者;分区策略已记录
- [ ] 需要事务一致性时的Saga/发件箱
- [ ] 追踪与重放已可操作
有效指导技巧
- - 编排舞可能隐藏流程——将关键序列记录为图表。
- 与消息队列和幂等性配合使用以实现具体细节。
处理偏差
- - 低流量:在完整Kafka拓扑之前,先从简单队列开始。