AlgoMaster Logo

Offsets and Log Position

High Priority12 min readUpdated November 11, 2026
Listen to this chapter
Unlock Audio

Kafka stores records in partitions, with each partition acting as an ordered, append-only log.

But how does Kafka keep track of millions of records within a partition? And how does a consumer know which records it has already read and where to resume after a restart?

Kafka solves both problems using offsets.

Offsets are fundamental to how Kafka identifies records, tracks consumer progress, and supports replaying historical events.

Let's understand how they work.

1. What Is a Kafka Offset?

An offset is a unique, sequentially increasing number assigned to each record within a Kafka partition.

When a producer publishes a record, Kafka appends it to the selected partition and assigns it the next available offset.

For example, consider an orders topic with a single partition:

Each record has an offset representing its position in the partition.

When a new record arrives, Kafka appends it to the end of the log and assigns it a higher offset.

Offsets serve two important purposes:

  1. Record identification: They allow Kafka and its clients to locate records within a partition.
  2. Consumer progress tracking: They allow consumers to remember where they are in the event stream.

One important detail is that offsets are assigned independently for each partition.

2. Offsets Are Partition-Specific

Suppose the orders topic has three partitions:

Notice that every partition has its own offset sequence.

This means an offset alone doesn't uniquely identify a record across the entire Kafka cluster.

For example, offset 2 can exist in both Partition 0 and Partition 2.

To identify a record's position, we need three pieces of information:

  • Topic name
  • Partition number
  • Offset

Together, they identify a record's location within Kafka.

For example:

This identifies the record at offset 5 in Partition 2 of the orders topic.

3. Understanding Log Position

A Kafka partition is an append-only log, but not every position in that log necessarily contains an available record.

Kafka uses several important offset boundaries to describe the state of a partition.

Log Start Offset

The log start offset represents the earliest offset currently retained in a partition.

For example, suppose a partition initially contains records at offsets 0 through 999.

After Kafka removes older records based on its retention policy, the earliest available record might be at offset 400.

The log start offset is now 400.

This matters because consumers cannot read records that Kafka has already deleted.

Log End Offset

The log end offset (LEO) represents the position where the next record would be appended to a partition's log.

For example:

Notice that the log end offset is 105, even though the last record has offset 104.

This is because the log end offset represents the next append position, not the offset of the last record.

The distinction becomes useful when measuring how far consumers are behind the producer.

Step through the two scenarios below to see how both boundaries move. Appends shows three partitions assigning offsets independently, each with its own log end. Retention removes old records, moving the log start forward while the remaining records keep their original offsets.

Loading simulation...

Notice that even when a partition is emptied, its log end stays at 10 instead of resetting to 0. Offsets are never reused.

High Watermark

Kafka also maintains a high watermark, which tracks how far a partition's records have been sufficiently replicated to be visible to ordinary consumers.

In a replicated partition, the leader might have received records that haven't yet been replicated to all required in-sync replicas.

Kafka doesn't immediately expose those records to consumers.

Instead, the high watermark defines the boundary up to which records are considered committed in the partition log.

This prevents consumers from reading records that could disappear during a normal leader failover.

For now, remember that the log end offset represents the end of the leader's log, while the high watermark determines how far consumers can safely read.

4. How Consumers Use Offsets

Let's say an Order Processing Service subscribes to the orders topic.

The consumer starts reading records from Partition 0:

Suppose the consumer has received records through offset 2.

Its current position is now offset 3, meaning the next record it expects to read is at offset 3.

A consumer's position always represents the offset of the next record to be fetched, not the last record it has read.

Kafka tracks this position separately for every partition assigned to the consumer.

For example, a consumer might be at:

This allows a single consumer to make progress independently across multiple partitions.

However, there is an important distinction between reading records and successfully processing them.

Just because Kafka has delivered a record to a consumer doesn't mean the application has finished processing it.

This brings us to committed offsets.

5. Consumer Position vs. Committed Offset

Kafka distinguishes between two concepts:

  • Consumer position: Where the consumer will read next.
  • Committed offset: The saved position from which the consumer can resume after a restart or partition reassignment.

Consider a consumer processing order events:

Suppose the consumer has fetched records through offset 104, but it has successfully processed only through offset 102.

Its current position may be 105, while its committed offset should remain at 103.

Why 103?

Because Kafka commits the offset of the next record to process, not the last successfully processed record.

If the consumer crashes, it can restart from committed offset 103 and process the remaining records.

The diagram below places both markers on the same partition so you can see the gap between them.

This distinction is essential for building reliable consumers.

