Skip to content
Kafka Is Fun

AdvancedArchitecture

How would you implement the Saga pattern using Kafka?

Reference answer

The Saga pattern is an application-level design pattern, not a Kafka feature; Kafka supplies the event log it can be built on. In a choreography-based saga each service: 1) consumes a domain event topic (for example OrderCreated), 2) performs its local transaction, 3) publishes a result event (for example PaymentCompleted or PaymentFailed). Because there is no distributed transaction, failures are handled with compensating transactions: when a step publishes a failure event, the services that already acted consume it and undo their changes with compensating actions. Practices that fit Kafka: use a transactional producer with read_committed consumers where a consume-transform-produce step must be atomic within Kafka (this does not cover a service's own database writes); make handlers idempotent using event IDs, because delivery is at-least-once by default; route messages that cannot be processed or compensated to a dead-letter topic; and Kafka Streams can keep saga state in a KTable backed by a state store and a changelog topic.

Expected key concepts: choreography, Saga, compensating transactions, domain event, local transaction, result event, idempotent, event IDs, dead-letter topic, transactional producer, KTable

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.