Skip to content
Mahesh Kadambala
Back to All Case Studies
BackendMaintained

Kafka Outbox

Distributed data consistency & zero-data-loss event messaging architecture

JavaSpring BootKafkaPostgreSQLDistributed SystemsReliabilityOpen Source

My Responsibility

Author & Architect

Engagement Timeline

2023

Project Artifacts

GitHub Repository

The Problem: The Silent Dual-Write Trap

In distributed microservices, state mutations in a local database (e.g., PostgreSQL) must often trigger downstream actions via a message broker (e.g., Apache Kafka or AWS SQS).

However, committing a database transaction and publishing a Kafka message are two independent network operations with two separate failure domains:

  1. Publish before DB commit: If the subsequent database commit fails or rolls back, your system has published a phantom event that never truly happened.
  2. Publish after DB commit: If the application crashes or the network drops immediately after the DB commit, the event is permanently lost, causing downstream services to silently drift out of synchronization.

The Architecture & Solution

To guarantee at-least-once message delivery without distributed two-phase commits (2PC), I implemented the Transactional Outbox Pattern with Idempotent Consumers:

[ Incoming Request ]
        │
        ▼
┌──────────────────────────────────────────────┐
│  Atomic Database Transaction                 │
│  ├── 1. Business State Mutation (INSERT/UPD) │
│  └── 2. Outbox Table Entry (INSERT event)    │
└──────────────────────────────────────────────┘
        │ (Transaction Committed)
        ▼
┌──────────────────────────────────────────────┐
│  Dedicated Polling Relay Engine              │
│  ├── 1. Polls pending outbox records         │
│  ├── 2. Publishes to Apache Kafka Topic      │
│  └── 3. Marks outbox record as PROCESSED     │
└──────────────────────────────────────────────┘
        │
        ▼
┌──────────────────────────────────────────────┐
│  Idempotent Consumer Microservice            │
│  ├── 1. Checks Processed Events Store        │
│  ├── 2. Executes business action if new      │
│  └── 3. Quarantines failures to Dead-Letter  │
└──────────────────────────────────────────────┘
Zero-Loss Event Messaging ArchitectureGuarantees atomic synchronization between transactional database writes and distributed event topics.Eliminates silent data discrepancies and ensures deterministic recovery during broker or network outages.

Technical Implementation & Failure Testing

  • Relay Publisher with Exponential Backoff: The relay polls the outbox table for unpublished events and publishes them in strict per-aggregate order. When no events are pending, it dynamically backs off to prevent unnecessary database load.
  • Consumer-Side Idempotency: Because delivery is at-least-once, consumers may receive duplicate messages during network retries. Every consumer verifies incoming event IDs against a dedicated processed-events index inside its local transaction, making duplicate executions a no-op.
  • Poison Message Quarantine (DLQ): Malformed or unprocessable payloads that exceed maximum retry thresholds are automatically routed to a Dead Letter Queue (DLQ) with contextual diagnostic metadata, preventing partition blockages.
  • Chaos Testing Harness: The repository includes automated failure-injection scripts that kill database connections, restart Kafka brokers, and terminate worker processes mid-transaction to verify zero data loss under adverse production conditions.

Measurable Results & Impact

  • Zero Lost Business Events: Verified through extensive chaos and automated fault-injection testing.
  • Standardized Enterprise Pattern: Adopted across multiple microservice domains as the foundational standard for inter-service communication.
  • Seamless Recovery: Systems resume event publishing automatically without manual operator intervention after network partitions or broker maintenance.

Building something similar for your product?

I help SaaS founders and engineering leaders design, scale, and modernize complex backend architectures. Let's discuss how to apply these patterns to your system.

Case Study published Oct 1, 2023