Quick revision
The short answer
Kafka stores the 500 records as one v2 record batch: a fixed header followed by the records section, which is lz4-compressed as a whole. The header carries the base offset, length, partition leader epoch, magic 2, a CRC, attributes (including the codec, a transactional flag and a control flag), the last offset delta, base and max timestamps, producer id, producer epoch, base sequence and record count. Each record inside stores only small varint deltas for its offset and timestamp plus its key, value and headers; the producer id, epoch and base sequence support idempotence, and the transactional flag with control batches supports transactions.
What to remember
In this answer, we saw that our 500 records travel and live as one v2 record batch: a 61-byte header with offsets, leader epoch, CRC, attributes, timestamps and producer fields, followed by a records section compressed as a whole with lz4. Each record keeps only varint deltas plus its key, value and headers, which keeps overhead small. Brokers store the batch as received and assign offsets, consumers decompress, the producer id, epoch and sequence give idempotence, and the transactional flag with control batches gives transactions.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Kafka stores the 500 records as one v2 record batch: a fixed header followed by the records section, which is lz4-compressed as a whole. The header carries the base offset, length, partition leader epoch, magic 2, a CRC, attributes (including the codec, a transactional flag and a control flag), the last offset delta, base and max timestamps, producer id, producer epoch, base sequence and record count. Each record inside stores only small varint deltas for its offset and timestamp plus its key, value and headers; the producer id, epoch and base sequence support idempotence, and the transactional flag with control batches supports transactions.
Why this question is asked
The record batch is the unit everything in Kafka works with: producers build it, brokers store it as it arrived, replicas copy it, and consumers decompress it. Understanding its layout explains why batching improves throughput, why compression ratios depend on linger.ms, and how idempotence and transactions work without a separate database. Interviewers ask about it to check that we know Kafka's design from the bytes up. In this answer, we will recap why Kafka batches at all, walk through the batch header field by field, look at how each record is packed, see how compression applies, and then see which fields serve idempotence and transactions.
1A Quick Recap of Why Kafka Batches
A producer does not send records one by one. It collects records for the same partition into a batch, waiting up to linger.ms or until the batch reaches batch.size, and sends the batch in one request. Since Kafka 0.11 the on-disk and on-the-wire format is the v2 record batch, identified by a magic byte of 2. Kafka 4.0 removed the old v0 and v1 message formats, so v2 is the only format a 4.x cluster writes.
In our example, 500 records of about 200 bytes are roughly 100 KB of raw data, so they fit in one batch if batch.size is large enough (the default is 16 KB, so this producer must have raised it, for example to 128 KB). The batch then travels and is stored as a single unit.
2The Batch Header, Field by Field
The header is a fixed 61 bytes. Some fields are filled by the producer, some by the broker when it appends the batch.
Notice that the batch knows its own offset range: from baseOffset to baseOffset plus lastOffsetDelta. The broker assigns baseOffset when it appends, which is why the producer can build the batch without knowing where it will land. The CRC covers everything from the attributes to the end, so a corrupted batch is detected on read, and it deliberately excludes the base offset and leader epoch so the broker can set them without recomputing the checksum.
The two timestamps make time lookups cheap. maxTimestamp lets the broker update the time index and decide retention without decompressing the batch.
3How Each Record Is Packed
Inside the batch, each record stores only what differs from the header. Its offset is an offsetDelta from the base offset and its timestamp is a timestampDelta from the base timestamp. Both are varints, variable-length integers that use one byte for small values. So record 37 stores the offset delta 37 in a single byte, and a timestamp 12 ms after the base also fits in one byte.
- Batch header, 61 bytesoffsets, epoch, CRC, codec, producer id and sequence
- Records section, compressed with lz4500 records, compressed together
- Recordvarint length, timestamp and offset deltas
- Key and valueraw bytes, as serialized by the client
- Headersoptional key-value pairs per record
5 layers, from the top down. Step through them one at a time.
Compression covers only the records section; the header stays readable.
So the per-record overhead is a handful of bytes instead of the 30 or so bytes the old formats spent on a full offset, timestamp, CRC and attributes per message. With small records like ours, that difference is a large share of the total size.
4Where Compression Fits
Compression is applied to the records section of the whole batch, not to each record. The codec is named in the low three bits of attributes: 0 for none, 1 gzip, 2 snappy, 3 lz4, 4 zstd. Because the compressor sees all 500 records together, repeated field names and similar values across records compress well. That is why bigger batches, and a few milliseconds of linger.ms, often give much better compression ratios.
The broker normally does not decompress and recompress. It validates the batch, assigns the offsets, and writes the bytes as they arrived; replicas copy them as they are; consumers receive them and decompress on their side. That keeps broker CPU low. The exception is when the topic's compression.type names a different codec than the producer used, in which case the broker must recompress, which costs CPU, so most teams leave the topic setting at producer.
5Fields for Idempotence and Transactions
producerId, producerEpoch and baseSequence make idempotence work. An idempotent producer gets a producer id from the broker and numbers its records per partition. The broker remembers the last sequence numbers it accepted for each producer and partition. If a retry arrives with a sequence it has already written, the broker acknowledges it without writing it again; if a batch arrives with a gap in the sequence, it rejects it as out of order. The epoch fences old instances: a newer epoch for the same transactional producer makes batches with an older epoch fail.
For transactions, bit 4 of attributes marks a batch as transactional, meaning its records belong to an open transaction and should be hidden from read_committed consumers until the outcome is known. When the transaction ends, the transaction coordinator has a control batch written into every involved partition: a batch with bit 5 set holding a single commit or abort marker. Consumers never see control records; their client uses them, together with the aborted transactions index, to decide which transactional records to return.
6Follow-up questions
Why does the CRC not cover the base offset?
Because the broker sets the base offset, and the partition leader epoch, when it appends the batch, after the producer computed the CRC. Excluding them means the broker can write the batch without recomputing a checksum over the compressed data, which keeps the append path cheap.
How do I see these fields on a real broker?
kafka-dump-log.sh --files 00000000000005120000.log prints one line per batch with baseOffset, lastOffset, count, the producer id, epoch and sequence, isTransactional, isControl, the compression codec and the CRC. Adding --print-data-log also decodes each record's key, value and headers.
Does a larger batch always help?
Up to a point. Bigger batches compress better and need fewer requests, but they add latency through linger.ms, use more producer memory, and must stay below the broker's message.max.bytes, which limits a batch's size after compression.
7Conclusion
In this answer, we saw that our 500 records travel and live as one v2 record batch: a 61-byte header with offsets, leader epoch, CRC, attributes, timestamps and producer fields, followed by a records section compressed as a whole with lz4. Each record keeps only varint deltas plus its key, value and headers, which keeps overhead small. Brokers store the batch as received and assign offsets, consumers decompress, the producer id, epoch and sequence give idempotence, and the transactional flag with control batches gives transactions.
Open with
Kafka stores those 500 records as a single v2 record batch, the format with magic byte 2, which is the only format in Kafka 4 since the old v0 and v1 formats were removed.
Then, if they want more
The batch has a fixed header with the base offset, the batch length, the partition leader epoch, the magic byte, a CRC-32C, an attributes field, the last offset delta, the base and max timestamps, the producer id, producer epoch, base sequence and the record count.
After the header come the records, and each record only stores a varint offset delta and timestamp delta from the batch base plus its key, value and headers, so per-record overhead is just a few bytes.
Compression applies to the whole records section, so lz4 sees all 500 records together, which is why bigger batches compress better, and the codec is in the low bits of attributes. The broker validates the batch, assigns offsets and writes it as received, so decompression happens on consumers.
For idempotence, the producer id, epoch and base sequence let the broker drop duplicate retries and reject out-of-order batches. For transactions, a flag in attributes marks the batch as transactional, and the coordinator later writes control batches with commit or abort markers that read_committed consumers use to decide what to show.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Kafka 4.0
- 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 "Describe how Kafka stores them in the v2 record batch format, and which batch fields...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/61cb4c4265/