<?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: Abdullah Yasir</title>
    <description>The latest articles on DEV Community by Abdullah Yasir (@abdullahyasir).</description>
    <link>https://dev.to/abdullahyasir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1173073%2F9f78fe7f-6dd5-4857-ab09-e4aa252bd0e1.jpg</url>
      <title>DEV Community: Abdullah Yasir</title>
      <link>https://dev.to/abdullahyasir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdullahyasir"/>
    <language>en</language>
    <item>
      <title>Flutter Development Setup for WSL2 + Windows Android Studio (Complete Guide)</title>
      <dc:creator>Abdullah Yasir</dc:creator>
      <pubDate>Sun, 02 Nov 2025 12:07:49 +0000</pubDate>
      <link>https://dev.to/abdullahyasir/flutter-development-setup-for-wsl2-windows-android-studio-complete-guide-4493</link>
      <guid>https://dev.to/abdullahyasir/flutter-development-setup-for-wsl2-windows-android-studio-complete-guide-4493</guid>
      <description>&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.amazonaws.com%2Fuploads%2Farticles%2Fppabcqxfarpav141o8kw.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.amazonaws.com%2Fuploads%2Farticles%2Fppabcqxfarpav141o8kw.png" alt="Flutter + Android + WSL2 Arch Overview" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you love using &lt;strong&gt;WSL2&lt;/strong&gt; for development but also need &lt;strong&gt;Android Studio&lt;/strong&gt; on Windows — this guide is for you.&lt;br&gt;&lt;br&gt;
This setup runs &lt;strong&gt;Flutter (via FVM)&lt;/strong&gt; natively inside WSL2, while all Android tools (SDK, Studio, emulators) stay on Windows.  &lt;/p&gt;

&lt;p&gt;After a lot of trial and error, I documented this configuration that works perfectly — Flutter builds, devices connect, and Gradle behaves nicely.&lt;/p&gt;


&lt;h2&gt;
  
  
  🧠 Overview
&lt;/h2&gt;

&lt;p&gt;We’ll be combining the best of both worlds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WSL2 (Ubuntu)&lt;/strong&gt; → Flutter, FVM, Java, Gradle
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Windows&lt;/strong&gt; → Android Studio, Android SDK, emulators
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wrapper scripts&lt;/strong&gt; → To bridge WSL2 and Windows SDK tools seamlessly
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  ⚙️ Environment Configuration (&lt;code&gt;~/.bashrc&lt;/code&gt;)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Android SDK (on Windows)
&lt;/h3&gt;

&lt;p&gt;Your Android SDK lives here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
/mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the following environment variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ANDROID_HOME&lt;/code&gt; and &lt;code&gt;ANDROID_SDK_ROOT&lt;/code&gt; → point to that SDK path
&lt;/li&gt;
&lt;li&gt;Add Android tools (&lt;code&gt;cmdline-tools&lt;/code&gt;, &lt;code&gt;emulator&lt;/code&gt;) to your PATH&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Java (WSL2 native)
&lt;/h3&gt;

&lt;p&gt;We’ll use OpenJDK 17 installed inside WSL2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add its &lt;code&gt;bin&lt;/code&gt; folder to your PATH.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flutter (via FVM)
&lt;/h3&gt;

