<?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: Sumit Negi</title>
    <description>The latest articles on DEV Community by Sumit Negi (@sumitnegi).</description>
    <link>https://dev.to/sumitnegi</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%2F1255250%2F8f331d97-d36e-46a8-b571-c454845f0d2a.png</url>
      <title>DEV Community: Sumit Negi</title>
      <link>https://dev.to/sumitnegi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sumitnegi"/>
    <language>en</language>
    <item>
      <title>💡 Using the `log/slog` Package in Go for Effective Logging in Web Applications</title>
      <dc:creator>Sumit Negi</dc:creator>
      <pubDate>Mon, 27 Jan 2025 15:22:00 +0000</pubDate>
      <link>https://dev.to/sumitnegi/using-the-logslog-package-in-go-for-effective-logging-in-web-applications-2ke4</link>
      <guid>https://dev.to/sumitnegi/using-the-logslog-package-in-go-for-effective-logging-in-web-applications-2ke4</guid>
      <description>&lt;h1&gt;
  
  
  💡 Using the &lt;code&gt;log/slog&lt;/code&gt; Package in Go for Effective Logging in Web Applications
&lt;/h1&gt;

&lt;p&gt;Logging is a crucial aspect of building robust and maintainable web applications. In Go, the &lt;code&gt;log/slog&lt;/code&gt; package provides a simple, structured, and concurrency-safe way to manage logs. Whether you're debugging, monitoring, or troubleshooting, proper logging allows you to track the behavior and performance of your application. In this article, we'll explore how to use the &lt;code&gt;log/slog&lt;/code&gt; package and explain its importance in a web application.&lt;/p&gt;

&lt;h2&gt;
  
  
  📊 Why Use &lt;code&gt;log/slog&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;log/slog&lt;/code&gt; package in Go allows you to log structured data with various severity levels. This structured logging is particularly useful in modern applications, where logs are used not only for debugging but also for performance tracking and error monitoring. By using structured logs, you can include more meaningful information, such as request data, method names, or error details, which is harder to achieve with traditional text-based logs.&lt;/p&gt;

&lt;h2&gt;
  
  
  📝 Key Features of &lt;code&gt;log/slog&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;📈 Structured Logging&lt;/strong&gt;: &lt;code&gt;log/slog&lt;/code&gt; supports logging with key-value pairs, making logs more readable and useful for debugging and monitoring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;🔒 Concurrency-Safety&lt;/strong&gt;: Custom loggers created by &lt;code&gt;log/slog&lt;/code&gt; are designed to be used across multiple goroutines without introducing race conditions, making them ideal for web applications where multiple requests are handled concurrently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;🔧 Custom Log Handlers&lt;/strong&gt;: You can create custom handlers that output logs in different formats (like JSON or text) or direct them to different destinations, such as files or logging services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;🔄 Log Levels&lt;/strong&gt;: The package provides several log levels (e.g., Debug, Info, Warn, Error), allowing you to categorize the importance of your log messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🛠️ Setting Up &lt;code&gt;log/slog&lt;/code&gt; in a Go Web Application
&lt;/h2&gt;

&lt;p&gt;Let’s walk through an example of using &lt;code&gt;log/slog&lt;/code&gt; in a simple Go web application. This example will show how to set up a logger and use it to log HTTP requests, errors, and other information.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔄 Step 1: Installing and Importing &lt;code&gt;log/slog&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;To get started, first import the &lt;code&gt;log/slog&lt;/code&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"log/slog"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🌟 Step 2: Create a Logger
&lt;/h3&gt;

&lt;p&gt;We can create a logger by choosing a handler. In the example below, we use the &lt;code&gt;JSONHandler&lt;/code&gt;, which formats logs as JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewJSONHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerOptions&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Level&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LevelDebug&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c"&gt;// Set the minimum log level&lt;/span&gt;
    &lt;span class="n"&gt;AddSource&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;         &lt;span class="c"&gt;// Include the source file and line number in the log&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  💪 Step 3: Log Requests and Information
&lt;/h3&gt;

