System Design AI

Kafka in System Design Interviews: When to Use It and What to Say

Kafka is a distributed, partitioned commit log that stores streams of records in immutable order. In an interview you can say it "provides a durable, ordered, replayable event stream that decouples producers from consumers".

The Problem Kafka Solves

Many modern services need to ingest a high volume of events, guarantee they are not lost, and allow multiple downstream systems to process the same data at their own pace. A classic example is an e-commerce site that records every order, inventory change, and user click. Without a reliable backbone, the order service might block the inventory service, and a downstream analytics pipeline could miss events if it is temporarily down.

How Kafka Works: Step-by-Step

  1. Producers write to a topic – Each topic is split into partitions. The producer picks a partition based on a key (e.g., order ID) to preserve ordering for that key.
  2. Brokers append the record – The chosen broker writes the record to the end of the partition log and replicates it to follower brokers for durability.
  3. Offsets are assigned – Each record gets a sequential offset within its partition. Consumers use offsets to track progress.
  4. Consumers read from the log – A consumer group consists of one or more instances. Kafka assigns each partition to only one member of the group, so each event is delivered to one member of the group (at least once by default).
  5. Retention policy – The log is retained based on time or size. Older records are eventually discarded, but they can be replayed as long as they are within the retention window.

Small Worked Example

Imagine a "click-stream" pipeline:

  • Producer: Front-end JavaScript sends click events to a Kafka producer service.
  • Topic: clicks with 3 partitions, keyed by user ID.
  • Broker: Each partition lives on three brokers (replication factor 3). The leader broker for a partition writes the record and replicates to the two followers.
  • Consumer Group A: Real-time analytics service with 2 instances. Partition 0 → instance A1, partition 1 → A2, partition 2 → A1 (round-robin).
  • Consumer Group B: Offline batch job that reprocesses the entire day’s clicks for training a recommendation model. Each day it seeks to the first offset at or after midnight (Kafka can look up offsets by timestamp) and reads to the end of the day, independent of Group A.

The diagram would show one write path and two independent read paths: click → producer → partition leader → followers; then consumer group A (A1/A2) → analytics, and separately consumer group B → batch job.

Where Kafka Shows Up in Classic Design Questions

Design QuestionWhere Kafka AppearsReason for Using Kafka
Real-time analytics pipeline (e.g., page-view stats)Between the event collector and the analytics serviceProvides ordered, durable stream and fan-out to multiple analytics workers
Order processing system with inventory syncBetween order service and inventory serviceGuarantees that inventory updates are not lost even if the order service crashes
Log aggregation / monitoring platformIngest logs from many servers into a central processing clusterHandles high write throughput and allows multiple consumers (alerting, storage, UI)

In each case, Kafka sits at the boundary where you need to decouple producers from multiple downstream consumers while preserving order for a given key.

Trade-offs and the Alternative Interviewer May Probe

AspectKafkaTypical Alternative (e.g., HTTP queue, RabbitMQ)
DurabilityRecords are replicated; with acks=all and min.insync.replicas=2, an acknowledged record is lost only if every in-sync replica holding it fails.Depends on implementation; often in-memory or single-node persistence.
OrderingGuarantees order per partition/key.May provide per-queue ordering but not across multiple queues.
ScalabilityHorizontal scaling by adding partitions and brokers; can handle millions of messages per second.Scaling may require sharding queues manually; throughput limits are lower.
LatencyTypically a few milliseconds to tens of ms; can increase with higher replication factor or large batch sizes.Lower latency for single-hop HTTP, but lacks durability and replay.
ComplexityRequires managing brokers, topics, partitions, and consumer offsets.Simpler to set up; fewer moving parts.

Interviewers often ask: "What if we need sub-second latency for critical alerts?" You can answer that a direct HTTP push may be faster, but you can still use Kafka with linger.ms=0 and acks=1 (the leader acknowledges before followers copy the record), accepting a higher risk of losing a record if the leader fails.

30-Second Pitch

"Kafka is a distributed log that lets producers write events to a durable, ordered stream, and lets multiple consumer groups read those events at their own pace. It solves the problem of reliable, replayable event ingestion for high-throughput systems like real-time analytics or order processing. The key ideas are partitions for ordering, replication for durability, and consumer offsets for independent progress. Trade-offs include higher latency than a direct HTTP call and operational complexity, but it scales to millions of messages per second and decouples services cleanly."

Common Mistakes Candidates Make

  • Treating Kafka as a database – Kafka stores streams, not arbitrary queryable data. Mention that you would still need a downstream store for ad-hoc queries.
  • Ignoring consumer groups – Claiming “Kafka guarantees exactly-once processing” without clarifying that exactly-once semantics require idempotent downstream writes and proper transaction configuration.
  • Over-partitioning – Adding too many partitions can increase overhead and cause small-batch latency spikes. Explain that the number of partitions should match the expected parallelism.
  • Skipping retention considerations – Assuming records are kept forever. Note that retention is time- or size-based, and you must size it for replay needs.
  • Forgetting about ordering scope – Saying “Kafka preserves global order” – it only guarantees order within a partition.

How to Practice This

  1. Read the free worked design, Design a Distributed Log like Kafka, which builds up Kafka itself one request path at a time.
  2. Draw your own design on paper or a whiteboard for a new problem—e.g., a video-upload service that needs transcoding and notification. Place Kafka between the upload service and the transcoding workers.
  3. Check your diagram: review your placement of Kafka, the number of partitions you chose, and how you handle consumer offsets. With Pro ($10/month), the Coach can review your canvas against the brief's rubric.

Questions people ask

When should I bring up Kafka in a design interview?

Mention Kafka when the problem needs high-volume event ingestion, durable replay, or fan-out to multiple downstream services. It signals you understand decoupling and stream processing.

What is the difference between a Kafka topic and a queue?

A topic is an immutable log retained for a configurable period, allowing many consumer groups to read the same data independently. A queue usually holds a single copy of each message and deletes it after a consumer acknowledges it.

How does Kafka achieve durability?

Each partition is replicated across several brokers. The leader broker writes the record and forwards it to follower brokers. A record is committed once every replica in the in-sync set has it; with acks=all the producer is acknowledged only then, and min.insync.replicas sets how small that set may shrink before writes are refused.

Can Kafka guarantee exactly-once processing?

Kafka provides at-least-once delivery by default. Exactly-once semantics are possible but require idempotent downstream writes and the use of Kafka transactions on the producer side.