Learn Labs
3. Kafka Producers: Writing Messages to Kafka

3.10 Headers

That last clause is the compelling one: headers let an intermediary route/trace data it is not authorized to read.

"Record headers give you the ability to add some metadata about the Kafka record, without adding any extra information to the key/value pair of the record itself."

Uses:

  • Lineage — indicating the source of the data in the record
  • Routing or tracing messages based on header information without having to parse the message itself — "perhaps the message is encrypted and the router doesn't have permissions to access the data."

That last clause is the compelling one: headers let an intermediary route/trace data it is not authorized to read.

Implementation: "an ordered collection of key/value pairs. The keys are always a String, and the values can be any serialized object — just like the message value."

ProducerRecord<String, String> record =
    new ProducerRecord<>("CustomerCountry", "Precision Products", "France");

record.headers().add("privacy-level",
    "YOLO".getBytes(StandardCharsets.UTF_8));