<?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: Krati Joshi</title>
    <description>The latest articles on DEV Community by Krati Joshi (@joshikrati03).</description>
    <link>https://dev.to/joshikrati03</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4040583%2F7d30ee5e-5124-4c74-8ef9-8fb626646302.png</url>
      <title>DEV Community: Krati Joshi</title>
      <link>https://dev.to/joshikrati03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshikrati03"/>
    <language>en</language>
    <item>
      <title>Redis for Node.js Backend Developers: Caching, Rate Limiting, and More</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Wed, 12 Aug 2026 17:04:18 +0000</pubDate>
      <link>https://dev.to/joshikrati03/redis-for-nodejs-backend-developers-caching-rate-limiting-and-more-2065</link>
      <guid>https://dev.to/joshikrati03/redis-for-nodejs-backend-developers-caching-rate-limiting-and-more-2065</guid>
      <description>&lt;p&gt;If you're preparing for a &lt;strong&gt;Node.js backend interview&lt;/strong&gt;, Redis is one of those technologies you should understand beyond just knowing that it's "fast."&lt;/p&gt;

&lt;p&gt;Redis is commonly used for &lt;strong&gt;caching, sessions, rate limiting, counters, Pub/Sub, distributed coordination, and fast data access&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, I'll cover the Redis concepts that are especially important for backend development and interviews.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What is Redis?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Redis is an in-memory data store that provides several useful data structures and very fast operations.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of primarily reading every value from disk like a traditional relational database, Redis keeps its working dataset in memory.&lt;/p&gt;

&lt;p&gt;A common backend architecture looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   ↓
Node.js / Express API
   ↓
Redis
   ↓
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redis can reduce the number of requests that reach the primary database.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ Why is Redis so fast?
&lt;/h2&gt;

&lt;p&gt;The major reason is that Redis is designed around &lt;strong&gt;in-memory data access&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application → Disk-based Database → Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application → Redis RAM → Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes Redis especially useful when the same data is requested repeatedly.&lt;/p&gt;




&lt;h1&gt;
  
  
  📦 Redis Data Types
&lt;/h1&gt;

&lt;p&gt;Redis isn't limited to simple key-value strings.&lt;/p&gt;

&lt;p&gt;Some important data types are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data Type&lt;/th&gt;
&lt;th&gt;Common Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Cache, tokens, counters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hash&lt;/td&gt;
&lt;td&gt;User/object data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;List&lt;/td&gt;
&lt;td&gt;Queues, recent items&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Set&lt;/td&gt;
&lt;td&gt;Unique values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sorted Set&lt;/td&gt;
&lt;td&gt;Leaderboards/ranking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stream&lt;/td&gt;
&lt;td&gt;Event/message processing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  String
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET user:1:name "Krati"
GET user:1:name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hash
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HSET user:1 name "Krati" role "Backend Developer"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hashes are useful when storing multiple fields belonging to an object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SADD online_users 101
SADD online_users 102
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sets automatically maintain uniqueness.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sorted Set
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ZADD leaderboard 1000 user101
ZADD leaderboard 1500 user102
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful for rankings and leaderboards.&lt;/p&gt;




&lt;h1&gt;
  
  
  ⏳ TTL — Time To Live
&lt;/h1&gt;

&lt;p&gt;When Redis is used as a cache, we usually don't want data to remain forever.&lt;/p&gt;

&lt;p&gt;We can set an expiration time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET user:1 "some-data" EX 300
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key expires after &lt;strong&gt;300 seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Check the remaining time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TTL user:1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TTL is important because it helps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove old cache data&lt;/li&gt;
&lt;li&gt;Control memory usage&lt;/li&gt;
&lt;li&gt;Reduce stale data&lt;/li&gt;
&lt;li&gt;Automatically clean temporary values&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🧠 Cache-Aside Pattern
&lt;/h1&gt;

&lt;p&gt;This is one of the &lt;strong&gt;most important Redis concepts for backend interviews&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suppose we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET /users/101
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of querying the database every time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Database
   ↓
Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we use Redis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Redis
   ↓
Cache HIT → Return data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Redis doesn't contain the data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Redis
   ↓
CACHE MISS
   ↓
Database
   ↓
Store result in Redis
   ↓
Return response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called the &lt;strong&gt;Cache-Aside pattern&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The application is responsible for reading and populating the cache.&lt;/p&gt;




&lt;h1&gt;
  
  
  🎯 Cache Hit vs Cache Miss
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Cache Hit
&lt;/h3&gt;

&lt;p&gt;The requested data exists in Redis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
 ↓
Redis
 ↓
HIT
 ↓
Return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database doesn't need to be queried.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cache Miss
&lt;/h3&gt;

&lt;p&gt;The requested data isn't present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
 ↓
Redis
 ↓
MISS
 ↓
Database
 ↓
Redis SET
 ↓
Return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A useful metric is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cache Hit Ratio =
Cache Hits / (Cache Hits + Cache Misses)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🗑️ Cache Invalidation
&lt;/h1&gt;

&lt;p&gt;One of the hardest problems with caching is &lt;strong&gt;keeping cached data consistent with the database&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suppose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Database:
User name = Alice

Redis:
User name = Alice
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the user changes their name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Database:
Alice → Bob
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But Redis still contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have &lt;strong&gt;stale cache data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A common solution is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE Database
       ↓
