apache-kafka

Kafka consumer reading messages parallel


Can we have multiple consumers to consume from a topic to achieve parallel processing in kafka. My use case is to read messages from a single partition in parallel.


Solution

  • Yes you can process messages in parallel using many Kafka consumers, but no, it's not possible if you only have one partition.

    Parallelism in Kafka consuming is defined by the number of partitions, you can easily re-partition your topic at any time to create more partitions.

    An example of how process messages in parallel using rapids-kafka-client below, a library to make Kafka parallel consuming easier.

    public static void main(String[] args){
      ConsumerConfig.<String, String>builder()
          .prop(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName())
          .prop(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName())
          .prop(GROUP_ID_CONFIG, "stocks")
          .topics("stock_changed")
          .consumers(7)
          .callback((ctx, record) -> {
            System.out.printf("status=consumed, value=%s%n", record.value());
          })
          .build()
          .consume()
          .waitFor();
    }