Capacity Planning, Sizing and Quotas

Capacity Planning, Sizing and Quotas in Apache Kafka: Most Kafka capacity incidents are arithmetic that nobody did.

Most Kafka capacity incidents are arithmetic that nobody did. The numbers are not hard; they are just skipped because the cluster works fine at launch volume.

Start from throughput, in both directions

Two independent numbers drive everything:

ingress  = records/sec x average record size (after compression)
egress   = ingress x number of consumer groups
           (+ replication traffic between brokers)

Egress is the one that gets forgotten. A topic taking 20 MB/s of writes and read by five consumer groups pushes roughly 100 MB/s out of the cluster, plus follower replication fetches. Network capacity is consumed by fan-out, not by writes.

Sizing partition count

There is no universal per-partition throughput number, and anyone quoting one has not accounted for your record size, compression, replication factor, disk, or consumer processing cost. The method is what transfers:

1. Measure, in a test on YOUR hardware with YOUR records:
     p_in  = sustained MB/s a single partition accepts
     p_out = sustained MB/s a single consumer instance processes
            (usually dominated by your own processing, not by Kafka)

2. partitions >= max( target_ingress / p_in , target_egress / p_out )

3. Add headroom for growth, because increasing later is disruptive.

4. Sanity-check against consumer parallelism:
     partitions >= the number of consumer instances you ever want running

Step 2 is usually decided by p_out. If each record takes 50 ms of your processing time, one consumer thread handles 20 records/sec regardless of how fast Kafka is. Partition count is then a statement about your processing capacity, not the broker's.

Storage arithmetic

daily_bytes  = records/day x avg record size (post-compression)
topic_bytes  = daily_bytes x retention_days x replication_factor
cluster_disk = sum(topic_bytes) + headroom

Worked shape, with round numbers:

 1,000 records/sec, 2 KB each, retention 7 days, RF 3

 per second   = 1,000 x 2 KB          =   2 MB/s
 per day      = 2 MB/s x 86,400       ≈ 173 GB/day
 x 7 days                             ≈ 1.2 TB
 x RF 3                               ≈ 3.6 TB across the cluster

The replication factor multiplier is the step most often missed, and it is a 3x error. Then add headroom, because:

  • Segments roll before they are deleted. A topic transiently holds more than retention.ms implies.
  • Index and time-index files sit alongside every segment.
  • A recovering replica re-fetches a large amount of data quickly, which can spike both disk and network.
  • A stuck consumer group is not a reason to keep data, but a replay is — and replaying from the start of retention doubles read load while it runs.

Target well under full disk. A broker that fills its disk does not degrade gracefully; it stops, and recovery involves either deleting data under pressure or adding capacity to a cluster that is already degraded. Alert on the trend (days-to-full), not just a static percentage.

retention.bytes is per partition

Worth its own heading because the mistake is so common.

retention.bytes = 100 GB, partitions = 24, RF = 3

 worst case on disk = 100 GB x 24 x 3 = 7.2 TB
 not 100 GB, and not 300 GB

Set retention.bytes as a safety net below your disk budget, and retention.ms as the business replay window. Whichever triggers first wins, so treat the byte limit as protection against unexpected volume rather than as your retention policy.

The cost of very high partition counts

"Just make it 500 partitions, partitions are cheap" is half true. Per-partition costs are small; they are also numerous and they land in places that hurt during failures.

CostEffect
Open file handles and memory per partition on the brokerEach partition means segment, index and time-index files; thousands of partitions means tens of thousands of handles
Producer memoryThe accumulator holds a batch buffer per partition, so a producer writing to many partitions needs more buffer.memory and gets smaller, less efficient batches
Replication workEvery partition is a fetch stream between brokers; more partitions means more requests for the same bytes
Controller and metadataLarger metadata to propagate; leader election after a broker failure has more partitions to move
Failover timeLosing a broker means electing new leaders for every partition it led. This scales with partition count and is exactly when you want it fast
Rebalance and restore durationMore partitions per group to reassign; for Streams, more state stores to restore
End-to-end latencySmaller batches per partition at a fixed record rate

The asymmetry from Module 1 still applies: too few partitions caps throughput and is disruptive to change, too many degrades failure behaviour. Size for the throughput you expect within the next year or two, not for the largest number you can imagine.

Quotas: stopping one client from taking the cluster down

Kafka is multi-tenant by default and fair by no means. One backfill job with a large buffer.memory and no throttling can saturate broker network and disk, and the victims are unrelated services whose latency quietly triples.

Quotas bound a client's resource use. Three kinds:

QuotaLimitsTypical use
producer_byte_rateBytes/sec publishedBackfills, importers, batch producers
consumer_byte_rateBytes/sec fetchedReplay jobs, analytics consumers reading from the start
request_percentageShare of broker request-handler and network thread timeClients that are cheap in bytes but expensive in requests

That third one matters more than people expect. A client polling with tiny fetches, or committing offsets constantly, consumes broker CPU without moving much data — a byte-rate quota will not touch it.

Quotas can be applied per client-id, per authenticated user, or per user+client-id combination, and they are enforced per broker. A 10 MB/s quota across 5 brokers can mean up to 50 MB/s in aggregate, so plan with that in mind.

# Bound a backfill job
kafka-configs.sh --bootstrap-server localhost:9092 \
  --alter --entity-type clients --entity-name nightly-backfill \
  --add-config 'producer_byte_rate=5242880,consumer_byte_rate=10485760'

# See what is currently applied
kafka-configs.sh --bootstrap-server localhost:9092 \
  --describe --entity-type clients --entity-name nightly-backfill

Enforcement is by delaying responses, not by returning errors. A throttled producer sees higher latency and, if it keeps pushing, eventually buffer pressure and max.block.ms timeouts. That is worth telling client teams up front, because "Kafka got slow" is how a working quota is usually reported.

Two operational habits: set a default quota for all clients so that a new, unreviewed application cannot hurt anyone, and require a meaningful client.id on every application. Quotas and most useful metrics are attributed by client.id, so a fleet of clients all reporting the default identity is both unthrottleable and undiagnosable.

A capacity review checklist

[ ] Ingress and egress estimated, including consumer-group fan-out
[ ] Partition count derived from measured per-partition and per-consumer rates
[ ] Partition count >= planned consumer instances, with growth headroom
[ ] Storage = daily bytes x retention x RF, plus headroom, per topic
[ ] retention.bytes understood as per partition
[ ] Disk alerting on days-to-full trend
[ ] Default quotas in place; heavy or bursty clients quoted explicitly
[ ] Every client sets a meaningful client.id
[ ] Failure math done: can the remaining brokers hold the traffic if one dies?

That last line is the one that separates a capacity plan from a wish. Sizing to 100% of current brokers means the first failure is also an overload.


Exercise: A topic must accept 8,000 records/sec of 1.5 KB records, be read by four consumer groups, retain 14 days at replication.factor=3, and each consumer instance can process about 400 records/sec. Work out a defensible partition count and the approximate cluster storage required, state which of the two numbers you would defend hardest to a sceptical reviewer and why, and name the one measurement you would insist on taking before committing to the partition count.