Logo

dev-resources.site

for different kinds of informations.

Breaking the Scale Barrier: 1 Million Messages with NodeJS and Kafka

Published at
1/15/2025
Categories
node
microservices
pubsub
kafka
Author
fahim_hasnainfahad_7e50d
Categories
4 categories in total
node
open
microservices
open
pubsub
open
kafka
open
Author
24 person written this
fahim_hasnainfahad_7e50d
open
Breaking the Scale Barrier: 1 Million Messages with NodeJS and Kafka

Apache Kafka is a distributed event streaming platform which can be used as a Pub-Sub broker in NodeJS microservices. Recently, I worked in a project where publisher needed to send messages to 1 million users and give the response back. Here, a cool thing can be done by Message Queue and we can send 1 million messages in a queue and it will send the messages to the users one by one. But, a pub-sub can broadcast to all instances of a consumer which are subscribed to a certain topic.

The Magic of Message Queues vs. Pub-Sub in Kafka

When faced with such a massive scale of messaging, two approaches often come to mind:

  1. Message Queue: Send 1 million messages to a queue, where each message is processed one by one by consumers.

  2. Pub-Sub: Broadcast a single message to all instances of consumers subscribed to a specific topic.

For our use case, Kafka’s flexibility in handling both patterns made it the ideal choice. Let’s dive into how to implement a Producer-Consumer microservice system with Kafka.

Check out the demo code: https://github.com/fahadfahim13/producer-consumer-kafka.git

Architecture

NodeJS Microservice with Kafka

Producer Microservice:

Let's create a Producer microservice and a Consumer microservice.
Code in producer/index.js will be like this:

const { Kafka } = require('kafkajs');

const kafka = new Kafka({
  clientId: 'my-producer',
  brokers: [process.env.KAFKA_BROKER],
});

const producer = kafka.producer();

(async () => {
  await producer.connect();
  for (let i = 0; i < 1000000; i++) {
    await producer.send({
      topic: process.env.TOPIC_NAME,
      messages: [{ value: `Message ${i}` }],
    });
    console.log(`Sent message ${i}`);
  }
  await producer.disconnect();
})();
Enter fullscreen mode Exit fullscreen mode

Here, we can see the producer is producing 1 million messages and sending into a Kafka topic.

Consumer Microservice

Code in consumer microservice's index.js file will be like this:

const { Kafka } = require('kafkajs');

const kafka = new Kafka({
  clientId: 'my-consumer',
  brokers: [process.env.KAFKA_BROKER],
});

const consumer = kafka.consumer({ groupId: 'test-group' });

(async () => {
  await consumer.connect();
  await consumer.subscribe({ topic: process.env.TOPIC_NAME, fromBeginning: true });

  await consumer.run({
    eachMessage: async ({ topic, partition, message }) => {
      console.log(`Received message: ${message.value} in topic: ${topic} at partition: ${partition}`);
    },
  });
})();
Enter fullscreen mode Exit fullscreen mode

The consumers will be subscribed to a Kafka topic and receive messages asynchronously and process those messages.

Docker-compose File

The docker-compose.yml file looks like this which will run zookeeper, kafka and producer & consumer services in different containers.

version: '3.8'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - "22181:2181"
    networks:
      - kafka-network
    volumes:
      - kafka_data:/var/lib/kafka/data

  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    networks:
      - kafka-network

  producer:
    build: 
      context: ./producer
      dockerfile: Dockerfile
    container_name: kafka-producer
    depends_on:
      - kafka
    networks:
      - kafka-network
    volumes:
      - ./producer:/app
    environment:
      KAFKA_BOOTSTRAP_SERVERS: kafka:9092
      KAFKA_BROKER: kafka:9092
      TOPIC_NAME: my-topic

  consumer:
    build: 
      context: ./consumer
      dockerfile: Dockerfile
    container_name: kafka-consumer
    depends_on:
      - kafka
    networks:
      - kafka-network
    volumes:
      - ./consumer:/app
    environment:
      KAFKA_BOOTSTRAP_SERVERS: kafka:9092
      KAFKA_BROKER: kafka:9092
      TOPIC_NAME: my-topic

networks:
  kafka-network:
    driver: bridge


volumes:
  kafka_data:
Enter fullscreen mode Exit fullscreen mode

Optimizing Kafka Performance with Partitions

To handle high throughput, it’s essential to increase the number of partitions in our Kafka topic. More partitions allow parallel processing, enabling producers and consumers to scale horizontally. For example:

kafka-topics --create \
  --zookeeper localhost:2181 \
  --replication-factor 1 \
  --partitions 10 \
  --topic my-topic
Enter fullscreen mode Exit fullscreen mode

Each partition acts as a separate queue, distributing the load among consumers.

Check out the demo code: https://github.com/fahadfahim13/producer-consumer-kafka.git

pubsub Article's
30 articles in total
Favicon
Breaking the Scale Barrier: 1 Million Messages with NodeJS and Kafka
Favicon
From Heist Strategy to React State: How data flows between components
Favicon
TOP 5 Brain-Boosting Logic Games for Your Phone
Favicon
RabbitMQ: conceitos fundamentais
Favicon
New article alert! Data Engineering with Scala: mastering data processing with Apache Flink and Pub/Sub ❀️‍πŸ”₯
Favicon
Pub-sub Redis in Micronaut
Favicon
Navigating the World of Event-Driven Process Orchestration for Technical Leaders
Favicon
Use cases of Kafka
Favicon
Create scalable and fault-tolerant microservices architecture
Favicon
Choose The Reliable MBA Assignment Help With These Top 10 Tips: A Comprehensive Guide!
Favicon
Communication Protocols in IoT: The Unsung Heroes of Our Connected World
Favicon
MQTT: The Whisperer of the IoT World
Favicon
Event types chaos in Event Driven Architecture
Favicon
Harness PubSub for Real-Time Features in Phoenix Framework
Favicon
How Subscription Management Software is Transforming Mobile Apps
Favicon
Google Pub/Sub
Favicon
New Rotating Shapes Animation
Favicon
Real-Time Data Processing with Node.js, TypeScript, and Apache Kafka
Favicon
Messaging in distributed systems using ZeroMQ
Favicon
Article checker html CSS Java Script
Favicon
When and how to load balance WebSockets at scale
Favicon
Getting Started with Apache Kafka: A Backend Engineer's Perspective
Favicon
WebSocket reliability in realtime infrastructure
Favicon
Realtime reliability: How to ensure exactly-once delivery in pub/sub systems
Favicon
[Event-Driven] understanding
Favicon
Achieving delivery guarantees in a pub/sub system
Favicon
How to build an autonomous news Generator with AI using Fluvio
Favicon
How to build an event-driven architecture with Fluvio
Favicon
Key Components and Tools for Event-Driven Architectures
Favicon
Leveling Up My GraphQL Skills: Real Time Subscriptions

Featured ones: