<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chandraprakash Soni</title>
    <description>The latest articles on DEV Community by Chandraprakash Soni (@icpsoni).</description>
    <link>https://dev.to/icpsoni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F290968%2Fab3277fa-2f17-4208-8cb3-11c0a0d05c42.jpg</url>
      <title>DEV Community: Chandraprakash Soni</title>
      <link>https://dev.to/icpsoni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/icpsoni"/>
    <language>en</language>
    <item>
      <title>Message Queue Service using Kafka</title>
      <dc:creator>Chandraprakash Soni</dc:creator>
      <pubDate>Sun, 09 May 2021 19:18:00 +0000</pubDate>
      <link>https://dev.to/icpsoni/message-queue-service-using-kafka-2bli</link>
      <guid>https://dev.to/icpsoni/message-queue-service-using-kafka-2bli</guid>
      <description>&lt;p&gt;In this article, we are going to create a Message Queue Service using Kafka and KafkaJS, somewhere similar to SQS, and we will make sure that messages are processed exactly once, in the exact order that they are sent. We will also implement the redelivery mechanism to make sure that if something fails in our business logic, we can add it to the queue again.&lt;br&gt;
&lt;strong&gt;Find the source code on Github:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/icpsoni/kafka-message-queue"&gt;https://github.com/icpsoni/kafka-message-queue&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1: Get Kafka&lt;/strong&gt;&lt;br&gt;
Download Kafka and extract it from here, and navigate to the directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ tar -xzf &amp;lt;file_name&amp;gt;.tgz
$ cd &amp;lt;file_name&amp;gt;tec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2: Start the Kafka Environment&lt;/strong&gt;&lt;br&gt;
NOTE: Your local environment must have Java 8+ installed.&lt;br&gt;
Run the following commands to start ZooKeeper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Start the ZooKeeper service
# Note: Soon, ZooKeeper will no longer be required by Apache Kafka.
$ bin/zookeeper-server-start.sh config/zookeeper.properties
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open another terminal session and run Kafka Broker service using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Start the Kafka broker service
$ bin/kafka-server-start.sh config/server.properties
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once all services have successfully launched, you will have a basic Kafka environment running and ready to use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3: Create a Topic to Store our Queue Messages&lt;/strong&gt;&lt;br&gt;
With the following command, we will create a topic called “message-queue” in our Kafka.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ bin/kafka-topics.sh --create --topic message-queue --bootstrap-server localhost:9092
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check the created topic use this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ bin/kafka-topics.sh --describe --topic message-queue --bootstrap-server localhost:9092
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can create a topic with any number of partitions, In here we are using 1 partition only.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating Services Using KafkaJS
&lt;/h2&gt;

&lt;p&gt;We basically need 3 things to make our queue system work perfectly.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sending Events to Kafka Queue topic.&lt;/li&gt;
&lt;li&gt;Subscribing to the topic and reading the Queue Message.&lt;/li&gt;
&lt;li&gt;Handling the redelivery.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  kafka-config.js
&lt;/h3&gt;

&lt;p&gt;The following snippet contains the basic config of Kafka that we need for our queue system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Using KafkaJs nodejs library
import { Kafka } from 'kafkajs';

// kafka broker running on localhost:9092 default port
const kafkaBroker = 'localhost:9092';

// kafka topic used for queue messages
export const kafkaTopic = 'message-queue';