&lt;p&gt;Add your FVM-managed Flutter to PATH and set the Dart &lt;code&gt;PUB_CACHE&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
~/fvm/default/bin

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧩 Wrapper Scripts (&lt;code&gt;~/bin/&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;Since WSL2 can’t directly execute &lt;code&gt;.exe&lt;/code&gt; files from Linux PATH, we’ll create small &lt;strong&gt;bash wrapper scripts&lt;/strong&gt; to call the Windows versions transparently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
~/bin/adb       → wraps adb.exe
~/bin/aapt      → wraps aapt.exe
~/bin/aapt2     → wraps aapt2.exe
~/bin/zipalign  → wraps zipalign.exe

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 Java wrappers are &lt;strong&gt;not needed&lt;/strong&gt; — we’re using native WSL2 Java.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🛠️ Android SDK Build Tools
&lt;/h2&gt;

&lt;p&gt;We’ll make Windows executables appear Linux-friendly.&lt;br&gt;&lt;br&gt;
Go to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
/mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/build-tools/36.1.0/

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then copy and symlink the essential build tools as shown below.&lt;/p&gt;




&lt;h2&gt;
  
  
  🪄 Flutter Configuration
&lt;/h2&gt;

&lt;p&gt;Tell Flutter to use WSL2’s Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flutter config &lt;span class="nt"&gt;--jdk-dir&lt;/span&gt; &lt;span class="s2"&gt;"/usr/lib/jvm/java-17-openjdk-amd64"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Project files will still reference the Windows SDK path (which is fine).&lt;/p&gt;




&lt;h2&gt;
  
  
  🧰 Step-by-Step Setup Guide
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1 — Install Prerequisites
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; openjdk-17-jdk openjdk-17-jre
java &lt;span class="nt"&gt;-version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2 — Install FVM and Flutter
&lt;/h3&gt;

&lt;p&gt;Install FVM (see &lt;a href="https://fvm.app/documentation/getting-started/installation" rel="noopener noreferrer"&gt;docs&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;fvm &lt;span class="nb"&gt;install &lt;/span&gt;stable
fvm global stable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3 — Configure &lt;code&gt;~/.bashrc&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Append the following block:&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="c"&gt;# --- Android SDK + Flutter Development (WSL2 with Windows Android Studio)&lt;/span&gt;

&lt;span class="c"&gt;# User bin (for wrappers like adb)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/bin:&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="c"&gt;# Android SDK (Windows)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ANDROID_HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;ANDROID_SDK_ROOT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$ANDROID_HOME&lt;/span&gt;

&lt;span class="c"&gt;# WSL2 native Java (OpenJDK 17)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;JAVA_HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/lib/jvm/java-17-openjdk-amd64
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$JAVA_HOME&lt;/span&gt;/bin:&lt;span class="nv"&gt;$PATH&lt;/span&gt;

&lt;span class="c"&gt;# Add Android tools&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$ANDROID_HOME&lt;/span&gt;&lt;span class="s2"&gt;/cmdline-tools/latest/bin"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$ANDROID_HOME&lt;/span&gt;&lt;span class="s2"&gt;/emulator"&lt;/span&gt;

&lt;span class="c"&gt;# Flutter (FVM)&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/fvm/default/bin:&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PUB_CACHE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/.pub-cache"&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$PUB_CACHE&lt;/span&gt;&lt;span class="s2"&gt;/bin"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 4 — Create Wrapper Scripts
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; ~/bin

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/bin/adb &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
#!/bin/bash
exec /mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/platform-tools/adb.exe "&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="sh"&gt;"
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/bin/aapt &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
#!/bin/bash
exec /mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/build-tools/36.1.0/aapt.exe "&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="sh"&gt;"
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/bin/aapt2 &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
#!/bin/bash
exec /mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/build-tools/36.1.0/aapt2.exe "&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="sh"&gt;"
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ~/bin/zipalign &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
#!/bin/bash
exec /mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/build-tools/36.1.0/zipalign.exe "&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="sh"&gt;"
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x ~/bin/adb ~/bin/aapt ~/bin/aapt2 ~/bin/zipalign
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 5 — Copy Android Build Tools
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/build-tools/36.1.0/
cmd.exe /c &lt;span class="s2"&gt;"copy aapt.exe aapt"&lt;/span&gt;
cmd.exe /c &lt;span class="s2"&gt;"copy aapt2.exe aapt2"&lt;/span&gt;
cmd.exe /c &lt;span class="s2"&gt;"copy zipalign.exe zipalign"&lt;/span&gt;
cmd.exe /c &lt;span class="s2"&gt;"copy aidl.exe aidl"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 6 — Create ADB Symlink
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; ~/bin/adb /mnt/c/Users/&amp;lt;your_windows_username&amp;gt;/AppData/Local/Android/Sdk/platform-tools/adb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 7 — Configure Flutter
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flutter config &lt;span class="nt"&gt;--jdk-dir&lt;/span&gt; &lt;span class="s2"&gt;"/usr/lib/jvm/java-17-openjdk-amd64"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 8 — Reload and Verify
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.bashrc
flutter doctor &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧱 Project Configuration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;android/local.properties&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Make sure it uses &lt;strong&gt;Windows path format&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;sdk.dir&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;your_windows_username&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;AppData&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Local&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Android&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Sdk&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Expected &lt;code&gt;flutter doctor&lt;/code&gt; Output
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Flutter&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Fully functional&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Android toolchain&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;Minor warnings are fine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;Optional (for web)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linux toolchain&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;Optional (for desktop)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Android Studio&lt;/td&gt;
&lt;td&gt;⚠️&lt;/td&gt;
&lt;td&gt;“Not installed” — totally normal (it’s on Windows)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connected devices&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Works fine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network resources&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;All good&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🏗️ Architecture Overview
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────┐
│         WSL2 (Ubuntu)               │
│  ┌───────────────────────────┐      │
│  │ Flutter (FVM)             │      │
│  │ Java (OpenJDK 17)         │      │
│  │ Gradle                    │      │
│  └───────────────────────────┘      │
│            ↓                        │
│  ┌───────────────────────────┐      │
│  │ Wrappers (~/bin/)         │      │
│  │ - adb, aapt, aapt2, etc   │      │
│  └───────────────────────────┘      │
│            ↓                        │
└────────────┼────────────────────────┘
             ↓
┌────────────┼────────────────────────┐
│    Windows Host                     │
│  ┌───────────────────────────┐      │
│  │ Android SDK               │      │
│  │ Android Studio            │      │
│  │ Emulators/Devices         │      │
│  └───────────────────────────┘      │
└─────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📂 Files Modified or Created
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File&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;&lt;code&gt;~/.bashrc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Environment config&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;~/bin/adb&lt;/code&gt;, &lt;code&gt;~/bin/aapt&lt;/code&gt;, etc.&lt;/td&gt;
&lt;td&gt;Wrapper scripts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;android/local.properties&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Windows SDK path&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🧩 Troubleshooting
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; &lt;code&gt;JAVA_HOME&lt;/code&gt; invalid&lt;br&gt;
✅ Solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;java &lt;span class="nt"&gt;-version&lt;/span&gt;
flutter config &lt;span class="nt"&gt;--jdk-dir&lt;/span&gt; &lt;span class="s2"&gt;"/usr/lib/jvm/java-17-openjdk-amd64"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; ADB not finding devices&lt;br&gt;
✅ Solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb kill-server &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; adb start-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Gradle build fails&lt;br&gt;
✅ Solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flutter clean
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; android/.gradle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;✅ &lt;strong&gt;That’s it!&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Your Flutter + WSL2 setup is now fully functional.&lt;br&gt;
No more Gradle confusion or path issues — just smooth, native Flutter builds in WSL2 with Android Studio staying comfortably on Windows.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>wsl2</category>
      <category>android</category>
      <category>fvm</category>
    </item>
    <item>
      <title>A Complete Roadmap for Software Engineers to Learn AI/ML in 2025</title>
      <dc:creator>Abdullah Yasir</dc:creator>
      <pubDate>Sat, 18 Jan 2025 12:08:15 +0000</pubDate>
      <link>https://dev.to/abdullahyasir/a-complete-roadmap-for-software-engineers-to-learn-aiml-in-2025-536c</link>
      <guid>https://dev.to/abdullahyasir/a-complete-roadmap-for-software-engineers-to-learn-aiml-in-2025-536c</guid>
      <description>&lt;p&gt;Are you a software engineer eager to jump into the world of Artificial Intelligence (AI) and Machine Learning (ML) in 2025? Great news! With the rapid growth of online resources and powerful tools, getting started is easier than ever. In this post, I'll share a simple, step-by-step roadmap to help you transition into this exciting field, no matter your starting point.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Learn AI/ML?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;AI and ML are transforming industries—from healthcare and finance to entertainment and autonomous vehicles. With AI/ML skills, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Work on cutting-edge technologies.&lt;/li&gt;
&lt;li&gt;Solve complex, real-world problems.&lt;/li&gt;
&lt;li&gt;Boost your career prospects (AI/ML jobs are among the highest-paying in tech).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s break it down into a clear, actionable roadmap.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Phase 1: Build Your Foundations (1-3 Months)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1: Brush Up on Math Basics&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You don’t need a PhD, but some math concepts are essential for understanding AI/ML:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linear Algebra&lt;/strong&gt;: Matrices, eigenvalues, eigenvectors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Calculus&lt;/strong&gt;: Gradients, derivatives.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Probability &amp;amp; Statistics&lt;/strong&gt;: Bayes' theorem, distributions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization&lt;/strong&gt;: Gradient descent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Get comfortable with math concepts used in ML.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/@3blue1brown" rel="noopener noreferrer"&gt;3Blue1Brown YouTube Channel&lt;/a&gt; (Visual math tutorials for beginners)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.khanacademy.org/math/linear-algebra" rel="noopener noreferrer"&gt;Khan Academy: Linear Algebra&lt;/a&gt; (Beginner-friendly and interactive)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/user/joshstarmer" rel="noopener noreferrer"&gt;StatQuest with Josh Starmer&lt;/a&gt; (Simple explanations of statistics and ML concepts)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2: Learn Python&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Python is the go-to language for AI/ML due to its simplicity and vast ecosystem. Focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basics: Loops, functions, conditionals.&lt;/li&gt;
&lt;li&gt;Libraries: NumPy, Pandas (data manipulation), Matplotlib, and Seaborn (visualization).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Be proficient in Python programming.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://automatetheboringstuff.com/" rel="noopener noreferrer"&gt;Automate the Boring Stuff with Python&lt;/a&gt; (Free book for complete beginners)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://realpython.com/start-here/" rel="noopener noreferrer"&gt;Python Basics: Real Python&lt;/a&gt; (Step-by-step tutorials for beginners)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=rfscVS0vtbw" rel="noopener noreferrer"&gt;freeCodeCamp’s Python Course&lt;/a&gt; (Comprehensive video for absolute beginners)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 3: Understand Data Science Basics&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Learn how to clean, process, and visualize data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Be able to explore datasets and extract insights.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.kaggle.com/learn" rel="noopener noreferrer"&gt;Kaggle Learn&lt;/a&gt; (Beginner-friendly modules on data analysis and ML)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://microsoft.github.io/Data-Science-For-Beginners/" rel="noopener noreferrer"&gt;Data Science for Beginners (Microsoft)&lt;/a&gt; (Free, easy-to-follow curriculum)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.freecodecamp.org/learn/data-analysis-with-python/" rel="noopener noreferrer"&gt;freeCodeCamp Data Analysis with Python&lt;/a&gt; (Complete course for beginners)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Phase 2: Dive into Machine Learning (3-6 Months)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 4: Learn Core Machine Learning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Understand key ML concepts and algorithms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Supervised Learning&lt;/strong&gt;: Linear regression, decision trees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unsupervised Learning&lt;/strong&gt;: Clustering, dimensionality reduction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model Evaluation&lt;/strong&gt;: Metrics like accuracy, precision, recall.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Be able to build, train, and evaluate ML models.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developers.google.com/machine-learning/crash-course" rel="noopener noreferrer"&gt;Machine Learning Crash Course (Google)&lt;/a&gt; (Interactive and beginner-friendly)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.kaggle.com/learn/intro-to-machine-learning" rel="noopener noreferrer"&gt;Introduction to Machine Learning by Kaggle&lt;/a&gt; (Perfect for new learners)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://amzn.to/3ZX1e7r" rel="noopener noreferrer"&gt;Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow&lt;/a&gt; (Beginner-accessible book with practical examples)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 5: Explore Deep Learning&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Learn about neural networks and advanced topics like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Convolutional Neural Networks (CNNs) for image data.&lt;/li&gt;
&lt;li&gt;Recurrent Neural Networks (RNNs) for sequential data.&lt;/li&gt;
&lt;li&gt;Pretrained models and transfer learning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Build deep learning models using TensorFlow or PyTorch.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=aircAruvnKk" rel="noopener noreferrer"&gt;Deep Learning for Beginners (freeCodeCamp)&lt;/a&gt; (Friendly introduction to neural networks)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.coursera.org/specializations/deep-learning" rel="noopener noreferrer"&gt;Deep Learning Specialization by Andrew Ng (Coursera)&lt;/a&gt; (Great for beginners; start free!)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.tensorflow.org/tutorials" rel="noopener noreferrer"&gt;TensorFlow for Beginners (Official)&lt;/a&gt; (Step-by-step guides)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Phase 3: Build Projects and Get Hands-On (6-12 Months)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 6: Work on Real-World Projects&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The best way to learn is by doing! Start with beginner-friendly projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Predict house prices (regression).&lt;/li&gt;
&lt;li&gt;Classify handwritten digits (MNIST dataset).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Move on to intermediate and advanced projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image classification with CNNs.&lt;/li&gt;
&lt;li&gt;Sentiment analysis with NLP models.&lt;/li&gt;
&lt;li&gt;Time-series forecasting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Complete 3-5 projects and showcase them in your portfolio.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.kaggle.com/" rel="noopener noreferrer"&gt;Kaggle Datasets and Competitions&lt;/a&gt; (Beginner-friendly challenges)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://datasetsearch.research.google.com/" rel="noopener noreferrer"&gt;Google Dataset Search&lt;/a&gt; (Find datasets for practice)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://course.fast.ai/" rel="noopener noreferrer"&gt;fast.ai’s Practical Deep Learning Course&lt;/a&gt; (Hands-on projects for beginners)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 7: Participate in Competitions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Compete in Kaggle or other platforms to learn from others and build your reputation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Participate in 1-2 Kaggle competitions.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Phase 4: Specialize and Deepen Knowledge (12-24 Months)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 8: Explore Advanced Topics&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once you’ve mastered the basics, dive deeper:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reinforcement Learning&lt;/strong&gt;: Used in robotics and gaming.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Natural Language Processing (NLP)&lt;/strong&gt;: For chatbots and text analysis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Computer Vision&lt;/strong&gt;: Object detection, image segmentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Gain expertise in 1-2 specialized areas.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.coursera.org/specializations/reinforcement-learning" rel="noopener noreferrer"&gt;Reinforcement Learning Specialization (Coursera)&lt;/a&gt; (Beginner-friendly course)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://huggingface.co/course/chapter1" rel="noopener noreferrer"&gt;Hugging Face’s NLP Course&lt;/a&gt; (Free and accessible)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.pyimagesearch.com/start-here/" rel="noopener noreferrer"&gt;Deep Learning for Computer Vision by PyImageSearch&lt;/a&gt; (Step-by-step tutorials for beginners)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Phase 5: Learn Deployment and MLOps&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 9: Deploy AI Models&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Learn to integrate AI/ML models into real-world applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Flask or FastAPI for APIs.&lt;/li&gt;
&lt;li&gt;Deploy models on AWS, GCP, or Azure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Deploy at least one project.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=XcZ8jyRK_3w" rel="noopener noreferrer"&gt;Deploying Machine Learning Models (YouTube)&lt;/a&gt; (Beginner’s guide)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://aws.amazon.com/machine-learning/" rel="noopener noreferrer"&gt;AWS AI/ML Services&lt;/a&gt; (Simple tools for deploying models)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://fullstackopen.com/en/" rel="noopener noreferrer"&gt;fullstackopen’s ML Deployment Guide&lt;/a&gt; (For beginners)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 10: Learn MLOps&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Understand how to manage, monitor, and optimize ML pipelines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: Automate and monitor ML workflows.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/microsoft/MLOps" rel="noopener noreferrer"&gt;MLOps for Beginners by Microsoft&lt;/a&gt; (Free and beginner-friendly)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mlflow.org/" rel="noopener noreferrer"&gt;MLflow for Beginners&lt;/a&gt; (Manage ML experiments easily)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Phase 6: Build Your Portfolio and Network&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 11: Showcase Your Work&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Create a portfolio to highlight your projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use GitHub for code.&lt;/li&gt;
&lt;li&gt;Write blog posts explaining your work (use platforms like &lt;a href="https://dev.to/"&gt;Dev.to&lt;/a&gt; or Medium).&lt;/li&gt;
&lt;li&gt;Create a personal website using GitHub Pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 12: Network and Stay Updated&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Join AI/ML communities on Discord, Reddit, or LinkedIn.&lt;/li&gt;
&lt;li&gt;Attend conferences like NeurIPS, ICML, or local meetups.&lt;/li&gt;
&lt;li&gt;Follow AI thought leaders like Andrew Ng and Lex Fridman.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Suggested Timeline&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s a rough timeline to keep you on track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;0-3 Months&lt;/strong&gt;: Learn math, Python, and data science basics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3-6 Months&lt;/strong&gt;: Dive into ML and DL concepts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;6-12 Months&lt;/strong&gt;: Build projects, join competitions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;12-24 Months&lt;/strong&gt;: Specialize, deploy models, and learn MLOps.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Learning AI/ML is a journey, not a sprint. Start small, build consistently, and keep learning. Remember, even small progress daily adds up to significant expertise over time. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The resources linked in this post are not affiliated or sponsored. They are chosen based on their quality and accessibility for beginners.&lt;/p&gt;

&lt;p&gt;If you’re ready to get started, bookmark this roadmap and begin today. Good luck, and welcome to the future of technology!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Have questions or need guidance? Drop a comment below!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>deeplearning</category>
      <category>roadmap</category>
    </item>
    <item>
      <title>Understanding LRU Cache: Efficient Data Storage and Retrieval</title>
      <dc:creator>Abdullah Yasir</dc:creator>
      <pubDate>Sat, 18 Jan 2025 10:20:15 +0000</pubDate>
      <link>https://dev.to/abdullahyasir/understanding-lru-cache-efficient-data-storage-and-retrieval-2jnc</link>
      <guid>https://dev.to/abdullahyasir/understanding-lru-cache-efficient-data-storage-and-retrieval-2jnc</guid>
      <description>&lt;p&gt;In the world of software engineering, one of the most common problems developers face is how to store data efficiently for quick retrieval. This becomes particularly challenging when dealing with large amounts of data or limited memory resources. This is where an &lt;strong&gt;LRU (Least Recently Used) Cache&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll take a closer look at what an LRU cache is, why it’s important, and how it can be implemented.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What is an LRU Cache?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;LRU cache&lt;/strong&gt; is a data structure used to store a fixed number of items, with the goal of evicting the least recently used item when the cache reaches its capacity. The idea is simple: when a new item needs to be added, the cache will evict the least recently accessed item to make room for the new one.&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LRU&lt;/strong&gt; stands for &lt;strong&gt;Least Recently Used&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The cache stores a limited number of items, and when it’s full, the item that hasn’t been used for the longest period is removed to make space for new data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This kind of caching mechanism is particularly useful for scenarios like &lt;strong&gt;memory caching&lt;/strong&gt;, &lt;strong&gt;web browsers&lt;/strong&gt;, and &lt;strong&gt;database management&lt;/strong&gt;, where you want to keep frequently accessed data available for quick retrieval but don’t have unlimited memory to store everything.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why Use an LRU Cache?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When building systems that require efficient data retrieval, an LRU cache can help you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Improve Performance&lt;/strong&gt;: By storing only the most recently accessed data, you can significantly speed up access times for repeated requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimize Memory Usage&lt;/strong&gt;: It ensures that only the most important or frequently used data stays in memory, preventing unnecessary memory bloat.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handle Large Datasets&lt;/strong&gt;: When working with large datasets, an LRU cache ensures that the system only keeps relevant items in memory, preventing the need to fetch data from slower storage (e.g., a database or API) repeatedly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduce Latency&lt;/strong&gt;: By reducing the need to fetch data from slower sources, you reduce the overall system latency, resulting in faster response times.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;How Does an LRU Cache Work?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An LRU cache is typically implemented using a combination of two data structures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Doubly Linked List&lt;/strong&gt;: To maintain the order of access (most recent to least recent).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hash Map (or Dictionary)&lt;/strong&gt;: To allow for constant time O(1) access to cache items.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s how the mechanism works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When a new item is accessed&lt;/strong&gt;: The item is moved to the front of the doubly linked list (marking it as the most recently used).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When the cache reaches its limit&lt;/strong&gt;: The item at the end of the list (the least recently used item) is evicted to make room for new data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inserting new items&lt;/strong&gt;: If the cache is not full, the item is added to the front of the list and placed in the hash map for O(1) access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By combining the hash map (for fast access) and the doubly linked list (for efficient item removal), an LRU cache can operate in constant time for both get and put operations.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;LRU Cache in Action: Example Implementation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let's now look at a simple implementation of an LRU Cache in JavaScript. We'll use a &lt;strong&gt;Map&lt;/strong&gt; object (which preserves insertion order) for storing the cache and a fixed limit for the cache size.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example Code (JavaScript):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&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;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Get value from cache&lt;/span&gt;
    &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&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="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Key not found&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Move the accessed key to the front (most recently used)&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&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="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&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="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Set value in cache&lt;/span&gt;
    &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Remove the old value (if exists)&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Cache is full, remove the least recently used item&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Insert the new key-value pair at the front&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&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="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example usage:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&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;LRUCache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;C&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;cache&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// Returns "A"&lt;/span&gt;
&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;D&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;         &lt;span class="c1"&gt;// Removes key 2 (least recently used)&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;cache&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// Returns -1 (not found)&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;cache&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// Returns "C"&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;cache&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// Returns "D"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Explanation:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;get(key)&lt;/code&gt;&lt;/strong&gt;: If the key exists, it’s moved to the front of the cache and its value is returned. If the key doesn’t exist, &lt;code&gt;-1&lt;/code&gt; is returned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;put(key, value)&lt;/code&gt;&lt;/strong&gt;: If the key already exists, the old value is removed and the new one is inserted. If the cache is full, the least recently used item (the one at the beginning of the list) is removed to make space for the new key-value pair.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example Flow:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;After inserting key-value pairs (1, "A"), (2, "B"), and (3, "C"), the cache looks like this: &lt;code&gt;{1: "A", 2: "B", 3: "C"}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When you access key 1, the cache becomes &lt;code&gt;{2: "B", 3: "C", 1: "A"}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When you insert key 4, the cache evicts key 2 (the least recently used) and becomes &lt;code&gt;{3: "C", 1: "A", 4: "D"}&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;LRU Cache: Use Cases&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here are some scenarios where LRU caches are particularly beneficial:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Web Caching&lt;/strong&gt;: Caching HTTP responses, images, or API results for faster retrieval.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database Query Caching&lt;/strong&gt;: Storing frequently accessed database query results to reduce load and latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session Management&lt;/strong&gt;: Storing user session data in memory, where the most recently used session remains active.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Management&lt;/strong&gt;: Optimizing memory usage by ensuring that only the most important or frequently used objects stay in memory.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Advantages and Limitations&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Advantages:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;O(1) Time Complexity&lt;/strong&gt;: Both &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;put&lt;/code&gt; operations have constant time complexity, making it highly efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Efficiency&lt;/strong&gt;: Ensures that only the most frequently used data is stored in memory, optimizing cache size.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Limitations:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Limited Cache Size&lt;/strong&gt;: The cache is limited by the specified capacity, meaning less frequently accessed data will be evicted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache Misses&lt;/strong&gt;: When the cache is full, any new access will result in a cache miss, requiring data to be fetched from the original source (e.g., database or API).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;LRU Cache&lt;/strong&gt; is an essential data structure that allows you to manage memory efficiently by storing and retrieving the most recently used data while evicting the least recently used. It’s widely applicable in caching scenarios where you need to optimize for both performance and memory usage.&lt;/p&gt;

&lt;p&gt;Whether you're building an API that requires fast access to frequently used data, developing a memory management system, or improving the responsiveness of a web application, understanding and implementing an LRU cache can help your system scale effectively.&lt;/p&gt;

</description>
      <category>node</category>
      <category>lru</category>
      <category>cache</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding Worker Threads in Node.js: A Deep Dive</title>
      <dc:creator>Abdullah Yasir</dc:creator>
      <pubDate>Sat, 18 Jan 2025 09:56:23 +0000</pubDate>
      <link>https://dev.to/abdullahyasir/understanding-worker-threads-in-nodejs-a-deep-dive-236n</link>
      <guid>https://dev.to/abdullahyasir/understanding-worker-threads-in-nodejs-a-deep-dive-236n</guid>
      <description>&lt;p&gt;Node.js is renowned for its non-blocking, event-driven architecture, making it an excellent choice for handling high concurrency, especially for I/O-bound operations. But what happens when you need to perform heavy computations? How do you ensure that CPU-intensive tasks don’t block the main event loop and degrade the performance of your Node.js application? This is where &lt;strong&gt;worker threads&lt;/strong&gt; come into play.&lt;/p&gt;

&lt;p&gt;In this blog post, we will explore what worker threads are in Node.js, how they work, and how they differ from threads in other languages like &lt;strong&gt;C++&lt;/strong&gt; or &lt;strong&gt;Java&lt;/strong&gt;. Additionally, we’ll simulate heavy computational tasks to see how worker threads can help handle these efficiently.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What Are Worker Threads in Node.js?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;By default, Node.js operates in a &lt;strong&gt;single-threaded&lt;/strong&gt; environment, meaning that JavaScript code runs in a single execution thread (the &lt;strong&gt;event loop&lt;/strong&gt;). While this is efficient for handling I/O operations asynchronously, it can be a bottleneck when it comes to CPU-bound tasks, such as processing large datasets, performing complex calculations, or handling intensive image or video processing.&lt;/p&gt;

&lt;p&gt;To address this, Node.js introduced the &lt;strong&gt;worker threads&lt;/strong&gt; module, which allows you to run JavaScript code in &lt;strong&gt;parallel threads&lt;/strong&gt;. These threads can offload heavy computations and prevent them from blocking the main event loop, thereby improving overall performance.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;How Do Worker Threads Work in Node.js?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Worker threads in Node.js are &lt;strong&gt;native OS threads&lt;/strong&gt;, meaning they are managed by the operating system just like threads in traditional multi-threaded applications. However, unlike traditional threads in some languages, worker threads in Node.js are designed to work within the constraints of the single-threaded JavaScript model, which means they have &lt;strong&gt;isolated memory&lt;/strong&gt; and communicate via &lt;strong&gt;message passing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here’s a simple example demonstrating how to use worker threads in Node.js:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isMainThread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parentPort&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker_threads&lt;/span&gt;&lt;span class="dl"&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;isMainThread&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Main thread: Create a worker&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__filename&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Running the same file in the worker&lt;/span&gt;
  &lt;span class="nx"&gt;worker&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="s1"&gt;message&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from worker:&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// Receiving result from worker&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Start the work&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="c1"&gt;// Worker thread: Handling the task&lt;/span&gt;
  &lt;span class="nx"&gt;parentPort&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="s1"&gt;message&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Received in worker:&lt;/span&gt;&lt;span class="dl"&gt;'&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Some CPU-bound task&lt;/span&gt;
    &lt;span class="nx"&gt;parentPort&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&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="c1"&gt;// Sending result back to the main thread&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// A naive recursive Fibonacci function to simulate heavy computation&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&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;In this example, the main thread creates a worker thread using the same script. The worker performs a heavy computation (in this case, calculating Fibonacci numbers) and sends the result back to the main thread via &lt;code&gt;postMessage()&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Characteristics of Worker Threads:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Real Operating System Threads&lt;/strong&gt;: Worker threads are &lt;strong&gt;actual OS threads&lt;/strong&gt;, and they run independently of the main thread, making them suitable for computationally expensive tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolated Memory&lt;/strong&gt;: Unlike traditional threads that can share memory, worker threads have their own &lt;strong&gt;isolated memory&lt;/strong&gt;. Communication between threads is done via message passing, ensuring data integrity and reducing the risk of race conditions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency Without Blocking&lt;/strong&gt;: Worker threads allow for &lt;strong&gt;concurrent execution&lt;/strong&gt; of code, ensuring that the main thread remains responsive while the worker threads handle CPU-intensive tasks.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;When to Use Worker Threads?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You should use worker threads in Node.js when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CPU-intensive tasks&lt;/strong&gt;: Tasks such as heavy calculations, image/video processing, or complex data manipulation that can block the event loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-blocking concurrency&lt;/strong&gt;: When you need to perform computations without disrupting the main event loop’s ability to handle other asynchronous I/O operations, such as handling HTTP requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding single-threaded performance bottlenecks&lt;/strong&gt;: If you are running Node.js on a multi-core machine, worker threads can make use of multiple cores to improve performance by distributing the computational load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, processing large sets of data, like parsing a huge CSV file or running deep learning models, can significantly benefit from being offloaded to worker threads.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Simulating Heavy Computation with Worker Threads&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s take a look at how we can &lt;strong&gt;simulate CPU-heavy tasks&lt;/strong&gt; and see how worker threads can offload such operations to keep the event loop responsive.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 1: Simulating Heavy Computation with Fibonacci Numbers
&lt;/h4&gt;

&lt;p&gt;In this example, we use a naive recursive algorithm to calculate Fibonacci numbers, which has exponential complexity and is ideal for simulating heavy computation.&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;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// A naive recursive Fibonacci function to simulate heavy computation&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&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;Starting heavy computation...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Simulate heavy computation with Fibonacci&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Using a high value to simulate delay&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;Computation result:&lt;/span&gt;&lt;span class="dl"&gt;"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function calculates Fibonacci numbers and can be very slow for large &lt;code&gt;n&lt;/code&gt;, making it an ideal task for worker threads. You can now use the previous example where a worker thread calculates the Fibonacci sequence while the main thread stays responsive.&lt;/p&gt;




&lt;h4&gt;
  
  
  Example 2: Simulating Heavy Computation with Sorting
&lt;/h4&gt;

&lt;p&gt;Sorting large datasets is another classic example of a CPU-heavy task. Here’s how we can simulate heavy computation by sorting a large array of random numbers:&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;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Generate an array of random numbers&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;e6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Sort the array (computationally expensive task)&lt;/span&gt;
  &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&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;Sorting complete.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&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="c1"&gt;// Return the smallest element for demo purposes&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;Starting heavy computation...&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&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;Smallest element after sorting:&lt;/span&gt;&lt;span class="dl"&gt;"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sorting a million numbers can take a significant amount of time, and this task is well-suited to be handled by worker threads. The worker can perform the sorting operation while the main thread continues to handle other tasks.&lt;/p&gt;




&lt;h4&gt;
  
  
  Example 3: Simulating Heavy Computation with Prime Number Calculation
&lt;/h4&gt;

&lt;p&gt;Prime number calculation is another example of a CPU-intensive task. Here's how to find all prime numbers in a large range using a naive method:&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;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&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;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;limit&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;primes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nx"&gt;primes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;primes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Return the count of prime numbers&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;Starting heavy computation...&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;heavyComputation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Check primes up to 10,000&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;Number of primes:&lt;/span&gt;&lt;span class="dl"&gt;"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This task requires checking each number up to &lt;code&gt;limit&lt;/code&gt; to determine whether it is prime, which can be computationally expensive. Using worker threads to offload this task will prevent it from blocking the event loop.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Worker Threads vs. Threads in Other Languages&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At this point, you might be wondering: How do worker threads in Node.js compare to traditional threads in other programming languages, like &lt;strong&gt;C++&lt;/strong&gt; or &lt;strong&gt;Java&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Here’s a breakdown of the key differences:&lt;br&gt;
Here’s the updated table without the "Aspect" column:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Worker Threads in Node.js&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Threads in C++/Java&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No shared memory; communication is done via message passing.&lt;/td&gt;
&lt;td&gt;Threads typically share memory, leading to easier data sharing but more potential for race conditions.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Each worker runs independently with its own event loop.&lt;/td&gt;
&lt;td&gt;Threads run concurrently, each with its own execution flow, but they share a common memory space.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Communicates using message passing (via &lt;code&gt;postMessage()&lt;/code&gt; and event listeners).&lt;/td&gt;
&lt;td&gt;Communicate via shared memory, variables, or specialized synchronization methods (mutexes, semaphores).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;More restrictive than traditional threads due to isolation and message passing, but safer in terms of concurrency.&lt;/td&gt;
&lt;td&gt;Easier to work with for shared memory access, but more prone to issues like deadlocks or race conditions.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ideal for offloading CPU-heavy tasks in a non-blocking environment.&lt;/td&gt;
&lt;td&gt;Best for tasks that require frequent interaction with shared memory and parallel execution in memory-heavy applications.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Memory Sharing and Communication:&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In &lt;strong&gt;Java&lt;/strong&gt; and &lt;strong&gt;C++&lt;/strong&gt;, threads typically share memory and can directly access the same variables. While this is efficient for communication between threads, it introduces the risk of &lt;strong&gt;race conditions&lt;/strong&gt; if multiple threads try to modify the same data simultaneously. To avoid this, synchronization techniques like &lt;strong&gt;mutexes&lt;/strong&gt; or &lt;strong&gt;semaphores&lt;/strong&gt; are often used, which can lead to complex and error-prone code.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;strong&gt;worker threads in Node.js&lt;/strong&gt; do not share memory directly. Instead, they communicate via &lt;strong&gt;message passing&lt;/strong&gt;, which makes them safer to use in highly concurrent applications. While this communication model is more restrictive, it avoids common issues found in multi-threaded programming.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Worker threads in Node.js provide a powerful mechanism for performing CPU-intensive tasks without blocking the main event loop. They allow for &lt;strong&gt;parallel execution&lt;/strong&gt; of code, enabling Node.js to handle computationally expensive operations like sorting large datasets, calculating Fibonacci numbers, or finding prime numbers more efficiently.&lt;/p&gt;

&lt;p&gt;When compared to threads in &lt;strong&gt;Java&lt;/strong&gt; or &lt;strong&gt;C++&lt;/strong&gt;, worker threads in Node.js offer a simpler model by enforcing memory isolation and communication through message passing. This reduces the risk of concurrency issues, making them easier and safer to use in applications where offloading tasks from the main thread is crucial.&lt;/p&gt;

&lt;p&gt;Whether you are building a web server, performing data analysis, or processing large amounts of data, worker threads can help you achieve better performance and keep your application responsive.&lt;/p&gt;

</description>
      <category>node</category>
      <category>threads</category>
      <category>concurrency</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding Queues in Node.js (The Easy Way!)</title>
      <dc:creator>Abdullah Yasir</dc:creator>
      <pubDate>Sat, 18 Jan 2025 09:38:58 +0000</pubDate>
      <link>https://dev.to/abdullahyasir/understanding-queues-in-nodejs-the-easy-way-1b61</link>
      <guid>https://dev.to/abdullahyasir/understanding-queues-in-nodejs-the-easy-way-1b61</guid>
      <description>&lt;p&gt;If you’ve been working with Node.js, you’ve probably heard terms like &lt;strong&gt;event loop&lt;/strong&gt;, &lt;strong&gt;promises&lt;/strong&gt;, and &lt;strong&gt;queues&lt;/strong&gt; thrown around. But what exactly are these queues, and how do they work together to make Node.js so efficient? Let’s break it down in simple, everyday English.&lt;/p&gt;




&lt;h3&gt;
  
  
  What Are Queues in Node.js?
&lt;/h3&gt;

&lt;p&gt;In Node.js, queues are places where tasks (like callbacks) are stored and processed by the &lt;strong&gt;event loop&lt;/strong&gt;. Think of the queues like different lines at a coffee shop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One line for people with mobile orders&lt;/strong&gt; (microtasks).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Another line for walk-in orders&lt;/strong&gt; (macrotasks like timers).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A separate line for special requests&lt;/strong&gt; (like closing the shop or cleaning up).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each queue has a specific role, and the event loop decides the order in which to serve these lines.&lt;/p&gt;




&lt;h3&gt;
  
  
  Types of Queues in Node.js
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Microtask Queue (VIP Line)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the queue for tiny, high-priority tasks. These include resolved &lt;strong&gt;promises&lt;/strong&gt; and &lt;code&gt;process.nextTick()&lt;/code&gt; callbacks.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine you have a VIP line at a coffee shop that always gets served before the regular line, even if the regular customers were waiting first. That’s how the microtask queue works—it gets priority over everything else.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;/ul&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="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="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;Promise resolved&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;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="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;Next tick&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;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;&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;  Start
  End
  Next tick
  Promise resolved
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; The synchronous code runs first. Then the microtasks (&lt;code&gt;process.nextTick&lt;/code&gt; and &lt;code&gt;Promise.then&lt;/code&gt;) are executed in order.&lt;/p&gt;




&lt;h4&gt;
  
  
  2. &lt;strong&gt;Timer Queue (Walk-In Customers)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This queue handles tasks scheduled by &lt;code&gt;setTimeout()&lt;/code&gt; and &lt;code&gt;setInterval()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine you walk into a coffee shop and place an order. Your order will only be prepared after a specific delay (the timeout period).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

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

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;Timer callback&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 of script&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;  End of script
  Timer callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even with a &lt;code&gt;0&lt;/code&gt; delay, the timer callback is placed in the &lt;strong&gt;timer queue&lt;/strong&gt; and only runs after the current code and microtasks finish.&lt;/p&gt;




&lt;h4&gt;
  
  
  3. &lt;strong&gt;I/O Queue (Order Processing)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This queue handles I/O tasks like reading files, database queries, or network requests.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine you placed an order that takes some time to prepare, like a custom latte. Your order goes into the “processing” line, and you’ll get it once it’s ready.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

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

&lt;/ul&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;file.txt&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;File read complete&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;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 of script&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;  End of script
  File read complete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file read is handled in the I/O queue, so it happens after the synchronous code finishes.&lt;/p&gt;




&lt;h4&gt;
  
  
  4. &lt;strong&gt;Check Queue (Special Express Line)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This queue handles &lt;code&gt;setImmediate()&lt;/code&gt; callbacks, which are processed after I/O tasks but before timers in the next loop iteration.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Think of this as a special express line for people who ordered something unusual but still get priority over timers.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

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

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="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;Immediate callback&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;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 of script&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;  End of script
  Immediate callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  5. &lt;strong&gt;Close Queue (Closing Time Tasks)&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This queue handles callbacks for the &lt;code&gt;close&lt;/code&gt; event, such as closing a file stream or socket.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;How does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Imagine it’s closing time at the coffee shop, and you’re finishing up orders for people who were already served.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

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

&lt;/ul&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;net&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;net&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;net&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;server&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;close&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;Server closed&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;close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Putting It All Together
&lt;/h3&gt;

&lt;p&gt;Here’s how the event loop processes these queues in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Execute all &lt;strong&gt;synchronous code&lt;/strong&gt; first.&lt;/li&gt;
&lt;li&gt;Process the &lt;strong&gt;microtask queue&lt;/strong&gt; (e.g., resolved promises).&lt;/li&gt;
&lt;li&gt;Process the &lt;strong&gt;timer queue&lt;/strong&gt; (e.g., &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Handle &lt;strong&gt;I/O callbacks&lt;/strong&gt; (e.g., file reads, network requests).&lt;/li&gt;
&lt;li&gt;Process the &lt;strong&gt;check queue&lt;/strong&gt; (e.g., &lt;code&gt;setImmediate&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Handle the &lt;strong&gt;close queue&lt;/strong&gt; (e.g., resource cleanup).&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Summary Table
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Queue Type&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Priority&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Microtask Queue&lt;/td&gt;
&lt;td&gt;Promises, &lt;code&gt;process.nextTick&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Highest&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timer Queue&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;After microtasks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;I/O Queue&lt;/td&gt;
&lt;td&gt;File system, network I/O callbacks&lt;/td&gt;
&lt;td&gt;After timers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Check Queue&lt;/td&gt;
&lt;td&gt;&lt;code&gt;setImmediate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;After I/O&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Close Queue&lt;/td&gt;
&lt;td&gt;Resource close events (&lt;code&gt;'close'&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;After check phase&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Why Should You Care?
&lt;/h3&gt;

&lt;p&gt;Understanding how these queues work can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Help you debug issues like &lt;strong&gt;unexpected delays&lt;/strong&gt; in callbacks.&lt;/li&gt;
&lt;li&gt;Optimize performance by knowing when and where to schedule tasks.&lt;/li&gt;
&lt;li&gt;Make you a better Node.js developer overall!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next time you’re debugging a Node.js app, think of the coffee shop queues and remember—Node.js might look single-threaded, but it’s got a lot happening behind the scenes!&lt;/p&gt;

</description>
      <category>node</category>
      <category>eventloop</category>
      <category>queues</category>
      <category>promises</category>
    </item>
  </channel>
</rss>