&lt;p&gt;Now that we have a logger, let’s use it to log some HTTP request details. Here’s a basic example of a Go web server using &lt;code&gt;log/slog&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"flag"&lt;/span&gt;
    &lt;span class="s"&gt;"log/slog"&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
    &lt;span class="s"&gt;"os"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mux&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServeMux&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"addr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;":4000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"HTTP network address"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewJSONHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerOptions&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Level&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LevelDebug&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;AddSource&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}))&lt;/span&gt;
    &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"starting server"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"addr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mux&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"server error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"request received"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Method&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;slog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔍 Why Structured Logging is Important in a Web App
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;🎨 Easier Debugging&lt;/strong&gt;: With structured logs, you can log additional context such as request methods, paths, user IDs, and error details.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;🌐 Improved Monitoring&lt;/strong&gt;: Structured logs allow you to track user interactions, detect anomalies, and gather metrics for performance tuning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;🎉 Concurrency-Safety&lt;/strong&gt;: &lt;code&gt;log/slog&lt;/code&gt; ensures logs from multiple goroutines don’t interfere with each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;🌯 Customizable Output&lt;/strong&gt;: Easily adjust the log output format and destination.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;💡 Maintainability&lt;/strong&gt;: Structured logging helps maintain high-quality code as your application grows.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  🔮 Advanced Logging Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Log Levels&lt;/strong&gt;: Categorize messages at different severity levels.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log Rotation&lt;/strong&gt;: Archive and rotate log files for efficient storage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remote Logging&lt;/strong&gt;: Send logs to centralized systems like Grafana Loki, Datadog, or ELK stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🌟 Conclusion
&lt;/h2&gt;

&lt;p&gt;Logging is essential for any application, but structured logging takes it to the next level. By using Go's &lt;code&gt;log/slog&lt;/code&gt; package, you can generate logs that are easy to understand, filter, and analyze. In a web application, logging helps you monitor user requests, track errors, and improve the overall user experience. By following best practices and utilizing &lt;code&gt;log/slog&lt;/code&gt; features, you'll build more robust and maintainable Go applications.&lt;/p&gt;