DELETE Redis key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&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;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;updateData&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;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;del&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`user:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next request will cause a cache miss and fetch the latest data from the database.&lt;/p&gt;




&lt;h1&gt;
  
  
  🟢 Using Redis with Node.js
&lt;/h1&gt;

&lt;p&gt;The official Node.js Redis client is &lt;code&gt;node-redis&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Basic setup:&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;createClient&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;redis&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;redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REDIS_URL&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&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;err&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Redis error:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;redis&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set a value:&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;await&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Krati&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get it:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;redis&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="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set with TTL:&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;await&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;otp:123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;456789&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="na"&gt;EX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Delete:&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;await&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;del&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;otp:123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  🚦 Redis for Rate Limiting
&lt;/h1&gt;

&lt;p&gt;Redis is also very useful for implementing &lt;strong&gt;API rate limiting&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to allow only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 attempts / minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can maintain a counter in Redis.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request
   ↓
Redis INCR
   ↓
Check count
   ↓
Limit exceeded?
   ├── YES → Reject
   └── NO  → Continue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INCR login:user123
EXPIRE login:user123 60
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redis works well here because operations such as &lt;code&gt;INCR&lt;/code&gt; are atomic.&lt;/p&gt;

&lt;p&gt;It's also shared across multiple Node.js instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             ┌── Node Server 1
Client ──────┼── Node Server 2
             └── Node Server 3
                    ↓
                  Redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All servers can use the same rate-limit state.&lt;/p&gt;




&lt;h1&gt;
  
  
  🔐 Redis for Sessions
&lt;/h1&gt;

&lt;p&gt;When an application has multiple servers, storing sessions only in one server's memory can become problematic.&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                ┌── Server 1
Client ─────────┼── Server 2
                └── Server 3
                       ↓
                     Redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All servers can access the same session information.&lt;/p&gt;

&lt;p&gt;Redis documentation specifically describes Redis as a useful shared session store for stateless application servers.&lt;/p&gt;




&lt;h1&gt;
  
  
  🔄 Atomic Operations
&lt;/h1&gt;

&lt;p&gt;An operation is &lt;strong&gt;atomic&lt;/strong&gt; when it executes as one indivisible operation from the perspective of other Redis commands.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INCR counter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET counter
   ↓
counter + 1
   ↓
SET counter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INCR counter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is especially useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Counters&lt;/li&gt;
&lt;li&gt;Rate limiting&lt;/li&gt;
&lt;li&gt;Concurrent requests&lt;/li&gt;
&lt;li&gt;Distributed applications&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  📢 Redis Pub/Sub
&lt;/h1&gt;

&lt;p&gt;Redis can also provide a publish/subscribe mechanism.&lt;/p&gt;

&lt;p&gt;Architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Publisher
    ↓
Redis Channel
    ↓
Subscribers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PUBLISH notifications "Order created"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another service can subscribe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SUBSCRIBE notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Notifications&lt;/li&gt;
&lt;li&gt;Real-time events&lt;/li&gt;
&lt;li&gt;Lightweight service communication&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Important interview point
&lt;/h3&gt;

&lt;p&gt;Redis Pub/Sub is &lt;strong&gt;not a durable message queue&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If a subscriber is disconnected, it can miss messages.&lt;/p&gt;

&lt;p&gt;For durable event processing, Redis Streams are a better concept to study. Redis Streams support persisted entries and consumer groups.&lt;/p&gt;




&lt;h1&gt;
  
  
  💾 Redis Persistence: RDB vs AOF
&lt;/h1&gt;

&lt;p&gt;Redis is primarily memory-based, but it supports persistence.&lt;/p&gt;

&lt;p&gt;Two important mechanisms are:&lt;/p&gt;

&lt;h2&gt;
  
  
  RDB
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;RDB = Redis Database snapshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redis periodically creates a point-in-time snapshot of the dataset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redis
  ↓
Snapshot
  ↓
Disk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compact&lt;/li&gt;
&lt;li&gt;Good for backups&lt;/li&gt;
&lt;li&gt;Efficient snapshot-based recovery&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changes made after the latest snapshot may be lost after a failure.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  AOF
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;AOF = Append Only File&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of only taking periodic snapshots, Redis records write operations in an append-only log.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET user:1 Alice
INCR counter
DEL user:2
        ↓
       AOF
        ↓
       Disk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When Redis restarts, the recorded operations can be replayed to reconstruct the dataset.&lt;/p&gt;

&lt;p&gt;AOF durability depends on its &lt;code&gt;fsync&lt;/code&gt; configuration; stronger durability generally comes with more I/O cost.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;RDB&lt;/th&gt;
&lt;th&gt;AOF&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Snapshot&lt;/td&gt;
&lt;td&gt;Write log&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Point-in-time&lt;/td&gt;
&lt;td&gt;Records changes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usually more compact&lt;/td&gt;
&lt;td&gt;Usually larger&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Good for backups&lt;/td&gt;
&lt;td&gt;Better durability options&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can lose recent changes&lt;/td&gt;
&lt;td&gt;Less data loss depending on fsync&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  🧹 Redis Eviction
&lt;/h1&gt;

&lt;p&gt;Redis data lives primarily in memory, so memory management is important.&lt;/p&gt;

&lt;p&gt;If Redis reaches its configured memory limit, an &lt;strong&gt;eviction policy&lt;/strong&gt; can determine what happens to keys.&lt;/p&gt;

&lt;p&gt;Some policies include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;noeviction
allkeys-lru
volatile-lru
allkeys-lfu
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interview question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What happens when Redis memory is full?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redis applies the configured memory policy. Depending on the policy, it may evict eligible keys or reject writes.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  💥 Cache Stampede
&lt;/h1&gt;

&lt;p&gt;Consider a popular API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1000 requests
      ↓
Same cached key
      ↓
Cache expires
      ↓
1000 CACHE MISS
      ↓
1000 Database queries
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database can suddenly become overloaded.&lt;/p&gt;

&lt;p&gt;This is called a &lt;strong&gt;cache stampede&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Possible solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distributed locking&lt;/li&gt;
&lt;li&gt;Request coalescing&lt;/li&gt;
&lt;li&gt;Background refresh&lt;/li&gt;
&lt;li&gt;TTL jitter&lt;/li&gt;
&lt;li&gt;Cache warming&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🔒 Redis Distributed Lock
&lt;/h1&gt;

&lt;p&gt;Redis can also be used for distributed coordination.&lt;/p&gt;

&lt;p&gt;A basic locking concept is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET lock:payment 123 NX EX 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;NX&lt;/code&gt; → set only if the key doesn't exist&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EX 30&lt;/code&gt; → lock expires after 30 seconds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This can help ensure that multiple application instances don't simultaneously perform the same critical operation.&lt;/p&gt;

&lt;p&gt;For production-grade distributed locking, however, you need to consider ownership, expiry, failures, and Redis topology rather than treating a single command as a complete locking solution.&lt;/p&gt;




&lt;h1&gt;
  
  
  🏗️ Redis in a Real Node.js Architecture
&lt;/h1&gt;

&lt;p&gt;A typical backend might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                     ┌──────────────┐
                     │   Client     │
                     └──────┬───────┘
                            ↓
                     ┌──────────────┐
                     │ Load Balancer│
                     └──────┬───────┘
                            ↓
                ┌───────────┴───────────┐
                ↓                       ↓
         Node.js Server 1       Node.js Server 2
                │                       │
                └───────────┬───────────┘
                            ↓
                         Redis
                            ↓
                         Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redis can handle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frequently accessed data&lt;/li&gt;
&lt;li&gt;Sessions&lt;/li&gt;
&lt;li&gt;Rate-limit counters&lt;/li&gt;
&lt;li&gt;Temporary tokens&lt;/li&gt;
&lt;li&gt;Distributed coordination&lt;/li&gt;
&lt;li&gt;Real-time messaging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;while the database remains the primary source of durable business data in many architectures.&lt;/p&gt;




&lt;h1&gt;
  
  
  🎯 Redis Interview Cheat Sheet
&lt;/h1&gt;

&lt;p&gt;Before a Node.js backend interview, remember:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Redis
 ↓
In-memory data store
 ↓
Fast
 ↓
Cache
 ↓
TTL
 ↓
Cache Hit / Miss
 ↓
Cache-Aside
 ↓
Cache Invalidation
 ↓
Rate Limiting
 ↓
Sessions
 ↓
Atomic Operations
 ↓
Pub/Sub
 ↓
Streams
 ↓
RDB / AOF
 ↓
Eviction
 ↓
Cache Stampede
 ↓
Distributed Lock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Questions you should be able to answer
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Why is Redis fast?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because it is designed around in-memory data access and efficient data structures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Redis vs PostgreSQL?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redis is commonly used for fast access, caching, sessions and transient/shared state; PostgreSQL is generally used for durable relational business data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What is Cache-Aside?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Application checks Redis → on miss queries DB → stores result in Redis → returns response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. How do you handle stale cache?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Invalidate or update the cache when the underlying database data changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Why Redis for rate limiting?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fast shared storage plus atomic operations such as &lt;code&gt;INCR&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. RDB vs AOF?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;RDB = snapshots.&lt;br&gt;
AOF = append-only write log.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. What happens if Redis goes down?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For cache use cases, the application should ideally fall back to the database where appropriate, with proper timeouts, error handling and monitoring.&lt;/p&gt;


&lt;h1&gt;
  
  
  💡 Final Takeaway
&lt;/h1&gt;

&lt;p&gt;Redis isn't just a "cache."&lt;/p&gt;

&lt;p&gt;For a backend developer, think of Redis as a &lt;strong&gt;high-speed shared data layer&lt;/strong&gt; that can solve several problems:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Caching       → Reduce DB load
TTL           → Automatically expire data
Rate limiting → Control requests
Sessions      → Share session state
Counters      → Atomic increments
Pub/Sub       → Lightweight messaging
Streams       → Durable event processing
Locks         → Distributed coordination
Persistence   → RDB / AOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're preparing for a &lt;strong&gt;2+ YOE Node.js interview&lt;/strong&gt;, focus less on memorizing commands and more on being able to explain &lt;strong&gt;why Redis is used, where it fits in the architecture, cache invalidation, failure scenarios, and the trade-offs involved&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  📚 Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Redis Documentation: &lt;a href="https://redis.io/docs/latest/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Redis Session Store: &lt;a href="https://redis.io/docs/latest/develop/use-cases/session-store/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/develop/use-cases/session-store/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Redis Streams: &lt;a href="https://redis.io/docs/latest/develop/data-types/streams/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/develop/data-types/streams/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Redis Distributed Locks: &lt;a href="https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  nodejs #redis #backend #javascript #webdevelopment #systemdesign #interview
&lt;/h1&gt;

&lt;p&gt;Full CheatSheet&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fopt7ugnq9ry0yssnrqcw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fopt7ugnq9ry0yssnrqcw.png" alt=" " width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>redis</category>
      <category>database</category>
      <category>node</category>
    </item>
    <item>
      <title>Postgresql Simplified</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Mon, 10 Aug 2026 18:15:49 +0000</pubDate>
      <link>https://dev.to/joshikrati03/postgresql-simplified-m8o</link>
      <guid>https://dev.to/joshikrati03/postgresql-simplified-m8o</guid>
      <description>&lt;h1&gt;
  
  
  PostgreSQL Backend Developer Essentials: The Concepts I Learned Beyond CRUD
&lt;/h1&gt;

&lt;p&gt;When working with Node.js backend applications, knowing SQL CRUD operations is only the beginning.&lt;/p&gt;

&lt;p&gt;As backend developers, we eventually need to understand transactions, indexes, concurrency, query optimization, JSONB, connection pooling, and how PostgreSQL actually executes our queries.&lt;/p&gt;

&lt;p&gt;Today I explored some of the PostgreSQL concepts that matter most for backend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  🐘 What is PostgreSQL?
&lt;/h2&gt;

&lt;p&gt;PostgreSQL is an open-source object-relational database management system (ORDBMS).&lt;/p&gt;

&lt;p&gt;It provides traditional relational database features such as tables, rows, columns, primary keys, foreign keys, and SQL, while also supporting advanced capabilities such as JSONB, arrays, custom types, functions, extensions, and specialized indexes.&lt;/p&gt;

&lt;p&gt;A typical Node.js backend flow looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   ↓
Node.js / Express
   ↓
Prisma / PostgreSQL driver
   ↓
PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔑 Primary Key vs Foreign Key
&lt;/h2&gt;

&lt;p&gt;A primary key uniquely identifies a row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A foreign key creates a relationship between tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The foreign key also helps maintain referential integrity.&lt;/p&gt;

&lt;p&gt;A simple way to remember:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Primary Key → identifies
Foreign Key → relates
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔒 Transactions and ACID
&lt;/h2&gt;

&lt;p&gt;A transaction groups multiple database operations into one logical unit.&lt;/p&gt;

&lt;p&gt;For example, a money transfer requires both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account A → -₹1000
Account B → +₹1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one operation fails, we don't want only half of the transaction to be committed.&lt;/p&gt;

&lt;p&gt;That's where ACID comes in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A → Atomicity
C → Consistency
I → Isolation
D → Durability
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Transactions help make database operations reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔄 MVCC
&lt;/h2&gt;

&lt;p&gt;One PostgreSQL concept I found particularly important is MVCC — Multi-Version Concurrency Control.&lt;/p&gt;

&lt;p&gt;PostgreSQL maintains different row versions/snapshots so concurrent transactions can work with consistent views of data.&lt;/p&gt;

&lt;p&gt;The goal is to allow reads and writes to happen concurrently with less blocking than a simple locking model would provide.&lt;/p&gt;

&lt;p&gt;For a backend developer, the key takeaway is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;MVCC is one of the mechanisms PostgreSQL uses to provide concurrency and transaction isolation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🧩 JSONB
&lt;/h2&gt;

&lt;p&gt;PostgreSQL supports JSONB for storing semi-structured data.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;preferences&lt;/span&gt; &lt;span class="n"&gt;JSONB&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"theme"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dark"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"notifications"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then query a property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;preferences&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'theme'&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSONB becomes particularly powerful when combined with appropriate indexing, such as a GIN index.&lt;/p&gt;

&lt;p&gt;However, JSONB shouldn't automatically replace relational columns. Frequently queried, constrained, and relational data is often better represented using normal columns.&lt;/p&gt;

&lt;h2&gt;
  
  
  📊 Indexes
&lt;/h2&gt;

&lt;p&gt;Indexes can significantly improve query performance by providing a faster access path to relevant rows.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_users_email&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But indexes aren't free.&lt;/p&gt;

&lt;p&gt;They consume storage and add overhead to INSERT, UPDATE, and DELETE operations because the index also needs to be maintained.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't create indexes blindly. Create them based on actual query patterns and execution plans.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🔍 EXPLAIN and EXPLAIN ANALYZE
&lt;/h2&gt;

&lt;p&gt;When a query becomes slow, we need to understand how PostgreSQL is executing it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'test@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; shows the planner's estimated execution plan.&lt;/p&gt;

&lt;p&gt;For actual execution information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'test@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; actually executes the query and reports actual runtime statistics.&lt;/p&gt;

&lt;p&gt;For deeper investigation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ANALYZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BUFFERS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can help identify CPU and I/O-related bottlenecks.&lt;/p&gt;

&lt;p&gt;Some important things to look for include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sequential Scan&lt;/li&gt;
&lt;li&gt;Index Scan&lt;/li&gt;
&lt;li&gt;Actual execution time&lt;/li&gt;
&lt;li&gt;Estimated vs actual rows&lt;/li&gt;
&lt;li&gt;Loops&lt;/li&gt;
&lt;li&gt;Buffer hits&lt;/li&gt;
&lt;li&gt;Buffer reads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Sequential Scan isn't automatically bad. If a query needs a large percentage of the table, PostgreSQL may correctly decide that scanning the table is cheaper than using an index.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 CTEs
&lt;/h2&gt;

&lt;p&gt;CTE stands for Common Table Expression.&lt;/p&gt;

&lt;p&gt;It allows us to define a named intermediate query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;active_users&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;active_users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CTEs can make complex SQL easier to read and can also be useful for recursive queries and multi-step data processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  📈 Window Functions
&lt;/h2&gt;

&lt;p&gt;Window functions allow calculations across related rows without collapsing the result into one row per group.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;RANK&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;rank&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common window functions include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROW_NUMBER()
RANK()
DENSE_RANK()
LAG()
LEAD()
SUM() OVER()
AVG() OVER()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an important distinction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GROUP BY
→ reduces/groups rows

Window Function
→ keeps rows + calculates across them
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧱 Other PostgreSQL Features
&lt;/h2&gt;

&lt;p&gt;Some other concepts worth knowing as a backend developer are:&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays
&lt;/h3&gt;

&lt;p&gt;PostgreSQL can store arrays directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;skills&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  UUID
&lt;/h3&gt;

&lt;p&gt;UUIDs provide globally unique identifiers and can be useful in distributed systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Views
&lt;/h3&gt;

&lt;p&gt;A view is a saved query that behaves like a virtual table.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions and Procedures
&lt;/h3&gt;

&lt;p&gt;PostgreSQL allows reusable logic to execute inside the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extensions
&lt;/h3&gt;

&lt;p&gt;Extensions add additional functionality to PostgreSQL.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pgcrypto
pg_trgm
PostGIS
citext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🚀 PostgreSQL in a Node.js Backend
&lt;/h2&gt;

&lt;p&gt;A typical architecture can look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   ↓
Express API
   ↓
Controller
   ↓
Service
   ↓
Prisma
   ↓
Connection Pool
   ↓
PostgreSQL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connection pooling is important because creating a new database connection for every request is expensive.&lt;/p&gt;

&lt;p&gt;A connection pool allows the application to reuse a controlled number of database connections.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 A Real Query Optimization Example
&lt;/h2&gt;

&lt;p&gt;One practical query I work with filters a large consumer table using conditions such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;DIV_CODE&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;BILL_CYC_CD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'SBM'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;CON_STATUS&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;SUPPLY_TYPE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of immediately creating an index, the better approach is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPLAIN
   ↓
EXPLAIN ANALYZE
   ↓
EXPLAIN (ANALYZE, BUFFERS)
   ↓
Inspect execution plan
   ↓
Check existing indexes
   ↓
Optimize
   ↓
Measure again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This taught me an important backend lesson:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Performance optimization should be measurement-driven, not guess-driven.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;p&gt;The PostgreSQL concepts I consider most important for backend interviews are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ACID
MVCC
Indexes
EXPLAIN ANALYZE
Connection Pooling
JSONB
JOINs
CTEs
Window Functions
Constraints
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Knowing CRUD tells us how to interact with a database.&lt;/p&gt;

&lt;p&gt;Understanding &lt;strong&gt;transactions, concurrency, indexing, query plans, and connection management&lt;/strong&gt; helps us understand how to build reliable and performant backend systems.&lt;/p&gt;

&lt;p&gt;That's the difference I'm aiming for: not just knowing how to write a query, but understanding &lt;strong&gt;what PostgreSQL is doing underneath it.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  PostgreSQL #NodeJS #BackendDevelopment #Database #SQL #WebDevelopment #Programming
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>database</category>
    </item>
    <item>
      <title>🚀 Day 14 of My Node.js Learning Journey: Understanding the HTTP Module in Node.js</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Tue, 04 Aug 2026 11:41:50 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-14-of-my-nodejs-learning-journey-understanding-the-http-module-in-nodejs-46k6</link>
      <guid>https://dev.to/joshikrati03/day-14-of-my-nodejs-learning-journey-understanding-the-http-module-in-nodejs-46k6</guid>
      <description>&lt;p&gt;As I continue my Node.js learning journey, today's focus was on one of the most fundamental concepts of backend development—the &lt;strong&gt;HTTP Module&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before learning frameworks like Express.js, it's important to understand how Node.js handles HTTP requests and responses under the hood. Since Express is built on top of the HTTP module, mastering this concept makes debugging and backend development much easier.&lt;/p&gt;




&lt;h1&gt;
  
  
  🌐 What is HTTP?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;HTTP (HyperText Transfer Protocol)&lt;/strong&gt; is the protocol that enables communication between a client (browser, mobile app, or Postman) and a server.&lt;/p&gt;

&lt;p&gt;Every time you visit a website, the following happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
   │
HTTP Request
   │
   ▼
Node.js Server
   │
HTTP Response
   ▼
Browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client sends a request, the server processes it, and then returns a response.&lt;/p&gt;




&lt;h1&gt;
  
  
  📦 What is the HTTP Module?
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;HTTP module&lt;/strong&gt; is a built-in Node.js module that allows us to create web servers and handle HTTP requests without installing any external packages.&lt;/p&gt;

&lt;p&gt;Importing the module is simple:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&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="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since it's a core module, no installation is required.&lt;/p&gt;




&lt;h1&gt;
  
  
  🛠️ Creating Your First HTTP Server
&lt;/h1&gt;

&lt;p&gt;One of the first things I learned was how easy it is to create a basic HTTP server.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&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="s2"&gt;http&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, Node.js!&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;server&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="mi"&gt;3000&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is running on port 3000&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;After running this program and opening &lt;code&gt;http://localhost:3000&lt;/code&gt;, the browser displays the response sent by the server.&lt;/p&gt;




&lt;h1&gt;
  
  
  🔍 Understanding &lt;code&gt;req&lt;/code&gt; and &lt;code&gt;res&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;The callback inside &lt;code&gt;createServer()&lt;/code&gt; receives two important objects:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;req&lt;/code&gt; (Request Object)
&lt;/h3&gt;

&lt;p&gt;Contains information sent by the client, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requested URL&lt;/li&gt;
&lt;li&gt;HTTP Method&lt;/li&gt;
&lt;li&gt;Headers&lt;/li&gt;
&lt;li&gt;Client IP&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some commonly used properties are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;req.url&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;req.method&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;req.headers&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;res&lt;/code&gt; (Response Object)
&lt;/h3&gt;

&lt;p&gt;Used to send data back to the client.&lt;/p&gt;

&lt;p&gt;Some frequently used methods include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;res.write()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res.end()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res.setHeader()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;res.writeHead()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One important thing I learned today:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Always call &lt;code&gt;res.end()&lt;/code&gt; to complete the response.&lt;/strong&gt; Without it, the client keeps waiting because the server doesn't know the response has finished.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🧭 Simple Routing Without Express
&lt;/h1&gt;

&lt;p&gt;Even without Express.js, we can create basic routes using conditions.&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;if &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;url&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Home Page&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="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &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;url&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/about&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;About Page&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="k"&gt;else&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="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;404&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Page Not Found&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 helped me understand how frameworks like Express simplify routing.&lt;/p&gt;




&lt;h1&gt;
  
  
  📌 Common HTTP Methods
&lt;/h1&gt;

&lt;p&gt;Every API uses HTTP methods to perform different operations.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;Retrieve data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;Create new data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;td&gt;Replace existing data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PATCH&lt;/td&gt;
&lt;td&gt;Update part of existing data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;Remove data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These methods form the foundation of REST APIs.&lt;/p&gt;




&lt;h1&gt;
  
  
  📄 HTTP Status Codes
&lt;/h1&gt;

&lt;p&gt;Servers use status codes to indicate the result of a request.&lt;/p&gt;

&lt;p&gt;Some commonly used ones are:&lt;/p&gt;

&lt;p&gt;✅ Success&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200 — OK&lt;/li&gt;
&lt;li&gt;201 — Created&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❌ Client Errors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;400 — Bad Request&lt;/li&gt;
&lt;li&gt;401 — Unauthorized&lt;/li&gt;
&lt;li&gt;403 — Forbidden&lt;/li&gt;
&lt;li&gt;404 — Not Found&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔥 Server Errors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;500 — Internal Server Error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learning these status codes is essential for backend interviews and API development.&lt;/p&gt;




&lt;h1&gt;
  
  
  📦 Headers and MIME Types
&lt;/h1&gt;

&lt;p&gt;HTTP headers carry metadata about requests and responses.&lt;/p&gt;

&lt;p&gt;One commonly used response header is:&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&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 tells the browser that the response contains JSON data.&lt;/p&gt;

&lt;p&gt;Some common MIME types include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;application/json&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/html&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/plain&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;image/png&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;application/pdf&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  ⚡ HTTP Module vs Express.js
&lt;/h1&gt;

&lt;p&gt;One question that often comes up is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"If Express exists, why learn the HTTP module?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer is simple.&lt;/p&gt;

&lt;p&gt;The HTTP module provides the core functionality, while Express builds on top of it by adding features like routing, middleware, and request parsing.&lt;/p&gt;

&lt;p&gt;Understanding the HTTP module gives you a much clearer picture of what happens behind the scenes whenever an Express application receives a request.&lt;/p&gt;




&lt;h1&gt;
  
  
  🎯 Key Takeaways
&lt;/h1&gt;

&lt;p&gt;Today I learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP enables communication between clients and servers.&lt;/li&gt;
&lt;li&gt;Node.js provides a built-in HTTP module for creating servers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createServer()&lt;/code&gt; creates an HTTP server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;req&lt;/code&gt; contains request information.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res&lt;/code&gt; is used to send responses.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;res.end()&lt;/code&gt; is mandatory to complete the response.&lt;/li&gt;
&lt;li&gt;Routing can be implemented without Express.&lt;/li&gt;
&lt;li&gt;HTTP methods define CRUD operations.&lt;/li&gt;
&lt;li&gt;Status codes communicate the outcome of a request.&lt;/li&gt;
&lt;li&gt;Express.js is built on top of the HTTP module.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💭 Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Today's session helped me understand what happens behind every API request.&lt;/p&gt;

&lt;p&gt;Whenever we use Express.js, call an API, or open a webpage, the HTTP module is working behind the scenes. Learning it first makes backend concepts much easier to understand and gives a stronger foundation for building scalable server-side applications.&lt;/p&gt;

&lt;p&gt;Every day of learning brings me one step closer to becoming a better backend developer. Looking forward to exploring more Node.js concepts in the coming days!&lt;/p&gt;




&lt;p&gt;If you're also learning Node.js, I'd love to hear your thoughts or discuss backend concepts in the comments. Let's keep learning together! 🚀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #ExpressJS #WebDevelopment #RESTAPI #100DaysOfCode #LearningInPublic #Programming #DevCommunity&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
      <category>ai</category>
    </item>
    <item>
      <title>🚀 Day 13 of My Node.js Learning Journey: Understanding Buffers in Node.js</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Mon, 03 Aug 2026 17:07:52 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-buffers-in-nodejs-5h9d</link>
      <guid>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-buffers-in-nodejs-5h9d</guid>
      <description>&lt;p&gt;Today, I learned one of the core concepts behind how Node.js handles binary data—&lt;strong&gt;Buffers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When working with files, images, videos, PDFs, or network communication, Node.js doesn't process everything as normal JavaScript strings. Instead, it uses &lt;strong&gt;Buffers&lt;/strong&gt; to efficiently store and manipulate raw binary data.&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 What is a Buffer?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Buffer&lt;/strong&gt; is a built-in Node.js class used to temporarily store &lt;strong&gt;raw binary data&lt;/strong&gt; in memory.&lt;/p&gt;

&lt;p&gt;Think of it as a temporary container that holds small chunks of data before they're processed or transferred.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Simple definition:&lt;/strong&gt; A Buffer is a fixed-size memory allocation used to store binary data efficiently.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  💡 Why Do We Need Buffers?
&lt;/h2&gt;

&lt;p&gt;JavaScript mainly works with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But backend applications often deal with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📄 PDF files&lt;/li&gt;
&lt;li&gt;🖼️ Images&lt;/li&gt;
&lt;li&gt;🎥 Videos&lt;/li&gt;
&lt;li&gt;🎵 Audio files&lt;/li&gt;
&lt;li&gt;🌐 Network packets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since these are binary data, Node.js uses &lt;strong&gt;Buffers&lt;/strong&gt; to handle them efficiently without converting everything into strings.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ Where Are Buffers Used?
&lt;/h2&gt;

&lt;p&gt;Buffers are used in many backend operations, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File System (&lt;code&gt;fs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Streams&lt;/li&gt;
&lt;li&gt;HTTP requests and responses&lt;/li&gt;
&lt;li&gt;TCP sockets&lt;/li&gt;
&lt;li&gt;WebSockets&lt;/li&gt;
&lt;li&gt;Image processing&lt;/li&gt;
&lt;li&gt;Video and audio streaming&lt;/li&gt;
&lt;li&gt;Encryption and compression&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're building backend applications, you're already using Buffers—even if you don't realize it.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Interesting Interview Fact
&lt;/h2&gt;

&lt;p&gt;One thing that surprised me today is that &lt;strong&gt;Buffers are not stored inside the V8 JavaScript heap.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, they are allocated in &lt;strong&gt;Native (C++) Memory&lt;/strong&gt;, which helps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce Garbage Collection overhead&lt;/li&gt;
&lt;li&gt;Improve performance&lt;/li&gt;
&lt;li&gt;Efficiently manage large binary data&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🛠️ Creating Buffers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create from a String
&lt;/h3&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;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Allocate Safe Memory
&lt;/h3&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;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Fixed size&lt;/li&gt;
&lt;li&gt;Filled with zeros&lt;/li&gt;
&lt;li&gt;Safe to use&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Allocate Fast Memory
&lt;/h3&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;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allocUnsafe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Faster allocation&lt;/li&gt;
&lt;li&gt;Memory isn't initialized&lt;/li&gt;
&lt;li&gt;Should always overwrite before using&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔥 Common Buffer Methods
&lt;/h2&gt;

&lt;p&gt;Some methods I learned today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Buffer.from()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Buffer.alloc()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Buffer.allocUnsafe()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;toString()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;write()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;copy()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;slice()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Buffer.concat()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These methods are frequently used while working with files and streams.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 Buffer vs Stream
&lt;/h2&gt;

&lt;p&gt;This was the most important takeaway for me.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Buffer&lt;/th&gt;
&lt;th&gt;Stream&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Stores binary data temporarily&lt;/td&gt;
&lt;td&gt;Transfers data continuously&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fixed-size memory&lt;/td&gt;
&lt;td&gt;Chunk-by-chunk data flow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Holds data&lt;/td&gt;
&lt;td&gt;Moves data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used internally by Streams&lt;/td&gt;
&lt;td&gt;Uses Buffers internally&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Easy way to remember:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer stores. Stream transports.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🎯 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Buffers store &lt;strong&gt;raw binary data&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;They are &lt;strong&gt;fixed-size&lt;/strong&gt; and &lt;strong&gt;mutable&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Buffers are stored in &lt;strong&gt;Native (C++) Memory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Streams use Buffers internally&lt;/li&gt;
&lt;li&gt;They improve performance while working with files and network communication&lt;/li&gt;
&lt;li&gt;Essential for backend development and frequently asked in Node.js interviews&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📚 What I Learned Today
&lt;/h2&gt;

&lt;p&gt;Today's topic helped me understand what actually happens behind the scenes when Node.js reads a file or receives data over the network.&lt;/p&gt;

&lt;p&gt;Instead of loading everything into memory at once, Node.js processes data efficiently using Buffers, making applications faster and more memory-efficient.&lt;/p&gt;

&lt;p&gt;Every new Node.js concept makes the backend ecosystem feel more connected. Understanding Buffers also makes it much easier to understand Streams, file handling, and network programming.&lt;/p&gt;




&lt;p&gt;Thanks for reading! 🙌&lt;/p&gt;

&lt;p&gt;I'm currently documenting my &lt;strong&gt;Node.js interview preparation journey&lt;/strong&gt; by sharing what I learn every day. If you're preparing for backend interviews or starting with Node.js, feel free to follow along.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #WebDevelopment #Programming #100DaysOfCode #Coding #OpenSource #LearningInPublic #DevCommunity&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>🚀 Day 12 of My Node.js Learning Journey: Mastering Streams &amp; Backpressure 📂⚡</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sun, 02 Aug 2026 11:07:22 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-12-of-my-nodejs-learning-journey-mastering-streams-backpressure-3889</link>
      <guid>https://dev.to/joshikrati03/day-12-of-my-nodejs-learning-journey-mastering-streams-backpressure-3889</guid>
      <description>&lt;p&gt;One of the biggest strengths of Node.js is its ability to process large amounts of data efficiently. Today, I explored &lt;strong&gt;Streams&lt;/strong&gt;, one of the most important concepts for backend development and a frequently asked topic in Node.js interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 What are Streams?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Stream&lt;/strong&gt; is a way to process data &lt;strong&gt;chunk by chunk&lt;/strong&gt; instead of loading the entire file into memory.&lt;/p&gt;

&lt;p&gt;Instead of reading a 2 GB file at once, Node.js reads small chunks (typically &lt;strong&gt;64 KB&lt;/strong&gt; for &lt;code&gt;fs.createReadStream()&lt;/code&gt;), making applications much more memory-efficient and scalable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use Streams?
&lt;/h3&gt;

&lt;p&gt;✅ Lower memory usage&lt;/p&gt;

&lt;p&gt;✅ Faster processing&lt;/p&gt;

&lt;p&gt;✅ Better performance&lt;/p&gt;

&lt;p&gt;✅ Ideal for large files&lt;/p&gt;

&lt;p&gt;✅ Non-blocking data transfer&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 What is a Chunk?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;chunk&lt;/strong&gt; is a small piece of data transferred by a stream.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 GB File
      ↓
Load everything into RAM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Streams work like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 GB File
      ↓
64 KB
      ↓
64 KB
      ↓
64 KB
      ↓
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why Node.js can efficiently handle file uploads, downloads, video streaming, and large datasets.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌊 Types of Streams
&lt;/h2&gt;

&lt;p&gt;Node.js provides four types of streams:&lt;/p&gt;

&lt;h3&gt;
  
  
  📖 Readable Stream
&lt;/h3&gt;

&lt;p&gt;Used to read data from a source.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createReadStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Request&lt;/li&gt;
&lt;li&gt;&lt;code&gt;process.stdin&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✍️ Writable Stream
&lt;/h3&gt;

&lt;p&gt;Used to write data to a destination.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createWriteStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Response&lt;/li&gt;
&lt;li&gt;&lt;code&gt;process.stdout&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔄 Duplex Stream
&lt;/h3&gt;

&lt;p&gt;Can both &lt;strong&gt;read&lt;/strong&gt; and &lt;strong&gt;write&lt;/strong&gt; data.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TCP Sockets&lt;/li&gt;
&lt;li&gt;Network connections&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ⚙️ Transform Stream
&lt;/h3&gt;

&lt;p&gt;A special type of Duplex Stream that modifies data while passing it through.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compression (&lt;code&gt;zlib.createGzip()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Encryption&lt;/li&gt;
&lt;li&gt;Decryption&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🚀 &lt;code&gt;pipe()&lt;/code&gt; — The Easiest Way to Transfer Data
&lt;/h1&gt;

&lt;p&gt;Instead of manually reading and writing chunks:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&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="s2"&gt;fs&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;readStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createReadStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input.txt&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;writeStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createWriteStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;readStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;writeStream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Benefits of &lt;code&gt;pipe()&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Less code&lt;/li&gt;
&lt;li&gt;Automatic chunk transfer&lt;/li&gt;
&lt;li&gt;Handles flow efficiently&lt;/li&gt;
&lt;li&gt;Automatically ends the destination stream&lt;/li&gt;
&lt;li&gt;Manages backpressure internally&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🌊 Understanding Backpressure
&lt;/h1&gt;

&lt;p&gt;One of the most interesting concepts I learned today was &lt;strong&gt;Backpressure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📥 Producer can generate &lt;strong&gt;100 MB/sec&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;📤 Consumer can process only &lt;strong&gt;20 MB/sec&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without flow control, data would keep accumulating in memory.&lt;/p&gt;

&lt;p&gt;Backpressure solves this by &lt;strong&gt;pausing the producer until the consumer catches up&lt;/strong&gt;, preventing unnecessary memory growth and improving stability.&lt;/p&gt;

&lt;p&gt;Node.js handles this automatically when using &lt;code&gt;pipe()&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  💧 &lt;code&gt;highWaterMark&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;highWaterMark&lt;/code&gt; defines the &lt;strong&gt;buffer threshold&lt;/strong&gt; that helps determine when backpressure should start.&lt;/p&gt;

&lt;p&gt;Some useful defaults:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fs.createReadStream()&lt;/code&gt; → &lt;strong&gt;64 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Most Readable Streams → &lt;strong&gt;16 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Most Writable Streams → &lt;strong&gt;16 KB&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to remember that &lt;strong&gt;&lt;code&gt;highWaterMark&lt;/code&gt; is a threshold, not a hard memory limit&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  🔄 Flowing Mode vs Paused Mode
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Flowing Mode
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Uses the &lt;code&gt;data&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;Data flows automatically&lt;/li&gt;
&lt;li&gt;Most commonly used&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Paused Mode
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Uses the &lt;code&gt;readable&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;Data is read manually using &lt;code&gt;stream.read()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Gives more control over reading&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  📦 Buffer vs Stream
&lt;/h1&gt;

&lt;p&gt;One interview question that helped me understand the difference:&lt;/p&gt;

&lt;h3&gt;
  
  
  Buffer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Temporary memory&lt;/li&gt;
&lt;li&gt;Stores binary data&lt;/li&gt;
&lt;li&gt;Holds one chunk at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stream
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Transfers data continuously&lt;/li&gt;
&lt;li&gt;Processes multiple chunks&lt;/li&gt;
&lt;li&gt;Better suited for large files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple way to remember it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer stores data. Stream moves data.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🚀 &lt;code&gt;pipeline()&lt;/code&gt; vs &lt;code&gt;pipe()&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Although &lt;code&gt;pipe()&lt;/code&gt; is simple and powerful, Node.js provides &lt;code&gt;pipeline()&lt;/code&gt; for production-ready applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use &lt;code&gt;pipeline()&lt;/code&gt;?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Better error handling&lt;/li&gt;
&lt;li&gt;Automatically cleans up streams&lt;/li&gt;
&lt;li&gt;Prevents resource leaks&lt;/li&gt;
&lt;li&gt;Recommended for production code&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🎯 Interview Takeaways
&lt;/h1&gt;

&lt;p&gt;Today's most important interview concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a Stream?&lt;/li&gt;
&lt;li&gt;Why Streams are better than &lt;code&gt;fs.readFile()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Readable vs Writable Stream&lt;/li&gt;
&lt;li&gt;Duplex vs Transform Stream&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pipe()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Backpressure&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;drain&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pause()&lt;/code&gt; and &lt;code&gt;resume()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;highWaterMark&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Flowing vs Paused Mode&lt;/li&gt;
&lt;li&gt;Buffer vs Stream&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pipeline()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  📚 Key Learning
&lt;/h1&gt;

&lt;p&gt;Streams are one of the core reasons why Node.js is highly efficient for backend development. They allow applications to process large files with minimal memory usage while maintaining excellent performance and scalability.&lt;/p&gt;

&lt;p&gt;Understanding &lt;strong&gt;Streams, Backpressure, and &lt;code&gt;pipe()&lt;/code&gt;&lt;/strong&gt; has given me a much deeper appreciation of how Node.js handles data under the hood.&lt;/p&gt;

&lt;p&gt;Looking forward to learning the &lt;strong&gt;HTTP module&lt;/strong&gt; next! 🚀&lt;/p&gt;




&lt;p&gt;If you have any interview tips or real-world use cases for Streams, I'd love to hear them in the comments.&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #WebDevelopment #Programming #Coding #100DaysOfCode #OpenSource #Developer #Streams #LearningInPublic
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>🚀 Day 11 of My Node.js Learning Journey: Understanding Events &amp; EventEmitter</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sat, 01 Aug 2026 15:30:42 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-events-eventemitter-4l97</link>
      <guid>https://dev.to/joshikrati03/day-11-of-my-nodejs-learning-journey-understanding-events-eventemitter-4l97</guid>
      <description>&lt;p&gt;One of the biggest reasons Node.js is so powerful is its &lt;strong&gt;event-driven architecture&lt;/strong&gt;. Today, I learned how Node.js uses &lt;strong&gt;events&lt;/strong&gt; to build scalable and loosely coupled applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 What are Events?
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;event&lt;/strong&gt; is an action or occurrence that happens during the execution of a program. Instead of constantly checking whether something has happened, Node.js waits for an event and reacts when it occurs.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User logs in&lt;/li&gt;
&lt;li&gt;HTTP request reaches the server&lt;/li&gt;
&lt;li&gt;File reading completes&lt;/li&gt;
&lt;li&gt;Database connection is established&lt;/li&gt;
&lt;li&gt;Timer finishes&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📌 EventEmitter
&lt;/h2&gt;

&lt;p&gt;Node.js provides the built-in &lt;strong&gt;EventEmitter&lt;/strong&gt; class from the &lt;code&gt;events&lt;/code&gt; module to create and handle custom events.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&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="s2"&gt;events&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;emitter&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;EventEmitter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;greet&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, Developer!&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;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;greet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Developer!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔥 Common EventEmitter Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ &lt;code&gt;on()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Registers a listener for an event.&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="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User Logged In&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;h3&gt;
  
  
  ✅ &lt;code&gt;emit()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Triggers an event and executes all registered listeners.&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="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;once()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Runs the listener only the first time the event is emitted.&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="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;once&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;welcome&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome!&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;h3&gt;
  
  
  ✅ Passing Data with Events
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&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;username&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;`Welcome &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;username&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="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Krati&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ Multiple Listeners
&lt;/h3&gt;

