<?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: Adarsh Hasnah</title>
    <description>The latest articles on DEV Community by Adarsh Hasnah (@adarshasnah).</description>
    <link>https://dev.to/adarshasnah</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%2F616181%2F37a927cf-7081-4d8a-b7b8-c41b6d672102.jpeg</url>
      <title>DEV Community: Adarsh Hasnah</title>
      <link>https://dev.to/adarshasnah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adarshasnah"/>
    <language>en</language>
    <item>
      <title>Kebab-Case Filenames and PascalCase Classes: Naming Conventions That Scale</title>
      <dc:creator>Adarsh Hasnah</dc:creator>
      <pubDate>Tue, 27 Jan 2026 08:50:21 +0000</pubDate>
      <link>https://dev.to/adarshasnah/kebab-case-filenames-and-pascalcase-classes-naming-conventions-that-scale-7dp</link>
      <guid>https://dev.to/adarshasnah/kebab-case-filenames-and-pascalcase-classes-naming-conventions-that-scale-7dp</guid>
      <description>&lt;p&gt;Naming conventions are not cosmetic.&lt;br&gt;
They are &lt;strong&gt;how developers understand intent at scale&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In modern JavaScript and TypeScript projects, inconsistent naming leads&lt;br&gt;
to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;slower onboarding &lt;/li&gt;
&lt;li&gt;harder navigation &lt;/li&gt;
&lt;li&gt;unnecessary debates in code reviews&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After years of working with Node.js, TypeScript, and modular systems,&lt;br&gt;
one combination consistently delivers clarity with minimal friction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;&lt;code&gt;kebab-case&lt;/code&gt; for filenames&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Explicit responsibility-based suffixes&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;&lt;code&gt;PascalCase&lt;/code&gt; for classes and public APIs&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article explains &lt;strong&gt;why these naming conventions work&lt;/strong&gt;, and why&lt;br&gt;
they scale better than common alternatives.&lt;/p&gt;


&lt;h2&gt;
  
  
  Why Naming Conventions Matter in TypeScript Projects
&lt;/h2&gt;

&lt;p&gt;File and symbol names are &lt;strong&gt;human interfaces&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Good naming conventions should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Predictable&lt;/li&gt;
&lt;li&gt;  Easy to scan&lt;/li&gt;
&lt;li&gt;  Cross-platform safe&lt;/li&gt;
&lt;li&gt;  Tooling-friendly&lt;/li&gt;
&lt;li&gt;  Explicit about responsibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is not cleverness --- it's &lt;strong&gt;removing cognitive load&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Why Use &lt;code&gt;kebab-case&lt;/code&gt; for Filenames?
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Example (Suffix-Enforced)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.service.ts
email-notification.controller.ts
cache-entry.repository.ts
data-cleanup.job.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Pattern:&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;&amp;lt;domain-or-feature&amp;gt;.&amp;lt;responsibility&amp;gt;.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  1. Cross-Platform Safety
&lt;/h3&gt;

&lt;p&gt;Operating systems treat filenames differently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Windows / macOS → case-insensitive&lt;/li&gt;
&lt;li&gt;  Linux → case-sensitive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two files are a common source of bugs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UserService.ts
userService.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;kebab-case&lt;/code&gt; eliminates ambiguity and avoids OS-specific issues.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Native to Web and Tooling Ecosystems
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;kebab-case&lt;/code&gt; is already the default for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  URLs&lt;/li&gt;
&lt;li&gt;  npm packages&lt;/li&gt;
&lt;li&gt;  Docker images&lt;/li&gt;
&lt;li&gt;  GitHub repositories&lt;/li&gt;
&lt;li&gt;  CI/CD pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using it for filenames creates &lt;strong&gt;consistency across your entire&lt;br&gt;
toolchain&lt;/strong&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  3. Better Readability for Long Filenames
&lt;/h3&gt;

&lt;p&gt;Long filenames are unavoidable in real-world systems.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;emailNotificationRetryConfigurationService.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;email-notification-retry-configuration.service.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;kebab-case&lt;/code&gt; improves scannability and reduces visual parsing effort.&lt;/p&gt;




