Search code examples
node.jsexpressapache-kafkakafka-consumer-apilibrdkafka

Network timeout error if more than 3 consumer instances created using node-rdkafka


I am using node-rdkafka to connect to a Kafka instance hosted on a cloud vm. I have multiple consumers and each of them using the following Kafka config:

"group.id": "librd-test",
"metadata.broker.list": `${process.env.KAFKA_HOST}:9092`,
"socket.keepalive.enable": true,
'enable.auto.commit': true,
"debug": "all"

There are only 5 topics and my consumer code works fine only if I create less than 4 consumer instances. If I add more than 3, my nodejs express server returns timeout on any external request. Like for eg. I am doing a dummy api call fetch in one of the consumer:

 const tmp = await fetch('https://jsonplaceholder.typicode.com/todos/1', {timeout: 10000})
                .then(response => response.json());

If I create more than 3 consumer instances, this code mysteriously times out. Although, It works perfectly fine if there are 3 or less than 3 consumer instances. The above fetch call is called inside consumer's data event:

 consumer.on("data", async function (m) {
        counter++;
        consumer.commit(m);
        // Fetch Call
        const tmp = await fetch('https://jsonplaceholder.ty....
       

Why I am getting these network timeout on adding more consumers?

Things I have tried:

  • Changing consumer group names.
  • Adding more partitions to specific topics.

For hosting Kafka: Iam using https://lenses.io/box/ with free license key.


Solution

  • For people facing this problem in future: Please set UV_THREADPOOL_SIZE to a value more than 4 as an environment variable in your node application.

    Explaination:

    Each node process will have at least UV_THREADPOOL_SIZE + v8_thread_pool_size + 1 threads running.

    the current default for both UV_THREADPOOL_SIZE and v8_thread_pool_size is 4.

    Source: Link Link2