Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

The Order That Shipped Twice: Fixing a Dual-Write Bug With Kafka and the Outbox Pattern

Learn to build a production-grade order pipeline that never loses or duplicates an event, using the transactional outbox and Kafka idempotent producers.

MCB Editorial

Tech Buzz Daily Desk · 11 min read ·

New story
Narrated by Study Buddy Narration
Voiceover Engine • Interactive Audio Sync
0:0011:00

Learning Objectives & Key Takeaways

0 / 4 Completed
Architecture Ready
100%
What you will learn
  1. A dual write to the database and Kafka in two separate steps can silently create duplicate or lost orders.
  2. The outbox pattern writes the business row and the event row in one database transaction to close that gap.
  3. Kafka producer idempotence plus transactional.id prevents retries from creating duplicate records in the log.
  4. Consumers must still be idempotent because the outbox publisher only guarantees at-least-once delivery.
  5. isolation.level=read_committed is required for consumers to get true end-to-end transactional visibility.

3 AM: The Order That Paid Twice

The on-call engineer at a mid-size e-commerce company got paged because a customer complained their card was charged twice for a single order. The order service had written the order row to Postgres, then made a separate call to publish an OrderPlaced event to Kafka. The database write succeeded, but the Kafka publish call timed out on the network, so the service retried the entire handler, which inserted the order again under a new ID and fired a second event to the payment service.

This is the dual-write problem in its purest form. Any time a service writes to two independent systems, a database and a broker, there is a window between the two writes where a crash, timeout, or restart leaves them inconsistent. No amount of retry logic on the API handler fixes this, because the retry itself cannot tell whether the first write succeeded.

In the rest of this story
  1. The Transactional Outbox: One Commit, Two Rows
  2. Publishing With a Transactional Kafka Producer
  3. Configuring Brokers and Consumers for Real Transactions
  4. Why the Consumer Still Needs Its Own Idempotency
2 code examples · 11 min read in full
Member-only story

Checking your membership…

Members read every Tech Buzz story in full, with code examples, audio and the discussion, plus every course, quiz and interview set in the Learning Dashboard.

0

Written by

MCB Editorial

Tech Buzz Daily Desk. Contributing deep-dive engineering architectures, cloud paradigms, and system design insights to Master Class Buddy.