&lt;h2&gt;
  
  
  Responsibility-Based Suffixes Improve Clarity
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;kebab-case&lt;/code&gt; improves readability, &lt;strong&gt;suffixes communicate&lt;br&gt;
intent&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Common Responsibility Suffixes
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.entity.ts
.value-object.ts
.repository.ts
.service.ts
.use-case.ts
.controller.ts
.route.ts
.dto.ts
.mapper.ts
.job.ts
.event.ts
.policy.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why the Responsibility Suffix Comes Last
&lt;/h3&gt;

&lt;p&gt;Responsibility is always the &lt;strong&gt;final signal&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;*.service.ts
*.repository.ts
*.controller.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Benefits: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster scanning &lt;/li&gt;
&lt;li&gt;Easier filtering &lt;/li&gt;
&lt;li&gt;Tooling enforcement (linting, boundaries) &lt;/li&gt;
&lt;li&gt;Clear architectural intent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should understand &lt;em&gt;what a file is&lt;/em&gt; without opening it.&lt;/p&gt;




&lt;h3&gt;
  
  
  Avoid Embedding Responsibility in Names
&lt;/h3&gt;

&lt;p&gt;❌ Avoid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user-service.ts
email-notification-controller.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Prefer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.service.ts
email-notification.controller.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps responsibility &lt;strong&gt;explicit, standardized, and enforceable&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Use &lt;code&gt;PascalCase&lt;/code&gt; for Classes?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserService&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotificationSender&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataCleanupJob&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  1. Immediate Type Signaling
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;PascalCase&lt;/code&gt; instantly signals:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is a class, type, or conceptual construct.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This distinction matters when scanning large codebases.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Aligns With TypeScript Standards
&lt;/h3&gt;

&lt;p&gt;In TypeScript: - Classes → &lt;code&gt;PascalCase&lt;/code&gt; - Interfaces → &lt;code&gt;PascalCase&lt;/code&gt; -&lt;br&gt;
Types → &lt;code&gt;PascalCase&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MessageSender&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmtpMessageSender&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;MessageSender&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consistency reduces mental overhead and ambiguity.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. File-to-Class Naming Symmetry
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.service.ts            → class UserService
email-notification.job.ts  → class EmailNotificationJob
cache-entry.entity.ts      → class CacheEntry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This symmetry makes navigation predictable and intuitive.&lt;/p&gt;




&lt;h2&gt;
  
  
  Naming Functions: PascalCase vs camelCase
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Practical Rule
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Public / exported / use-case functions&lt;/strong&gt; → &lt;code&gt;PascalCase&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Local helper functions&lt;/strong&gt; → &lt;code&gt;camelCase&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;RegisterUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;SendNotification&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;sanitizeInput&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;retryWithBackoff&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;&lt;code&gt;PascalCase&lt;/code&gt; functions read like &lt;strong&gt;actions&lt;/strong&gt;, not utilities.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Not &lt;code&gt;camelCase&lt;/code&gt; or &lt;code&gt;snake_case&lt;/code&gt; for Filenames?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;snake_case&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Rare in modern JS/TS ecosystems&lt;/li&gt;
&lt;li&gt;  Uncommon in URLs and tooling&lt;/li&gt;
&lt;li&gt;  Inconsistent with npm and Docker norms&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;camelCase&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Harder to scan&lt;/li&gt;
&lt;li&gt;  Case-sensitivity issues&lt;/li&gt;
&lt;li&gt;  Visually dense in file explorers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For filenames, &lt;code&gt;kebab-case&lt;/code&gt; is the most readable and compatible option.&lt;/p&gt;




&lt;h2&gt;
  
  
  Reduced Cognitive Load Is the Real Benefit
&lt;/h2&gt;

&lt;p&gt;Once these conventions are established: - naming debates disappear -&lt;br&gt;
navigation becomes intuitive - onboarding gets faster&lt;/p&gt;

&lt;p&gt;Good conventions fade into the background --- exactly where they belong.&lt;/p&gt;