</description>
      <category>go</category>
      <category>logging</category>
      <category>webdev</category>
      <category>devdiscuss</category>
    </item>
    <item>
      <title>AWS EC2 ALB setup with ASG</title>
      <dc:creator>Sumit Negi</dc:creator>
      <pubDate>Fri, 22 Mar 2024 09:53:49 +0000</pubDate>
      <link>https://dev.to/sumitnegi/aws-ec2-alb-setup-with-asg-1ebp</link>
      <guid>https://dev.to/sumitnegi/aws-ec2-alb-setup-with-asg-1ebp</guid>
      <description>&lt;p&gt;Creating an Amazon Machine Image (AMI) from an EC2 instance and then using it to set up an Application Load Balancer (ALB), a target group, and an Auto Scaling Group (ASG) involves several steps. This guide will walk you through the process, leveraging both the AWS Management Console for a comprehensive understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an AMI from an EC2 Instance
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using the AWS Management Console:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to the EC2 Dashboard and select "Instances" from the sidebar.&lt;/li&gt;
&lt;li&gt;Right-click the instance you wish to create an AMI from and choose "Create Image" from the context menu.&lt;/li&gt;
&lt;li&gt;In the "Create Image" dialog box, provide a unique name and description for your AMI.&lt;/li&gt;
&lt;li&gt;By default, AWS will shut down the instance, take snapshots of any attached volumes, create and register the AMI, and then reboot the instance. You can choose "No reboot" if you prefer not to shut down the instance, but be aware that this might affect the file system integrity of the created image.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating an Application Load Balancer (ALB)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using the AWS Management Console:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Go to the EC2 Dashboard and select "Load Balancers" under the "Load Balancing" section.&lt;/li&gt;
&lt;li&gt;Click "Create Load Balancer" and choose "Application Load Balancer".&lt;/li&gt;
&lt;li&gt;Follow the wizard to configure your ALB, including specifying the name, scheme (Internet-facing or Internal), and IP address type (IPv4 or Dualstack). You'll also need to select the VPC, subnets, and security groups for your ALB.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating a Target Group
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using the AWS Management Console:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to the EC2 Dashboard and select "Target Groups" under the "Load Balancing" section.&lt;/li&gt;
&lt;li&gt;Click "Create target group" and specify the name, target type (e.g., instance, IP), protocol, and port. You'll also need to select the VPC for your target group.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating an Auto Scaling Group (ASG) with a Launch Template
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Using the AWS Management Console:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Go to the EC2 Dashboard and select "Auto Scaling Groups" under the "Auto Scaling" section.&lt;/li&gt;
&lt;li&gt;Click "Create Auto Scaling group" and follow the wizard to configure your ASG. You'll need to specify the launch template, which includes the AMI ID you created earlier.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Using AWS Certificate Manager (ACM) to Forward Traffic from HTTP to HTTPS
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Request or Import an SSL/TLS Certificate in ACM:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request a Public Certificate: Navigate to the ACM console and request a public certificate for your domain. You'll need to validate ownership of the domain by adding a CNAME record to your DNS configuration or by using an email validation method.&lt;/li&gt;
&lt;li&gt;Import a Certificate: If you have an existing certificate from a third party, you can import it into ACM. This includes the certificate body, private key, and certificate chain (if applicable).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create an HTTPS Listener for Your ALB:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to the EC2 Dashboard and select "Load Balancers".&lt;/li&gt;
&lt;li&gt;Choose your ALB and navigate to the "Listeners" tab.&lt;/li&gt;
&lt;li&gt;Click "Add listener" and select "HTTPS" as the protocol.&lt;/li&gt;
&lt;li&gt;For the "Default SSL/TLS certificate", choose "From ACM" and select the certificate you requested or imported.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Configure Redirection from HTTP to HTTPS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Still in the "Listeners" tab, select the HTTP listener (if it exists) and choose "Edit".&lt;/li&gt;
&lt;li&gt;Change the "Default action(s)" to "Redirect to" and specify the HTTPS listener you created.&lt;/li&gt;
&lt;li&gt;You can choose between a temporary (HTTP 302) or permanent (HTTP 301) redirect based on your needs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Associate the ACM Certificate with Your ALB:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure that the ACM certificate is associated with your ALB as the default SSL/TLS certificate for the HTTPS listener. This step is crucial for the ALB to terminate HTTPS connections using the certificate.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Update DNS Records (Optional):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you're using a custom domain, update your DNS records to point to your ALB. This step is necessary for clients to resolve your domain name to the ALB's IP address.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Additional Considerations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Security Policy:&lt;/strong&gt; When configuring your HTTPS listener, it's recommended to use the latest predefined security policy to ensure the highest level of security.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple Certificates:&lt;/strong&gt; ALBs support multiple SSL/TLS certificates using Server Name Identification (SNI). This allows you to serve different certificates based on the domain name requested by the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By following these steps, you can secure your application by forwarding traffic from HTTP to HTTPS using ACM and configuring your ALB to use HTTPS. This setup not only enhances the security of your application but also simplifies the management of SSL/TLS certificates.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>AWS EC2 Node.js deployment</title>
      <dc:creator>Sumit Negi</dc:creator>
      <pubDate>Wed, 13 Mar 2024 18:38:09 +0000</pubDate>
      <link>https://dev.to/sumitnegi/aws-ec2-nodejs-deployment-3b63</link>
      <guid>https://dev.to/sumitnegi/aws-ec2-nodejs-deployment-3b63</guid>
      <description>&lt;p&gt;&lt;strong&gt;Step 1: Launching an EC2 Instance&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to EC2 and click on &lt;strong&gt;Launch Instance&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a name for your instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;strong&gt;Ubuntu&lt;/strong&gt; from Quick Start.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose the desired instance type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For connecting to a Key pair, use a pre-existing key or create a new one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Network setting, add a security group. A security group acts as a firewall, controlling both incoming and outgoing traffic from your instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Storage configuration is fine as &lt;strong&gt;gp2&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7ynscmunggibjaeqcnh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7ynscmunggibjaeqcnh.png" alt="Image 1" width="800" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsafyy53oopwfukha9nne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsafyy53oopwfukha9nne.png" alt="Image 2" width="800" height="586"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this connect to your EC2 instance install node and create a mini project:&lt;/p&gt;

