Skip to content

Lesson 8 of 9 · 24m

Topic Design, Throughput, Batching, and Cost

Design topics around access patterns and ownership, then tune batching, compression, partitioning, and retention with cost and latency in mind.

Kafka performance work often starts with someone increasing partitions and ends with everyone learning that distributed systems charge interest. Throughput is shaped by record size, batching, compression, replication, client behavior, disk, network, and consumer parallelism. Topic design is an economic decision: every extra copy, retention day, and oversized payload becomes permanent infrastructure cost.

Design topics around consumption boundaries

A good topic has a coherent event type, ownership, retention policy, schema, and consumer audience. Resist the mega-topic where every service emits anything because routing feels convenient:

good: payments.authorized
good: inventory.stock-adjusted
bad:  company.events

The mega-topic forces every consumer to deserialize and discard irrelevant traffic. It also couples schema governance and retention requirements that should never have shared a blast radius. Too many tiny topics are not purity either; metadata and operational overhead grow quickly.

Producer batching is deliberate latency tradeoff

Important producer knobs:

linger.ms=20          batch.size=131072      compression.type=lz4
acks=all              delivery.timeout.ms=120000
max.in.flight.requests.per.connection=5      enable.idempotence=true

linger.ms waits briefly so records can batch. batch.size caps bytes per partition batch. Compression reduces network and disk cost, but CPU shifts to producers and brokers. LZ4 is a practical default for many workloads; zstd can compress better at higher CPU cost. Measure with production-shaped payloads, not a thousand copies of "hello".

Partition count is a parallelism budget

Estimate with arithmetic:

target 30,000 records/sec / 3,000 per partition = 10 minimum partitions
with headroom for skew and recovery: start around 12-16

This is a starting point, not gospel. Key skew matters. If one tenant creates 80 percent of events under one key, one partition remains the bottleneck no matter how many others sit idle. Sometimes the key design, not the partition count, is the real problem.

Retention and replication dominate storage cost

Rough sizing starts simple:

200 GiB/day uncompressed / 4:1 compression = 50 GiB/day before replicas
50 GiB * 14 days * replication factor 3 = 2.1 TiB minimum raw log capacity

Then add segment overhead, compaction behavior, growth, broker balancing slack, and headroom for recovery. Never run disks near full because Kafka's failure behavior becomes operationally awful. Tiered storage can reduce broker-local cost in some platforms, but it does not remove the need to understand hot-path performance.

Large messages are usually a design smell

Kafka can carry large records if configured, but large payloads amplify broker memory pressure, replication cost, retries, consumer processing time, and failure blast radius. If you are tempted to raise:

message.max.bytes, replica.fetch.max.bytes, max.request.size

ask whether the payload belongs in object storage with a reference in the event. This claim-check pattern is useful for images, exports, and large documents. It is less useful when consumers need strong transactional coupling between object and event. Finally, measure end to end: producer enqueue, broker write, consumer fetch, processing, and downstream commit. The slowest stage owns user-visible latency.

Field notes

  • Compression effectiveness depends on payload shape. Small JSON with repeated field names compresses well; already-compressed media does not.
  • Excessively large max.poll.records can create long batches that trigger rebalances and duplicate work.
  • Cross-zone replication traffic can cost more than broker compute, while thousands of tiny topics create metadata and operational burden. Coherence beats absolutism.
  • Keep a reproducible load harness. Performance conclusions made during an incident are usually theater.

Was this lesson helpful?

Sign in to rate