&lt;h2&gt;
  
  
  TL;DR: Recommended Naming Conventions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Filenames&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;kebab-case + explicit responsibility suffix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Classes / Types&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;PascalCase&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Local variables &amp;amp; helpers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;camelCase&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Clean naming conventions are not about style.\&lt;br&gt;
They're about &lt;strong&gt;clarity, scalability, and developer experience&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When filenames communicate responsibility and symbols reflect intent,&lt;br&gt;
your codebase becomes easier to reason about --- today and years from&lt;br&gt;
now.&lt;/p&gt;

&lt;p&gt;Consistency beats cleverness. Every time.&lt;/p&gt;

</description>
      <category>convention</category>
      <category>naming</category>
      <category>architecture</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Node.js Production Flags: Which Runtime Flags Are Safe (and Which Will Break Your App)</title>
      <dc:creator>Adarsh Hasnah</dc:creator>
      <pubDate>Tue, 16 Dec 2025 10:26:25 +0000</pubDate>
      <link>https://dev.to/adarshasnah/nodejs-production-flags-which-runtime-flags-are-safe-and-which-will-break-your-app-1ocp</link>
      <guid>https://dev.to/adarshasnah/nodejs-production-flags-which-runtime-flags-are-safe-and-which-will-break-your-app-1ocp</guid>
      <description>&lt;p&gt;Node.js provides dozens of runtime flags, but &lt;strong&gt;very few are safe to use in production&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Misusing Node.js flags is a common cause of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;unstable performance&lt;/li&gt;
&lt;li&gt;memory crashes&lt;/li&gt;
&lt;li&gt;unpredictable behavior under load&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article explains &lt;strong&gt;Node.js production flags&lt;/strong&gt; that are actually safe, which ones should be avoided, and &lt;strong&gt;how to think about runtime flags as part of your production contract&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you run Node.js in production (Express, Fastify, containers, Kubernetes), this guide is for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Are Node.js Runtime Flags?
&lt;/h2&gt;

&lt;p&gt;Node.js runtime flags are command-line options passed to the &lt;code&gt;node&lt;/code&gt; process that modify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;memory behavior&lt;/li&gt;
&lt;li&gt;error handling&lt;/li&gt;
&lt;li&gt;module loading&lt;/li&gt;
&lt;li&gt;debugging and diagnostics&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--enable-source-maps&lt;/span&gt; dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In production, runtime flags should be &lt;strong&gt;deliberate and minimal&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Simple Rule for Node.js Production Flags
&lt;/h2&gt;

&lt;p&gt;Before enabling any Node.js flag in production, ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does this flag improve &lt;strong&gt;correctness&lt;/strong&gt;, &lt;strong&gt;stability&lt;/strong&gt;, or &lt;strong&gt;observability&lt;/strong&gt;?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is “developer convenience” or “local debugging”, it does &lt;strong&gt;not&lt;/strong&gt; belong in production.&lt;/p&gt;




&lt;h2&gt;
  
  
  Safe Node.js Flags for Production
&lt;/h2&gt;

