Kafka Anatomy

5 min read

Reading Progress0%
Streaming Systems Index
Streaming Systems Index

Kafka Anatomy

1. What Is It?

is a distributed, replicated, append-only commit log packaged as a message . Producers append events to topics; topics are split into partitions that live on brokers (the physical servers); consumers read at their own pace from offsets within partitions. Metadata coordination is handled by a quorum-based controller (KRaft in modern Kafka; ZooKeeper in older deployments).

The problem it solves: most messaging systems delete messages once consumed, so you can't add a new later, debug history, or replay after a bug. Kafka treats the log itself as the source of truth, retains messages for a configured window, and lets many independent groups read at independent positions. That single design choice — log as primitive — is why Kafka became the spine of event-driven backends.

QUICK CHECK

Your team deployed a bug that corrupted how certain user events were processed. You need to re-process all events from the past 48 hours using a corrected version of the consumer logic. Which characteristic of Kafka makes this possible without any special backup or export step?

Choose one answer

2. How It Works

The key components:

  • — a server process. A cluster is N brokers; production clusters are typically 3 to dozens.
  • — a named stream of messages, conceptually a category like orders or clicks.
  • — a single append-only log file (actually segment files on disk). A is split into P partitions; each lives on one (the leader) with replicas on other brokers.
  • — a monotonically increasing 64-bit integer assigned per-partition. position = (topic, partition, ).
  • — client library that appends to a partition (chosen by key hash or round-robin).
  • — client library that reads from one or more partitions in order.
  • — a set of consumers cooperating to share the partitions of subscribed topics.
  • Controller — one broker elected to manage cluster metadata (partition leaders, topic creation). In KRaft mode this is a Raft quorum of controller brokers; in ZooKeeper mode it's external.

Concrete example. You create topic payments with 12 partitions and factor 3 on a 5-broker cluster.

  • assigns 12 partition leaders spread across the 5 brokers (so each broker holds ~2–3 leaders).
  • Each partition has 3 replicas (1 leader + 2 followers) on 3 different brokers.
  • A sending payment_id=ABC hashes that key, gets partition 7, and writes to whatever broker currently leads partition 7.
  • A fraud-scoring with 6 instances ends up with each instance owning 2 partitions; another group analytics with 1 instance owns all 12 — independently.

3. What Mid-Senior SWEs Actually Need to Know

  • is the unit of parallelism, ordering, and assignment. You don't get more concurrency than partitions. You don't get ordering across partitions. Choose count carefully — easy to add, painful to reduce.
  • factor (RF) and min.insync.replicas (min ) together set durability. Common safe combo: RF=3, min.insync.replicas=2, acks=all. This survives one loss without data loss or unavailability for writes.
  • Brokers are stateful and disk-bound. Each holds the partitions assigned to it on local disk. Replacing a broker means streaming all its replica data from peers — costly above ~TB. Operationally, treat brokers more like databases than stateless services.
  • configs override broker defaults. Retention (retention.ms, retention.bytes), compaction (cleanup.policy), and can all be per-. Defaults bite you — set them per topic.
  • Controller is mission-critical metadata. In KRaft mode, the controller quorum is its own broker set (or co-located). Don't size it too small; outages here mean leader-election and topic-create operations stall.
  • Common useful CLI surface:
    • -topics.sh --describe --topic foo — shows partition leaders, replicas, .
    • --groups.sh --describe --group bar — current , end , lag. This is your primary operational signal.
    • kafka-configs.sh --entity-type topics --entity-name foo --describe — per-topic overrides.
  • Common misunderstanding: "Add more consumers to go faster." Only works up to the partition count. Beyond that, extra consumers sit idle.
QUICK CHECK

A Kafka topic has 6 partitions and your team spins up 10 consumer instances in the same consumer group to handle a traffic spike. What actually happens?

Choose one answer

4. Tradeoffs & Decisions

If you need...Choose...Tradeoff
Strict per-entity orderingPartition by entity key, fewer partitions per entityHot keys can overload a partition; less parallelism
Maximum throughputMore partitions (hundreds per topic)More metadata; rebalance time grows; harder to reduce later
Long history for replay / backfillLong retention (retention.ms = weeks/months)Disk cost, slower recovery on broker replacement
Compact "latest value per key" semantics (e.g. user profile)cleanup.policy=compact topicCosts background compaction; loses time-ordered history of intermediate values
Low operational footprintFewer, larger brokers with RF=3Larger blast radius if a broker dies

Key tradeoff: count. Too few → bottlenecked throughput and parallelism. Too many → metadata load on controller, slow rebalances, slow operations. Plan for 2–4× current peak throughput, not 100×.

tradeoff: acks=all, min.insync.replicas=2, RF=3 is the standard safe setting. Lowering min.insync.replicas to 1 increases availability during failover but exposes you to data loss; raising it to 3 makes any single outage block writes.

QUICK CHECK

A team is designing a Kafka topic to store user profile data, where only the most recent state of each user's profile matters and historical intermediate updates are not needed. They want to avoid unbounded disk growth over time. Which configuration best fits this use case, and what is the key tradeoff they accept?

Choose one answer

5. Interview & System Design Cheat Sheet

  • is a distributed, replicated, append-only log — that primitive (log, not queue) is what unlocks replay, multi- fan-out, and durable history.
  • The is the unit of ordering, , and assignment — almost every design question reduces to "how do you this?"
  • Durability is set jointly by RF, min.insync.replicas, and acks — never quote one without the others.
  • A can have at most partitions active consumers; you scale consumer parallelism by more, not by adding consumers.
  • Brokers are stateful, disk-heavy services — operationally closer to a database than to a stateless microservice.

Common follow-ups:

  • "What's the role of ZooKeeper / KRaft?" — Cluster metadata: which brokers are alive, who leads each partition, configs. KRaft replaces ZooKeeper with an internal Raft quorum for the same job.
  • "Why is factor usually 3?" — Survives one planned outage + one unplanned outage simultaneously while still maintaining a quorum of replicas. RF=2 + min.insync.replicas=2 blocks writes whenever any is down.
  • "How does Kafka know which leads a partition?" — The controller assigns leaders; clients fetch metadata from any broker (it returns the leader map) and then talk directly to the leader broker for that partition.

If asked to design X, anchor on this: Sketch the (s), the partition count, the partition key, the replication factor, and the consumer-group layout. That five-element sketch is a Kafka design.

QUICK CHECK

Your team's Kafka topic has 6 partitions and processes order events at peak load. The engineering lead suggests scaling throughput by adding 4 more consumers to the existing consumer group (bringing the total to 10), without changing the partition count. What is the actual outcome of this change?

Choose one answer