0Pricing
Microservices Communication Patterns (Saga, Circuit Breaker) · Lektion

Idempotente Event-Consumer entwickeln

Machen Sie Teilnehmer einer Choreografie-Saga durch idempotente Consumer, das Inbox-Muster und das Outbox-Muster sicher gegenüber doppelten und ungeordneten Ereignissen.

Idempotente Event-Consumer entwickeln ist eine kostenlose Microservices Communication Patterns (Saga, Circuit Breaker)-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 Microservices Communication Patterns (Saga, Circuit Breaker)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.

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

Events Get Redelivered

In a choreography saga, services react to events from a broker. Brokers deliver at least once, so the same event can arrive twice — and sometimes out of order. Naive handlers double-charge or double-ship.

The Goal: Idempotent Consumers

An idempotent consumer can process the same event repeatedly with no extra effect. This is essential for correctness in any event-driven saga.

Track Processed Event Ids

Give every event a unique id. Before handling, check whether you have already processed that id; if so, skip and acknowledge.

if (inbox.exists(event.id())) { ack(); return; }
handle(event);
inbox.save(event.id());
ack();

The Inbox Pattern

The inbox pattern stores processed event ids in a table inside the same database transaction as the business change. Insert + work commit together, so a redelivery is rejected by the unique constraint.

BEGIN;
  INSERT INTO inbox(event_id) VALUES (?); -- unique
  UPDATE orders SET status = 'PAID' WHERE id = ?;
COMMIT;

Why Atomicity Is Key

If you ack the event but crash before committing the business change, the work is lost. Doing both in one transaction guarantees they succeed or fail together.

The Dual-Write Problem

A participant must often update its DB and publish a new event. Doing these as two separate calls risks one succeeding and the other failing — the classic dual-write problem.

The Outbox Pattern

The outbox pattern solves it: write the outgoing event into an outbox table in the same transaction as the business change. A separate relay publishes from the outbox to the broker.

BEGIN;
  UPDATE orders SET status = 'SHIPPED' WHERE id = ?;
  INSERT INTO outbox(payload) VALUES (?);
COMMIT;

Relaying the Outbox

A poller or change-data-capture (CDC) tool like Debezium reads new outbox rows and publishes them, then marks them sent. Publishing is at-least-once, so downstream consumers must be idempotent too.

Handling Out-of-Order Events

Events may arrive out of order. Use version numbers or sequence ids and ignore stale events, or design handlers that converge to the correct state regardless of order.

if (event.version() <= current.version()) return; // stale

Compensations Must Be Idempotent Too

When a saga fails, compensating events also flow through the broker and can be redelivered. A compensation (e.g. refund) must itself be idempotent, or you refund twice.

Putting It Together

A robust choreography participant: consume via the inbox (dedup), do work and write the outbox in one transaction, relay the outbox to the broker, and make every handler — including compensations — idempotent.

Quick Check

Test your event-consumer knowledge.

Recap

You hardened choreography consumers:

  • Brokers deliver at-least-once, so events repeat and may reorder
  • Idempotent consumers process duplicates safely
  • The inbox pattern dedups within the business transaction
  • The outbox pattern solves the dual-write problem; a relay/CDC publishes it
  • Handle out-of-order events and make compensations idempotent too

Häufig gestellte Fragen

Ist die Lektion „Idempotente Event-Consumer entwickeln“ kostenlos?

Ja — der vollständige Text von „Idempotente Event-Consumer entwickeln“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Microservices Communication Patterns (Saga, Circuit Breaker)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Idempotente Event-Consumer entwickeln“?

Machen Sie Teilnehmer einer Choreografie-Saga durch idempotente Consumer, das Inbox-Muster und das Outbox-Muster sicher gegenüber doppelten und ungeordneten Ereignissen. Du übst Microservices Communication Patterns (Saga, Circuit Breaker) 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 Microservices Communication Patterns (Saga, Circuit Breaker) zu starten?

Keine Vorkenntnisse erforderlich. Microservices Communication Patterns (Saga, Circuit Breaker) 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 „Idempotente Event-Consumer entwickeln“?

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 Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion Code schreiben und ausführen?

Ja. Jede Microservices Communication Patterns (Saga, Circuit Breaker)-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. Ereignisgesteuerte Sagas entwerfen
  2. Event Bus und Message-Broker
  3. Kompensation mit Ereignissen verarbeiten
  4. Idempotente Event-Consumer entwickeln
← Zurück zu Microservices Communication Patterns (Saga, Circuit Breaker)