title: Reactor Kafka - At-Least-Once - handling failures and offsets in multi partition
body: <p>Below is the consumer code to receive messages from kafka topic (8 partition) and processing it.</p>
  
  <pre><code>    @Component
      public class MessageConsumer {
  
          private static final String TOPIC = "mytopic.t";
          private static final String GROUP_ID = "mygroup";
          private final ReceiverOptions consumerSettings;
          private static final Logger LOG = LoggerFactory.getLogger(MessageConsumer.class);
  
          @Autowired
          public MessageConsumer(@Qualifier("consumerSettings") ReceiverOptions consumerSettings)
          {
              this.consumerSettings=consumerSettings;
              consumerMessage();
          }
  
          private void consumerMessage()
          {
  
          KafkaReceiver&lt;String, String&gt; receiver = KafkaReceiver.create(receiverOptions(Collections.singleton(TOPIC)));
  
          Scheduler scheduler = Schedulers.newElastic("FLUX_DEFER", 10, true);
  
          Flux.defer(receiver::receive)
                  .groupBy(m -&gt; m.receiverOffset().topicPartition())
                  .flatMap(partitionFlux -&gt;
                          partitionFlux.publishOn(scheduler)
                                  .concatMap(m -&gt; {
                                      LOG.info("message received from kafka : " + "key : " + m.key()+ " partition: " + m.partition());
                                      return process(m.key(), m.value())
                                              .thenEmpty(m.receiverOffset().commit());
                                  }))
                  .retryBackoff(5, Duration.ofSeconds(2), Duration.ofHours(2))
                  .doOnError(err -&gt; {
                      handleError(err);
                  }).retry()
                  .doOnCancel(() -&gt; close()).subscribe();
  
      }
  
      private void close() {
      }
  
      private void handleError(Throwable err) {
          LOG.error("kafka stream error : ",err);
      }
  
      private Mono&lt;Void&gt; process(String key, String value)
      {
          if(key.equals("error"))
              return Mono.error(new Exception("process error : "));
  
          LOG.error("message consumed : "+key);
          return Mono.empty();
      }
  
  
      public ReceiverOptions&lt;String, String&gt; receiverOptions(Collection&lt;String&gt; topics) {
          return consumerSettings
                  .commitInterval(Duration.ZERO)
                  .commitBatchSize(0)
                  .addAssignListener(p -&gt; LOG.info("Group {} partitions assigned {}", GROUP_ID, p))
                  .addRevokeListener(p -&gt; LOG.info("Group {} partitions assigned {}", GROUP_ID, p))
                  .subscription(topics);
      }
  
  
  }
      @Bean(name="consumerSettings")
      public ReceiverOptions&lt;String, String&gt; getConsumerSettings() {
          Map&lt;String, Object&gt; props = new HashMap&lt;&gt;();
          props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
          props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
          props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
          props.put(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID);
          props.put(ConsumerConfig.CLIENT_ID_CONFIG, GROUP_ID);
          props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
          props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
          props.put("max.block.ms", "3000");
          props.put("request.timeout.ms", "3000");
  
          return ReceiverOptions.create(props);
      }
  </code></pre>
  
  <p>On receiving each message, my processing logic returns on empty mono if the consumed message processed successfully. </p>
  
  <p>Everything works as expected if there is no error returned in the processing logic.</p>
  
  <p>But if i throw an error to simulate the exception behaviour in my processing logic for a particular message then i am missing to process that message which caused the exception. The stream moves to the next message.</p>
  
  <p>What i want to achieve is, process the current message and commit the offset if its successful then move to the next record.</p>
  
  <p>If any exception in processing the message don't commit the current offset and retry the same message until its successful. Don't move to the next message until the current message is successful.</p>
  
  <p>Please let me know how to handle process failures without skipping the message and make the stream start from the offset where the exception is thrown.</p>
  
  <p>Regards,</p>
  
  <p>Vinoth</p>