&lt;p&gt;These Node.js runtime flags are widely used in real production systems and have predictable behavior.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;--enable-source-maps&lt;/code&gt; (Strongly Recommended)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--enable-source-maps&lt;/span&gt; dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Maps JavaScript stack traces back to the original source files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it’s safe in production&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Essential for TypeScript applications&lt;/li&gt;
&lt;li&gt;Dramatically improves error diagnostics&lt;/li&gt;
&lt;li&gt;Negligible performance overhead&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run Node.js with TypeScript and don’t enable source maps, production debugging becomes guesswork.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;code&gt;--max-old-space-size&lt;/code&gt; (Memory Control)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--max-old-space-size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1024 dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Limits the V8 heap size in megabytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it’s important&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevents unexpected out-of-memory kills&lt;/li&gt;
&lt;li&gt;Makes Node.js memory usage predictable&lt;/li&gt;
&lt;li&gt;Essential in Docker and Kubernetes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best practice&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Set this to &lt;strong&gt;70–80% of container memory&lt;/strong&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;code&gt;--unhandled-rejections=strict&lt;/code&gt; (Correctness)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--unhandled-rejections&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;strict dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Crashes the process on unhandled promise rejections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it belongs in production&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exposes broken async code immediately&lt;/li&gt;
&lt;li&gt;Prevents silent data corruption&lt;/li&gt;
&lt;li&gt;Forces correct error handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unhandled promise rejections are one of the most common hidden Node.js production bugs.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;code&gt;--import&lt;/code&gt; / &lt;code&gt;-r&lt;/code&gt; (Preloading for Instrumentation)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--import&lt;/span&gt; ./dist/otel.js dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-r&lt;/span&gt; ./dist/otel.js dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Loads a module before your application starts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Valid production use cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenTelemetry instrumentation&lt;/li&gt;
&lt;li&gt;Global diagnostics&lt;/li&gt;
&lt;li&gt;Performance monitoring setup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Avoid&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Business logic&lt;/li&gt;
&lt;li&gt;Application configuration&lt;/li&gt;
&lt;li&gt;Feature flags&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Node.js preloading should prepare the runtime — not change application behavior.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;code&gt;--trace-warnings&lt;/code&gt; (Conditional Use)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--trace-warnings&lt;/span&gt; dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Adds stack traces to runtime warnings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Staging environments&lt;/li&gt;
&lt;li&gt;Short-term production debugging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid leaving this enabled permanently due to log noise.&lt;/p&gt;


&lt;h2&gt;
  
  
  Node.js Flags You Should Avoid in Production
&lt;/h2&gt;

&lt;p&gt;Many Node.js flags exist for development or debugging and should &lt;strong&gt;never&lt;/strong&gt; run in production.&lt;/p&gt;


&lt;h3&gt;
  
  
  Development-Only Node.js Flags
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;Why It’s Unsafe&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--watch&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Auto-restart breaks stability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;--inspect&lt;/code&gt;, &lt;code&gt;--inspect-brk&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Security risk&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--loader ts-node/esm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Runtime TypeScript compilation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ts-node&lt;/code&gt;, &lt;code&gt;tsx&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dev tooling only&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h3&gt;
  
  
  Experimental Node.js Flags
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;Risk&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--experimental-*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unstable behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;--experimental-fetch&lt;/code&gt; (older Node)&lt;/td&gt;
&lt;td&gt;Inconsistent semantics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--experimental-vm-modules&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Tooling only&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If a flag is marked &lt;em&gt;experimental&lt;/em&gt;, it is not production-ready.&lt;/p&gt;


&lt;h3&gt;
  
  
  High-Overhead Diagnostic Flags
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Flag&lt;/th&gt;
&lt;th&gt;Why to Avoid&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--trace-gc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Severe performance impact&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--prof&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Profiling only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;--trace-events-enabled&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Heavy overhead&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Use these flags only during controlled profiling sessions.&lt;/p&gt;


&lt;h2&gt;
  
  
  Node.js Runtime Flags vs Application Code
&lt;/h2&gt;

&lt;p&gt;A healthy production system respects this boundary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Node.js flags configure the runtime&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Application code defines behavior&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a flag:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;changes request handling&lt;/li&gt;
&lt;li&gt;toggles features&lt;/li&gt;
&lt;li&gt;alters business logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;…it’s being misused.&lt;/p&gt;


&lt;h2&gt;
  
  
  Recommended Node.js Production Startup Command
&lt;/h2&gt;

&lt;p&gt;Most production Node.js applications need &lt;strong&gt;very few flags&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--enable-source-maps&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--max-old-space-size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1024 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--unhandled-rejections&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;strict &lt;span class="se"&gt;\&lt;/span&gt;
  dist/server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;--import&lt;/code&gt; only when you use runtime instrumentation like OpenTelemetry.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Node.js Flags Are Part of Your Production Contract
&lt;/h2&gt;