&lt;p&gt;A single event can notify multiple independent listeners.&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="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Inventory Updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Email Sent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&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="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invoice Generated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orderPlaced&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚠️ The Special &lt;code&gt;error&lt;/code&gt; Event
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;"error"&lt;/code&gt; event is treated specially in Node.js. If it is emitted without an error listener, the application may terminate.&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="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&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;err&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;emitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&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;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something went wrong&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always register an &lt;code&gt;"error"&lt;/code&gt; listener when emitting error events.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌍 Real-World Example
&lt;/h2&gt;

&lt;p&gt;Imagine an e-commerce application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Placed
      │
      ├── Update Inventory
      ├── Send Confirmation Email
      ├── Generate Invoice
      ├── Notify Admin
      └── Reward Loyalty Points
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of calling every function manually, the application emits a single &lt;code&gt;orderPlaced&lt;/code&gt; event, and different modules respond independently. This makes the system easier to maintain and extend.&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Interview Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;EventEmitter is provided by the built-in &lt;code&gt;events&lt;/code&gt; module.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;on()&lt;/code&gt; registers a listener.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;emit()&lt;/code&gt; triggers an event.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;once()&lt;/code&gt; executes only once.&lt;/li&gt;
&lt;li&gt;One event can have multiple listeners.&lt;/li&gt;
&lt;li&gt;EventEmitter listeners execute &lt;strong&gt;synchronously&lt;/strong&gt; by default.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;"error"&lt;/code&gt; event should always have an error listener.&lt;/li&gt;
&lt;li&gt;EventEmitter is &lt;strong&gt;different from the Event Loop&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Streams, HTTP servers, and the &lt;code&gt;process&lt;/code&gt; object all use EventEmitter internally.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📚 What I Learned Today
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;✔️ What events are in Node.js&lt;/li&gt;
&lt;li&gt;✔️ How EventEmitter works&lt;/li&gt;
&lt;li&gt;✔️ Difference between &lt;code&gt;on()&lt;/code&gt;, &lt;code&gt;emit()&lt;/code&gt;, and &lt;code&gt;once()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;✔️ Passing data through events&lt;/li&gt;
&lt;li&gt;✔️ Handling multiple listeners&lt;/li&gt;
&lt;li&gt;✔️ Why the &lt;code&gt;error&lt;/code&gt; event is important&lt;/li&gt;
&lt;li&gt;✔️ Real-world use cases of EventEmitter&lt;/li&gt;
&lt;li&gt;✔️ Why Node.js follows an event-driven architecture&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💭 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Events are one of the core concepts that make Node.js efficient and scalable. By using &lt;strong&gt;EventEmitter&lt;/strong&gt;, different parts of an application can communicate without being tightly connected, making code cleaner, more modular, and easier to maintain.&lt;/p&gt;

