I have a kafka consumer class which has 2 listener TOPICS and a DLQ listener. When load testing with million records i.e.producing 15 requests per second that reaches topic-1 and 1 request per second reaches topic-2 simultaneously, I find that there is a lag in kafka consumption. Eg.if the 15 requests are produced at 2.00 am, the topic-1 listener is consuming only around 2.20am. Where as the Topic -2 that consumes 1 request per second does not show any time lag. Can someone please help me out on how to find the cause of this time lag and solution? Thank you in advance!
Please find the edited Simple Java code that illustrates this issue
Kafka consumerconfig.java
public class KafkaConsumer {
final ObjectMapper mapper =new ObjectMapper();
//MAIN TOPIC LISTENER 1
@KafkaListener(id = "topic-1", topics = "topic-1", groupId = "main", containerFactory= "kafkaListenerContainerFactory", clientIdPrefix = "topic-1")
public void mainListener(ConsumerRecord<String, String> consumerRecord, Acknowledgement ack) {
dbService.saveTodb(consumerRecord.value(),mapper);
ack.acknowledge();
}
//MAIN TOPIC LISTENER 2
@KafkaListener(id = "topic-2", topics = "topic-2", groupId = "main", containerFactory= "kafkaListenerContainerFactory", clientIdPrefix = "topic-2")
public void mainListener(ConsumerRecord<String, String> consumerRecord, Acknowledgement ack) {
dbService.saveTodb(consumerRecord.value(), mapper);
ack.acknowledge();
}
}
KafkaBeanFactory.java
@Configuration
public class KafkaBeanFactory{
@Bean(name = "kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory(
ConcurrentKafkaListenerContainerFactoryConfigurer configurer,
ConsumerFactory<Object, Object> kafkaConsumerFactory, KafkaTemplate<Object, Object> template) {
ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
configurer.configure(factory, kafkaConsumerFactory);
var recoverer = new DeadLetterPublishingRecoverer(template,
(record, ex) -> new TopicPartition("DLQ-topic", record.partition()));
var errorHandler = new DefaultErrorHandler(recoverer, new FixedBackOff(3, 20000));
errorHandler.addRetryableExceptions(JsonProcessingException.class, DBException.class);
errorHandler.setAckAfterHandle(true);
factory.setCommonErrorHandler(errorHandler);
return factory;
}
}
application.yaml
spring:
kafka:
bootstrap-servers: localhost:9092 # sample value
client-id: mainDLQ
properties:
security:
protocol: SASL_SSL
sasl:
mechanism: PLAIN
jaas:
config: org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="<string>";
security:
protocol: SASL_SSL
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
groupId: main
enable-auto-commit: false
auto.offset.reset: earliest
listener:
ack-mode: MANUAL_IMMEDIATE