Learn Labs
Producers

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.

produce()returns at oncePartitionerkey → partitionbatch: P0linger.ms / batch.sizebatch: P1batch: P2broker 1leads P0broker 2leads P1broker 3leads P2the sender thread ships each batch to its partition leader; delivery callbacks fire on poll()

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.

customer-0customer-1customer-2customer-4partition 1partition 3two keys share itpartition 0hash(key) % 6: every record for a key lands in one partition, in order

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/s

About 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 outstanding

Lab

  1. Run producer_basics.py and compare your numbers.
  2. Add "partitioner": "murmur2_random" and check whether the customer → partition mapping changes.
  3. Add a run with compression.type=zstd and linger.ms=50.
  4. Use kafka-dump-log.sh on a partition of orders and find a compressed batch (look for compresscodec).

On this page