&lt;p&gt;Installation guide:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04"&gt;https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Create a new project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;npm init -y&lt;/li&gt;
&lt;li&gt;add a new file index.js
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Example app listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;npm i pm2 express&lt;/li&gt;
&lt;li&gt;pm2 start index.js&lt;/li&gt;
&lt;li&gt;pm2 save&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Caddy&lt;br&gt;
Step 1: Install Caddy&lt;br&gt;
&lt;a href="https://caddyserver.com/docs/install"&gt;https://caddyserver.com/docs/install&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo vim /etc/caddy/Caddyfile
:80 {
    reverse_proxy localhost:3000
}
sudo systemctl restart caddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update the Caddyfile to use your domain name and enable HTTPS.&lt;/p&gt;

&lt;p&gt;sudo vim /etc/caddy/Caddyfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mydomain.com {
    reverse_proxy localhost:3000
}
sudo systemctl restart caddy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Apache Kafka: A brief beginner's guide part 1</title>
      <dc:creator>Sumit Negi</dc:creator>
      <pubDate>Tue, 13 Feb 2024 07:29:18 +0000</pubDate>
      <link>https://dev.to/sumitnegi/apache-kafka-a-brief-beginners-guide-part-1-3n93</link>
      <guid>https://dev.to/sumitnegi/apache-kafka-a-brief-beginners-guide-part-1-3n93</guid>
      <description>&lt;h1&gt;
  
  
  Kafka: An Open-Source Distributed Event Streaming Platform
&lt;/h1&gt;

&lt;p&gt;Kafka is an open-source distributed event streaming platform that enables publishing, subscribing, storing, and processing streams of records in real-time. It serves as a highly scalable, fault-tolerant, and durable messaging system, commonly used for building real-time data pipelines, streaming analytics, and event-driven architectures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Concepts of Kafka
&lt;/h2&gt;

&lt;p&gt;At its core, Kafka is a distributed publish-subscribe messaging system. Data is written to Kafka topics by producers and consumed from those topics by consumers. Kafka topics can be partitioned, enabling parallel data processing, and can be replicated across multiple brokers for fault tolerance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj61ea0cm9z1b7rqr1mbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj61ea0cm9z1b7rqr1mbt.png" alt="Image description" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Events and Event Streaming in Kafka
&lt;/h2&gt;

&lt;p&gt;Apache Kafka serves as a distributed event streaming platform used for building real-time data pipelines and streaming applications. Events and event streaming are fundamental concepts in Kafka:&lt;/p&gt;

&lt;h3&gt;
  
  
  Events
&lt;/h3&gt;

&lt;p&gt;In Kafka, an event represents a piece of data, often in the form of a message, produced by a publisher (producer) and consumed by one or more subscribers (consumers). Events are typically small units of data, such as log entries or transactions. They are immutable, meaning once produced, they cannot be modified. Instead, new events can be produced to reflect updates or changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Event Streaming
&lt;/h3&gt;

&lt;p&gt;Event streaming involves continuously capturing, processing, and distributing events in real-time. Kafka provides a distributed, fault-tolerant, and scalable infrastructure for event streaming. It allows applications to publish events to topics and consume events from topics asynchronously, facilitating various use cases such as real-time analytics, log aggregation, and messaging systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Concepts Related to Events and Event Streaming in Kafka
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Brokers
&lt;/h3&gt;

&lt;p&gt;Brokers are servers in the Kafka storage layer that store event streams from one or more sources. A Kafka cluster typically consists of several brokers. Each broker in a cluster serves as a bootstrap server, enabling connections to every broker in the cluster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Producers
&lt;/h3&gt;

