可靠事件发布的发件箱模式
学习事务性发件箱模式如何确保数据库写入与 Kafka 事件保持一致,避免双重写入造成的数据丢失。
可靠事件发布的发件箱模式 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Dual-Write Problem
A service often needs to update its database and publish a Kafka event. Doing both as separate steps risks one succeeding and the other failing.
This dual-write problem leaves the database and Kafka inconsistent.
Why You Can't Just Try-Catch
If you commit the DB then publish, a crash in between loses the event.
If you publish then commit, a failed commit emits a phantom event. There is no atomic transaction spanning a relational DB and Kafka by default.
The Outbox Idea
The outbox pattern records the event in the same database transaction as the business change, in a dedicated outbox table.
One atomic commit now contains both the state change and the intent to publish.
The Outbox Table
A typical outbox schema captures everything needed to build the Kafka record.
CREATE TABLE outbox (
id UUID PRIMARY KEY,
aggregate_type VARCHAR,
aggregate_id VARCHAR,
event_type VARCHAR,
payload JSONB,
created_at TIMESTAMP DEFAULT now()
);Writing Atomically
Within one transaction, write the business row and the outbox row together.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 'a1';
INSERT INTO outbox (id, aggregate_type, aggregate_id, event_type, payload)
VALUES (gen_random_uuid(), 'account', 'a1', 'DebitMade',
'{"amount":100}');
COMMIT;Relaying to Kafka
A separate relay reads new outbox rows and publishes them to Kafka.
Two common approaches: poll the table, or use Change Data Capture to stream the inserts.
CDC + Debezium
Debezium has a dedicated Outbox Event Router SMT. It tails the DB transaction log, picks up outbox inserts, and routes each to the right Kafka topic — no polling, low latency.
At-Least-Once Delivery
The relay guarantees at-least-once delivery: an event is never lost, but it may be published more than once after a crash and retry.
Therefore consumers must be idempotent.
Idempotent Consumers
Use the event id (or aggregate id + version) to deduplicate downstream.
// Pseudocode
if (alreadyProcessed(event.id)) {
return; // skip duplicate
}
apply(event);
markProcessed(event.id);Ordering & Partitioning
To preserve per-aggregate order, use the aggregate_id as the Kafka message key.
All events for one account then land on the same partition and are consumed in order.
Pros & Cons
Pros: no dual-write loss, works with any DB, decouples publishing.
Cons: extra table and relay, at-least-once duplicates, slight latency. Still the standard solution for reliable event publishing.
Quick Check
Test your understanding of the outbox pattern.
Recap
You learned the outbox pattern.
- Solves the dual-write problem by committing state + event atomically.
- A relay (polling or CDC/Debezium) publishes outbox rows to Kafka.
- Delivery is at-least-once, so consumers must be idempotent.
- Key by aggregate id to preserve ordering.
常见问题解答
「可靠事件发布的发件箱模式」课时是免费的吗?
是的 — 「可靠事件发布的发件箱模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「可靠事件发布的发件箱模式」这节课中我会学到什么?
学习事务性发件箱模式如何确保数据库写入与 Kafka 事件保持一致,避免双重写入造成的数据丢失。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「可靠事件发布的发件箱模式」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 Kafka 实现事件溯源
- 变更数据捕获(CDC)
- 微服务通信模式
- 可靠事件发布的发件箱模式