&lt;p&gt;I'm excited to continue exploring more Node.js concepts in the coming days! 🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #WebDevelopment #100DaysOfCode #OpenSource #Coding #EventEmitter #LearningInPublic #DevCommunity&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>javascriptlibraries</category>
      <category>programming</category>
    </item>
    <item>
      <title>🚀 Day 10 of My Node.js Learning Journey – Mastering the Path &amp; OS Modules</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Fri, 31 Jul 2026 14:42:50 +0000</pubDate>
      <link>https://dev.to/joshikrati03/day-10-of-my-nodejs-learning-journey-mastering-the-path-os-modules-49hg</link>
      <guid>https://dev.to/joshikrati03/day-10-of-my-nodejs-learning-journey-mastering-the-path-os-modules-49hg</guid>
      <description>&lt;p&gt;One thing I really like about Node.js is that it provides several built-in modules that solve common backend problems without requiring any external packages.&lt;/p&gt;

&lt;p&gt;Today I learned two of those modules: &lt;strong&gt;Path&lt;/strong&gt; and &lt;strong&gt;OS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At first, these modules might look “too basic” or even optional—but in reality, they are &lt;strong&gt;fundamental building blocks of backend development&lt;/strong&gt;. We don’t just learn them for interviews; we learn them because they solve real problems that every backend application eventually faces.&lt;/p&gt;




