liveBook · Manning

Offset

As explained in Kafka Streams in Action, Second Edition

An offset is a nonnegative integer that identifies a record's logical position within a Kafka partition and advances sequentially as records are appended.

An offset is the broker-assigned ID and logical position of a record within a topic partition. It starts at 0 and increases by 1 as records are appended. Because records are appended at the end of a partition log, offset order gives Kafka’s ordering guarantee within that partition—not across partitions, and not necessarily by event time. section 2.4.1

Offsets let a consumer say where reading should begin. A fetch request names a topic partition and starting offset; the broker returns a batch from that point. Reading does not remove records or change their availability to other consumers. section 2.3

Continue after a processed batch

If a consumer reads offsets 10 through 20, its next read begins at offset 21. To continue after a restart or failure, commit the position only after successful processing. A committed position is the highest successfully consumed offset plus one; with automatic commits, KafkaConsumer commits based on enable.auto.commit and auto.commit.interval.ms. section 4.3.5

Use offsets with their partition-specific meaning in mind:

  • Treat an offset as unique within its partition. The same numeric offset in another partition identifies a different record. section 2.4.1
  • Do not infer event time from offset order; records are ordered by arrival at the broker. section 2.4.1
  • Commit after processing when progress must survive restarts. A restart before a commit can cause records to be processed again. section 4.3.5
  • Avoid assuming automatic commits match asynchronous processing completion; committing can potentially mark records consumed before that work finishes. section 4.3.5

An offset is logical, not a byte address. On disk, a broker records each record’s offset, size, and physical segment position. Its memory-mapped index maps offsets to positions so the broker can locate a fetch starting point with a binary search. Index entries store offsets relative to a segment’s base offset, keeping each offset and position entry to 4 bytes. section 2.6.3

Figure 2.5 Offsets indicate where a consumer has left off reading records.
figure

Trace how a consumer that has read one range uses the next offset as its next starting position. section 2.4.1

Record produced by a Spring Kafka application

A produce callback can inspect RecordMetadata after a send completes and log the destination topic, assigned offset, and timestamp. This makes the broker’s assigned position visible to application code while processing a loan application. section 12.2.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines offsets, traces fetching and commits, and descends into index-based physical record lookup.
Examples
Uses sequential consumer batches, offset commits, partition logs, and a Spring Kafka produce callback.
What it assumes
Introduces offsets alongside topics and partitions, then assumes consumer processing when explaining commits.
What you can do after
Operate/configure consumer progress by selecting fetch positions and committing processed offsets.
How it connects
Connects offsets to partitions, consumer commits, fetch requests, timestamps, and log indexes.

Also explained in

Compare how 4 books explain offset →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.