Producers and Message Keys

Kafka · lesson 3 of 34 · 4 min read

Send records, choose keys on purpose, and know exactly which partition a record will land on.

Open this lesson in the learning hub

Key points

  • A record is key + value + headers + timestamp. The key decides the partition.
  • With a key, partition = murmur2(key) % partitionCount. One key always lands on one partition, so its order is kept.
  • With a null key, Kafka spreads records around, sticking to one partition per batch so batching stays efficient.
  • Key by whatever must stay ordered: orderId, customerId, accountId. Never key by something random.
  • send() is asynchronous. It buffers and batches; the callback is what tells you the broker really acked it.
  • Closing the producer flushes pending batches. Skip the close and you drop whatever was still buffered.

Example

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;

public class OrderProducer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        // try-with-resources: close() flushes buffered batches
        try (Producer<String, String> producer = new KafkaProducer<>(props)) {

            // key = orderId, so every event for order-42 keeps its order
            ProducerRecord<String, String> record =
                new ProducerRecord<>("orders", "order-42", "PAID");

            producer.send(record, (meta, ex) -> {
                if (ex != null) {
                    ex.printStackTrace();
                } else {
                    System.out.printf("partition=%d offset=%d%n",
                                      meta.partition(), meta.offset());
                }
            });
        }
    }
}

The key is not metadata. It is your routing and ordering decision.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Kafka course, and every lesson in it is listed on the Kafka contents page.