Explain the Transactional Outbox pattern and why it prevents dual-write bugs
Quick Summary: The Transactional Outbox pattern eliminates dual-write failures by inserting domain changes and an outbox_events row in one local ACID database transaction, then letting a separate relay publish events to Kafka asynchronously.
Direct Interview Answer
Calling db.save(order) followed by kafka.send(event) across two independent systems creates a classic dual-write bug: if the database commits and Kafka times out (or the pod crashes), downstream services never receive the event.
With the Transactional Outbox Pattern, you persist both the business entity and the serialized event row in an outbox_events table within the same local DB transaction. An async CDC connector (like Debezium) or polling relay then publishes pending outbox rows to the message broker at-least-once.
Detailed Technical Explanation
Because relays provide at-least-once delivery, downstream consumers must deduplicate using a unique processed_events(event_id) primary key constraint inside their own database transaction.
Practical Walkthrough
Single SQL transaction storing business order row and outbox payload atomically.
BEGIN;
INSERT INTO orders (id, customer_id, total_paise, status)
VALUES ('ord_901', 42, 5310000, 'CONFIRMED');
INSERT INTO outbox_events (id, aggregate_id, event_type, payload_json, published_at)
VALUES ('evt_771', 'ord_901', 'OrderConfirmed', '{"orderId":"ord_901","amountPaise":5310000}', NULL);
COMMIT;
Both order state and outbound event commit or roll back together atomically.
Real-World Production Use Case
Reliable payment confirmation and invoice-generation pipelines in fintech and SaaS backends where zero dropped domain events are tolerated.
Common Interview Mistakes to Avoid
- Opening a @Transactional block around kafka.send().get(), holding precious DB connections hostage waiting on network brokers.
- Forgetting consumer idempotency when using at-least-once outbox relays.