Design considerations for Kafka in a microservices architecture.

Instruction: Discuss how Kafka can be leveraged in a microservices architecture, including service communication, event sourcing, and CQRS.

Context: This question evaluates the candidate's understanding of microservices patterns and their ability to architect Kafka as the central event streaming backbone.

Updated

Official answer

Kafka can carry asynchronous domain events between services while allowing independent consumers to replay retained data. Producers and consumers remain coupled through schemas and business meaning, even when they do not call each other synchronously.

For an order service, commit the order and an outbox event in one database transaction, then publish that event through a retryable relay or CDC process. A database write followed by a separate Kafka send can leave either a missing event or a duplicate on recovery. Give events stable IDs and make downstream effects idempotent; acknowledge input progress only after the intended effect is durable.

Event sourcing means an authoritative history of accepted business changes, not merely putting copies of database updates into a topic. Decide who validates commands and expected entity versions, how events are ordered, how schemas evolve, and how history is retained/backed up. Kafka retention or compaction can remove events needed for a full reconstruction. A Kafka key does not enforce an application-level expected-version condition by itself.

CQRS separates the write model from read projections. Consumers can build search or reporting views, but those views are normally eventually consistent. Expose freshness/version information when read-after-write matters and define rebuild and correction procedures.

Test duplicate delivery, out-of-order business events, poison records, database/relay crashes and replay against external effects. Use synchronous communication where the workflow requires an immediate authoritative answer; Kafka does not remove distributed consistency tradeoffs.

Kafka delivery semantics, Debezium outbox pattern

Related Questions