トランザクショナルアウトボックスパターン
トランザクショナルアウトボックスパターンでデータベーストランザクションとKafkaへの発行を確実につなぎ、Spring Bootで二重書き込みの不整合を避ける方法を学びます。
「トランザクショナルアウトボックスパターン」はCoddyKit上の無料Advanced Spring Boot 4: Event-Driven Architecture (Kafka)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAdvanced Spring Boot 4: Event-Driven Architecture (Kafka)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Dual-Write Problem
A common bug: a service updates its database and publishes a Kafka event in two separate operations. If one succeeds and the other fails, the system becomes inconsistent.
The transactional outbox pattern eliminates this risk.
Core Idea
Instead of publishing directly, write the event into an outbox table within the same database transaction as your business change.
A separate process then reads the outbox and publishes to Kafka.
The Outbox Table
The outbox table stores serialized events plus metadata.
CREATE TABLE outbox (
id UUID PRIMARY KEY,
aggregate_type VARCHAR(255),
aggregate_id VARCHAR(255),
event_type VARCHAR(255),
payload JSONB,
created_at TIMESTAMP DEFAULT now(),
published BOOLEAN DEFAULT false
);Writing in One Transaction
Both the domain entity and the outbox row are saved inside one @Transactional method, so they commit or roll back together.
@Transactional
public void placeOrder(Order order) {
orderRepository.save(order);
outboxRepository.save(OutboxEvent.from(order));
}Atomicity Guarantee
Because both writes share the database transaction, there is no window where the order exists without its event recorded. This is the key correctness property.
The Relay Process
A background relay polls unpublished outbox rows and sends them to Kafka, marking them published on success.
@Scheduled(fixedDelay = 500)
public void relay() {
for (OutboxEvent e : outboxRepository.findUnpublished()) {
kafkaTemplate.send(e.getTopic(), e.getPayload());
e.markPublished();
}
}At-Least-Once Publishing
If the relay crashes after sending but before marking published, the event is sent again. Consumers must therefore be idempotent, often using the event id as a dedup key.
Change Data Capture Alternative
Instead of polling, tools like Debezium tail the database transaction log and stream outbox inserts to Kafka automatically — lower latency and no polling load.
Cleaning Up the Outbox
Periodically delete or archive published rows to keep the table small and queries fast.
DELETE FROM outbox WHERE published = true AND created_at < now() - INTERVAL '7 days';When to Use It
Use the outbox when you must keep a database state change and an event publication consistent. It is simpler and more portable than spanning a Kafka transaction across an external database.
Putting It Together
The outbox pattern turns two unreliable writes into one atomic database commit plus a reliable relay. Combine it with idempotent consumers for end-to-end consistency.
Quick Check
Test your understanding of the outbox pattern.
Recap
You learned the transactional outbox pattern.
- Avoids dual-write inconsistency by using one DB transaction.
- An outbox table stores pending events.
- A relay (polling or CDC) publishes to Kafka.
- Consumers must be idempotent due to at-least-once delivery.
よくある質問
「トランザクショナルアウトボックスパターン」レッスンは無料ですか?
はい。「トランザクショナルアウトボックスパターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Advanced Spring Boot 4: Event-Driven Architecture (Kafka)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)コースには全4レッスンが含まれています。
「トランザクショナルアウトボックスパターン」で何を学びますか?
トランザクショナルアウトボックスパターンでデータベーストランザクションとKafkaへの発行を確実につなぎ、Spring Bootで二重書き込みの不整合を避ける方法を学びます。 ブラウザで直接実行するハンズオンコードでAdvanced Spring Boot 4: Event-Driven Architecture (Kafka)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Advanced Spring Boot 4: Event-Driven Architecture (Kafka)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのAdvanced Spring Boot 4: Event-Driven Architecture (Kafka)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「トランザクショナルアウトボックスパターン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このAdvanced Spring Boot 4: Event-Driven Architecture (Kafka)レッスンでコードを書いて実行できますか?
はい。すべてのAdvanced Spring Boot 4: Event-Driven Architecture (Kafka)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Kafkaトランザクションを理解する
- トランザクション対応プロデューサーの実装
- Exactly-Once処理セマンティクス
- トランザクショナルアウトボックスパターン