Scrivio Interview library
ConceptFoundations5 min read · updated 13 November 2026

Besides a key and a value, every Kafka record can carry extra metadata that frameworks and applications read without touching the payload.

What are Kafka record headers, what are they good for, and what are their limits? Give examples of headers that a Spring Kafka application adds or reads automatically.

Record headers are key-value pairs attached to each Kafka record, with String keys and byte-array values; a key may appear more than once and the order is kept. They carry metadata such as trace context, event type, content type and correlation id without touching the payload. Brokers cannot filter or index by them, they count toward the record size, and Spring Kafka uses them for things like __TypeId__ type information and kafka_dlt-* exception details on dead-letter records.

Why this question is asked

Most of us meet headers when something else adds them: a trace id that appears in the consumer, or a strange __TypeId__ entry when we inspect a topic. Knowing what they are explains a lot of framework behaviour, and designing them well is part of a good event contract. Interviewers ask about them to check that we know where metadata belongs and what Kafka will and will not do with it. In this answer, we will recap what a header is, see how to read and write them, look at what they are good for, list their limits, and go through the headers Spring Kafka adds and reads for us.

1A Quick Recap of a Kafka Record

A Kafka record has a key, used for partitioning and compaction, a value, the payload, a timestamp, and since Kafka 0.11 a list of headers. A header is a pair: a String key and a byte[] value. The list is ordered and allows the same key more than once, which is why the API offers both the last header with a name and all headers with that name.

Kafka treats header values as opaque bytes. It does not know whether they hold text, a number or JSON, so the producer and consumer must agree on the encoding, usually UTF-8 text.

Java
ProducerRecord<String, String> rec = new ProducerRecord<>("orders", "order-42", payloadJson);
rec.headers().add("event-type", "OrderPlaced".getBytes(StandardCharsets.UTF_8));
rec.headers().add("tag", "a".getBytes(StandardCharsets.UTF_8));
rec.headers().add("tag", "b".getBytes(StandardCharsets.UTF_8));
producer.send(rec);
// on the consumer side
Header last = record.headers().lastHeader("tag"); // value "b"
for (Header h : record.headers().headers("tag")) { ... } // "a", then "b"
new String(record.headers().lastHeader("event-type").value(), StandardCharsets.UTF_8); // "OrderPlaced"

Notice that adding a second tag does not replace the first. If we want replace semantics, we call remove("tag") before add().

2What Headers Are Good For

Headers hold information about the event rather than the event itself. The most common use is trace context: the W3C traceparent header carries the trace id and parent span id so that a consumer's span joins the producer's trace. Others are the event type and version, so a consumer can route or skip a record before deserialising it; the content type or schema reference; a correlation id that ties together every event caused by one user action; and the name of the source service.

Keeping this out of the payload has two benefits. Infrastructure such as tracing, routing and dead-letter handling can work on any topic without knowing each payload's schema, and the business payload stays clean for the teams that own it.

3Where Headers Stop Helping

Brokers do not index or filter by headers. A consumer that only wants OrderCancelled events still fetches every record and discards the rest in its own code, so headers save deserialisation work, not network or broker work. Headers are part of the record, so they count toward max.request.size and the batch size limits, and they are stored and replicated with every record; a 500-byte header on a 300-byte payload more than doubles the topic's size.

Headers are also visible to anyone who can read the topic, and they are rarely covered by the payload encryption or masking a team applies. So we keep values small, document every header name and its encoding in the event contract, and never put secrets, tokens or personal data in them.

Header uses and limits
QuestionAnswer
Can a key repeat?Yes, and insertion order is kept
Value typebyte[]; producer and consumer agree on the encoding
Can the broker filter by header?No; consumers read and discard in code
Do they count toward size limits?Yes, with every record
Safe for secrets or personal data?No; anyone who reads the topic sees them

4Headers Spring Kafka Adds and Reads

Spring Kafka maps between Kafka headers and Spring message headers, and the constants in KafkaHeaders name them. On the consumer side we can inject values such as the topic, partition, offset and key into a listener method. Our own headers arrive as byte arrays unless the header mapper is told to map them as text.

Java
@KafkaListener(topics = "orders")
void onOrder(String payload,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition, // 3
@Header(KafkaHeaders.OFFSET) long offset, // 1817
@Header("event-type") byte[] type) { // "OrderPlaced" as bytes
log.info("{} at {}-{}", new String(type, StandardCharsets.UTF_8), partition, offset);
}

Several Spring Kafka features write headers automatically. The JSON serializer adds __TypeId__ (and __Key_TypeId__ for keys) holding the class name, so the JSON deserializer knows which type to build; this is convenient between Spring services but couples consumers to the producer's class names, so many teams turn it off and configure the target type instead. The DeadLetterPublishingRecoverer adds kafka_dlt-original-topic, kafka_dlt-original-partition, kafka_dlt-original-offset, kafka_dlt-exception-fqcn, kafka_dlt-exception-message and kafka_dlt-exception-stacktrace, so a dead-lettered record explains where it came from and why it failed. Non-blocking retry topics add their own retry headers, and with observation enabled KafkaTemplate writes traceparent. Header names can change between major versions, so check your version's reference documentation.

5Follow-up questions

Should the event type go in a header or in the payload?

Often both. A header lets routers and filters act without deserialising, and a field in the payload keeps the event self-describing when it is stored elsewhere, such as in a data lake where headers may be lost.

Do headers survive compaction and replication?

Yes. They are part of the record, so they are replicated, kept by compaction with the latest record for a key, and copied by tools such as MirrorMaker. Some connectors and sinks drop them, though, so check before relying on them outside Kafka.

Why did my consumer fail with a ClassNotFoundException after another team renamed a class?

The producer's JSON serializer wrote the old class name into __TypeId__, and the consumer's deserializer tried to load it. Configure the consumer with a fixed target type or type mappings, and disable type headers on the producer for topics shared between teams.

6Conclusion

In this answer, we saw that headers are ordered String to byte[] pairs on every record, with repeatable keys. They are the right place for trace context, event type, content type, correlation id and source, but brokers cannot filter by them, they add to every record's size, and they must never hold secrets or personal data. Spring Kafka maps them into listener parameters through KafkaHeaders, uses __TypeId__ for JSON types, and adds kafka_dlt-* headers when it dead-letters a record.

What to say in the interviewAbout 80 seconds aloud

Open with

Headers are key-value pairs on each Kafka record, next to the key and value. Keys are strings, values are byte arrays, the same key can appear more than once and the order is preserved, so the API has lastHeader and headers by name.

Then, if they want more

They are for metadata about the event: the W3C traceparent for tracing, the event type and version, the content type or schema, a correlation id and the source service. That lets infrastructure route, trace and dead-letter records without knowing every payload.

The limits are that brokers do not index or filter on headers, so a consumer still fetches and discards records it does not want, they count toward the record size and are stored with every record, and anyone who can read the topic sees them, so no secrets or personal data.

In Spring Kafka, KafkaHeaders constants let me inject the partition, offset or key into a listener, the JSON serializer adds __TypeId__ so the deserializer knows the class, which I often disable between teams, the dead-letter recoverer adds kafka_dlt headers with the original topic, offset and exception, and with observation enabled the template writes traceparent. I document every header in the event contract.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are Kafka record headers, what are they good for, and what are their limits? Give...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/b140b381c0/