Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Tips

    Top 5 Free Website Builders in 2019

    Beginners

    WordPress for Non-Programmers: Introduction to the Web Development World

    Entrepreneurship

    Leveraging Crowdfunding: A Strategic Guide for Startups

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 17
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / The Ultimate Introduction to Kafka with JavaScript
    JavaScript

    The Ultimate Introduction to Kafka with JavaScript

    Ranvir SinghBy Ranvir SinghDecember 12, 2019Updated:December 12, 2019No Comments10 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    The Ultimate Introduction to Kafka with JavaScript
    Introduction to Kafka
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Introduction to Kafka
    Introduction to Kafka

    Apache Kafka is an Open-source asynchronous publish-subscribe messaging system that can help you build any of the offline task handling system, streaming service or any other data pipeline service.

    My first interaction with such a system was back in Google Summer of Code – 2017 when I used celery for handling the long-running asynchronous tasks.

    In this article, you’ll learn about the following things

    1. Basic Introduction to Kafka
    2. Basic terms and their definitions used in Kafka
    3. Architecture of Kafka
    4. Zookeeper
    5. How to create a producer and consumer using JavaScript that can interact with Kafka broker.

    History of Kafka

    Kafka was originally built at LinkedIn and was later open-sourced and donated to Apache Software Foundation. It is basically written in Scala. It is being used by all the top companies out there like Twitter, Airbnb and many more.

    Other tools in the same domain at that time were giving high latency for the event data being passed and consumed by the services. The folks at LinkedIn wanted to build a tool that can replace the already existing non-scalable version of the data processing pipelines and create something that handles high scales.

    Companies that use Kafka Source: redmonk
    Companies that use Kafka

    source: https://redmonk.com/fryan/2016/05/23/kafka-summit-the-four-comma-club/

    Introduction to Kafka

    According to the official documentation of Kafka, it is a distributed streaming platform and is similar to an enterprise messaging system.

    What is a simple messaging system?

    A simple messaging system consists of 3 main parts.

    1. Producer

    A producer/ publisher is the part of the system which produces the messages. A message can be anything. Mostly there is a limit to the size of the message that a producer can produce and with the broker.

    For SQS the upper limit of the size if 256 KB.

    1. Broker

    A Broker is an interface/ intermediate are the receivers of the messages. They store the messages for some time and pass it on to the consumers for further usage. In most of the brokers, there is a limit on till when you can store the message on a broker.

    For SQS the limit is 15 minutes whereas in Kafka the messages can stay for a longer time which can be configured according to the requirement.

    1. Consumer

    The consumer is the part of the application which consumes the messages passed on by the broker. The consumer can handle the messages accordingly.

    This simple setup can be used to handle log shipping to S3 or sending notifications to the customers.

    Kafka's messaging system
    Kafka’s messaging system

    This simple setup seems to solve small problems when the number of messages being consumed daily is somewhere below a few thousand. But once your application starts growing you will start facing the latency issues in the setup. The messages will be consumed at a slower rate leading to more messages in the queue at any given time.

    As the number of producers increases, this kind of combination is hard to maintain and your broker becomes the bottleneck for your solutions to provide real-time solutions to your customers.

    The growing number of producers and consumers
    The growing number of producers and consumers

    The basic Kafka features help us to solve all the problems that the other queue systems had at that time.

    The main feature of Kafka are:

    1. It allows the saving of the messages in a fault-tolerant way by using a Log mechanism storing messages in with a timestamp.
    2. It is distributed so if one of the nodes fails, your messages are still safe at some other node.
    3. It processes messages as they occur by using the timestamp which is saved along with the message.
    4. It provides both the features of the queue and producer-consumer architecture i.e. You can send the messages to a group of consumers in which case only one of the consumers will get the message or you can send it over to all the consumers.
    Read More:  Create simple POS with React.js, Node.js, and MongoDB #13: Grouping, Sorting, Pagination

    Architecture of Kafka

    Kafka provides 4 core APIs which you can use to carry any operations related to Kafka.

    1. Producer API
    2. Consumer API
    3. Stream API
    4. Connector API
    Kafka's Architecture
    Kafka’s Architecture

    source: https://kafka.apache.org/intro

    Topic

    A topic is a unique name given to the Kafka data stream so that we can easily identify it.

    For example, All the user click data is given a separate topic name so that the consumers who want to subscribe to the given topic can subscribe to the stream easily.

    Anatomy of a Topic
    Anatomy of a Topic

    source: https://kafka.apache.org/intro

    Kafka stores the messages as a partitioned log which saves offset for any given message on the partition log. This offset is unique across the whole partitioned log.

    The performance of the Kafka is never deteriorated with the size of data being present on the Kafka this is because the only pointer that the application has to keep is the which record is being read and in case of Kafka, this is handled by the consumer.

    Comparing to others O(log N), read and write are done in constant time O(1) if you know the record ID.

    Reading and writing are independent of one another and writing to one log don’t lock it for reading or some other purposes.

    Since the messages are not removed from the Kafka after they are consumed. They are only removed when a given time is reached which is again configurable. Due to this reason, the consumer can read the messages in whatever way they want to.

    Clustered architecture

    Since Kafka is a distributed system, there are a number of instances each executing a separate instance of Kafka broker. So at any given instance a group of Kafka brokers will be receiving the messages from producers and a number of broker instances will be sending the messages to Kafka producers.

    Due to the same reason, every Kafka topic has a few partitions. A given Kafka topic can be running on multiple machines and they can have multiple partitions.

    For uniquely identifying any message on the Kafka cluster, you need to know 3 things,

    1. Topic Name of the message.
    2. Partition number of the message on a Topic.
    3. Offset value of the message on that partition.

    So, each topic has a few partitions and each partition has a few messages stored with unique offsets.

    On the destination side as well we can have a few consumers running to handle the consumption of messages. We have to tell the Consumer Group on which partitions they should be taking data from.

    To stop a message being read by two consumers, each partition is at max tied to one consumer.

    Zookeeper

    Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination.

    Zookeeper is distributed, centralized and robust coordination service which helps the Kafka nodes to coordinate between themselves. Zookeeper just helps you manage a large number of hosts with reliability.

    Zookeeper helps you with all the management tasks that you can think for while running any distributed system.

    It can help you with the availability, naming configuration management, acquiring and managing locks in the system, master-slave management and many more.

    When a producer wants to send a message to the broker, it sends a message to the broker asking for the metadata of the broker, this metadata will contain the information related to the leader broker.

    Read More:  Node.js Lesson 14: Asynchronous Development

    The metadata information is passed on to Kafka using the connected Zookeeper, which keeps the full metadata information related to the whole cluster. Once the producer knows the related information, it writes the message to the leader broker instance.

    How to install and use Kafka

    The installation part of Kafka is pretty straight forward. Go to the Kafka quickstart page and download the code.

    If you are on MAC, you can directly do,

    brew install kafka

    and then start Kafka services

    brew services start kafka

    For starting a dev environment, you will have to start the zookeeper.

    zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties

    Now you will have to start the Kafka server.

    kafka-server-start /usr/local/etc/kafka/server.properties

    Now you will have to start the Kafka topic named test on 9092 port.

    kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

    You can list down all the Kafka topics running.

    kafka-topics --list --bootstrap-server localhost:9092

    This should show the newly created topic.

    Now create a consumer subscribing to the test topic in a separate shell.

    kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning

    Log in to the producer console using the following command.

    kafka-console-producer --broker-list localhost:9092 --topic test

    Now pass in any message from the producer console and you will be able to see the message being delivered to the consumer on the other side.

    Producer vs consumer console
    Producer vs consumer console

    The next step is to create separate producers and consumers according to your needs in which the client-side you want to choose for yourself.

    Creating a Kafka producer using JavaScript

    Let’s create a simple producer application in JavaScript. Kafka clients are present in all languages, you can choose from any one of them available on the apache website.

    Let’s first install the following Kafka client using NPM.

    npm i kafka-client

    Write the following code in a file using the node shell, while console consumer with test topic is running

    const kafka = require('kafka-node');
    const Producer = kafka.Producer;
    const client = new kafka.KafkaClient();
    const producer = new Producer(client);
    
    const payloads = [
        { topic: 'test', messages: 'New sale happened', partition: 0 },
        { topic: 'test', messages: ['Refund', 'Sale'] }
    ];
    
    producer.send(payloads, function(error, data) {
        if (error) {
            console.error(error);
        } else {
            console.log(data);
        }
    

    You will see the messages appeared in the consumer console.

    Consumer console
    Consumer console

    Creating a Kafka consumer using JavaScript

    Stop the console consumer and create write a consumer using the following code,

    const Consumer = kafka.Consumer;
    const consumer = new Consumer(
        client,
            [
                { topic: 'test', partition: 0 }, { topic: 'test', partition: 1 }
            ],
            {
                autoCommit: false
            }
        );
    
    consumer.on('message', function (message) {
        console.log(message);
    });
    

    This will show all the messages available consoled out on the node terminal. You might have to tweak it a little to make it favorable for your use.

    Running javascript consumer
    Running javascript consumer

    How does Kafka handle failure

    We already have discussed that Kafka is a distributed system and we know that it is highly fault-tolerant. This means that when you use Kafka it sends the data in the broker to many nodes distributed over various systems( Might be in separate geolocations).

    So even if some of the nodes fail in the system your data will still be available.

    The number of copies made for a single message over the Kafka system (or any other distributed system) is defined using the replication factor. In a Kafka system, we apply this replication factor to a given topic.

    This way a Kafka system takes care of the failures and tries to minimize it. So, if your data is very critical, you can set the replication factor to some high number.

    Hopefully, you liked the introduction to Kafka, there is a lot more to the distributed world which you might want to explore.

    Please follow and share this article on social media.

    JavaScript programming web-development
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Ranvir Singh

      Related Posts

      Mastering REST APIs: Essential Techniques for Programmers

      December 18, 2024

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Crafting Interactive User Interfaces Using JavaScript Techniques

      December 17, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      JavaScript December 30, 2015

      Task Estimation

      First of all, I’d like to mention that everything described below is based on the real experience and is not my personal wishes.

      In order to correctly estimate the task, it is necessary to make the following steps in relation to the project:

      RxJS Methods. Part 1

      April 14, 2017

      Top Seven Remote Working Tools You Simply Need In 2020

      May 8, 2020

      GraphQL

      October 5, 2017

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Handling Mutations and Data Fetching Using React Query

      JavaScript September 12, 2023

      Enhancing B2B Lead Generation with Data and Analytics Strategies

      B2B Leads November 29, 2024

      Handling HEIC Images in Angular: A Comprehensive Tutorial

      Angular April 20, 2023

      How to build a full stack serverless application with React and Amplify

      JavaScript May 5, 2020

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      B2B Leads

      Unlock B2B Leads: Harnessing Strategic Partnerships Effectively

      Startups

      Women in Tech Series: Pioneers of ‘Coding for Everyone’ Movement

      JavaScript

      Top Seven Remote Working Tools You Simply Need In 2020

      Most Popular

      Strategies for Navigating Difficult Interview Questions Effectively

      Interview

      Build a Python Command-line Program Using OOP

      Python

      Setting CSS Styles with JavaScript

      CSS
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.