&lt;h1&gt;
  
  
  🤔 Why do we even need to learn Path &amp;amp; OS modules?
&lt;/h1&gt;

&lt;p&gt;Before jumping into methods, it’s important to understand &lt;em&gt;why these modules matter in the first place&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ 1. Because backend apps deal with files and servers constantly
&lt;/h3&gt;

&lt;p&gt;In real-world Node.js applications, we often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upload files (images, PDFs, videos)&lt;/li&gt;
&lt;li&gt;Store logs&lt;/li&gt;
&lt;li&gt;Read configuration files&lt;/li&gt;
&lt;li&gt;Serve static assets&lt;/li&gt;
&lt;li&gt;Deploy apps on different servers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these involve &lt;strong&gt;file paths and system-level information&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Without the &lt;code&gt;path&lt;/code&gt; module, we would be manually handling strings like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uploads/images/profile.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This becomes risky because different operating systems handle paths differently.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 2. Because Node.js runs on different operating systems
&lt;/h3&gt;

&lt;p&gt;Your code might run on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows (development)&lt;/li&gt;
&lt;li&gt;Linux (production servers)&lt;/li&gt;
&lt;li&gt;macOS (team machines)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each OS behaves differently in terms of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File paths&lt;/li&gt;
&lt;li&gt;CPU architecture&lt;/li&gt;
&lt;li&gt;Memory availability&lt;/li&gt;
&lt;li&gt;System uptime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;os&lt;/code&gt; module helps us &lt;strong&gt;write code that adapts to the environment instead of breaking in production&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 3. Because production systems need reliability
&lt;/h3&gt;

&lt;p&gt;Small mistakes like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;wrong file path&lt;/li&gt;
&lt;li&gt;incorrect directory reference&lt;/li&gt;
&lt;li&gt;assuming server specs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;can cause real production bugs.&lt;/p&gt;

&lt;p&gt;These modules help prevent that.&lt;/p&gt;




&lt;h3&gt;
  
  
  ✅ 4. Because interviews expect this understanding
&lt;/h3&gt;

&lt;p&gt;Companies don’t just ask “what is path.join()?”&lt;/p&gt;

&lt;p&gt;They want to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do you understand file system behavior?&lt;/li&gt;
&lt;li&gt;Do you know cross-platform issues?&lt;/li&gt;
&lt;li&gt;Can you write production-safe backend code?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So yes—this is also &lt;strong&gt;high-frequency interview knowledge&lt;/strong&gt;, but more importantly, it’s &lt;strong&gt;real engineering knowledge&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  📂 The Path Module
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;path&lt;/code&gt; module helps us work with file and directory paths in a &lt;strong&gt;cross-platform way&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Why is this important?&lt;/p&gt;

&lt;p&gt;Windows and Linux use different path formats:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Windows&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\Krati\Documents\resume.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Linux/macOS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/home/krati/Documents/resume.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we manually concatenate strings, our application may break on different systems. The &lt;code&gt;path&lt;/code&gt; module solves this by generating correct paths automatically.&lt;/p&gt;




&lt;h3&gt;
  
  
  Key Methods I Learned
&lt;/h3&gt;

&lt;p&gt;✅ &lt;strong&gt;path.join()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Safely joins multiple path segments.&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="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uploads&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;images&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;profile.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Best for building folder structures dynamically.&lt;/p&gt;




&lt;p&gt;✅ &lt;strong&gt;path.resolve()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Converts a relative path into an absolute path.&lt;/p&gt;

&lt;p&gt;Useful when a library or API requires a full file location.&lt;/p&gt;




&lt;p&gt;✅ &lt;strong&gt;path.basename()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Extracts the filename from a full path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/users/docs/resume.pdf → resume.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;✅ &lt;strong&gt;path.dirname()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns the parent directory of a file.&lt;/p&gt;




&lt;p&gt;✅ &lt;strong&gt;path.extname()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns file extension like &lt;code&gt;.pdf&lt;/code&gt;, &lt;code&gt;.jpg&lt;/code&gt;, &lt;code&gt;.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Very useful for file validation during uploads.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⭐ My Biggest Learning Today
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;path.join()&lt;/code&gt; vs &lt;code&gt;path.resolve()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Before today, I thought they were interchangeable.&lt;/p&gt;

