Il pattern Outbox per una pubblicazione affidabile
Impari a usare il pattern transactional outbox per eseguire il commit atomico dello stato applicativo e dei messaggi in uscita, eliminando il problema della doppia scrittura nei sistemi basati sugli eventi.
Il pattern Outbox per una pubblicazione affidabile è una lezione RabbitMQ Messaging & Async Systems 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 RabbitMQ Messaging & Async Systems, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso RabbitMQ Messaging & Async Systems include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Dual-Write Problem
Saving to a database and publishing to RabbitMQ are two separate operations. If one succeeds and the other fails, your state and your events drift apart.
This is the dual-write problem.
What Is the Outbox Pattern?
The outbox pattern writes the business change and the outgoing message into the same database transaction, then publishes the message asynchronously from the outbox table.
The Outbox Table
Create a table that stores pending messages alongside your domain data.
CREATE TABLE outbox (
id UUID PRIMARY KEY,
payload JSONB,
published BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ DEFAULT now()
);Atomic Write
In one transaction, update the business row and insert the outbox row. Either both commit or neither does.
BEGIN;
INSERT INTO orders (id, status) VALUES ('42', 'paid');
INSERT INTO outbox (id, payload) VALUES (gen_random_uuid(), '{"order":"42"}');
COMMIT;The Relay Process
A separate relay (or message dispatcher) polls the outbox for unpublished rows, publishes them to RabbitMQ, and marks them published.
Polling the Outbox
The relay selects pending rows in order and publishes each one.
SELECT id, payload FROM outbox WHERE published = FALSE ORDER BY created_at;At-Least-Once Delivery
If the relay crashes after publishing but before marking the row, it republishes on restart. So consumers must be idempotent to tolerate duplicates.
Change Data Capture Alternative
Instead of polling, tools like Debezium tail the database write-ahead log and stream outbox inserts to RabbitMQ, reducing latency and load.
Combining With Confirms
The relay should use publisher confirms so it only marks a row published after the broker acknowledges the message, closing the loss window.
Outbox vs Saga
The outbox guarantees a single service reliably emits its events. A saga coordinates a multi-service workflow. They complement each other: each saga step can publish via its own outbox.
Cleanup and Retention
- Periodically delete or archive published rows
- Index on
publishedfor fast polling - Monitor relay lag to catch stalls early
Quick Check
Test your outbox understanding.
Recap
The outbox pattern stores outgoing messages in the same transaction as business data, then a relay publishes them to RabbitMQ asynchronously.
It eliminates the dual-write problem, requires idempotent consumers due to at-least-once delivery, and pairs well with publisher confirms and sagas.
Domande Frequenti
La lezione «Il pattern Outbox per una pubblicazione affidabile» è gratuita?
Sì — il testo completo di «Il pattern Outbox per una pubblicazione affidabile» è 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 RabbitMQ Messaging & Async Systems, passa a CoddyKit PRO. Il corso RabbitMQ Messaging & Async Systems include 4 lezioni in totale.
Cosa imparerò in «Il pattern Outbox per una pubblicazione affidabile»?
Impari a usare il pattern transactional outbox per eseguire il commit atomico dello stato applicativo e dei messaggi in uscita, eliminando il problema della doppia scrittura nei sistemi basati sugli… Eserciti RabbitMQ Messaging & Async Systems 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 RabbitMQ Messaging & Async Systems?
Non è richiesta alcuna esperienza precedente. RabbitMQ Messaging & Async Systems 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 una pubblicazione affidabile»?
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 RabbitMQ Messaging & Async Systems?
Sì. Ogni lezione RabbitMQ Messaging & Async Systems 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
- Idempotenza nell'elaborazione dei messaggi
- Pattern Saga con RabbitMQ
- Separazione delle responsabilità comando-query (CQRS)
- Il pattern Outbox per una pubblicazione affidabile