Il pattern Outbox per la pubblicazione affidabile degli eventi
Scopra come il pattern transactional outbox garantisca la coerenza tra scritture del database ed eventi Kafka, evitando la perdita di dati dovuta alle doppie scritture.
Il pattern Outbox per la pubblicazione affidabile degli eventi è una lezione Apache Kafka & Stream Processing Fundamentals gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Apache Kafka & Stream Processing Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Apache Kafka & Stream Processing Fundamentals include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara Apache Kafka & Stream Processing Fundamentals con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Il pattern Outbox per la pubblicazione affidabile degli eventi» è gratuita?
Sì — il testo completo di «Il pattern Outbox per la pubblicazione affidabile degli eventi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Apache Kafka & Stream Processing Fundamentals, passa a CoddyKit PRO. Il corso Apache Kafka & Stream Processing Fundamentals include 4 lezioni in totale.
Cosa imparerò in «Il pattern Outbox per la pubblicazione affidabile degli eventi»?
Scopra come il pattern transactional outbox garantisca la coerenza tra scritture del database ed eventi Kafka, evitando la perdita di dati dovuta alle doppie scritture. Eserciti Apache Kafka & Stream Processing Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Apache Kafka & Stream Processing Fundamentals?
Non è richiesta alcuna esperienza precedente. Apache Kafka & Stream Processing Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Il pattern Outbox per la pubblicazione affidabile degli eventi»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Apache Kafka & Stream Processing Fundamentals?
Sì. Ogni lezione Apache Kafka & Stream Processing Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Event sourcing con Kafka
- Change Data Capture (CDC)
- Pattern di comunicazione tra microservizi
- Il pattern Outbox per la pubblicazione affidabile degli eventi