Skip to content

AxonOps — AI-Native Control Plane for Open Source Data Platforms

Kafka Design Patterns

This section covers architectural patterns commonly implemented with Kafka for building event-driven systems. These patterns address challenges in distributed systems including data consistency, service coordination, and system evolution.


PatternPurposeComplexityUse Case
Event SourcingStore state as event sequenceHighAudit trails, temporal queries
CQRSSeparate read/write modelsMediumRead-heavy workloads, complex queries
SagaDistributed transactionsHighMulti-service workflows
OutboxReliable event publishingMediumDatabase + event consistency
Event CollaborationService choreographyLowLoosely coupled services
MicroservicesKafka architecture for microservicesMediumTopic ownership, tracing, deployment
Shock AbsorberLoad leveling and bufferingLowTraffic spikes, legacy protection

Event sourcing is appropriate when:

  • Complete audit history is required
  • Temporal queries (“what was the state at time T?”) are needed
  • Event replay for debugging or recovery is valuable
  • Domain events are natural to the business model

Event sourcing adds complexity and should be avoided for simple CRUD applications.

CQRS is appropriate when:

  • Read and write workloads have different scaling requirements
  • Read models require denormalized views optimized for queries
  • Multiple read representations of the same data are needed
  • Write operations are complex but queries are simple (or vice versa)

CQRS adds operational complexity through eventual consistency.

Saga is appropriate when:

  • Business processes span multiple services
  • Distributed transactions (2PC) are not feasible
  • Compensating actions can undo partial failures
  • Long-running processes need coordination

Sagas require careful design of compensation logic.

Outbox is appropriate when:

  • Database changes and event publishing must be atomic
  • Exactly-once event publishing semantics are required
  • Dual-write problems must be avoided
  • Reliable event delivery without distributed transactions is needed

Outbox adds database polling or CDC infrastructure.


These patterns are often used together:

Common combinations of event sourcing, CQRS, saga, and outbox patternsCommon combinations of event sourcing, CQRS, saga, and outbox patternsCommon CombinationsEvent Sourcing + CQRS Events are the write modelProjections are the read modelSaga + Outbox Saga commands via outboxReliable step coordinationCQRS + Outbox Write to database + outboxProject to read models

The most common combination. Events serve as the write model while projections serve as read models:

  • Write side: Append events to event store (Kafka topic with compaction disabled)
  • Read side: Consume events and build optimized query models

Ensures reliable saga step execution:

  • Saga state changes written to database with outbox entries
  • Outbox publisher sends saga commands to Kafka
  • Guarantees saga progression even after failures

Combines database writes with reliable event publishing:

  • Write operations update database and outbox atomically
  • Outbox publisher sends change events to Kafka
  • Consumers build read models from events

All patterns require idempotent message handling:

PatternIdempotency Mechanism
Event SourcingEvent sequence numbers, deduplication by event ID
CQRSProjection offsets, idempotent projections
SagaSaga instance ID + step tracking
OutboxOutbox entry ID, consumer deduplication

Kafka provides ordering within partitions:

  • Use consistent partition keys for related events
  • Event sourcing: partition by aggregate ID
  • Saga: partition by saga instance ID
  • CQRS: partition by entity ID for ordered projections

Each pattern has specific error handling requirements:

PatternError Strategy
Event SourcingEvents are immutable; publish compensating events
CQRSRetry projection; rebuild from events if corrupted
SagaExecute compensation steps; track failed state
OutboxRetry publishing; dead letter for persistent failures

OptionDescriptionTrade-offs
Kafka (raw)Events in Kafka topicsSimple, requires careful retention config
Kafka + compactionLatest state per keyGood for CQRS, loses event history
Kafka + databaseEvents in bothFlexible querying, operational overhead
EventStoreDBPurpose-built event storeRich features, additional infrastructure
OptionDescriptionTrade-offs
Polling publisherPoll database for outbox entriesSimple, adds latency
CDC (Debezium)Stream database changesLower latency, more infrastructure
Transactional outboxDatabase triggersDatabase-specific, tight coupling

  • Event Sourcing - Storing state as a sequence of events
  • CQRS - Command Query Responsibility Segregation
  • Saga - Distributed transaction coordination
  • Outbox - Reliable event publishing with transactional outbox
  • Event Collaboration - Service choreography through events
  • Microservices - Kafka architecture patterns for microservices
  • Shock Absorber - Load leveling and traffic spike absorption