0Pricing
Apache Kafka & Stream Processing Fundamentals · レッスン

信頼性の高いイベント発行のためのアウトボックスパターン

トランザクショナルアウトボックスパターンによってデータベースへの書き込みとKafkaイベントの一貫性を保証し、二重書き込みによるデータ損失を避ける方法を学びます。

「信頼性の高いイベント発行のためのアウトボックスパターン」はCoddyKit上の無料Apache Kafka & Stream Processing Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「信頼性の高いイベント発行のためのアウトボックスパターン」レッスンは無料ですか?

はい。「信頼性の高いイベント発行のためのアウトボックスパターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Apache Kafka & Stream Processing Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Apache Kafka & Stream Processing Fundamentalsコースには全4レッスンが含まれています。

「信頼性の高いイベント発行のためのアウトボックスパターン」で何を学びますか?

トランザクショナルアウトボックスパターンによってデータベースへの書き込みとKafkaイベントの一貫性を保証し、二重書き込みによるデータ損失を避ける方法を学びます。 ブラウザで直接実行するハンズオンコードでApache Kafka & Stream Processing Fundamentalsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Kafkaによるイベントソーシング
  2. 変更データキャプチャ(CDC)
  3. マイクロサービスの通信パターン
  4. 信頼性の高いイベント発行のためのアウトボックスパターン
← Apache Kafka & Stream Processing Fundamentalsに戻る