Quick revision
The short answer
A polling relay selects unsent outbox rows in id order, publishes them and marks them sent; it is simple but adds query load and latency, and it needs a single active relay, or work split by aggregate, because SKIP LOCKED with several relays can reorder events. A CDC relay with Debezium reads the database log in commit order with low latency and no polling, at the cost of running Kafka Connect and Debezium. Either way we use the aggregate id as the Kafka key so each aggregate's events stay in one partition in order, delete or drop old rows on a schedule, which with CDC can be right after insert, and monitor the age of the oldest unpublished row as the key health metric.
What to remember
In this answer, we designed an outbox relay. Polling selects unpublished rows in order, publishes them with the aggregate id as key and marks them after the acknowledgements, and needs a single active relay or work split by aggregate because SKIP LOCKED with two relays can reorder events. CDC with Debezium reads the log in commit order with low latency but adds infrastructure and replication-slot risk. We kept the table small by deleting or partitioning, or deleting right after insert with CDC, and used the age of the oldest unpublished row as the key metric.
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.
A polling relay selects unsent outbox rows in id order, publishes them and marks them sent; it is simple but adds query load and latency, and it needs a single active relay, or work split by aggregate, because SKIP LOCKED with several relays can reorder events. A CDC relay with Debezium reads the database log in commit order with low latency and no polling, at the cost of running Kafka Connect and Debezium. Either way we use the aggregate id as the Kafka key so each aggregate's events stay in one partition in order, delete or drop old rows on a schedule, which with CDC can be right after insert, and monitor the age of the oldest unpublished row as the key health metric.
Why this question is asked
The outbox pattern is easy to describe and surprisingly easy to get subtly wrong at high volume. A relay that is too slow builds a backlog, one that runs twice publishes events out of order, and a table that nobody cleans grows until the inserts slow down the business transactions. Interviewers ask this to see whether we can design the part of the outbox that is usually left as an exercise. In this answer, we will recap the relay's job, design a polling relay, answer what happens with two relay instances, design the CDC alternative, then handle ordering, table growth and monitoring.
1A Quick Recap of the Relay's Job
In the transactional outbox, a service writes each event as a row in an outbox table in the same transaction as its business change. The relay is the separate process that takes committed rows and publishes them to Kafka. It has three duties: publish every row at least once, publish each aggregate's events in order (an aggregate being the business entity the events are about, such as one order), and keep the table from growing forever.
2Design 1: A Polling Relay
A polling relay runs a loop: fetch a batch of unpublished rows in id order, send them to Kafka, wait for the acknowledgements, and mark them published in one update.
The producer runs with enable.idempotence=true, the default since Kafka 3.0, so retries inside the producer do not duplicate or reorder records within a partition. A partial index on rows where published_at is null keeps the fetch fast however big the table gets. With a 100 millisecond poll interval, typical latency is a few hundred milliseconds, and batches of 500 keep up with thousands of rows per second.
3What Happens to Ordering if Two Relay Instances Run at Once?
For availability we may want two relay instances. The tempting tool is SELECT ... FOR UPDATE SKIP LOCKED: each instance locks the rows it takes, and the other skips them, so no row is sent twice at the same moment. But ordering breaks. Relay A takes rows 1 to 500, which include OrderCreated for order 42 at row 480. Relay B takes rows 501 to 1000, including OrderPaid for order 42 at row 510. If B's send finishes first, OrderPaid reaches the partition before OrderCreated, and the consumer sees a payment for an order it has never heard of.
5 messages in order. Play them, or step through them one at a time.
Each row is sent once, but one order's events arrive out of order.
There are two fixes. The simpler is a single active relay: all instances run, but only the one holding a lock publishes, for example a PostgreSQL advisory lock taken with pg_try_advisory_lock, or Spring Integration's leader election. The others take over within seconds if the leader dies. The other fix is to split the work by aggregate rather than by row: instance i only publishes rows where hash(aggregate_id) mod N equals i, so each aggregate always goes through one relay in order. That scales further but needs coordination when N changes.
4Design 2: Change Data Capture With Debezium
CDC avoids polling altogether. Debezium runs as a Kafka Connect source connector and reads the database's write-ahead log through a logical replication slot. Every committed insert into the outbox appears in the log, in commit order, and Debezium turns it into a Kafka record. Its outbox event router, a Kafka Connect transform, routes each row to a topic based on a column such as the aggregate type, and uses the aggregate id column as the record key.
The benefits are low latency, often tens of milliseconds, and no query load on the database, since reading the log is cheap. The costs are operational: Kafka Connect and Debezium to run, upgrade and monitor, and a replication slot that makes PostgreSQL keep the log until Debezium has read it. If the connector stops for a day, the database keeps a day of log on disk, which can fill it.
5Keeping Per-Aggregate Order
Ordering in Kafka is guaranteed only within a partition, so every event for one aggregate must go to the same partition. Using the aggregate id as the record key does exactly that, since the default partitioner hashes the key. The relay must then publish each aggregate's rows in the order they were committed.
There is a catch with id order in polling relays: ids from a sequence are assigned when a row is inserted, not when its transaction commits, so a row with id 100 can become visible after row 101. That is why the polling query looks for unpublished rows rather than rows above a last-seen id, which could skip 100 forever. For one aggregate, the business transaction normally updates the aggregate's row first, which locks it, and then inserts the outbox row; a second transaction on the same order waits for that lock, so its outbox id is both larger and committed later. CDC sidesteps the issue by reading in commit order.
6Stopping the Table Growing, and Monitoring It
Published rows have no further use, so we remove them. With polling, a scheduled job deletes published rows older than, say, a day, in batches of a few thousand to avoid long locks. At very high volume, partitioning the table by day and dropping whole old partitions is far cheaper than deleting rows. With CDC, the relay never queries the table, so a service can insert the outbox row and delete it in the same transaction: the insert is still in the log for Debezium, and the table stays empty.
The one metric that summarises the relay's health is the age of the oldest unpublished row, now() minus the smallest created_at where published_at is null, or for CDC the connector's lag behind the log. A healthy relay keeps it under a second or two; an alert at, say, 60 seconds catches a stuck relay long before consumers notice.
7Follow-up questions
What happens to ordering if two relay instances run at once?
With plain polling, both may send the same rows, giving duplicates and possible reordering. With SKIP LOCKED, each row is sent once, but rows of one aggregate can be taken by different instances and arrive out of order, as with OrderPaid before OrderCreated. Use a single active relay with a lock or leader election, or split the work so each aggregate always goes through the same instance.
Why wait for Kafka acknowledgements before marking rows published?
Because marking first and then failing to send would lose events, which is exactly what the outbox exists to prevent. Waiting for the acks means the worst case is sending a batch twice after a crash, which idempotent consumers handle.
8Conclusion
In this answer, we designed an outbox relay. Polling selects unpublished rows in order, publishes them with the aggregate id as key and marks them after the acknowledgements, and needs a single active relay or work split by aggregate because SKIP LOCKED with two relays can reorder events. CDC with Debezium reads the log in commit order with low latency but adds infrastructure and replication-slot risk. We kept the table small by deleting or partitioning, or deleting right after insert with CDC, and used the age of the oldest unpublished row as the key metric.
Open with
I'd consider two designs. A polling relay fetches unpublished rows ordered by id in batches, say five hundred, sends them to Kafka with the aggregate id as key and idempotence on, waits for all acks, then marks them published.
Then, if they want more
A crash between sending and marking means resending the batch, so consumers must be idempotent. A partial index on unpublished rows keeps the query fast. The trap is running two relays.
With SKIP LOCKED each row goes out once, but two events for the same order can be picked up by different instances, and OrderPaid can reach the partition before OrderCreated. So I'd run one active relay, using an advisory lock or leader election with a standby, or shard the work by hash of aggregate id.
I'd also query for unpublished rows rather than ids above a high-water mark, because sequence ids aren't assigned in commit order. The CDC design uses Debezium on Kafka Connect, reading the write-ahead log in commit order with the outbox event router keyed by aggregate id.
It's lower latency and adds no query load, but it's more infrastructure, and a stopped connector makes the database keep log on disk. For growth, I'd delete published rows in batches or drop daily partitions, and with CDC delete right after insert. The metric I'd alert on is the age of the oldest unpublished row, or the connector's lag.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Kafka 3.0
- 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
- Apache Kafka documentation, Spring for Apache Kafka reference
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 "Design an outbox relay for a high-volume service. Compare polling the outbox table with...": https://scrivio-interview-library.netlify.app/spring-boot/event-driven-patterns/51a2c86607/