&lt;p&gt;Producers are applications that generate events and publish them to Kafka topics. They determine the topics to which events will be sent and how events are partitioned among different partitions within a topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consumers
&lt;/h3&gt;

&lt;p&gt;Consumers retrieve events from Kafka topics. Each consumer maintains its position within a topic using metadata called the offset. Consumers can read events sequentially or move to specific offsets to reprocess past data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Topics
&lt;/h3&gt;

&lt;p&gt;Topics are categories or channels to which events are published. Each topic consists of partitions, allowing parallel processing and scalability. Events within topics are immutable and organized for efficient storage and retrieval.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partitions
&lt;/h3&gt;

&lt;p&gt;Partitions are segments of a topic where events are stored in an ordered sequence. Each partition is replicated across multiple Kafka brokers for fault tolerance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Offsets
&lt;/h3&gt;

&lt;p&gt;Offsets are unique identifiers assigned to each event within a partition. They track the position of consumers within a partition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consumer Groups
&lt;/h3&gt;

&lt;p&gt;Consumer groups collectively consume events from one or more topics. Each consumer group maintains its offset for each partition it consumes from, enabling parallel event processing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Replication
&lt;/h3&gt;

&lt;p&gt;Replication ensures data redundancy and fault tolerance. Kafka replicates topics across brokers, ensuring multiple copies of data for resilience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Install Docker and Docker Compose.&lt;/li&gt;
&lt;li&gt;git clone &lt;a href="https://github.com/conduktor/kafka-stack-docker-compose.git"&gt;kafka docker repo&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;cd kafka-stack-docker-compose.&lt;/li&gt;
&lt;li&gt;Run: &lt;code&gt;docker-compose -f zk-single-kafka-single.yml up -d&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Check to make sure both the services are running:
docker-compose -f zk-single-kafka-single.yml ps&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Node.js Coding
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Create a kafkaClient.js file:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;kafkajs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Kafka&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brokers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost:9092&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code initializes a Kafka client using the &lt;code&gt;kafkajs&lt;/code&gt; library, specifying a client ID and broker address for connecting to a Kafka cluster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;producer.js:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./kafkaClient.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Partitioners&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;kafkajs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;createPartitioner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Partitioners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;LegacyPartitioner&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;test-topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Hello, what's up &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code imports a Kafka client from a module, sets up a producer with a legacy partitioner, connects it to Kafka, then sends messages to the 'test-topic' topic every 3 seconds with a timestamp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;consumer.js:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./kafkaClient.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;groupId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;test-group&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;test-topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fromBeginning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;eachMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code imports a Kafka client from a module, sets up a consumer with a specified group ID, connects it to Kafka, subscribes to the 'test-topic' topic from the beginning, then runs a loop to handle incoming messages, logging their values to the console.&lt;/p&gt;

&lt;p&gt;Thank you for reading. Have a wonderful day!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>DNS basics</title>
      <dc:creator>Sumit Negi</dc:creator>
      <pubDate>Tue, 06 Feb 2024 07:25:31 +0000</pubDate>
      <link>https://dev.to/sumitnegi/dns-basics-4b08</link>
      <guid>https://dev.to/sumitnegi/dns-basics-4b08</guid>
      <description>&lt;h2&gt;
  
  
  What is DNS?
&lt;/h2&gt;

&lt;p&gt;Domain Name System which translates the human friendly hostnames into the machine IP addresses&lt;/p&gt;

&lt;p&gt;Each device connected to the Internet has a unique IP address which other machines use to find the device. DNS servers eliminate the need for humans to memorize IP addresses such as 192.168.1.1 (in IPv4), or more complex newer alphanumeric IP addresses such as 2400:cb00:2048:1::c629:d7a2 (in IPv6).&lt;/p&gt;

&lt;h2&gt;
  
  
  DNS Terminologies
&lt;/h2&gt;