// kafka client with basic config
export const KafkaClient = new Kafka({
  brokers: [kafkaBroker]
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  producer.js
&lt;/h3&gt;

&lt;p&gt;This service will send messages to the Kafka topic we created earlier. We can use &lt;code&gt;sendMessageToQueue&lt;/code&gt; function and pass the message object which needs to be sent to Kafka.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { KafkaClient } from './index.js';
import { kafkaTopic } from "./kafka-config";

export const sendMessageToQueue = async (message) =&amp;gt; {
  const producer = KafkaClient.producer();
  await producer.connect();
  await producer.send({
    topic: kafkaTopic,
    messages: [
      {
        value: message // Your message data goes here
      }
    ]
  });
  // Disconnect producer once message sending is done.
  await producer.disconnect();
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  message-queue.js
&lt;/h3&gt;

&lt;p&gt;This is our main service that takes care of receiving the messages from the Kafka queue doing the business logic and handling the redelivery if something goes wrong in business logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { sendMessageToQueue } from "./producer.js";
import { KafkaClient, kafkaTopic, kafkaGroupId } from "./kafka-config.js";

export const consumeMessage = async () =&amp;gt; {
  // Creating a Consumer Instance
  const consumer = KafkaClient.consumer({
    groupId: kafkaGroupId,
  });

  await consumer.connect();
  // Subscribing to out Kafka topic
  await consumer.subscribe({ topic: kafkaTopic, fromBeginning: true});

  await consumer.run({
    autoCommit: false, // It won't commit message acknowledge to kafka until we don't do manually
    eachMessage: async ({ topic, partition, message}) =&amp;gt; {
      const messageData = message.value.toString();
      try {
        // Do the business Logic
        console.info('Received Message', messageData);
      } catch (error) {
        console.error(error);
        // Resending message to kafka queue for redelivery
        await sendMessageToQueue(messageData);
      } finally {
        const offset = +message.offset + 1;
        // Committing the message offset to Kafka
        await consumer.commitOffsets([{topic: kafkaTopic, partition, offset: offset.toString()}]);
      }
    }
  });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Download the source&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Bonus: It contains test sample files too.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://github.com/icpsoni/kafka-message-queue"&gt;https://github.com/icpsoni/kafka-message-queue&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://kafka.apache.org/quickstart"&gt;https://kafka.apache.org/quickstart&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kafka.js.org/docs/getting-started"&gt;https://kafka.js.org/docs/getting-started&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>kafka</category>
      <category>sqs</category>
      <category>queue</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Getting Started with Caching (Redis + NodeJS)</title>
      <dc:creator>Chandraprakash Soni</dc:creator>
      <pubDate>Tue, 04 Feb 2020 14:19:03 +0000</pubDate>
      <link>https://dev.to/icpsoni/getting-started-with-caching-redis-nodejs-1na6</link>
      <guid>https://dev.to/icpsoni/getting-started-with-caching-redis-nodejs-1na6</guid>
      <description>&lt;p&gt;This post is a basic introduction to caching and how it works. We will be using Redis for caching with Nodejs to show you how we can take advantage of caching to improve the speed and performance of the application and to reduce the overhead from server resources.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IVT4T5Wz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/r9f1r0kv3p2n3jvncb23.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IVT4T5Wz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/r9f1r0kv3p2n3jvncb23.png" alt="NodeJS + Caching"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Caching ?&lt;/strong&gt;&lt;br&gt;
Caching (pronounced “cashing”) is the process of storing data in a cache. A cache is a temporary storage area. A cache as a data store is easier for the client (or server) to reach, as opposed to a permanent data store that might be located on a different service, which takes more time and resources to reach (a database or an external API endpoint).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; The files you automatically request by looking at a Web page are stored on your hard disk in a cache subdirectory under the directory for your browser.&lt;/p&gt;

&lt;p&gt;Now let’s take a look on Redis. (Source: Redis Official)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Redis ?&lt;/strong&gt;&lt;br&gt;
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.&lt;/p&gt;

&lt;p&gt;Let’s build very basic project to implement caching using redis:&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@iCPSoni/getting-started-with-caching-redis-nodejs-9f90bc002c7c" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L38aG-oq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/56/56/2%2AqYdvkeoNH8Bay5SLepsKJA.jpeg" alt="Chandraprakash Soni"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@iCPSoni/getting-started-with-caching-redis-nodejs-9f90bc002c7c" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Getting Started with Caching (Redis + NodeJS) | by Chandraprakash Soni | Medium&lt;/h2&gt;
      &lt;h3&gt;Chandraprakash Soni ・ &lt;time&gt;Dec 3, 2019&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ze5yh_2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>redis</category>
      <category>node</category>
      <category>javascript</category>
      <category>cache</category>
    </item>
  </channel>
</rss>
