Quick revision
The short answer
In the transactional outbox pattern, the service writes the order and an outbox row describing OrderPlaced in the same database transaction, so either both exist or neither does. Debezium reads the outbox inserts from the database log and its outbox event router sends each one to a topic chosen by the aggregate type, keyed by the aggregate id so events for one order stay in order. Delivery is at least once, so consumers deduplicate by event id; the outbox table must be cleaned to avoid bloat, and the replication slot's lag must be monitored because an inactive slot keeps WAL on the primary.
What to remember
In this answer, we saw that writing to PostgreSQL and Kafka separately can lose or invent events, and that the transactional outbox fixes this by inserting the event into an outbox table in the same transaction as the order. Debezium streams the committed outbox inserts, and its outbox event router picks the topic from the aggregate type and uses the aggregate id as the key, preserving order per aggregate. We also covered the pitfalls: at-least-once delivery that requires idempotent consumers with event ids, cleanup of the outbox table, where the router ignores deletes, and monitoring of replication slot lag so an inactive slot does not fill the primary's disk.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In the transactional outbox pattern, the service writes the order and an outbox row describing OrderPlaced in the same database transaction, so either both exist or neither does. Debezium reads the outbox inserts from the database log and its outbox event router sends each one to a topic chosen by the aggregate type, keyed by the aggregate id so events for one order stay in order. Delivery is at least once, so consumers deduplicate by event id; the outbox table must be cleaned to avoid bloat, and the replication slot's lag must be monitored because an inactive slot keeps WAL on the primary.
Why this question is asked
Saving data and publishing an event about it sounds like two lines of code, and it is one of the classic traps in microservices. If the service commits to PostgreSQL and then sends to Kafka, a crash between the two loses the event; if it sends first, a failed commit invents an event for an order that does not exist. Interviewers ask about the outbox because it is the standard answer, and because the details, ordering, duplicates, cleanup and replication slots, separate people who have run it from those who have read about it. In this answer, we will see why the dual write fails, build the outbox with Spring and Debezium, configure the event router, and go through the pitfalls.
1Why Writing to Both Is Not Enough
PostgreSQL and Kafka are two separate systems, and there is no transaction that covers both. Whatever order we choose, there is a moment when one write has happened and the other has not.
Retrying the send or moving it after the commit only changes which of the two cases we get. The outbox removes the second system from the transaction: we write the event into the same database as the order, and something else moves it to Kafka afterwards.
2Writing the Order and the Outbox Row Together
The outbox is an ordinary table in the service's database. Each row is one event: a unique id, the aggregate type and id it belongs to, the event type and a JSON payload. The service inserts the order and the outbox row in one local transaction. Because PostgreSQL guarantees atomicity, either both rows commit or neither does, which is exactly the guarantee the dual write lacked.
7 messages in order. Play them, or step through them one at a time.
Notice that the service no longer talks to Kafka at all. Debezium reads the committed outbox inserts from the WAL, so only events of committed transactions are ever published, and none is lost, because the log is durable and the replication slot remembers what has been read.
3Configuring the Outbox Event Router
Debezium's raw CDC events for the outbox table would carry before and after images and source metadata, which is not what consumers want. The outbox event router, a single message transformation (SMT) that runs inside Kafka Connect, reshapes each outbox insert into a clean event: the payload becomes the message value, the aggregate id becomes the key, the event id and type become headers, and the topic is chosen from the aggregate type.
Using the aggregate id as the key matters for ordering. Kafka keeps order only within a partition, and all messages with the same key go to the same partition. So OrderPlaced, OrderPaid and OrderShipped for order 501 arrive in the order they were committed, while events for different orders are processed in parallel across partitions. The column names above follow the router's defaults; others can be mapped in its configuration.
4Pitfall 1: At-Least-Once Delivery
The outbox guarantees that no event is lost and none is invented, but not that each is delivered exactly once. If Debezium crashes after sending events and before storing its offset, it reads them again after restart and sends them again. Consumers can also receive a message twice after a rebalance. So consumers must be idempotent: they record the ids of events they have processed, in the same transaction as their own changes, and skip duplicates.
5Pitfall 2: The Outbox Table Grows Forever
Every order adds a row to the outbox, and nothing reads it after Debezium has captured it from the log. Left alone, it grows by millions of rows and slows down vacuum and backups. A neat trick is to delete the row in the same transaction right after inserting it. The insert still appears in the WAL, so Debezium captures it, and the delete that follows is ignored by the event router, so no extra message is produced. The table then stays almost empty. Alternatives are a scheduled job that deletes rows older than a few days, which keeps them for debugging, or partitioning the outbox by day and dropping old partitions, which avoids the cost of large deletes.
6Pitfall 3: Replication Slot Lag
Debezium reads through a replication slot, and PostgreSQL keeps all WAL that the slot has not confirmed. If the connector stops, for a failed deployment or a registry error, the slot becomes inactive and WAL piles up on the primary's disk. On a busy database that can be tens of gigabytes an hour, and a full disk stops the database, taking the whole service down because of a side channel.
So we monitor the slot: an alert when it is inactive for more than a few minutes and when retained WAL passes a threshold. Since PostgreSQL 13, max_slot_wal_keep_size caps how much WAL a slot can hold; past that, the slot is invalidated, which protects the primary but means a new snapshot and possibly missed events, so it is a last line of defence, not a substitute for alerts.
- Is the slot active?YesCheck retained WALNoAlert: connector down
- Retained WAL above threshold?YesAlert: consumer laggingNoHealthy
- Outbox row count growing?YesCheck cleanup jobNoCleanup working
3 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Why not use Kafka transactions instead of an outbox?
Kafka transactions make writes to several Kafka partitions atomic, and with exactly-once processing they cover consume-transform-produce within Kafka. They cannot include a PostgreSQL commit, so the gap between the database and Kafka remains. The outbox is needed whenever the source of truth is a database.
Could we poll the outbox table instead of using Debezium?
Yes, a scheduled job can select unsent rows in id order, publish them and mark them sent. It is simpler to run, but adds polling load and latency, and getting ordering right with concurrent transactions is tricky, because ids are assigned before commit. Log-based capture delivers in commit order without these problems.
Does the outbox guarantee global ordering of all events?
No. It guarantees order per aggregate, because events for one aggregate share a key and partition. Events for different aggregates can be consumed in any relative order, so consumers must not rely on cross-aggregate ordering.
8Conclusion
In this answer, we saw that writing to PostgreSQL and Kafka separately can lose or invent events, and that the transactional outbox fixes this by inserting the event into an outbox table in the same transaction as the order. Debezium streams the committed outbox inserts, and its outbox event router picks the topic from the aggregate type and uses the aggregate id as the key, preserving order per aggregate. We also covered the pitfalls: at-least-once delivery that requires idempotent consumers with event ids, cleanup of the outbox table, where the router ignores deletes, and monitoring of replication slot lag so an inactive slot does not fill the primary's disk.
Open with
The problem is the dual write: PostgreSQL and Kafka have no shared transaction, so if I save and then send, a crash can lose the event, and if I send first, a failed commit invents one.
Then, if they want more
With the outbox, the service writes the order and an outbox row, with an event id, aggregate type, aggregate id, event type and JSON payload, in the same local transaction, so both commit or neither does. The service never talks to Kafka.
Debezium reads the committed outbox inserts from the WAL through a replication slot, and the outbox event router SMT turns each row into a clean message: topic from the aggregate type, key from the aggregate id, so all events for one order land in one partition in commit order, and the event id goes into a header.
The pitfalls: delivery is at least once, because Debezium can resend after a crash, so consumers store processed event ids in the same transaction as their work and skip duplicates.
The outbox table grows forever unless I clean it; a nice trick is deleting the row right after inserting it, since the insert is still in the WAL and the router ignores deletes, or I partition by day and drop partitions.
And I monitor the replication slot, because if the connector stops, the slot keeps WAL on the primary until the disk fills.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 13
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A service writes an order to PostgreSQL and must publish an OrderPlaced event to Kafka...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/62d404821c/