title: Apache Kafka: seek and assignment. Reliable read from beginning
body: <p>I'm trying to implement basic scenario, re-read topic from beginning (at least 1 message), and I'm facing unexpected behavior.</p>
  
  <p>Suppose, there are 1 partition topic holding exactly 1 million messages, 1 consumer with offset already committed somewhere at middle, no active producers.</p>
  
  <p>First I've tried </p>
  
  <pre><code>  consumer.subscribe(Collections.singletonList(topic));
    consumer.seekToBeginning(Collections.emptySet());
    consumer.poll(Duration.ofMillis(longTimeout)); //no loop to simplify
  </code></pre>
  
  <p>And that doesn't work (no messages polled). I've read that <code>seekToBeginning</code> is lazy (and that's ok), but it turns out, <code>seekToBeginning</code> doesn't impact at all, cause it need partitions to be already assigned, what will only happen with first poll. Should it be described at docs, or have I missed it?</p>
  
  <p>Then I've tried </p>
  
  <pre><code>  consumer.subscribe(Collections.singletonList(topic));
    consumer.poll(Duration.ofMillis(assignTimeout));
    consumer.seekToBeginning(Collections.emptySet());
    consumer.poll(Duration.ofMillis(longTimeout));//no loop to simplify
  </code></pre>
  
  <p>And turns out, it depends on <code>assignTimeout</code>. It should be enough to complete join process. That time may vary and it's not possible to rely on it.</p>
  
  <p>Then I've provided <code>ConsumerRebalanceListener</code> with </p>
  
  <pre><code>    @Override
      public void onPartitionsAssigned(Collection&lt;TopicPartition&gt; partitions) {
        consumer.seekToBeginning(partitions);
      }
  </code></pre>
  
  <p>And single <code>poll</code> left. And it finally seems to work.</p>
  
  <p>So the questions are:</p>
  
  <ol>
  <li>Is <code>seekToBeginning</code> right after <code>subscribe</code> useless? Should it be documented?</li>
  <li>Is solution with <code>ConsumerRebalanceListener</code> reliable? Does it guarantee that no messages from middle (committed offset) would be polled before seek will apply?</li>
  </ol>