blog by OSO

Kafka Message Structure: Keys, Values and Headers

Sion Smith
Sion Smith 24 November 2026
OSO guide to Kafka message structure, keys, values and headers, with white title text on a navy panel and a lime Blogs label.

What fields does a Kafka message contain?

Kafka’s documentation uses “message” and “record” for the same thing. A record is the unit a producer sends and a consumer reads. If the key concepts of Apache Kafka are new to you, start there.

Every record a consumer receives carries these fields:

FieldTypeWho sets itWhat it is for
TopicStringProducerThe named stream the record belongs to
PartitionIntegerProducer or partitionerThe ordered log inside the topic
Offset64-bit integerBrokerThe record’s position in its partition
Timestamp64-bit integer (ms)Producer or brokerCreate time or log append time
KeyBytes, nullableProducerPartitioning, ordering and compaction
ValueBytes, nullableProducerThe payload your application cares about
HeadersList of key/value pairsProducerMetadata such as trace ids

The broker sees the key and value only as bytes. Your client serialises them, for example as Avro, JSON or Protobuf, and the consumer must use a matching deserialiser.

On disk, the Kafka message format stores each record with deltas against its batch. The offset and timestamp are small offsets from the batch’s base values, which keeps records compact.

This page covers the record itself. How you shape a domain event inside the value is a separate question, covered in our guide to designing Kafka events.

How do keys, partitions and ordering relate?

The key decides where a record lands. With the default partitioner, a record with a key goes to a partition chosen by a hash of the key. The same key therefore always lands on the same partition, as long as the partition count stays the same.

Kafka only guarantees order within a partition. So records that share a key are read in the order they were written. Records with different keys can be read in any relative order.

A record with a null key has no fixed partition. Current producers use a sticky strategy: they fill one partition until about batch.size bytes are sent, then move on. The producer configuration reference describes this default.

Choose keys with these rules in mind:

  • Use a business id, such as an order or account id, when events for that entity must stay in order.
  • Use a null key when order does not matter and you want an even spread.
  • Avoid a handful of very busy keys, because one hot key loads one partition.
  • Always set a key on compacted topics, because compaction keeps the latest value per key.

Adding partitions later changes which partition a key maps to. Plan the partition count before ordering matters. If you need several schemas on one topic, read about multiple message types in one topic.

What are Kafka record headers used for?

Headers are optional key/value pairs attached to a record. The header key is a string and the header value is bytes. They arrived in Kafka 0.11 through KIP-82.

Teams use headers for metadata that should travel with the record but sit outside the payload:

  • trace and correlation ids for distributed tracing;
  • content type or schema hints;
  • routing or tenant information read by intermediaries;
  • the source system and original topic after replication.

Headers count towards the record size, so they are not a place to hide a large payload. Keep them short.

Where the schema id lives depends on your serialiser. The classic Confluent wire format puts it in the payload prefix: a version byte, then a 4-byte schema id, then the data. The Confluent wire format documentation also describes a newer option, from Confluent Platform 8.1.1, that writes a 16-byte schema GUID to the __value_schema_id or __key_schema_id header instead. Check which format your producers use before you write a consumer by hand. Our Schema Registry guide covers compatibility across systems.

Key takeaway: Treat the Kafka record as bytes with a key, a value and headers; design the event shape separately, and keep payloads small enough for the batch.

How are records batched and compressed?

Kafka always writes records in batches. A record batch holds one or more records for a single partition, and the broker stores batches, not individual records.

The current on-disk format is magic version 2. Each batch header carries the base offset, base timestamp, producer id and epoch, a CRC and an attributes field. The attributes record the compression codec, the timestamp type and whether the batch is transactional.

Three producer settings shape batches. The defaults below come from the producer configuration reference:

SettingDefaultWhat it does
batch.size16384 bytesUpper bound of a batch per partition
linger.ms5 ms (since Kafka 4.0)How long the producer waits to fill a batch
compression.typenonenone, gzip, snappy, lz4 or zstd

Compression applies to whole batches, so larger batches usually compress better. The linger.ms default changed from 0 to 5 in Kafka 4.0, because larger batches tend to give similar or lower latency. Tune these with the rest of your Kafka producer settings.

How does message size affect the cluster?

Three settings cap how big a record batch can be:

SettingScopeDefault
message.max.bytesBroker1048588 bytes
max.message.bytesTopic override1048588 bytes
max.request.sizeProducer1048576 bytes

The broker and topic limits apply to the record batch after compression. The producer limit caps the request and, in effect, the uncompressed batch. These values come from the broker, topic and producer configuration references.

So the practical ceiling is about 1 MiB unless you raise all the relevant limits together. Raising only one side leads to rejected sends or replication and consumer surprises.

Large records also cost more than their size suggests. They use more broker memory, slow replication and make consumers spend longer on each poll. For files, images or big documents, store the object in object storage and send a small record with a reference to it.

If your records keep hitting these limits, our Kafka consulting services team can review your topic design, and Kafka support can help when a production cluster is already struggling.

Frequently asked questions

What is the difference between a Kafka message and a record?

There is no practical difference. Kafka’s documentation uses “message” and “record” for the same unit of data, and current client APIs call it a record, for example ProducerRecord and ConsumerRecord.

Does a Kafka message have to have a key?

No. The key is optional and can be null. Without a key the producer spreads records across partitions, so you lose per-key ordering, and compacted topics need a key.

What are Kafka record headers?

Headers are optional key/value pairs on a record, added in Kafka 0.11 through KIP-82. Each header has a string key and a bytes value, and they usually carry metadata such as trace ids.

How is a Kafka record stored on the broker?

The broker stores records inside record batches on a partition’s log. The current batch format is magic version 2, and each record stores its offset and timestamp as deltas from the batch’s base values.

What is the default Kafka message size?

The default broker limit, message.max.bytes, is 1048588 bytes, and the producer’s max.request.size defaults to 1048576 bytes. That is roughly 1 MiB per batch unless you raise the limits.

Where does the schema id live in a Kafka message?

With the classic Confluent serialisers, it sits in the payload prefix: a version byte, then a 4-byte schema id. Since Confluent Platform 8.1.1 you can instead write a 16-byte schema GUID to a record header.

Designing your Kafka records?

OSO engineers run Apache Kafka in production for banks, retailers and the public sector. Tell us what you are weighing up and we will give you a straight answer.

CONTACT US
OSO
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.