Creare consumer di eventi idempotenti
Renda i partecipanti di una saga basata sulla choreography sicuri rispetto a eventi duplicati o fuori ordine usando consumer idempotenti, il pattern inbox e il pattern outbox.
Creare consumer di eventi idempotenti è una lezione Microservices Communication Patterns (Saga, Circuit Breaker) 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 Microservices Communication Patterns (Saga, Circuit Breaker), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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; // staleCompensations 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
Impara Microservices Communication Patterns (Saga, Circuit Breaker) 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 «Creare consumer di eventi idempotenti» è gratuita?
Sì — il testo completo di «Creare consumer di eventi idempotenti» è 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 Microservices Communication Patterns (Saga, Circuit Breaker), passa a CoddyKit PRO. Il corso Microservices Communication Patterns (Saga, Circuit Breaker) include 4 lezioni in totale.
Cosa imparerò in «Creare consumer di eventi idempotenti»?
Renda i partecipanti di una saga basata sulla choreography sicuri rispetto a eventi duplicati o fuori ordine usando consumer idempotenti, il pattern inbox e il pattern outbox. Eserciti Microservices Communication Patterns (Saga, Circuit Breaker) 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 Microservices Communication Patterns (Saga, Circuit Breaker)?
Non è richiesta alcuna esperienza precedente. Microservices Communication Patterns (Saga, Circuit Breaker) 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 «Creare consumer di eventi idempotenti»?
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 Microservices Communication Patterns (Saga, Circuit Breaker)?
Sì. Ogni lezione Microservices Communication Patterns (Saga, Circuit Breaker) 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
- Progettazione di Saga basate sugli eventi
- Event bus e broker di messaggi
- Gestione delle compensazioni con gli eventi
- Creare consumer di eventi idempotenti