Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Lektion

Das Transactional-Outbox-Muster

Lernen Sie, wie das Transactional-Outbox-Muster eine Datenbanktransaktion und die Veröffentlichung in Kafka zuverlässig verbindet und in Spring Boot Inkonsistenzen durch doppelte Schreibvorgänge vermeidet.

Lektion 4 von 413 Schritte

Das Transactional-Outbox-Muster ist eine kostenlose Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.
Kostenlos starten

Lerne Advanced Spring Boot 4: Event-Driven Architecture (Kafka) mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Das Transactional-Outbox-Muster“ kostenlos?

Ja — der vollständige Text von „Das Transactional-Outbox-Muster“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Das Transactional-Outbox-Muster“?

Lernen Sie, wie das Transactional-Outbox-Muster eine Datenbanktransaktion und die Veröffentlichung in Kafka zuverlässig verbindet und in Spring Boot Inkonsistenzen durch doppelte Schreibvorgänge verm… Du übst Advanced Spring Boot 4: Event-Driven Architecture (Kafka) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Advanced Spring Boot 4: Event-Driven Architecture (Kafka) zu starten?

Keine Vorkenntnisse erforderlich. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Das Transactional-Outbox-Muster“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Lektion Code schreiben und ausführen?

Ja. Jede Advanced Spring Boot 4: Event-Driven Architecture (Kafka)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Kafka-Transaktionen verstehen
  2. Transaktionale Producer implementieren
  3. Exactly-Once-Verarbeitungssemantik
  4. Das Transactional-Outbox-Muster
← Zurück zu Advanced Spring Boot 4: Event-Driven Architecture (Kafka)