&lt;p&gt;Node.js runtime flags are not an implementation detail.&lt;br&gt;&lt;br&gt;
They directly affect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;memory behavior&lt;/li&gt;
&lt;li&gt;error handling&lt;/li&gt;
&lt;li&gt;observability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changing flags is a &lt;strong&gt;production change&lt;/strong&gt;, just like changing container limits or deployment configs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts on Node.js Production Flags
&lt;/h2&gt;

&lt;p&gt;Node.js gives you powerful runtime controls.&lt;br&gt;&lt;br&gt;
Production systems should use &lt;strong&gt;as few of them as possible&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If your application needs many Node.js flags to stay stable, the problem isn’t the flags — it’s the architecture.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Keep the Node.js runtime boring.&lt;br&gt;&lt;br&gt;
Boring systems are reliable systems.&lt;/p&gt;

</description>
      <category>node</category>
      <category>production</category>
      <category>backend</category>
      <category>devops</category>
    </item>
    <item>
      <title>A Practical Introduction to AsyncLocalStorage in Node.js (With Real Use Cases)</title>
      <dc:creator>Adarsh Hasnah</dc:creator>
      <pubDate>Fri, 12 Dec 2025 06:50:13 +0000</pubDate>
      <link>https://dev.to/adarshasnah/a-practical-introduction-to-asynclocalstorage-in-nodejs-with-real-use-cases-50mo</link>
      <guid>https://dev.to/adarshasnah/a-practical-introduction-to-asynclocalstorage-in-nodejs-with-real-use-cases-50mo</guid>
      <description>&lt;p&gt;AsyncLocalStorage (ALS) is a powerful but often misunderstood feature in Node.js. At its core, ALS provides a reliable way to maintain &lt;strong&gt;execution context across asynchronous operations&lt;/strong&gt;—a challenge every Node.js backend eventually faces.&lt;/p&gt;

&lt;p&gt;If you’ve ever needed to keep track of metadata such as &lt;strong&gt;request IDs&lt;/strong&gt;, &lt;strong&gt;tenant identifiers&lt;/strong&gt;, &lt;strong&gt;locales&lt;/strong&gt;, or &lt;strong&gt;feature flags&lt;/strong&gt; across nested &lt;code&gt;async/await&lt;/code&gt; calls, ALS is the tool designed for that job.&lt;/p&gt;

&lt;p&gt;This article explains what AsyncLocalStorage is, how it works, and how to apply it effectively using practical examples.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is AsyncLocalStorage in Node.js?
&lt;/h2&gt;

&lt;p&gt;AsyncLocalStorage is part of the &lt;code&gt;async_hooks&lt;/code&gt; module and functions similarly to thread-local storage in multi-threaded languages—except adapted for Node’s event-loop and asynchronous model.&lt;/p&gt;

&lt;p&gt;It creates a &lt;strong&gt;context store&lt;/strong&gt; that persists across all asynchronous boundaries triggered within a specific scope.&lt;/p&gt;

