Designing a Kafka System: Capacity, Topics and Trade-Offs
Senior level ยท The questions to ask before drawing a single box, and the numbers that defend the design.
Prerequisites
- !Kafka partitioning and consumer groups
- !Replication and ISR behavior
What You'll Learn
- 1Work a design prompt through requirement, capacity, topology and failure
- 2Size partitions and clusters from measured throughput rather than defaults
- 3Defend retention, key and ordering choices under questioning
- 4Recognize the four designs that always fail review
Start with requirements, not brokers
Design interviews reward the candidate who asks for numbers before drawing topology. Ingest rate in records per second and bytes per second, peak versus average, retention requirement in days, whether ordering per key is contractual, acceptable latency for producers and consumers, and whether any consumer may process records out of order. Those six answers determine almost every subsequent choice, and without them a design is decoration.
| Question | Why it changes the design |
|---|---|
| Records per second and average bytes | Broker count and disk sizing |
| Ordering requirement | Key choice and partition count |
| Retention in days | Topic config and storage tier |
| Consumer latency budget | Batching, acks, consumer count |
| Multi-region requirement | Cluster topology and mirror layer |
| Failure tolerance | Replication factor and min.insync.replicas |
Ask for the peak, not the average. A topic that averages forty megabytes per second but peaks at three hundred during a nightly backfill needs the peak provisioned, because that is when the ISR shrinks and the alerts fire.
Sizing partitions from throughput
# Rule of thumb: a partition handles roughly ten to thirty megabytes per
# second of ingest on modest hardware. Measure with kafka-producer-perf-test.
kafka-producer-perf-test.sh --topic sizing-probe \
--num-records 500000 --record-size 1024 --throughput -1 \
--producer-props bootstrap.servers=kafka1:9092 linger.ms=20 \
batch.size=262144 compression.type=lz4
# Then check consumer side: one consumer thread per partition, so
# partitions must be at least the desired consumer parallelism
kafka-topics.sh --bootstrap-server kafka1:9092 \
--describe --topic orders --under-replicated-partitions
The two constraints pull in opposite directions. Throughput wants enough partitions that no single leader is a bottleneck; consumer parallelism wants few enough that one group can rebalance in seconds rather than minutes. A topic with two hundred partitions and a group that must rebalance on every rolling restart will spend more time rebalancing than processing, so prefer one hundred partitions with wider consumers over four hundred with narrow ones unless the measured ingest demands it.
Partitions are not free to change. Increasing them is safe for throughput but breaks the key-to-partition mapping and therefore per-key ordering; every existing key may land on a new partition. Treat initial partition count as a decision you defend with a measurement, not a number you can raise later without an application change.
Cluster and broker topology
# Broker defaults worth stating in a design, with the reason
num.partitions=6
default.replication.factor=3
min.insync.replicas=2
unclean.leader.election.enable=false
log.retention.hours=168
log.segment.bytes=1073741824
offsets.retention.minutes=10080
unclean.leader.election.enable=false is the setting that keeps data loss out of a failure. With it true, a partition whose entire ISR has died elects an out-of-sync replica and accepts writes that were never acknowledged to producers elsewhere, which converts a broker outage into silent data loss. Leaving it false means a partition may be unavailable until a replica catches up, and unavailability is a better trade than corruption for almost every workload.
Replication factor three with min.insync.replicas two and acks=all is the default you should be able to defend without notes: two replicas must confirm a write, so one node can fail without pausing producers, and a second failure makes the partition unavailable rather than under-acknowledged.
Topic design and the key question
# One topic per domain event, key by the aggregate, compact where state matters
kafka-topics.sh --bootstrap-server kafka1:9092 --create \
--topic orders.created --partitions 24 --replication-factor 3 \
--config retention.ms=604800000 \
--config segment.ms=3600000 \
--config min.insync.replicas=2
# Compacted state topic
kafka-topics.sh --bootstrap-server kafka1:9092 --create \
--topic orders.state --partitions 12 --replication-factor 3 \
--config cleanup.policy=compact \
--config min.cleanable.dirty.ratio=0.1 \
--config segment.ms=604800
Key by the natural aggregate id so every change to one order lands on the same partition and preserves order for consumers that care. A null key scatters records and is only acceptable when the consumer explicitly does not need per-entity ordering, which for an audit trail it almost always does.
Compaction is for state, not for history. A compacted topic keeps the latest value per key and retains tombstones long enough for consumers to learn about deletes, which is what makes it a viable backing store for a materialized view. Compaction never replaces retention on an event topic, because the events themselves are the product there.
๐กState the failure you are designing against
Consumer scale-out and lag management
Scale consumers by partitions, not by threads. A group with more members than partitions assigns idle members that do nothing but inflate rebalance time. When one consumer is the bottleneck and partitions allow it, add members; when partitions do not allow it, the topic needs more partitions and the key mapping must tolerate the change.
// Static membership avoids a full rebalance on rolling restarts
props.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, "order-processor-" + podName);
props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
CooperativeStickyAssignor.class.getName());
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 500);
Static membership plus cooperative sticky assignment turns a rolling restart into a partial rebalance that moves one partition at a time. Without them, every restart triggers a stop-the-world rebalance that can take minutes on a large group and looks like an outage to downstream latency monitors.
Four designs that always fail review
# 1. Single broker with replication.factor=1 and a durability requirement
# 2. acks=1 or acks=0 on a financial event topic
# 3. One topic with a null key carrying every event type
# 4. No retention or cleanup.policy, so the disk fills eventually
Each of these is a missing decision rather than a wrong one. The first ignores that the stated requirement includes surviving a node loss. The second accepts silent producer-acknowledged loss. The third makes ordering undefined and consumers responsible for filtering. The fourth converts a capacity planning problem into a midnight page. Naming them out loud in an interview, before being asked, signals the experience the interviewer is looking for.
Defending the design under questioning
A design review is not a monologue; it is a negotiation under uncertainty, and the questions you get will probe the assumptions you did not state. The most common line of questioning starts with a failure scenario: what happens when one broker dies, when a disk fills, when a consumer falls an hour behind during a traffic spike. If your design has a number for each of those, you win the review even if the numbers are imperfect. If your design has a shrug, the interviewer concludes you have drawn boxes rather than designed a system, regardless of how many boxes there are.
The second common line of questioning probes a choice you could have made differently. Why three replicas and not two? Why this partition count and not double it? Why a key on the aggregate id rather than the customer id? Each of these has a defensible answer, but only if you state the constraint that forced the choice. Three replicas survives one node loss with the in-sync floor intact; two replicas cannot lose a node without either pausing producers or dropping below the durability floor. Double the partition count would exceed the measured per-partition throughput requirement and double rebalance time for no benefit. The aggregate id is the unit that must be ordered; the customer id may span many concurrent orders and would serialize unrelated work onto one partition.
The third line of questioning is about operational readiness rather than the topology itself. How do you monitor it? How do you deploy a change to a topic configuration? What happens when retention fills the disk faster than expected? These questions do not have a single correct answer, but they have a wrong answer, which is to have no answer. A design that includes a monitoring plan, a change process and a capacity alert is a design that someone has operated, and the interview is often decided at that point rather than at the partition-count discussion.
The last piece of advice is to write the design down as a small set of numbered decisions, each with the constraint that drove it and the failure it tolerates. Not a diagram, not a slide: a list of statements you can defend. Diagrams impress for thirty seconds; a list of decisions with constraints survives an hour of questioning and leaves the interviewer with something concrete to evaluate.
Common Mistakes
Choosing a partition count without measuring throughput
Fix: Run a producer perf test with production-like record sizes and size partitions from the measured ingest, then check consumer parallelism against the same number.
Enabling unclean leader election to keep availability during an incident
Fix: Leave it false. An out-of-sync leader accepts writes that were never acknowledged, which is data loss; unavailability during a second failure is the better trade.
Increasing partition count on a live topic to fix lag
Fix: More partitions break key-to-partition mapping and per-key ordering. Accept the lag, drain it, then plan a migration with an application change and a rekeying strategy.
Compacting an event topic to save disk
Fix: Compaction is for state topics keyed by entity. An event topic's history is its value, so use retention with a measured retention period instead.
Best Practices
- Ask for ingest rate, peak load, retention, ordering and latency requirements before proposing topology
- Defend replication factor three, min.insync.replicas two and unclean leader election false as the durability baseline
- Key every event topic by aggregate id and treat partition count as an irreversible decision until a rekeying plan exists
- Use static membership and cooperative sticky assignment so rolling restarts do not trigger stop-the-world rebalances
- Document the failure domain each design tolerates, and name the one it does not
Interview Questions
Q1: How do you decide the partition count for a new topic?
Size it from measured ingest throughput divided by per-partition capacity, then check that the number is at least the desired consumer parallelism and small enough that rebalances stay fast. Measure with a producer perf test using production record sizes.
Q2: Your financial event topic is losing records under broker failure. What do you check?
acks, replication factor, min.insync.replicas and unclean leader election. Financial topics need acks=all, replication factor three, min.insync.replicas two and unclean.leader.election.enable=false.
Q3: Why is raising partition count on a live topic risky?
The key-to-partition mapping changes, so records for one entity can land on different partitions and per-key ordering is lost for existing consumers. Migrate with a rekeying plan rather than an in-place increase.
Q4: How do you keep a consumer group from rebalancing on every rolling restart?
Enable static membership with group.instance.id per instance and use cooperative sticky assignment, so a restart only moves the partitions owned by the departing member instead of reshuffling the whole group.
Q5: How would you design for a region-level failure?
Separate clusters per region with an explicit mirror such as MirrorMaker 2, regional write isolation if active-active is needed, and a drilled failover that cuts producers first and repoints them last. Never a stretch cluster across the regions.
Key Takeaways
- Requirements before topology: ingest rate, peak, retention, ordering, latency and failure tolerance
- Partition count comes from measurement and is effectively irreversible without a rekeying plan
- Replication factor three, min.insync.replicas two and unclean leader election false are the defensible durability baseline
- Key by aggregate id; a null key makes ordering undefined and consumers pay for it
- Name the failure you are designing against, and the one you are not, before the interviewer has to ask