Producer Basics
Keys and partitioning, the sticky partitioner, batching, and compression
A producer looks simple: call produce() and the record is sent. Under the
hood it's an asynchronous batching engine, and its settings decide your
throughput, latency, ordering, and durability. The book's
Producers chapter covers the
Java client; the labs here use confluent-kafka (librdkafka), which works
the same way.
Keys decide partitions
With a key, the partition is hash(key) % partition_count. The same key
always lands in the same partition, so records for one entity stay in
order:
1. keys decide partitions
customer-0: partition [1]
customer-1: partition [3]
customer-2: partition [3]
customer-3: partition [5]
customer-4: partition [0]Each customer's 12 orders all went to exactly one partition. Two customers sharing partition 3 is normal: keys map to partitions statistically.
Hot keys create hot partitions. If 40% of orders come from one big
customer and you key by customer, one partition (and one consumer)
gets 40% of the load, and adding partitions doesn't help. Choose keys
with many values and even traffic, or add a suffix (customer-42-3) and
give up strict ordering for that customer.
Note also that librdkafka and the Java client hash keys differently
(librdkafka's default is consistent_random, CRC32-based; Java uses
murmur2). A Python producer and a Java producer writing the same key can
put it in different partitions. Set partitioner=murmur2_random in
librdkafka when mixing clients.
No key: the sticky partitioner
Without a key, modern clients use the sticky partitioner: send to one
partition until a batch is ready (or sticky.partitioning.linger.ms,
10 ms in librdkafka, passes), then switch. This fills batches faster than
round-robin. The lab shows it:
2. no key: the sticky partitioner
3000 in a burst: {0: 1, 4: 2999}
3000 over ~3 seconds: {0: 569, 1: 455, 2: 504, 3: 505, 4: 496, 5: 471}A burst produced within a few milliseconds lands almost entirely on one partition. Over time it evens out. Don't be surprised by short-term skew on unkeyed topics.
Batching: linger.ms and batch.size
Records for the same partition are collected into a batch, sent when
it reaches batch.size bytes or when linger.ms has passed. Waiting a few
milliseconds lets batches fill up, which means fewer requests, better
compression, and much higher throughput:
3. throughput: 100k x 200-byte messages
linger.ms=0, no compression 119,304 msg/s
linger.ms=20, batch 256KB 981,930 msg/s
linger.ms=20, batch 256KB, lz4 1,120,722 msg/sAbout 8x from batching alone, on the same cluster. The cost is up to
linger.ms of extra latency per record. For most pipelines, 5-50 ms is
an easy trade. (librdkafka already defaults linger.ms to 5; this lab
sets 0 explicitly to show the difference.)
Compression
compression.type compresses whole batches: lz4 and zstd are the usual
choices (fast, good ratio), gzip compresses more but costs CPU, snappy
is legacy. Since brokers store batches as received, compression saves
network, disk, and replication traffic. Bigger batches compress
better, so compression and linger.ms work together.
Delivery reports
produce() returns immediately; the record is just queued. Success or
failure arrives later, in a delivery callback, when you call poll()
or flush():
def record(err, msg):
if err:
log.error("delivery failed", err) # retries are exhausted at this point
else:
print(msg.topic(), msg.partition(), msg.offset())
p.produce("orders", key=customer, value=payload, on_delivery=record)
p.poll(0) # serve callbacks for earlier sends (do this regularly)
...
p.flush() # before exiting: wait for everything outstandingTwo classic producer bugs: never calling poll(), so callbacks never
fire and errors go unnoticed, and exiting without flush(), so records
still sitting in the local queue are silently lost. A third: catching
BufferError (the local queue is full) and dropping the record instead
of polling and retrying, as the lab does.
Lab
- Run
producer_basics.pyand compare your numbers. - Add
"partitioner": "murmur2_random"and check whether the customer → partition mapping changes. - Add a run with
compression.type=zstdandlinger.ms=50. - Use
kafka-dump-log.shon a partition ofordersand find a compressed batch (look forcompresscodec).