&lt;p&gt;This enables predictable context propagation through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promises
&lt;/li&gt;
&lt;li&gt;Timers (&lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;Database clients
&lt;/li&gt;
&lt;li&gt;External API calls
&lt;/li&gt;
&lt;li&gt;Streams
&lt;/li&gt;
&lt;li&gt;Message queue handlers
&lt;/li&gt;
&lt;li&gt;Any nested async function
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Core benefit:&lt;/strong&gt; You no longer need to pass metadata manually through every function call.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why AsyncLocalStorage Matters for Modern Node.js Applications
&lt;/h2&gt;

&lt;p&gt;Maintaining context is important for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request correlation and structured logging
&lt;/li&gt;
&lt;li&gt;Distributed tracing
&lt;/li&gt;
&lt;li&gt;Multi-tenant routing
&lt;/li&gt;
&lt;li&gt;User or locale propagation
&lt;/li&gt;
&lt;li&gt;Feature flag consistency
&lt;/li&gt;
&lt;li&gt;Tracking background job executions
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ALS is fundamentally about &lt;strong&gt;context propagation&lt;/strong&gt;, not just logging.&lt;/p&gt;




&lt;h2&gt;
  
  
  Architecture: How ALS Propagates Context
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Incoming Request / Task
        │
        ▼
┌────────────────────────┐
│ Initialize ALS Context │  ← store.run(...)
└───────────┬────────────┘
            │
            ▼
Handlers → Services → Async Calls → DB/API → Response
            │
            ▼
   The ALS context remains accessible everywhere
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ALS creates a scope once, and all subsequent async operations continue to use the same context.&lt;/p&gt;




&lt;h2&gt;
  
  
  Minimal AsyncLocalStorage Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:async_hooks&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;store&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;AsyncLocalStorage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&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="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="s1"&gt;Context:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStore&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;50&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;&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;Context: { value: 123 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even across timeout boundaries, the context is preserved.&lt;/p&gt;




&lt;h2&gt;
  
  
  Using AsyncLocalStorage in an Express Application
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Initialize Context per Request
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:async_hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;randomUUID&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;crypto&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;asyncStore&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;AsyncLocalStorage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-request-id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;asyncStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&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="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Access the Context Later in the Flow
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getContext&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;asyncStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStore&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&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;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getContext&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;Request ID:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requestId&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the classic &lt;strong&gt;request ID correlation&lt;/strong&gt; pattern and is widely used for production observability.&lt;/p&gt;




&lt;h2&gt;
  
  
  Other Practical AsyncLocalStorage Use Cases
&lt;/h2&gt;

&lt;p&gt;Beyond correlation IDs, ALS supports several operational patterns:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Multitenancy Context
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;asyncStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;acme&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="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Locale &amp;amp; Internationalization
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;locale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;accept-language&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Feature Flag Snapshot
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;featureFlags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;evaluateFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Background Job Tracking
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&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;job&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;job&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;asyncStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;processJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;job&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;These examples rely on the same mechanism: &lt;strong&gt;execution-scoped context&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Should You Use AsyncLocalStorage?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;ALS Recommended?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Request/Execution scoped metadata&lt;/td&gt;
&lt;td&gt;✔ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request ID correlation&lt;/td&gt;
&lt;td&gt;✔ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Distributed tracing&lt;/td&gt;
&lt;td&gt;✔ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multi-tenant context&lt;/td&gt;
&lt;td&gt;✔ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Passing large mutable objects&lt;/td&gt;
&lt;td&gt;✘ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Global config&lt;/td&gt;
&lt;td&gt;✘ No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;ALS should store lightweight metadata, not application state.&lt;/p&gt;




&lt;h2&gt;
  
  
  FAQ: Common Questions About AsyncLocalStorage
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is AsyncLocalStorage used for in Node.js?
&lt;/h3&gt;

&lt;p&gt;It maintains execution-scoped context across async operations, enabling request correlation, multitenancy, tracing, and more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is AsyncLocalStorage only for request IDs?
&lt;/h3&gt;

&lt;p&gt;No. Request IDs are just one example. ALS can hold any metadata relevant to the async execution scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does ALS work with async/await?
&lt;/h3&gt;

&lt;p&gt;Yes. ALS propagates through promises, timers, callbacks, and most async libraries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can ALS be used outside Express?
&lt;/h3&gt;

&lt;p&gt;Yes. It works in Fastify, NestJS, message queues, CRON jobs, and any async workflow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is there a performance cost?
&lt;/h3&gt;

&lt;p&gt;A small one, but acceptable for most workloads. ALS is used by major frameworks and tracing tools.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;AsyncLocalStorage is a foundational tool for building reliable, observable Node.js applications. It provides a clean and efficient way to maintain execution context without manually passing metadata through every function. While request ID correlation is the most common pattern, ALS supports many other practical use cases such as multitenancy, localization, feature flags, and background job correlation.&lt;/p&gt;

&lt;p&gt;If you're building production-grade Node.js services, understanding ALS will significantly improve your architecture, debugging capabilities, and observability.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>backend</category>
      <category>observability</category>
    </item>
    <item>
      <title>Mastering Request Correlation: The Key to Debugging Microservices Without Losing Your Mind</title>
      <dc:creator>Adarsh Hasnah</dc:creator>
      <pubDate>Thu, 11 Dec 2025 06:41:01 +0000</pubDate>
      <link>https://dev.to/adarshasnah/mastering-request-correlation-the-key-to-debugging-microservices-without-losing-your-mind-2kho</link>
      <guid>https://dev.to/adarshasnah/mastering-request-correlation-the-key-to-debugging-microservices-without-losing-your-mind-2kho</guid>
      <description>&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; &lt;br&gt;
Ever struggled debugging microservices? Request correlation is the missing thread that connects logs, metrics, and traces. This practical, story-driven guide explains why it matters, how it works, and how to implement it cleanly—with diagrams and real examples.&lt;/p&gt;



&lt;p&gt;Distributed systems are fun—right up until something goes wrong.&lt;/p&gt;

&lt;p&gt;If you’ve ever tailed logs from multiple services at 1AM, hopping between dashboards while your coffee goes cold, trying to explain why a simple request took twelve seconds… then you’ve already met the exact problem request correlation exists to solve.&lt;/p&gt;

&lt;p&gt;Most teams only start caring about it once things catch fire. But once you implement it properly, you’ll wonder how you ever operated without it.&lt;/p&gt;


&lt;h2&gt;
  
  
  What Request Correlation Actually Is
&lt;/h2&gt;

&lt;p&gt;Request correlation is simply the idea of giving every incoming request a unique ID—and then making sure that ID travels through every service, log entry, async job, and metric connected to that request.&lt;/p&gt;

&lt;p&gt;In microservices, this is the closest thing we have to a universal translator.&lt;/p&gt;

&lt;p&gt;Here’s what the “story thread” looks like as a request moves through your system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   │
   ▼
┌─────────────────────────────┐
│         API Gateway         │
│   assigns X-Request-ID      │
│            abc123           │
└───────────────┬─────────────┘
                │
      X-Request-ID: abc123
                │
                ▼
       ┌───────────────────┐
       │     Service A     │
       │ logs cid=abc123   │
       └──────────┬────────┘
                  │
        X-Request-ID: abc123
                  │
                  ▼
       ┌───────────────────┐
       │     Service B     │
       │ logs cid=abc123   │
       └──────────┬────────┘
                  │
        X-Request-ID: abc123
                  │
                  ▼
       ┌───────────────────┐
       │     Service C     │
       │ logs cid=abc123   │
       └───────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One request, one ID, one coherent story.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Request Correlation Matters for Distributed Systems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Debugging becomes sane again
&lt;/h3&gt;

&lt;p&gt;Without correlation IDs, debugging microservices feels like detective work with no fingerprints.&lt;br&gt;&lt;br&gt;
With them, one search gives you the entire request journey.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. You finally see the real bottlenecks
&lt;/h3&gt;

&lt;p&gt;A slow endpoint is usually not the culprit—it’s one of the services it calls.&lt;br&gt;&lt;br&gt;
Correlation exposes the actual hotspot.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Your logs become dramatically more valuable
&lt;/h3&gt;

&lt;p&gt;Most logs tell you &lt;em&gt;what&lt;/em&gt; happened.&lt;br&gt;&lt;br&gt;
Correlation tells you &lt;em&gt;why&lt;/em&gt; and &lt;em&gt;in which order&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Observability becomes a unified system
&lt;/h3&gt;

&lt;p&gt;Correlation IDs are the glue between logs, metrics, and traces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────┐     ┌──────────┐     ┌──────────┐
│  Logs    │ --&amp;gt; │  Metrics │ --&amp;gt; │  Traces  │
└────┬─────┘     └────┬─────┘     └────┬─────┘
     │                │                │
     └────────────── correlation ID ───┘

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

&lt;/div&gt;



&lt;p&gt;When everything shares the same ID, your observability stack becomes one narrative instead of three silos.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where Teams Get Request Correlation Wrong
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🚫 Mistake 1 — Not propagating the ID to downstream services
&lt;/h3&gt;

&lt;p&gt;If Service A creates it but doesn’t pass it to Service B, the trail goes cold.&lt;/p&gt;

&lt;h3&gt;
  
  
  🚫 Mistake 2 — Overwriting an existing client-provided ID
&lt;/h3&gt;

&lt;p&gt;If the request already has &lt;code&gt;X-Request-ID&lt;/code&gt; or &lt;code&gt;traceparent&lt;/code&gt;, keep it.&lt;br&gt;&lt;br&gt;
You’re continuing a story, not starting a new one.&lt;/p&gt;
&lt;h3&gt;
  
  
  🚫 Mistake 3 — Logging without structure
&lt;/h3&gt;

&lt;p&gt;Correlation IDs buried inside text logs are nearly useless.&lt;br&gt;&lt;br&gt;
Use JSON logs so tools can index them properly.&lt;/p&gt;
&lt;h3&gt;
  
  
  🚫 Mistake 4 — Treating correlation as optional
&lt;/h3&gt;

&lt;p&gt;It must be consistent.&lt;br&gt;&lt;br&gt;
It should behave like authentication: always present, always accurate.&lt;/p&gt;


&lt;h2&gt;
  
  
  A Practical Example Using Node.js and &lt;code&gt;AsyncLocalStorage&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s a clean and robust approach for correlation in Node.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AsyncLocalStorage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:async_hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:crypto&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;storage&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;AsyncLocalStorage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;correlationMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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;correlationId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-request-id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;correlationId&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-request-id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;correlationId&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="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCorrelationId&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;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStore&lt;/span&gt;&lt;span class="p"&gt;()?.&lt;/span&gt;&lt;span class="nx"&gt;correlationId&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;Behind the scenes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP request arrives
        │
        ▼
 create context { cid: abc123 }
        │
        ▼
 handler → DB call → internal fn → logger
 (context preserved the entire time)

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

&lt;/div&gt;



&lt;p&gt;This is what turns spaghetti logs into a timeline you can trust.&lt;/p&gt;




&lt;h2&gt;
  
  
  Debugging With Correlation (the good part)
&lt;/h2&gt;

&lt;p&gt;This is where you feel the payoff:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request ID: abc123
───────────────────────────────────────────
[Gateway]      received request
[Service A]    validated payload
[Service A]    calling Service B
[Service B]    fetching user profile
[Service C]    cache miss (140ms)
[Service B]    returned profile
[Service A]    sent final response
───────────────────────────────────────────
Total time: 412ms
Bottleneck: Service C

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

&lt;/div&gt;



&lt;p&gt;No guessing.&lt;br&gt;
No grepping through chaos.&lt;br&gt;
Just answers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Debugging Without Correlation (the old way)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2025-01-11T10:02:33Z Service A - incoming call
2025-01-11T10:02:33Z Service B - fetch user
2025-01-11T10:02:33Z Service C - cache miss
2025-01-11T10:02:34Z Service B - returning data
2025-01-11T10:02:34Z Service A - responding

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

&lt;/div&gt;



&lt;p&gt;Which logs belong to which user?&lt;br&gt;
Which request failed?&lt;br&gt;
Which step was slow?&lt;/p&gt;

&lt;p&gt;It’s like being handed random pages from random books and told to interpret the story.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Real Lesson
&lt;/h2&gt;

&lt;p&gt;If your system is growing, add request correlation now.&lt;br&gt;
If it’s already misbehaving, add it immediately.&lt;/p&gt;

&lt;p&gt;It’s one of the smallest pieces of code you’ll ever write…&lt;br&gt;
…for one of the biggest improvements in how you understand and operate your system.&lt;/p&gt;

&lt;p&gt;Once you follow a single ID end-to-end and it leads you straight to the issue, you’ll wonder how you ever debugged without it.&lt;/p&gt;




&lt;h2&gt;
  
  
  What about you?
&lt;/h2&gt;

&lt;p&gt;Have you implemented request correlation in your stack?&lt;br&gt;
What tools or patterns worked best for you?&lt;/p&gt;

&lt;p&gt;I’d love to hear how you approach debugging in distributed systems.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>observability</category>
      <category>architecture</category>
      <category>node</category>
    </item>
  </channel>
</rss>
