In this lesson · topic 6 of 7
Topic 6
Publishing without losing the event
7 min read
The producer side has the same problem
The rest of the lesson looks at the consumer. The producer has its own gap. A service accepts an order. It must save the order row, and it must publish an “order placed” event so other services can react. Those are two systems: a database and a broker. No transaction covers both.
Do the database write first, then publish. If the process dies in between, the order exists and the event never goes out. Publish first, then write, and a crash leaves an event for an order that was never saved. Retrying the publish does not remove the gap, because the crash can happen before the retry. This is the consumer’s ack problem again, one step earlier.
Make it one write
The transactional outbox removes the second system from the critical path. The service writes the order row and an event row into an outbox table in the same database transaction. Either both commit or neither does. The database now holds the truth: the order and a record that its event is waiting to be sent.
A separate relay reads unsent rows from the outbox and publishes them to the broker. After a successful publish, it marks the row as sent or deletes it. The relay can be a polling loop, or it can read the database’s change log, which is how change-data-capture tools do it.
Now follow a crash. If the service dies after the commit, the outbox row is still there and the relay publishes it. If the relay dies after publishing and before marking the row, the next run publishes it again. The pattern gives you at-least-once publishing. It cannot give exactly once, since the relay faces the same gap between the publish and the mark.
Predict first
Duplicates are handled at the consumer
Give each outbox row a unique id and put it in the event. Consumers use it as the idempotency key from the earlier topics. The same id is stable across every relay retry, which is the condition for a key to work.
Some brokers can help a little. Kafka’s idempotent producer removes duplicates caused by the producer’s own retries within a session, using a producer id and sequence numbers per partition. It does not cover a relay that restarts with no memory of what it sent, so the consumer-side check is still needed.
Worked numbers
An order service handles 200 orders a second, and the relay polls every 100 ms, reading up to 500 rows. Each poll finds about 20 rows. Median added delay is about 50 ms, half the polling interval, and the worst case is about 100 ms plus the publish time. If the relay is down for a minute, 12,000 rows pile up, and when it returns, 500 rows a poll clears them in 24 polls, a little over 2 seconds at the same interval. The table is the buffer, and its size is bounded only by disk.
Rows should be removed or archived once sent. A table that grows forever makes the polling query slower, and it is the most common operational complaint with the pattern.
Sources
- Chris Richardson, Transactional outbox, microservices.io.
- Martin Kleppmann, Designing Data-Intensive Applications, O’Reilly, 2017. The chapters on stream processing and change data capture.
- Apache Kafka documentation, Message delivery semantics.