Where Are Committed Offsets Stored?

Kafka typically stores consumer group offsets in an internal topic called:

__consumer_offsets

This topic maintains committed positions for consumer groups, organized by topic and partition.

For example:

Each consumer group maintains its own progress.

This means the Analytics Service and Order Processing Service can consume the same topic at different speeds without interfering with each other.

The __consumer_offsets topic is managed internally by Kafka. Applications normally interact with it through the consumer API rather than reading or modifying it directly.

6. Why Offset Commit Timing Matters

The timing of offset commits affects how an application handles failures.

Consider a consumer that reads an order event and saves it to a database.

There are two possible approaches.

Commit Before Processing

The consumer commits the offset first and then processes the record.

Commit After Processing

The consumer first completes the database operation and then commits the offset.

If the application crashes after processing but before committing, it will read the same record again after restarting.

This can result in duplicate processing, but the record isn't silently skipped.

This approach is commonly used to achieve at-least-once processing.

For applications where duplicate processing matters, such as payments or inventory updates, the processing logic should also be idempotent.

Here are the two orderings side by side, each with a crash at the worst possible moment.

Kafka supports both automatic and manual offset commits.

7. Starting a Consumer: Earliest vs. Latest

What happens when a new consumer group subscribes to a topic for the first time?

Since the group has no previously committed offset, Kafka needs to decide where consumption should begin.

This behavior is controlled by the consumer configuration:

auto.offset.reset

Two commonly used options are earliest and latest.

Earliest

With earliest, the consumer starts from the earliest available offset.

For example:

A new consumer starts at offset 100 and can read all four retained records.

This is useful when building analytics pipelines, rebuilding application state, or processing existing data.

Latest

With latest, the consumer starts at the end of the available log.

In our example, the consumer starts at the end position, so it waits for new records rather than reading the existing ones.

The diagram below marks where each setting places a new consumer group in this partition.

This can be useful when an application only cares about events published after it begins consuming.

There are also other options, including none, which raises an exception if no valid starting offset is available, and by_duration, which allows a supported consumer to start relative to a configured time duration.

8. Replaying Events Using Offsets

One of Kafka's most useful features is the ability to replay previously published events.

Suppose an Analytics Service processes sales events but produces incorrect reports because of a bug.

After fixing the bug, we can reset its consumer group offsets to an earlier position and process the events again.

For example:

The consumer can now reprocess available records starting from offset 1000.

This doesn't change the records stored in Kafka or affect other consumer groups.

Replay is only possible for records Kafka still retains.

If an event has already been removed by retention or compaction, resetting offsets cannot bring it back.

Kafka also allows consumers to seek to specific offsets programmatically or locate offsets associated with particular timestamps.

These capabilities are useful for debugging, recovery, and rebuilding downstream systems.

9. Understanding Consumer Lag

Offsets also help us monitor how quickly consumers process events.

Consumer lag measures how far a consumer is behind the available end of a partition.

A common approximation is:

For example:

This means the consumer group is approximately 1,500 offset positions behind the end of the partition.

For ordinary, non-compacted topics without gaps, this closely represents the number of records remaining.

In compacted topics or transactional workloads, offset differences don't always equal the exact number of readable records.

Lag is useful for detecting slow consumers and processing bottlenecks.

If the lag keeps increasing, the consumer group may not be processing events as quickly as producers publish them.

Possible causes include slow database operations, insufficient consumer parallelism, expensive processing, or uneven partition workloads.

You can inspect consumer group offsets and lag using Kafka's command-line tools:

The output includes the group's committed offsets, log-end positions, and reported lag for its topic partitions.

10. Do Offsets Always Increase by One?

Offsets increase as records are appended, but the records currently available to consumers may not have consecutive offsets.

For example:

Gaps can appear because Kafka's retention or compaction mechanisms remove records, or because of transactional record handling.

Kafka doesn't renumber the remaining records.

If a consumer reads offset 10 and offset 11 is unavailable, it can continue to the next readable record.

This is why offsets should be treated as logical positions rather than record counts.

Key Takeaways

Offsets identify records within Kafka partitions and allow consumers to track their progress.

Each partition has its own offset sequence, and a record is identified by its topic, partition, and offset.

The log start offset marks the beginning of retained data, while the log end offset represents the next append position. Kafka also uses a high watermark to control which replicated records are visible to consumers.

Consumers maintain both a current position and a committed offset. The committed offset determines where consumption resumes after a restart or reassignment.

Offsets also enable replaying historical events, managing failures, and measuring consumer lag.

Quiz

Offsets and Log Position Quiz

10 quizzes