Kafka · Producers & Consumers

Producer write flow

Easyproducerwrite pathpartitionerbatching

Interview question

Walk through what happens when a Kafka producer sends a message.

How to answer

The short answer

Here is a fresher-friendly produce path:

Step by step

  1. 1Create producer with bootstrap.servers, serializers, acks, etc.
  2. 2Send a ProducerRecord (topic, optional key, value, optional headers/).
  3. 3Producer serializes key/value (e.g. String, Avro, ).
  4. 4Partitioner chooses a partition:
  • If partition set explicitly → use it
  • Else if key present → hash(key) % numPartitions
  • Else → sticky / round-robin style distribution
  1. 1Producer may batch records for that partition (linger + batch size).
  2. 2Send batch to the leader broker for that partition.
  3. 3Leader appends to its log and replicates per acks policy.
  4. 4Broker returns success/failure; producer callbacks / futures complete.
  5. 5On success (depending on acks), the record has an assigned offset.

Tiny mental example

Common producer settings to mention

  • acks
  • retries / delivery timeout
  • linger.ms and batch.size
  • enable.idempotence
  • compression (snappy, lz4, zstd)

Interview tip: Narrate key → partition → leader → ack. That shows you understand the path, not just the API.