&lt;p&gt;• Domain Registrar : Amazon Route 53, GoDaddy,namecheap …&lt;br&gt;
• DNS Records: A, AAAA, CNAME, NS, …&lt;br&gt;
• Zone File: contains DNS records&lt;br&gt;
• Name Server : resolves DNS queries (Authoritative or Non-Authoritative)&lt;br&gt;
• Top Level Domain (TLD): .com, .us, .in, .gov, .org, …&lt;br&gt;
• Second Level Domain (SLD): amazon.com, google.com&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6qracsr0ywk5rrc6q4e9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6qracsr0ywk5rrc6q4e9.png" alt="Image description" width="777" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 4 DNS servers involved in loading a web page:&lt;/p&gt;

&lt;p&gt;Here's a simplified explanation of the terms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DNS recursor&lt;/strong&gt;: It's like a librarian who helps you find a book in a library. When you ask for a webpage, your device talks to the DNS recursor, which then searches for the webpage's location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Root nameserver&lt;/strong&gt;: This is the first place the DNS recursor goes to find the address of a webpage. It's like the index in a library that shows where different sections of books are located.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;TLD nameserver&lt;/strong&gt;: It's like a specific section of books in a library. When the root nameserver points to a TLD nameserver, it's like finding a specific section of books related to a certain topic, such as ".com" websites.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authoritative nameserver&lt;/strong&gt;: Think of it as a dictionary on a shelf of books. It's the final place the DNS recursor checks to find the exact address of the webpage you're looking for. If it finds the address, it tells the DNS recursor, which then gives you the webpage's location.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In simpler terms, an authoritative DNS server and a recursive DNS resolver play different roles in the DNS system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Recursive DNS resolver&lt;/strong&gt;: This is like a detective that helps you find information. When you type a web address into your browser, the recursive resolver starts looking for the address. It asks other servers for help until it finds the right one. Sometimes, it already knows where to look because it remembers previous searches (caching).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authoritative DNS server&lt;/strong&gt;: This is like the ultimate source of information. It's the server that actually has the address you're looking for. When the recursive resolver finally finds the right server, that server gives it the exact address you need. This is what allows your browser to find and display the website you want to visit.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cloudflare, for example, not only provides recursive DNS resolver services like Google DNS or OpenDNS but also hosts infrastructure-level authoritative nameservers, like the F-root server network. These authoritative nameservers are essential for handling vast amounts of DNS traffic and ensuring the smooth functioning of the Internet.&lt;/p&gt;

&lt;p&gt;There are the simplified steps in a DNS lookup:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user types a website name (like example.com) into a web browser.&lt;/li&gt;
&lt;li&gt;The request travels to a DNS resolver.&lt;/li&gt;
&lt;li&gt;The resolver asks a root nameserver for help.&lt;/li&gt;
&lt;li&gt;The root nameserver points the resolver to a TLD (like .com).&lt;/li&gt;
&lt;li&gt;The resolver asks the TLD server for the domain's nameserver.&lt;/li&gt;
&lt;li&gt;The TLD server gives the resolver the nameserver's IP address.&lt;/li&gt;
&lt;li&gt;The resolver queries the domain's nameserver.&lt;/li&gt;
&lt;li&gt;The nameserver provides the IP address for the website.&lt;/li&gt;
&lt;li&gt;The resolver sends the IP address back to the web browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These steps allow the web browser to find the correct IP address for the requested website.&lt;/p&gt;

&lt;p&gt;After obtaining the IP address for example.com through the DNS lookup process, the browser proceeds with the following steps to fetch the webpage:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The browser sends a HTTP request to the obtained IP address.&lt;/li&gt;
&lt;li&gt;The server located at that IP address responds by sending the webpage content back to the browser.&lt;/li&gt;
&lt;li&gt;The browser then renders and displays the webpage for the user to interact with.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphu3lb9964iy52bimacx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphu3lb9964iy52bimacx.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>j</title>
      <dc:creator>Sumit Negi</dc:creator>
      <pubDate>Wed, 24 Jan 2024 06:32:27 +0000</pubDate>
      <link>https://dev.to/sumitnegi/j-19kb</link>
      <guid>https://dev.to/sumitnegi/j-19kb</guid>
      <description>&lt;p&gt;m&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
