Video in progress
How do you choose the partition count for a new topic?
I divide the required throughput by the measured throughput of a single consumer and add headroom for 2-3 years of growth. But too many partitions are expensive too: more files, slower failover, longer rebalances.
📖 Answer
I start the calculation from the consumer side, because that's usually the narrow point. Say the peak is 6,000 messages/s, and one consumer thread, with its DB writes, handles 400 messages/s. That means at least 15 parallel consumers, so 15 partitions. Allowing for double growth, 24 or 30. It helps to pick a number with many divisors (12, 24, 48), so matching the pod count is easy.
On the producer side, a single partition usually handles tens of MB/s, so it's rarely the limit.
Why "more is better" is wrong:
- every partition replica means open files, memory, and work for the replication threads;
- when a broker dies, there's an election for all of its leader partitions, and with many of them it takes longer;
- producer batches get split across many partitions and shrink, which can reduce throughput;
- consumer rebalances get slower.
With KRaft the cluster-wide partition limit went up a lot, but going past a few thousand per broker still takes care.
And most important: if you rely on keyed ordering, increasing later is painful. So putting in a bit more at the start is cheaper. On a topic without keys where ordering doesn't matter, you can easily start small and increase later.
Don't guess the number; measure it in staging with kafka-producer-perf-test.sh and your own consumer.
Sign up to mark studied questions and save your progress.