Sobes.tech
Senior

What to do if Kafka lag is increasing and horizontal scaling is not possible? How else to read messages?

sobes.tech AI

Answer from AI

If Kafka lag is increasing and horizontal scaling is not possible, consider alternative approaches to message reading:

  • Increase processing speed: Optimize message processing logic to reduce the time taken for each message.
  • Parallel reading within existing partitions: Use multiple threads to read from a single partition if possible (for example, using Kafka Consumer API with task division within one consumer).
  • Batch processing: Read multiple messages at once and process them in batches to reduce overhead.
  • Change commit offset strategy: Commit offsets less frequently to reduce load on Kafka.
  • Use more efficient clients or settings: For example, increase fetch.min.bytes, fetch.max.wait.ms for more efficient reading.
  • Redistribute load: If possible, redistribute load among existing consumers.

If scaling and optimization are not feasible, consider alternative systems or architectural changes, such as caching, pre-aggregating data, or changing message generation logic.


Example of batch processing in Java Kafka Consumer:

ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
    // Process messages in batch
}
consumer.commitSync();
What to do if Kafka lag is increasing and horizontal… - sobes.tech