Skip to content

The Kafka Design Pattern That Changed the Game

We treated Kafka as just a message queue until duplicate messages and out-of-sync services forced a rethink. How the Outbox Pattern gave us atomic, replayable, debuggable events.

Ivan Jordan

Co-Founder, CodingBand

Follow on LinkedIn

3 min read

A database with an arrow pointing to the Kafka logo, under the title The Kafka Design Pattern that changed the Game

When we initially adopted Kafka for a project, we treated it as just a message queue. Producers would send events, consumers would pick them up, and everything seemed fine, until it wasn't.

We started encountering issues we couldn't explain: duplicate messages, microservices going out of sync, and strange bugs that only appeared under load. For a time, we dismissed it as growing pains. But the reality was simpler:

We were using Kafka incorrectly. And then we discovered the Outbox Pattern.

The Outbox Pattern: a command reaches a microservice, which writes state and an outbox table in the same database; a delivery process reads the outbox and publishes to Apache Kafka

The struggles before the Outbox

Our architecture resembled many modern distributed systems:

  • Microservices supported by databases
  • Kafka topics for event-driven communication
  • Services publishing events for key actions — like a user signing up or an order being placed

Sounds good, right?

The issue, however, was data consistency.

Picture this: a service processes a request, writes to its database, and then publishes an event to Kafka. Two operations. Two systems. But what happens if the first succeeds, but the second fails?

You end up with a situation where:

  • The database confirms an order was placed
  • But downstream services remain unaware of it

This creates fragmented systems and elusive bugs.

The Outbox Pattern, explained

The Outbox Pattern changed everything for us. Here's the idea:

Instead of writing to the database and Kafka separately, we:

  1. Write the event to an "outbox" table inside the same database transaction that updates business data
  2. A separate Kafka producer reads from the outbox table and publishes the events to Kafka

This guarantees atomicity: either both the database change and the event are recorded — or neither are.

No more partial failures. No more lost events.

Why it's so effective

Let's dive into why this pattern is such a game-changer.

Transactional guarantees. Since the business operation and the event are stored within the same transaction, they're guaranteed to occur together.

Replayability. If a Kafka publish fails, no problem. The event remains in the outbox table, and the relay process will keep retrying until it succeeds.

Debuggability. You can check the outbox table to see exactly which events were meant to be published. No more guesswork or black-box issues.

No distributed transactions. This approach bypasses the complexity of two-phase commits or XA transactions between Kafka and your database.

What it took to implement

We made some practical choices:

  • Added an outbox_events table to each service database with fields like id, aggregate_type, event_type, payload, timestamp and published
  • Built a small background worker that scans the table and publishes new rows to Kafka
  • Used Kafka producers with idempotent writes enabled for safety

Later, we optimized with:

  • Change Data Capture (CDC) tools like Debezium to auto-publish DB changes to Kafka
  • Kafka compacted topics to store only the latest version of each aggregate, ideal for syncing downstream systems

Conclusion

Kafka is incredibly powerful, but raw power isn't enough. Without the proper patterns in place, it's easy to stumble into subtle, frustrating pitfalls.

The Outbox Pattern provided us with correctness, resilience, and clarity. When paired with compacted topics, it brought us closer to the ideal of clean, real-time data flow across independent services.

If you're building event-driven systems and facing strange consistency issues, consider this your signal!

This Kafka design pattern was a game changer!

Originally published on LinkedIn.

Written by

Ivan Jordan

Co-Founder, CodingBand

Ivan writes about engineering leadership, delivery, architecture, and AI-enabled development — drawn from building products and teams at CodingBand.

Follow on LinkedIn

More from the blog

  • A solid block of modules beside a tangled network of connected nodes, under the title Microservices vs. Monoliths

    Architecture

    Microservices vs. Monoliths: Why Simpler Architecture Might Be Your Competitive Advantage

    The pendulum is swinging back. What Amazon Prime Video, Segment, Shopify, Stack Overflow and 37signals learned about distributed systems, and how to decide which architecture gives your company an edge.

    Ivan Jordan14 min read

  • A hard hat surrounded by a winding road, under the title Roadmap of a Software Architect

    Architecture

    Roadmap of a Software Architect

    Software architects are senior actors in a development team, and it takes time to become one. The types of architects, and ten areas to master on the way there.

    Ivan Jordan3 min read

  • Dollar bills flying into a cloud with an upward arrow, under the title FinOps: The Secret to Smarter Cloud Spending

    Cloud & FinOps

    FinOps: The Secret to Smarter Cloud Spending

    Moving to the cloud doesn't automatically mean saving money. How FinOps brings engineering, finance and business together to cut waste and make every cloud dollar work harder.

    Ivan Jordan7 min read

Get started

Contact us