事务性发件箱
在数据库事务内将事件写入发件箱表,再由独立进程发布到消息代理。
结构与工作原理
按步骤查看箭头所示的流程。
业务数据与待发布事件保存在同一事务。消费者需具备幂等性,以应对中继重复发布。
预期收益
只有对应数据变更提交后才存在可发布事件,减少丢失并提高数据库与事件的一致性。
权衡
需要额外的发件箱表和中继,轮询查询也有开销。
向 Kafka 等系统投递可能是至少一次,因此可能收到重复消息。
运维与应用指南
综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。
何时使用
适用于需保证数据库更新与事件发布一致的订单、支付、通知和审计日志流程。减少事件丢失,并通过消费端幂等处理潜在重复投递。
应用场景
一家网上宠物用品商店日处理 100 万份订单,要求订单提交与 OrderPlaced 事件保持一致。订单服务在同一个事务保存订单和发件箱记录,中继把已提交记录发布到 Kafka。支付确认和配送通知不能丢失,消费者以订单 ID 做幂等处理。监控发布延迟和未发布积压,并把失败消息隔离到死信队列。
部署原则
- 将订单与发件箱记录置于同一数据库和模式
- 中继可嵌入应用,也可作为独立工作进程或 Deployment
- 使用 Kafka 或托管 Pub/Sub 作为代理
- 多个中继 Pod 用 SKIP LOCKED 或唯一 Owner 协调锁竞争
- 监控最旧未发布行、发布延迟、死信队列和保留策略。
本地部署
在本地现有的关系数据库和 Kafka 等设施上配置发件箱和中继,自主管理事务边界、备份保留、轮询负荷以及中继高可用。多中继时用 SKIP LOCKED 或主节点协调,并记录故障恢复与死信保留流程。
云环境
结合托管数据库事务及消息服务,部署变更流、CDC 或轮询中继。使用托管 IAM、死信、保留、自动扩缩和观测功能,不假定恰好一次投递,由消费者保证幂等。
Kubernetes
中继以 Deployment 或边车部署,使用 Pod 独立 Owner 与 SKIP LOCKED 协调。配置 maxUnavailable 为 0,延长终止宽限期,readiness 判断中继是否就绪、liveness 检查进程;同步运维数据库和代理连接、资源限制、HPA 与积压指标。
故障应对
典型故障包括中继中断或反复崩溃、毒消息、发件箱表锁竞争、代理连接失败及重复发布。检查最旧未发布行及延迟,把错误分为暂时或永久;暂时错误退避重试,永久错误隔离到死信队列后重处理,并确认消费者幂等及积压消化情况。
14 个维度的证据
反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。
定义与概览有证据
在数据库事务内将事件写入发件箱表,再由独立进程发布到消息代理。
This pattern saves events in a data store that's typically in an outbox table in your database before it pushes them to a message broker.解决的问题有证据
解决数据库写入与事件发布无法原子提交所形成的双写和数据不一致问题。
A dual write operation occurs when an application writes to two different systems关键组件与资源有证据
包含发件箱表、业务数据存储、独立中继或工作进程及消息代理。
a separate service or worker process queries the outbox table for unhandled entries, publishes them, and marks them as processed.数据与资产放置原则有证据
在同一数据库事务中记录发件箱条目与订单等业务数据,使两者共同提交或回滚。
`Outbox.add()` inserts the message into an outbox table in the same database transaction as the order, so both commit or neither does.代表性使用场景有证据
适用于订单通知、支付确认、审计日志等重视数据库变更与事件发布可靠性和一致性的流程。
You need to update a database and publish an event reliably应用场景示例有证据
网店订单 API 一并保存订单和 OrderPlaced 事件,随后由中继发布事件。
the order API saves the order and announces it with an `OrderPlaced` event.不适用情形与注意事项有证据
向 Kafka 等系统投递可能是至少一次,因此可能收到重复消息。
We guarantee every message in the outbox will eventually arrive in Kafka, but it may arrive more than once.本地部署:开发、发布与运维有证据
一种部署与运维场景是先在本地 Kubernetes 集群运行服务,再迁移上云。
Initially, services were operated in an on-premise Kubernetes cluster.云环境:开发、发布与运维有证据
云环境可使用 MongoDB 变更流,以及 Google Pub/Sub 等托管消息代理。
using MongoDB change streams and Google Pub Sub as message broker.Kubernetes:开发、发布与运维有证据
在 Kubernetes 中以 Deployment 运行中继,可考虑 Pod 独立 Owner、SKIP LOCKED 协调、readiness/liveness 探针和较长终止宽限期。
Run the relay embedded or as a Deployment. Give every pod a unique `Owner`.故障应对有证据
中继中断、毒消息和发件箱表锁竞争可能停止发布。监控最旧的未发布行以及发布延迟。
The relay is a polling worker or a change data capture process, and either can stop.优势有证据
只有对应数据变更提交后才存在可发布事件,减少丢失并提高数据库与事件的一致性。
The event exists if and only if the data change committed.代表性技术与工具有证据
例如 MongoDB 变更流、Google Pub/Sub、消息代理、发件箱表及中继。
using MongoDB change streams and Google Pub Sub as message broker更多参考资料 (21)
- Transactional outbox pattern - AWS Prescriptive Guidance ↗
- Pattern: Transactional outbox ↗
- Implement the Transactional Outbox Pattern by Using Azure Cosmos DB - Azure Architecture Center | Microsoft Learn ↗
- Transactional Outbox | Arcflow ↗
- The Transactional Outbox Pattern ↗
- Transactional outbox | NestJS - A progressive Node.js framework ↗
- Transactional outbox - pg_tide Documentation ↗
- aws-samples/transactional-outbox-pattern ↗
- Medium ↗
- xeno097/transactional-outbox-pattern-with-mongodb ↗
- docs/operations.md ↗
- ssa1004/gpu-job-orchestrator ↗
- KinNeko - Knowledge ↗
- hitesh22rana/chronoverse ↗
- Lessons from using the outbox pattern at scale ↗
- Transactional Outbox: Retry, Backoff and Monitoring ↗
- Transactional Outbox Pattern Problems That Start on Day Two ↗
- The transactional outbox: a write and a message as one step | ShardCraft ↗
- Part 2: Dual-Write and Retries - the Transactional Outbox, Backoff, Circuit Breakers | Hiep Lam ↗
- Transactional Outbox and Relay | CDC: The Missing Manual ↗
- Revisiting the Outbox Pattern ↗