Skip to content

Reader without consumer group read at last offset #1368

@zekth

Description

@zekth

Describe the solution you would like

Currently the implementation when spinning a consumer without consumer group will always start at the first offset:

offset: FirstOffset,

Would it be possible to support the StartOffset parameter instead?

The goal in here is to spin a consumer without a consumer group idea to get only the latest offsets

Activity

ns-gzhang

ns-gzhang commented on Feb 21, 2025

@ns-gzhang

There is a way to work around to see if it fits your need. Here is how we did it to read from any offset:

  1. Get the first and last offsets on a topic/partition:
    ...
    conn, err = dialer.DialLeader(ctx, "tcp", broker, k.Topic, k.Partition) // we use kafka.DefaultDialer
    if err != nil {
         // should retry, or another broker
    }
    first, last, err = conn.ReadOffsets()
   ...
  1. use the above offsets to set your desired starting offset k.Offset between first and last (inclusive):
        k.client = kafka.NewReader(config)
	_ = k.client.SetOffset(k.Offset)
        ...
        m, err := k.client.ReadMessage(k.context)
petedannemann

petedannemann commented on Mar 21, 2025

@petedannemann
Contributor

Yeah this seems reasonable and was probably a mistake in the initial implementation, can you submit a PR with the change?

zekth

zekth commented on Mar 21, 2025

@zekth
Author

I'll try to find an implementation to avoid breaking changes in the initial behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @zekth@petedannemann@ns-gzhang

      Issue actions

        Reader without consumer group read at last offset · Issue #1368 · segmentio/kafka-go