&lt;p&gt;Now I understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;path.join()&lt;/code&gt;&lt;/strong&gt; → just combines path segments&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;path.resolve()&lt;/code&gt;&lt;/strong&gt; → builds an absolute path from the current working directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This difference matters a lot in real backend projects.&lt;/p&gt;




&lt;h1&gt;
  
  
  📁 Another Important Concept
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__dirname&lt;/code&gt; vs &lt;code&gt;process.cwd()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is a classic Node.js interview question—but also a real-world debugging tool.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;__dirname&lt;/code&gt;&lt;/strong&gt; → directory of the current file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;process.cwd()&lt;/code&gt;&lt;/strong&gt; → directory where the Node app was started&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This difference becomes important when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loading config files&lt;/li&gt;
&lt;li&gt;serving static files&lt;/li&gt;
&lt;li&gt;handling uploads&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💻 The OS Module
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;os&lt;/code&gt; module gives us information about the operating system.&lt;/p&gt;

&lt;p&gt;Instead of guessing server details, we can directly access them using Node.js.&lt;/p&gt;




&lt;h3&gt;
  
  
  Useful methods I explored:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;os.platform()&lt;/code&gt; → OS type (win32, linux, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.arch()&lt;/code&gt; → CPU architecture&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.hostname()&lt;/code&gt; → machine name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.cpus()&lt;/code&gt; → CPU core details&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.totalmem()&lt;/code&gt; → total RAM&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.freemem()&lt;/code&gt; → available RAM&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;os.uptime()&lt;/code&gt; → system running time&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🌍 Real-World Backend Usage
&lt;/h1&gt;

&lt;p&gt;These modules are not theoretical—they are used in production systems daily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Path module is used for:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;File uploads&lt;/li&gt;
&lt;li&gt;Static file serving&lt;/li&gt;
&lt;li&gt;Configuration management&lt;/li&gt;
&lt;li&gt;Cross-platform file handling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  OS module is used for:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Health check APIs&lt;/li&gt;
&lt;li&gt;Monitoring dashboards&lt;/li&gt;
&lt;li&gt;Performance tracking&lt;/li&gt;
&lt;li&gt;Scaling decisions (based on CPU cores)&lt;/li&gt;
&lt;li&gt;Logging system environment details&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  🎯 Interview Takeaways
&lt;/h1&gt;

&lt;p&gt;Today’s key concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why we should never manually build file paths&lt;/li&gt;
&lt;li&gt;Difference between &lt;code&gt;path.join()&lt;/code&gt; and &lt;code&gt;path.resolve()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Difference between &lt;code&gt;__dirname&lt;/code&gt; and &lt;code&gt;process.cwd()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Why OS-level information matters in backend systems&lt;/li&gt;
&lt;li&gt;Real-world use cases of Path and OS modules&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  💡 What I Learned Today
&lt;/h1&gt;

&lt;p&gt;The biggest realization today is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Backend development is not just about writing APIs—it’s about understanding the environment your code runs in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;path&lt;/code&gt; module ensures our file handling is safe and cross-platform, while the &lt;code&gt;os&lt;/code&gt; module helps our application understand the system it is running on.&lt;/p&gt;

&lt;p&gt;These modules may look small, but they are &lt;strong&gt;core to building reliable, production-ready backend systems&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;Looking forward to continuing my Node.js journey and exploring more modules that power real-world applications! 🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;#NodeJS #JavaScript #BackendDevelopment #WebDevelopment #100DaysOfCode #LearningInPublic #ExpressJS #SoftwareEngineering #Coding #SystemDesign&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀 fs Module=&gt; Loaded</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Thu, 30 Jul 2026 17:16:14 +0000</pubDate>
      <link>https://dev.to/joshikrati03/fs-module-loaded-14od</link>
      <guid>https://dev.to/joshikrati03/fs-module-loaded-14od</guid>
      <description>&lt;p&gt;Today I explored one of the most fundamental Node.js modules—&lt;strong&gt;the File System (&lt;code&gt;fs&lt;/code&gt;) module&lt;/strong&gt;. It's the module that enables Node.js applications to interact with files and directories, making it essential for backend development.&lt;/p&gt;

&lt;h2&gt;
  
  
  💡 Key Takeaways
&lt;/h2&gt;

&lt;p&gt;✅ &lt;strong&gt;&lt;code&gt;fs&lt;/code&gt; is a built-in module&lt;/strong&gt; – No installation required.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Three ways to work with files&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callback API&lt;/li&gt;
&lt;li&gt;Promise API (&lt;code&gt;fs/promises&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Synchronous API (&lt;code&gt;readFileSync&lt;/code&gt;, &lt;code&gt;writeFileSync&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Asynchronous operations are preferred&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't block the Event Loop.&lt;/li&gt;
&lt;li&gt;File operations are handled by &lt;strong&gt;libuv's thread pool&lt;/strong&gt;, allowing the server to continue processing other requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Common File Operations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fs.readFile()&lt;/code&gt; → Read a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.writeFile()&lt;/code&gt; → Create or overwrite a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.appendFile()&lt;/code&gt; → Add content to an existing file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.rename()&lt;/code&gt; → Rename a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.unlink()&lt;/code&gt; → Delete a file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.mkdir()&lt;/code&gt; → Create a directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.readdir()&lt;/code&gt; → Read directory contents&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.stat()&lt;/code&gt; → Get file metadata&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ &lt;strong&gt;Always handle errors&lt;/strong&gt;&lt;br&gt;
Every file operation can fail (missing file, permissions, etc.), so proper error handling is essential.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Use Streams for large files&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;fs.readFile()&lt;/code&gt; loads the entire file into memory, whereas streams process data in chunks, making them much more memory-efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎯 Interview Nuggets
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;There is &lt;strong&gt;no &lt;code&gt;fs.createFile()&lt;/code&gt;&lt;/strong&gt; method. Use &lt;code&gt;fs.writeFile()&lt;/code&gt; to create a new file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.writeFile()&lt;/code&gt; overwrites existing content, while &lt;code&gt;fs.appendFile()&lt;/code&gt; preserves it and adds new data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fs.readFile()&lt;/code&gt; is asynchronous and doesn't block the Event Loop.&lt;/li&gt;
&lt;li&gt;Avoid synchronous (&lt;code&gt;Sync&lt;/code&gt;) methods in production request handlers because they block the Event Loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📚 Example
&lt;/h2&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;fs&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="s2"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;users.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf8&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;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&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="nx"&gt;data&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;Every backend developer works with files at some point—whether it's reading configuration files, generating reports, processing uploads, or maintaining logs. Understanding the &lt;code&gt;fs&lt;/code&gt; module is a foundational step toward building robust Node.js applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Complete. On to Path &amp;amp; OS Module next! 🚀&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #WebDevelopment #100DaysOfCode #OpenToWork #LearningInPublic #FullStack #SoftwareEngineer #Developers
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀 Node.js Streams &amp; Buffers Explained: Handle Large Files Without Crashing Your Server</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Wed, 29 Jul 2026 17:39:42 +0000</pubDate>
      <link>https://dev.to/joshikrati03/nodejs-streams-buffers-explained-handle-large-files-without-crashing-your-server-3lb8</link>
      <guid>https://dev.to/joshikrati03/nodejs-streams-buffers-explained-handle-large-files-without-crashing-your-server-3lb8</guid>
      <description>&lt;p&gt;If you've ever used &lt;code&gt;fs.readFile()&lt;/code&gt; to load a large file, you might have noticed high memory usage or even application crashes.&lt;/p&gt;

&lt;p&gt;The reason? &lt;code&gt;fs.readFile()&lt;/code&gt; loads the &lt;strong&gt;entire file into memory&lt;/strong&gt; before your application can process it.&lt;/p&gt;

&lt;p&gt;Node.js solves this problem with &lt;strong&gt;Streams&lt;/strong&gt; and &lt;strong&gt;Buffers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand why they are one of the most important concepts for every backend developer.&lt;/p&gt;




&lt;h2&gt;
  
  
  📌 What is a Stream?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Stream&lt;/strong&gt; is a way to process data &lt;strong&gt;chunk by chunk&lt;/strong&gt; instead of loading everything into memory at once.&lt;/p&gt;

&lt;p&gt;Think of watching a movie on Netflix. The entire movie isn't downloaded before playback starts—you receive small chunks continuously.&lt;/p&gt;

&lt;p&gt;That's exactly how Streams work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use Streams?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;✅ Memory Efficient&lt;/li&gt;
&lt;li&gt;✅ Better Performance&lt;/li&gt;
&lt;li&gt;✅ Handles Large Files&lt;/li&gt;
&lt;li&gt;✅ Ideal for Network Communication&lt;/li&gt;
&lt;li&gt;✅ Used in File Uploads &amp;amp; Downloads&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📦 What is a Buffer?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Buffer&lt;/strong&gt; is a temporary memory area that stores &lt;strong&gt;raw binary data&lt;/strong&gt; before it's processed or transmitted.&lt;/p&gt;

&lt;p&gt;Whenever Node.js reads data from a file or receives data from a network, it first stores it in a Buffer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Buffer = Temporary Binary Storage&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🌊 Types of Streams
&lt;/h2&gt;

&lt;p&gt;Node.js provides four types of Streams:&lt;/p&gt;

&lt;h3&gt;
  
  
  1️⃣ Readable Stream
&lt;/h3&gt;

&lt;p&gt;Reads data from a source.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createReadStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Request&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2️⃣ Writable Stream
&lt;/h3&gt;

&lt;p&gt;Writes data to a destination.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.createWriteStream()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;HTTP Response&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3️⃣ Duplex Stream
&lt;/h3&gt;

&lt;p&gt;Supports both reading and writing.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TCP Socket&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4️⃣ Transform Stream
&lt;/h3&gt;

&lt;p&gt;Reads, modifies, and writes data.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compression&lt;/li&gt;
&lt;li&gt;Encryption&lt;/li&gt;
&lt;li&gt;Decompression&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔄 &lt;code&gt;pipe()&lt;/code&gt; — Connecting Streams
&lt;/h2&gt;

&lt;p&gt;One of the most useful Stream methods is &lt;code&gt;pipe()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of manually reading chunks and writing them yourself, you can directly connect a Readable Stream to a Writable Stream.&lt;/p&gt;

&lt;p&gt;This keeps the code simple and automatically handles data flow efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚦 Backpressure
&lt;/h2&gt;

&lt;p&gt;Imagine a fast producer sending data to a slow consumer.&lt;/p&gt;

&lt;p&gt;Without flow control, memory usage keeps increasing.&lt;/p&gt;

&lt;p&gt;Node.js solves this using &lt;strong&gt;Backpressure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When the Writable Stream becomes slower, Node pauses the Readable Stream until the destination catches up.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Backpressure = Flow Control&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  💧 What is &lt;code&gt;highWaterMark&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;highWaterMark&lt;/code&gt; defines the maximum amount of data that can be buffered before backpressure is applied.&lt;/p&gt;

&lt;p&gt;Think of it as a &lt;strong&gt;water level limit&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once the internal buffer reaches this limit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading pauses&lt;/li&gt;
&lt;li&gt;Writable Stream processes buffered data&lt;/li&gt;
&lt;li&gt;Reading resumes automatically&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📊 Stream Flow
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Large File
     │
Readable Stream
     │
   Chunks
     │
Internal Buffer
     │
HighWaterMark Reached
     │
Backpressure
     │
Writable Stream
     │
Output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💡 Key Interview Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Streams process data &lt;strong&gt;chunk by chunk&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Buffers temporarily store &lt;strong&gt;binary data&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pipe()&lt;/code&gt; connects Readable and Writable Streams.&lt;/li&gt;
&lt;li&gt;Backpressure prevents memory overflow.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;highWaterMark&lt;/code&gt; controls when backpressure starts.&lt;/li&gt;
&lt;li&gt;Streams are ideal for large files, uploads, downloads, and video streaming.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Streams and Buffers are at the heart of Node.js performance.&lt;/p&gt;

&lt;p&gt;If you're building APIs that handle file uploads, large downloads, logs, media, or network communication, understanding these concepts will help you write scalable and memory-efficient applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What real-world use case have you used Streams for? Share it in the comments! 👇&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  #NodeJS #JavaScript #Backend #WebDevelopment #Programming #Streams #Buffer #Performance #100DaysOfCode
&lt;/h3&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>🚀 Understanding Async Patterns in Node.js: From Callbacks to Async/Await</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Tue, 28 Jul 2026 14:13:02 +0000</pubDate>
      <link>https://dev.to/joshikrati03/understanding-async-patterns-in-nodejs-from-callbacks-to-asyncawait-2gih</link>
      <guid>https://dev.to/joshikrati03/understanding-async-patterns-in-nodejs-from-callbacks-to-asyncawait-2gih</guid>
      <description>&lt;p&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;, but Node.js can efficiently handle thousands of concurrent operations. The secret lies in its &lt;strong&gt;asynchronous programming model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Whether you're reading files, querying databases, calling APIs, or communicating with Redis or Kafka, understanding async patterns is essential for writing scalable Node.js applications.&lt;/p&gt;

&lt;p&gt;Let's explore how asynchronous programming in Node.js has evolved over time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Asynchronous Programming?
&lt;/h2&gt;

&lt;p&gt;Imagine your application needs to read a large file.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;synchronous&lt;/strong&gt; approach blocks the entire program until the file is read.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf8&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;data&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finished&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing else executes until the file operation completes.&lt;/p&gt;

&lt;p&gt;Now compare that with an asynchronous approach:&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf8&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;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finished&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Finished
&amp;lt;File Content&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of waiting, Node.js continues executing other code while the file is being read in the background.&lt;/p&gt;

&lt;p&gt;That's the power of asynchronous programming.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Evolution of Async Patterns
&lt;/h1&gt;

&lt;p&gt;Over time, Node.js introduced better ways to manage asynchronous code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Callbacks
      ↓
Callback Hell
      ↓
Promises
      ↓
Async/Await
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each step improved readability, maintainability, and error handling.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Callbacks
&lt;/h2&gt;

&lt;p&gt;A callback is simply a function passed to another function that executes after an asynchronous task completes.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Node.js&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;Output&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="nx"&gt;Hello&lt;/span&gt;
&lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Callbacks were the foundation of asynchronous programming in Node.js.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem: Callback Hell
&lt;/h2&gt;

&lt;p&gt;As applications grow, callbacks become deeply nested.&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="nf"&gt;login&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="nf"&gt;fetchProfile&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="nf"&gt;fetchOrders&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="nf"&gt;sendEmail&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="nf"&gt;logout&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="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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates the infamous &lt;strong&gt;"Pyramid of Doom."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Problems include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ Hard to read&lt;/li&gt;
&lt;li&gt;❌ Difficult to debug&lt;/li&gt;
&lt;li&gt;❌ Complex error handling&lt;/li&gt;
&lt;li&gt;❌ Poor maintainability&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Promises
&lt;/h2&gt;

&lt;p&gt;Promises solve callback hell by representing the eventual result of an asynchronous operation.&lt;/p&gt;

&lt;p&gt;A Promise has three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;li&gt;Fulfilled&lt;/li&gt;
&lt;li&gt;Rejected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Creating a Promise:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&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;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;success&lt;/span&gt; &lt;span class="o"&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Success&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Failed&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using a Promise:&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="nx"&gt;promise&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="nx"&gt;result&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;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;Promises make asynchronous code much easier to understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise Chaining
&lt;/h2&gt;

&lt;p&gt;Instead of nesting callbacks, Promises allow chaining.&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="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The execution flow becomes much cleaner.&lt;/p&gt;




&lt;h2&gt;
  
  
  Useful Promise Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Promise.all()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Runs multiple asynchronous operations in parallel.&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;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;getOrders&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;getNotifications&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;If &lt;strong&gt;one promise rejects&lt;/strong&gt;, the entire operation fails.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;Promise.allSettled()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Waits for every promise to finish, regardless of success or failure.&lt;/p&gt;

&lt;p&gt;Useful when every result matters.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;Promise.race()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Returns whichever promise settles first.&lt;/p&gt;

&lt;p&gt;Perfect for implementing request timeouts.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;Promise.any()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Returns the first successfully resolved promise while ignoring rejected ones.&lt;/p&gt;

&lt;p&gt;Useful when multiple sources can provide the same data.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Async/Await
&lt;/h2&gt;

&lt;p&gt;Async/Await is built on top of Promises and makes asynchronous code look almost synchronous.&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&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="nx"&gt;user&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;Much cleaner than multiple &lt;code&gt;.then()&lt;/code&gt; calls.&lt;/p&gt;




&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;One of the biggest advantages of Async/Await is cleaner error handling.&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;registerUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;try&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createUser&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;Instead of attaching &lt;code&gt;.catch()&lt;/code&gt; everywhere, a single &lt;code&gt;try...catch&lt;/code&gt; block handles failures gracefully.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sequential vs Parallel Execution
&lt;/h2&gt;

&lt;p&gt;Sequential execution:&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;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchProducts&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each operation waits for the previous one.&lt;/p&gt;

&lt;p&gt;Parallel execution:&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;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchProducts&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;Independent tasks execute simultaneously, significantly reducing response time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; Use &lt;code&gt;Promise.all()&lt;/code&gt; whenever tasks don't depend on each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Forgetting &lt;code&gt;await&lt;/code&gt;
&lt;/h3&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="nb"&gt;Promise&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;pending&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Correct:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Using &lt;code&gt;await&lt;/code&gt; Outside an Async Function
&lt;/h3&gt;

&lt;p&gt;Incorrect:&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;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Correct:&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&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;h3&gt;
  
  
  Ignoring Errors
&lt;/h3&gt;

&lt;p&gt;Always handle rejected promises.&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;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;
  
  
  Real-World Example
&lt;/h2&gt;

&lt;p&gt;A user registration API typically performs multiple asynchronous operations.&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;registerUser&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="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;try&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;hashedPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;hashPassword&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;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hashedPassword&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sendWelcomeEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&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="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 pattern is common in modern Express.js applications.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Node.js uses asynchronous programming to avoid blocking the Event Loop.&lt;/li&gt;
&lt;li&gt;Callbacks introduced async programming but often led to callback hell.&lt;/li&gt;
&lt;li&gt;Promises improved readability and simplified error handling.&lt;/li&gt;
&lt;li&gt;Async/Await provides the cleanest syntax for asynchronous code.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Promise.all()&lt;/code&gt; for independent operations to improve performance.&lt;/li&gt;
&lt;li&gt;Always wrap asynchronous code in &lt;code&gt;try...catch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose Async/Await for modern Node.js applications whenever possible.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Asynchronous programming is one of the biggest reasons Node.js excels at building scalable backend services.&lt;/p&gt;

&lt;p&gt;Mastering callbacks, Promises, and Async/Await isn't just about passing interviews—it's about writing cleaner, faster, and more maintainable production code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What async pattern do you use most in your projects—Callbacks, Promises, or Async/Await? Let me know in the comments! 🚀&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #AsyncProgramming #Promises #AsyncAwait #WebDevelopment #100DaysOfCode #DevCommunity
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀 Backend Internals #7: libuv — The Real Reason Node.js Is Asynchronous</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Mon, 27 Jul 2026 18:15:04 +0000</pubDate>
      <link>https://dev.to/joshikrati03/backend-internals-7-libuv-the-real-reason-nodejs-is-asynchronous-16bj</link>
      <guid>https://dev.to/joshikrati03/backend-internals-7-libuv-the-real-reason-nodejs-is-asynchronous-16bj</guid>
      <description>&lt;p&gt;&lt;em&gt;By Krati Joshi&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Most developers say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js is single-threaded."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's true—but it's also incomplete.&lt;/p&gt;

&lt;p&gt;If JavaScript runs on a single thread, then who reads files?&lt;br&gt;
Who handles thousands of HTTP connections?&lt;br&gt;
Who performs DNS lookups and cryptographic operations without freezing your application?&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;libuv&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding libuv is the moment Node.js starts making sense.&lt;/p&gt;

&lt;p&gt;Let's dive in.&lt;/p&gt;


&lt;h1&gt;
  
  
  The Problem
&lt;/h1&gt;

&lt;p&gt;Imagine this code:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&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="s2"&gt;fs&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;movie.mp4&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;movie.mp4&lt;/code&gt; is 5 GB, your application waits until the file is completely read.&lt;/p&gt;

&lt;p&gt;Nothing else executes.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;blocking I/O&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now imagine an Express server handling 500 users.&lt;/p&gt;

&lt;p&gt;If every request performs blocking file operations, your server quickly becomes unresponsive.&lt;/p&gt;

&lt;p&gt;Clearly, Node.js needed a better approach.&lt;/p&gt;




&lt;h1&gt;
  
  
  Enter libuv
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;libuv&lt;/strong&gt; is a &lt;strong&gt;cross-platform C library&lt;/strong&gt; used internally by Node.js.&lt;/p&gt;

&lt;p&gt;Its job is to provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Asynchronous I/O&lt;/li&gt;
&lt;li&gt;Event Loop implementation&lt;/li&gt;
&lt;li&gt;Worker Thread Pool&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;File System APIs&lt;/li&gt;
&lt;li&gt;Networking abstraction&lt;/li&gt;
&lt;li&gt;DNS operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without libuv, Node.js would not be able to perform asynchronous operations efficiently.&lt;/p&gt;




&lt;h1&gt;
  
  
  Where Does libuv Fit?
&lt;/h1&gt;

&lt;p&gt;Every Node.js program roughly follows this architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript Code
        │
        ▼
     V8 Engine
        │
        ▼
Node.js Native C++ Bindings
        │
        ▼
      libuv
   ┌────┴────┐
   │         │
   ▼         ▼
Thread Pool  Operating System
             Async I/O APIs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each component has a different responsibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  V8
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Executes JavaScript&lt;/li&gt;
&lt;li&gt;Performs JIT compilation&lt;/li&gt;
&lt;li&gt;Manages memory and garbage collection&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Node.js Native Bindings
&lt;/h3&gt;

&lt;p&gt;Expose APIs like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dns&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;to JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  libuv
&lt;/h3&gt;

&lt;p&gt;Coordinates asynchronous work with the operating system.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why JavaScript Can't Read Files
&lt;/h1&gt;

&lt;p&gt;JavaScript was originally designed for browsers.&lt;/p&gt;

&lt;p&gt;It has no built-in ability to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read files&lt;/li&gt;
&lt;li&gt;Create sockets&lt;/li&gt;
&lt;li&gt;Open network connections&lt;/li&gt;
&lt;li&gt;Access operating system APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you write:&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;notes.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript doesn't read the file itself.&lt;/p&gt;

&lt;p&gt;Instead, the request travels through Node.js into libuv, which performs the work using the operating system or its worker thread pool.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Flow of &lt;code&gt;fs.readFile()&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Internally, the request looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript

↓

V8 Engine

↓

Node.js Native Bindings

↓

libuv

↓

Worker Thread

↓

Operating System

↓

File Read Complete

↓

Completion Queue

↓

Event Loop

↓

JavaScript Callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important:&lt;/p&gt;

&lt;p&gt;The JavaScript thread never waits.&lt;/p&gt;

&lt;p&gt;It simply continues executing other work until the callback is ready.&lt;/p&gt;

&lt;p&gt;That's the foundation of Node.js's non-blocking model.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Thread Pool
&lt;/h1&gt;

&lt;p&gt;Some operations cannot rely entirely on the operating system's asynchronous APIs.&lt;/p&gt;

&lt;p&gt;For these, libuv maintains a &lt;strong&gt;Worker Thread Pool&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 Worker Threads
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typical operations that use the thread pool include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File System (&lt;code&gt;fs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Cryptography (&lt;code&gt;crypto&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Compression (&lt;code&gt;zlib&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Some DNS lookups (&lt;code&gt;dns.lookup()&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pool size can be increased when appropriate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;UV_THREADPOOL_SIZE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8 node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can improve throughput for workloads dominated by thread-pool operations like hashing or compression.&lt;/p&gt;




&lt;h1&gt;
  
  
  Not Every Async Operation Uses the Thread Pool
&lt;/h1&gt;

&lt;p&gt;One of the biggest interview misconceptions is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"All asynchronous work uses the thread pool."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;h3&gt;
  
  
  Uses Thread Pool
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.readFile()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto.pbkdf2()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto.scrypt()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zlib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dns.lookup()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Doesn't Use Thread Pool
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;HTTP requests&lt;/li&gt;
&lt;li&gt;TCP sockets&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;Most network I/O&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Networking is usually handled using efficient operating-system mechanisms, while libuv waits for readiness notifications and schedules callbacks.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Can Node.js Handle Thousands of Connections?
&lt;/h1&gt;

&lt;p&gt;Unlike many traditional server models, Node.js does &lt;strong&gt;not&lt;/strong&gt; create one thread per connection.&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thousands of Clients

↓

Operating System

↓

libuv Event Loop

↓

JavaScript Callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most connections spend their time waiting for network events.&lt;/p&gt;

&lt;p&gt;Because JavaScript isn't blocked waiting for I/O, one thread can coordinate a huge number of concurrent connections.&lt;/p&gt;




&lt;h1&gt;
  
  
  Event Loop vs Thread Pool
&lt;/h1&gt;

&lt;p&gt;These two concepts are often confused.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Event Loop&lt;/th&gt;
&lt;th&gt;Thread Pool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Executes callbacks&lt;/td&gt;
&lt;td&gt;Performs supported blocking work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runs on the JavaScript thread&lt;/td&gt;
&lt;td&gt;Runs on worker threads&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-threaded&lt;/td&gt;
&lt;td&gt;Multi-threaded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schedules completed tasks&lt;/td&gt;
&lt;td&gt;Executes background operations&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The Event Loop doesn't read files.&lt;/p&gt;

&lt;p&gt;The Thread Pool doesn't execute JavaScript.&lt;/p&gt;

&lt;p&gt;They work together.&lt;/p&gt;




&lt;h1&gt;
  
  
  Common Interview Questions
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Why is Node.js called single-threaded?
&lt;/h3&gt;

&lt;p&gt;Because JavaScript execution happens on a single main thread.&lt;/p&gt;

&lt;p&gt;Node.js itself also uses libuv, worker threads, and operating-system asynchronous I/O.&lt;/p&gt;




&lt;h3&gt;
  
  
  Does every async API use the thread pool?
&lt;/h3&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Only certain APIs such as &lt;code&gt;fs&lt;/code&gt;, &lt;code&gt;crypto&lt;/code&gt;, &lt;code&gt;zlib&lt;/code&gt;, and some DNS operations use it.&lt;/p&gt;

&lt;p&gt;Most networking operations rely on the operating system's asynchronous I/O facilities.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why is libuv written in C?
&lt;/h3&gt;

&lt;p&gt;Because it needs direct access to low-level operating system APIs while providing a consistent interface across Linux, Windows, and macOS.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript execution is single-threaded.&lt;/li&gt;
&lt;li&gt;libuv powers Node.js asynchronous behavior.&lt;/li&gt;
&lt;li&gt;libuv implements the Event Loop and manages the worker thread pool.&lt;/li&gt;
&lt;li&gt;File system, crypto, compression, and some DNS operations use the thread pool.&lt;/li&gt;
&lt;li&gt;HTTP requests and most networking do not use the thread pool.&lt;/li&gt;
&lt;li&gt;Understanding libuv helps explain why Node.js scales well for I/O-heavy applications.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you've ever wondered &lt;strong&gt;why Node.js can serve thousands of concurrent requests without creating thousands of threads&lt;/strong&gt;, the answer isn't magic—it's the combination of &lt;strong&gt;libuv, the operating system, and the Event Loop&lt;/strong&gt; working together.&lt;/p&gt;

&lt;p&gt;In the next article, we'll explore &lt;strong&gt;how the Node.js HTTP module works internally&lt;/strong&gt; and see what actually happens when a browser sends a request to your server.&lt;/p&gt;

</description>
      <category>node</category>
      <category>backenddevelopment</category>
      <category>saas</category>
      <category>opensource</category>
    </item>
    <item>
      <title>🚀 Backend Internals #6: The Node.js Event Loop Explained — Why Async Code Doesn't Block</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Sun, 26 Jul 2026 08:58:43 +0000</pubDate>
      <link>https://dev.to/joshikrati03/-backend-internals-6-the-nodejs-event-loop-explained-why-async-code-doesnt-block-1552</link>
      <guid>https://dev.to/joshikrati03/-backend-internals-6-the-nodejs-event-loop-explained-why-async-code-doesnt-block-1552</guid>
      <description>&lt;p&gt;&lt;em&gt;By Krati Joshi&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you've ever wondered how Node.js can handle thousands of requests while running on a &lt;strong&gt;single JavaScript thread&lt;/strong&gt;, the answer is the &lt;strong&gt;Event Loop&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Many developers know &lt;em&gt;what&lt;/em&gt; the Event Loop is, but understanding &lt;em&gt;how&lt;/em&gt; it works can completely change the way you write and debug asynchronous code.&lt;/p&gt;

&lt;p&gt;Let's break it down.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 The Problem
&lt;/h2&gt;

&lt;p&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;, which means it can execute only &lt;strong&gt;one task at a time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Imagine reading a large file or waiting for a database query. If JavaScript had to wait for these operations to finish, the entire application would freeze.&lt;/p&gt;

&lt;p&gt;That's where Node.js shines.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ How Node.js Solves This
&lt;/h2&gt;

&lt;p&gt;Node.js delegates long-running operations to &lt;strong&gt;libuv&lt;/strong&gt;, which works with the operating system to handle asynchronous tasks.&lt;/p&gt;

&lt;p&gt;While those tasks are running, JavaScript continues executing other code.&lt;/p&gt;

&lt;p&gt;When an operation completes, its callback is placed in a queue, waiting for the Event Loop to execute it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript Code
      │
      ▼
  Call Stack
      │
      ▼
Async Operation (libuv)
      │
      ▼
Callback Queue
      │
      ▼
  Event Loop
      │
      ▼
  Call Stack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Event Loop constantly checks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is the Call Stack empty?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is &lt;strong&gt;Yes&lt;/strong&gt;, it moves the next callback to the Call Stack.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 A Simple Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&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="mi"&gt;0&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
Timeout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even with a delay of &lt;code&gt;0&lt;/code&gt;, the callback cannot execute until the current synchronous code finishes and the Call Stack becomes empty.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 The Event Loop Phases
&lt;/h2&gt;

&lt;p&gt;The Node.js Event Loop consists of several phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Executes &lt;code&gt;setTimeout()&lt;/code&gt; and &lt;code&gt;setInterval()&lt;/code&gt; callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Pending Callbacks&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Executes certain deferred system callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Poll&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Processes completed I/O operations such as file reads, network requests, and database responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Check&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Executes callbacks scheduled with &lt;code&gt;setImmediate()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Close Callbacks&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Handles cleanup events like socket close callbacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;Poll phase&lt;/strong&gt; is where most asynchronous I/O callbacks are processed, making it one of the most important phases to understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚔️ &lt;code&gt;process.nextTick()&lt;/code&gt; vs Promise vs Timers
&lt;/h2&gt;

&lt;p&gt;Node.js executes asynchronous callbacks in a specific order.&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextTick&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nextTick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setImmediate&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Immediate&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typical output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start
End
nextTick
Promise
Timeout
Immediate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Priority Order
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current Synchronous Code
        ↓
process.nextTick()
        ↓
Promise Microtasks
        ↓
Timers
        ↓
Poll
        ↓
Check (setImmediate)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding this execution order helps explain many "unexpected" behaviors in asynchronous JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Node.js remains &lt;strong&gt;non-blocking&lt;/strong&gt; by delegating asynchronous work to &lt;strong&gt;libuv&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Event Loop&lt;/strong&gt; moves completed callbacks to the Call Stack when it's empty.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;process.nextTick()&lt;/code&gt; executes before Promise callbacks.&lt;/li&gt;
&lt;li&gt;Promise callbacks execute before timer callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setImmediate()&lt;/code&gt; executes during the &lt;strong&gt;Check&lt;/strong&gt; phase.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🎯 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The Event Loop is the engine behind Node.js's scalability. Once you understand how callbacks, microtasks, and Event Loop phases interact, asynchronous code becomes much easier to reason about.&lt;/p&gt;

&lt;p&gt;Mastering this concept is essential for writing efficient Node.js applications and performing well in backend interviews.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What's the most confusing Event Loop behavior you've encountered? Drop it in the comments, and let's discuss! 👇&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  NodeJS #JavaScript #Backend #EventLoop #AsyncProgramming #WebDevelopment #Coding #Programming #100DaysOfCode
&lt;/h1&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
