<?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: Mohsen Fallahnejad</title>
    <description>The latest articles on DEV Community by Mohsen Fallahnejad (@mohsenfallahnjd).</description>
    <link>https://dev.to/mohsenfallahnjd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F349786%2F6dbdcc88-15a0-4220-b445-20638b4909eb.jpeg</url>
      <title>DEV Community: Mohsen Fallahnejad</title>
      <link>https://dev.to/mohsenfallahnjd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohsenfallahnjd"/>
    <language>en</language>
    <item>
      <title>Does AI Actually Learn? Human vs Machine Learning</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Fri, 04 Dec 2026 15:28:41 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/does-ai-actually-learn-human-vs-machine-learning-22g0</link>
      <guid>https://dev.to/mohsenfallahnjd/does-ai-actually-learn-human-vs-machine-learning-22g0</guid>
      <description>&lt;p&gt;Picture this: you correct an AI chatbot on a bug in your codebase. It replies, &lt;em&gt;"Got it, I'll remember that!"&lt;/em&gt; Five minutes later you open a new chat, and it makes the exact same mistake.&lt;/p&gt;

&lt;p&gt;So — is &lt;strong&gt;"learning"&lt;/strong&gt; even a meaningful word for what AI does?&lt;/p&gt;

&lt;p&gt;The honest answer: &lt;strong&gt;yes in a narrow, mathematical sense — and no in the human sense.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model
&lt;/h2&gt;

&lt;p&gt;Human learning is &lt;strong&gt;building and updating a live mental model of the world&lt;/strong&gt;. Touch a hot stove once, and you instantly understand &lt;em&gt;cause and effect&lt;/em&gt;. You don't need 10,000 burns, and you carry that lesson into every kitchen for the rest of your life.&lt;/p&gt;

&lt;p&gt;AI "learning" is &lt;strong&gt;tuning billions of numerical dials during a one-time factory setup, then locking them behind glass&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once training ends, the dials stop moving. When you chat with an AI, it isn't learning from you — it is running frozen math on whatever text you hand it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inside an LLM: nodes, neurons, and weights
&lt;/h2&gt;

&lt;p&gt;Words like &lt;strong&gt;"neural network"&lt;/strong&gt; and &lt;strong&gt;"neuron"&lt;/strong&gt; make AI sound biological. Under the hood, it is much more mechanical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A node (artificial neuron)&lt;/strong&gt; is not a living brain cell. It is a tiny math formula: take numbers coming in, multiply each one by a &lt;strong&gt;weight&lt;/strong&gt; (a dial that says how much that connection matters), add them up, and pass out a new number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Layers&lt;/strong&gt; are stacks of these nodes wired together. Early layers pick up simple patterns (spelling, syntax); deeper layers combine them into abstract ones (tone, code structure, logic).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An LLM (Large Language Model)&lt;/strong&gt; is a massive neural network built on the &lt;strong&gt;Transformer&lt;/strong&gt; architecture, which adds &lt;strong&gt;attention&lt;/strong&gt; — a mechanism that lets the network weigh which earlier words in your prompt matter most for predicting the next word.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compare that to a human brain: biological neurons are living electrochemical cells that grow new connections and rewire themselves &lt;em&gt;continuously&lt;/em&gt; as you live. An artificial node is a floating-point number inside a GPU matrix multiplication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbitlyst.blog%2Fassets%2Fdoes-ai-learn-neural-net.jpg" 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%2Fbitlyst.blog%2Fassets%2Fdoes-ai-learn-neural-net.jpg" alt="AI-generated diagram. Data flows forward through weighted connections to make a guess; during training, the error flows backward to nudge those weights." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How AI actually "learns" (and then uses it)
&lt;/h2&gt;

&lt;p&gt;In machine learning, "learning" means &lt;strong&gt;adjusting weights to reduce prediction error on data&lt;/strong&gt;. For an LLM, that happens in two training phases — both finished &lt;em&gt;before&lt;/em&gt; you ever open the chat window.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Pre-training: turning the dials
&lt;/h3&gt;

&lt;p&gt;During pre-training, the model reads trillions of words and plays one game over and over: &lt;strong&gt;predict the next token&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Forward pass:&lt;/strong&gt; Text flows left-to-right through the layers and the model outputs a guess.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loss:&lt;/strong&gt; We compare the guess against the real next word and score how wrong it was.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backpropagation &amp;amp; gradient descent:&lt;/strong&gt; The error flows backward through the network so every weight gets nudged a tiny step in the direction that would have made the guess less wrong.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is that entire loop mocked in 10 lines of JavaScript for a &lt;strong&gt;single node&lt;/strong&gt; learning &lt;code&gt;y = 2x&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1. TRAINING — the dial (weight) starts wrong and gets nudged&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.2&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="n"&gt;step&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="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;step&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// input x = 3, target = 6&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;guess&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// how wrong was the guess?&lt;/span&gt;
  &lt;span class="n"&gt;weight&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// nudge the dial toward less error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// weight is now ~1.99  ← "learned"&lt;/span&gt;

&lt;span class="c1"&gt;// 2. INFERENCE — the dial is frozen; we only read it&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;predict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ~9.95  ← uses the learned weight, never changes it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Real LLMs run the exact same idea — just with &lt;strong&gt;hundreds of billions of weights&lt;/strong&gt; instead of one.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Post-training: shaping behavior
&lt;/h3&gt;

&lt;p&gt;A raw pre-trained model only completes text. To turn it into a useful assistant, labs run a second round of weight updates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Supervised Fine-Tuning (SFT):&lt;/strong&gt; Training on curated examples of helpful answers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reinforcement Learning (RLHF / RLVR):&lt;/strong&gt; Rewarding answers that humans prefer or that pass automated checks (like unit tests or math verifiers), nudging the weights toward those patterns.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Inference: using what was learned
&lt;/h3&gt;

&lt;p&gt;When the model ships and you chat with it (&lt;strong&gt;inference&lt;/strong&gt;), &lt;strong&gt;zero weights change&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Training vs Inference: when dials move vs when they are used&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;1. Raw Text &amp;amp; Examples  — Trillions of tokens or your chat prompt
2. Model Weights        — Billions of numbers stored in the network
  ├─ Training (learning) → Guess Next Token → Measure Loss → Nudge Weights
  └─ Inference (using) → Frozen Weights → Pick Likely Next Token → Reply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Training changes the weights. Inference only reads them — your chat never turns a single dial.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Why don't models keep updating their weights live as you talk to them? Because of &lt;strong&gt;catastrophic forgetting&lt;/strong&gt; (editing weights on new data tends to overwrite older skills), plus massive compute cost and the risk of users poisoning the model for everyone else.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbitlyst.blog%2Fassets%2Fdoes-ai-learn-dials.jpg" 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%2Fbitlyst.blog%2Fassets%2Fdoes-ai-learn-dials.jpg" alt="AI-generated illustration. After training, the model's weights are locked; chat memory is just a temporary scratchpad." width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So when an AI seems to "learn" your coding style mid-conversation, it isn't changing its brain. It is simply re-reading your earlier messages inside its &lt;strong&gt;context window&lt;/strong&gt; — a temporary scratchpad that disappears the moment you start a new chat.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learning vs understanding: where the word misleads us
&lt;/h2&gt;

&lt;p&gt;Using the same word for humans and machines hides four big differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sample efficiency:&lt;/strong&gt; A person learns what a stove or a dog is from one or two examples. A neural network needs massive datasets to carve that boundary into its weights.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continual vs frozen:&lt;/strong&gt; Humans learn continuously from every experience without erasing older skills. AI splits &lt;em&gt;training&lt;/em&gt; (learning) from &lt;em&gt;inference&lt;/em&gt; (using).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grounded reality vs statistical geometry:&lt;/strong&gt; Humans learn through physical cause-and-effect and lived feedback. LLMs learn relationships between symbols — they can explain gravity clearly without ever having dropped a cup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learning vs understanding:&lt;/strong&gt; In AI, "learning" means finding weights that minimize error. Human &lt;strong&gt;understanding&lt;/strong&gt; means knowing &lt;em&gt;what&lt;/em&gt; words point to in the real world, not just which words tend to appear together.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"In AI, 'learning' is a mathematical optimization process, not human understanding. During training, a neural network adjusts billions of numerical weights through forward passes, loss calculation, and backpropagation to reduce prediction error. Once deployed, those weights are frozen — anything an LLM seems to 'learn' during a chat is just temporary in-context adaptation inside its prompt window, not permanent memory."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dimension&lt;/th&gt;
&lt;th&gt;Human learning&lt;/th&gt;
&lt;th&gt;AI "learning"&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Building blocks&lt;/td&gt;
&lt;td&gt;Living electrochemical neurons&lt;/td&gt;
&lt;td&gt;Math nodes &amp;amp; weights in GPU matrices&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How it improves&lt;/td&gt;
&lt;td&gt;Cause-and-effect, feedback, reflection&lt;/td&gt;
&lt;td&gt;Loss function + backpropagation + gradient descent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After deployment&lt;/td&gt;
&lt;td&gt;Keeps learning continuously&lt;/td&gt;
&lt;td&gt;Weights are frozen (read-only)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sample efficiency&lt;/td&gt;
&lt;td&gt;Often one experience ("one-shot")&lt;/td&gt;
&lt;td&gt;Massive datasets and GPU compute&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Remembering" in chat&lt;/td&gt;
&lt;td&gt;Long-term memory&lt;/td&gt;
&lt;td&gt;Temporary scratchpad (context window)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Three things people confuse with "AI learning"
&lt;/h3&gt;

&lt;p&gt;When someone says "the AI learned X", they usually mean one of three very different mechanisms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Weight updates (Training / Fine-tuning):&lt;/strong&gt; The model's parameters genuinely change. Expensive, offline, permanent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-context learning (Few-shot prompting):&lt;/strong&gt; You put examples in the prompt and the model follows the pattern. No weights change — the attention mechanism simply uses your examples as live input signals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External memory (RAG / memory files):&lt;/strong&gt; The app saves a note to a database or file and silently pastes it into your next prompt. The &lt;em&gt;app&lt;/em&gt; remembered; the &lt;em&gt;model&lt;/em&gt; did not.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why did the word "learning" stick?
&lt;/h3&gt;

&lt;p&gt;Arthur Samuel popularized &lt;strong&gt;"machine learning"&lt;/strong&gt; in 1959 while building a checkers program. His definition was strictly behavioral: giving computers the ability to improve at a task from data &lt;em&gt;without being explicitly programmed with every rule&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;By that engineering definition, "learning" is accurate — performance measurably improves with data. It only misleads us when we forget it's a metaphor and assume human comprehension, intention, or lifelong memory.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/does-ai-actually-learn" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>engineering</category>
      <category>concepts</category>
    </item>
    <item>
      <title>Why "Works on My Machine" Happens (And How to Fix It)</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Thu, 03 Dec 2026 10:49:45 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/why-works-on-my-machine-happens-and-how-to-fix-it-3a7m</link>
      <guid>https://dev.to/mohsenfallahnjd/why-works-on-my-machine-happens-and-how-to-fix-it-3a7m</guid>
      <description>&lt;p&gt;Picture this: your PR passes every test locally, you push it, and two minutes later CI turns red — or worse, production crashes right after deploy.&lt;/p&gt;

&lt;p&gt;"Works on my machine" is rarely a mystery. It almost always means your laptop has &lt;strong&gt;hidden state&lt;/strong&gt; that the server does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model
&lt;/h2&gt;

&lt;p&gt;Your laptop is a &lt;strong&gt;lived-in kitchen&lt;/strong&gt;: half the ingredients are already on the counter (cached builds, global tools, &lt;code&gt;.env.local&lt;/code&gt;, macOS defaults).&lt;/p&gt;

&lt;p&gt;CI and production start from an &lt;strong&gt;empty kitchen&lt;/strong&gt;. They only get what you explicitly wrote on the recipe — your git repo, your lockfile, and your config. Anything missing from the recipe breaks the dish.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faan20fcu94iq6hs2ual6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faan20fcu94iq6hs2ual6.jpg" alt="AI-generated illustration. Local machines accumulate hidden state; CI and production only get what is committed." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The 6 usual suspects (and how to fix each)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Different runtime or package versions
&lt;/h3&gt;

&lt;p&gt;Your laptop runs Node 22 with an older &lt;code&gt;node_modules&lt;/code&gt; folder; CI runs Node 20 and installs a newer minor version allowed by &lt;code&gt;^&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Commit your lockfile, install with &lt;code&gt;npm ci&lt;/code&gt; (or &lt;code&gt;bun install --frozen-lockfile&lt;/code&gt;) in CI, and pin your runtime version with &lt;code&gt;.nvmrc&lt;/code&gt; or &lt;code&gt;package.json#engines&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Missing environment variables
&lt;/h3&gt;

&lt;p&gt;Your app reads &lt;code&gt;process.env.STRIPE_KEY&lt;/code&gt; from &lt;code&gt;.env.local&lt;/code&gt;, which is gitignored. Production doesn't have it, so it crashes at runtime — or silently uses &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Keep a committed &lt;code&gt;.env.example&lt;/code&gt; with every required key, and validate env vars at startup (for example with Zod) so missing config fails loudly on boot.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Case-sensitive file paths (macOS vs Linux)
&lt;/h3&gt;

&lt;p&gt;On macOS, &lt;code&gt;import Button from "./button"&lt;/code&gt; happily loads &lt;code&gt;./Button.tsx&lt;/code&gt; because the filesystem is case-insensitive. Push to Linux CI or Vercel, and the build fails with &lt;code&gt;Module not found&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Match exact file casing and enable &lt;code&gt;"forceConsistentCasingInFileNames": true&lt;/code&gt; in &lt;code&gt;tsconfig.json&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Timezones and locales
&lt;/h3&gt;

&lt;p&gt;Your laptop runs in your local timezone; servers almost always run in &lt;strong&gt;UTC&lt;/strong&gt;. A date string or formatted number that looks right locally can shift by a day on the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Store and compute all dates in UTC, and convert to the user's timezone only when rendering in the UI.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Fast localhost hides timing bugs
&lt;/h3&gt;

&lt;p&gt;Local APIs respond in 2ms, so you never notice a missing loading state, a tight timeout, or a &lt;a href="https://bitlyst.blog/p/race-condition-explained" rel="noopener noreferrer"&gt;race condition&lt;/a&gt; where two requests return out of order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Throttle your network in Chrome DevTools (Slow 3G) and test clicking or typing quickly before you ship.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Stale caches or global tools
&lt;/h3&gt;

&lt;p&gt;A leftover &lt;code&gt;.next&lt;/code&gt; folder, an old database seed, or a CLI you installed globally months ago (&lt;code&gt;npm i -g ...&lt;/code&gt;) makes the build pass on your machine and fail on a fresh checkout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Run a clean build (&lt;code&gt;rm -rf .next node_modules &amp;amp;&amp;amp; npm ci&lt;/code&gt;), put every CLI in &lt;code&gt;devDependencies&lt;/code&gt;, or run the app in &lt;a href="https://bitlyst.blog/p/docker-in-2-minutes" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; so everyone shares the exact same container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"'Works on my machine' happens when code depends on hidden local state — like unpinned versions, missing env vars, case-insensitive macOS paths, local timezones, or stale caches. The fix is making the environment explicit and reproducible: commit lockfiles, pin runtime versions, validate env vars at startup, and run CI or Docker from a clean slate."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Quick fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Different Node / package versions&lt;/td&gt;
&lt;td&gt;Commit lockfile, use &lt;code&gt;npm ci&lt;/code&gt;, pin &lt;code&gt;.nvmrc&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Missing &lt;code&gt;.env&lt;/code&gt; secrets&lt;/td&gt;
&lt;td&gt;Commit &lt;code&gt;.env.example&lt;/code&gt;, validate env on startup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;macOS vs Linux file casing&lt;/td&gt;
&lt;td&gt;Enable &lt;code&gt;forceConsistentCasingInFileNames&lt;/code&gt; in &lt;code&gt;tsconfig.json&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timezone / date bugs&lt;/td&gt;
&lt;td&gt;Compute in UTC, format in the UI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Localhost too fast&lt;/td&gt;
&lt;td&gt;Throttle network in DevTools to catch races &amp;amp; timeouts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stale cache / global CLI&lt;/td&gt;
&lt;td&gt;Clean build, move CLIs to &lt;code&gt;devDependencies&lt;/code&gt;, or use Docker&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/works-on-my-machine" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>engineering</category>
      <category>debugging</category>
      <category>docker</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Race Condition Explained: How to Find and Fix It</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Thu, 03 Dec 2026 09:56:07 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/race-condition-explained-how-to-find-and-fix-it-2i0j</link>
      <guid>https://dev.to/mohsenfallahnjd/race-condition-explained-how-to-find-and-fix-it-2i0j</guid>
      <description>&lt;p&gt;Picture this: you type &lt;code&gt;"react"&lt;/code&gt; into a search box. The results flash &lt;code&gt;"react"&lt;/code&gt; for a split second, then silently flip back to &lt;code&gt;"re"&lt;/code&gt;. No error in the console. No crash.&lt;/p&gt;

&lt;p&gt;You just hit a &lt;strong&gt;race condition&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model
&lt;/h2&gt;

&lt;p&gt;A race condition is &lt;strong&gt;two runners writing their name on the same whiteboard&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Your code assumes the runner who started first will finish first. Networks, databases, and threads make no such promise. If the first runner trips and arrives late, they erase the second runner's name — and the whiteboard ends up showing old data.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it happens
&lt;/h2&gt;

&lt;p&gt;Every race condition needs two ingredients: &lt;strong&gt;shared state&lt;/strong&gt; (a React state variable, a database row, a file) and &lt;strong&gt;overlapping work&lt;/strong&gt; (two network calls, two requests, two threads) where the final result depends on &lt;strong&gt;timing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is the classic frontend version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`/api/search?q=${query}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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;// ← whichever finishes last wins&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="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type &lt;code&gt;"re"&lt;/code&gt;, then quickly finish typing &lt;code&gt;"react"&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Request 1 (&lt;code&gt;"re"&lt;/code&gt;) leaves first, but the server takes &lt;strong&gt;600ms&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Request 2 (&lt;code&gt;"react"&lt;/code&gt;) leaves 50ms later, hits a warm cache, and returns in &lt;strong&gt;100ms&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setResults&lt;/code&gt; shows &lt;code&gt;"react"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;450ms later, Request 1 finally arrives and calls &lt;code&gt;setResults&lt;/code&gt; with &lt;code&gt;"re"&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvoc4w9p8w6ehnmyjrsw9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvoc4w9p8w6ehnmyjrsw9.jpg" alt="AI-generated illustration. Request 1 starts earlier, finishes later, and overwrites the newer result on screen." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The exact same bug happens on the backend. Two users click "Buy" on the last item in stock at the same millisecond. Both read &lt;code&gt;stock = 1&lt;/code&gt;, both see "great, there's one left!", and both write &lt;code&gt;stock = 0&lt;/code&gt; — you just sold the same item twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find a race condition
&lt;/h2&gt;

&lt;p&gt;Race conditions are famous for "works on my machine" because localhost is fast and predictable. Three reliable ways to smoke them out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow down the network on purpose.&lt;/strong&gt; In Chrome DevTools, throttle to Slow 3G (or add a random &lt;code&gt;setTimeout&lt;/code&gt; delay in a mock API) and click, type, or switch tabs fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audit every &lt;code&gt;async&lt;/code&gt; write to shared state.&lt;/strong&gt; In code review, scan for &lt;code&gt;useEffect&lt;/code&gt; fetches without cleanup, and backend code that does &lt;strong&gt;read → check → write&lt;/strong&gt; in separate queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Let tools stress-test timing for you.&lt;/strong&gt; Turn on &lt;strong&gt;React Strict Mode&lt;/strong&gt; in dev (it double-runs effects to expose missing cleanups), use &lt;code&gt;go test -race&lt;/code&gt; in Go, or fire 20 concurrent requests at the same endpoint in a test.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to fix it
&lt;/h2&gt;

&lt;p&gt;The fix depends on where the whiteboard lives.&lt;/p&gt;

&lt;h3&gt;
  
  
  On the frontend: cancel or ignore stale work
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;AbortController&lt;/code&gt; in your &lt;code&gt;useEffect&lt;/code&gt; cleanup so an older request is cancelled the moment a newer one starts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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="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="n"&gt;controller&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;AbortController&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="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`/api/search?q=${query}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"AbortError"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abort&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;// ← old runner leaves the track&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="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you can't abort the call, use a boolean &lt;code&gt;let ignore = false&lt;/code&gt; flag in the effect and flip it to &lt;code&gt;true&lt;/code&gt; in the cleanup before calling &lt;code&gt;setResults&lt;/code&gt;. Better yet, data-fetching libraries like TanStack Query handle request cancellation and stale responses for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  On the backend: make the update atomic
&lt;/h3&gt;

&lt;p&gt;Never check a value in one query and update it in another. Push the check into a single &lt;strong&gt;atomic&lt;/strong&gt; database operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Before: two queries (race window in between)&lt;/span&gt;
&lt;span class="c1"&gt;-- SELECT stock FROM items WHERE id = 1;&lt;/span&gt;
&lt;span class="c1"&gt;-- UPDATE items SET stock = stock - 1 WHERE id = 1;&lt;/span&gt;

&lt;span class="c1"&gt;-- After: one atomic write&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If zero rows are updated, the item is out of stock — no second runner can slip in between. For multi-step workflows (like charging a card), combine a &lt;strong&gt;database transaction&lt;/strong&gt; with an &lt;strong&gt;idempotency key&lt;/strong&gt; so duplicate requests don't double-charge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"A race condition happens when two operations overlap on the same shared state and the outcome depends on which one finishes last. On the frontend, you fix it by cancelling or ignoring stale requests — like with &lt;code&gt;AbortController&lt;/code&gt; in a &lt;code&gt;useEffect&lt;/code&gt; cleanup. On the backend, you eliminate the gap between reading and writing using atomic updates, database locks, or idempotency keys."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Where it happens&lt;/th&gt;
&lt;th&gt;How to spot it&lt;/th&gt;
&lt;th&gt;How to fix it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;React &lt;code&gt;useEffect&lt;/code&gt; fetch&lt;/td&gt;
&lt;td&gt;Throttle network, type/click fast, enable Strict Mode&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;AbortController&lt;/code&gt; or &lt;code&gt;ignore&lt;/code&gt; flag in cleanup (or TanStack Query)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database read-then-write&lt;/td&gt;
&lt;td&gt;Send concurrent requests to the same endpoint&lt;/td&gt;
&lt;td&gt;Single atomic &lt;code&gt;UPDATE ... WHERE&lt;/code&gt;, row locks, or transactions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Double-click / retries&lt;/td&gt;
&lt;td&gt;Click Submit twice on a slow connection&lt;/td&gt;
&lt;td&gt;Disable button + server-side idempotency key&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait — isn't JavaScript single-threaded?
&lt;/h3&gt;

&lt;p&gt;Yes, JavaScript runs one line at a time on the main thread, so two functions never mutate the same variable at the exact same nanosecond (no low-level &lt;em&gt;data race&lt;/em&gt; on memory).&lt;/p&gt;

&lt;p&gt;What JavaScript &lt;em&gt;does&lt;/em&gt; have is &lt;strong&gt;async interleaving&lt;/strong&gt;. Every &lt;code&gt;await&lt;/code&gt; pauses your function and hands control back to the event loop. While function A is paused at &lt;code&gt;await fetch(...)&lt;/code&gt;, function B can run, read the same state, and write to it. When function A resumes, its assumptions are already stale.&lt;/p&gt;

&lt;h3&gt;
  
  
  TOCTOU: the classic backend pattern
&lt;/h3&gt;

&lt;p&gt;The "read stock, check &lt;code&gt;&amp;gt; 0&lt;/code&gt;, then update" bug has a name: &lt;strong&gt;TOCTOU&lt;/strong&gt; (&lt;em&gt;Time-of-Check to Time-of-Use&lt;/em&gt;). Between checking a condition and acting on it, another request changes the world underneath you.&lt;/p&gt;

&lt;p&gt;When a single atomic &lt;code&gt;UPDATE&lt;/code&gt; isn't enough (for example, checking a balance across two tables), wrap the read and write in a transaction using row-level locking (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;) or optimistic concurrency control (a &lt;code&gt;version&lt;/code&gt; column: &lt;code&gt;WHERE id = $1 AND version = $2&lt;/code&gt;).&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/race-condition-explained" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>engineering</category>
      <category>debugging</category>
    </item>
    <item>
      <title>What Is MCP? The USB-C Port for AI</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Wed, 02 Dec 2026 11:21:03 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/what-is-mcp-the-usb-c-port-for-ai-5597</link>
      <guid>https://dev.to/mohsenfallahnjd/what-is-mcp-the-usb-c-port-for-ai-5597</guid>
      <description>&lt;p&gt;Picture this: you ask your AI editor to read a GitHub issue, check a table in your Postgres database, and grab a layout from Figma.&lt;/p&gt;

&lt;p&gt;Two years ago, every AI app had to write its own custom glue code for every single tool. Now you paste one URL or command into a config file, and the same connector works in Cursor, Claude, and VS Code.&lt;/p&gt;

&lt;p&gt;What's doing the connecting? &lt;strong&gt;MCP&lt;/strong&gt;, short for &lt;strong&gt;Model Context Protocol&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole idea in one picture
&lt;/h2&gt;

&lt;p&gt;Remember the drawer of cables we all used to have? One charger for your phone, a different one for your camera, another for your headphones. If you had 3 laptops and 5 devices, you needed 15 cables.&lt;/p&gt;

&lt;p&gt;Then &lt;strong&gt;USB-C&lt;/strong&gt; showed up. Every laptop has the same port, every device uses the same plug, and everything fits together.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;MCP is USB-C for AI apps. Build a connector for a tool once, and any AI app that speaks MCP can plug into it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's really all MCP is. It's not a new AI model, and it's not a framework. It's a &lt;strong&gt;shared rulebook&lt;/strong&gt; for how an AI app talks to outside tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem it solves
&lt;/h2&gt;

&lt;p&gt;An AI model by itself is stuck in a box: it can only read the text you paste into the chat. To do real work, it needs to reach outside—into your files, your database, your issue tracker, or your Slack.&lt;/p&gt;

&lt;p&gt;Without a standard, every AI app has to build its own GitHub plugin, its own Slack plugin, and its own Postgres plugin. And every tool maker has to build a different plugin for every AI app.&lt;/p&gt;

&lt;p&gt;With MCP, the tool maker builds &lt;strong&gt;one&lt;/strong&gt; MCP server, and every AI app builds &lt;strong&gt;one&lt;/strong&gt; MCP client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnaxsl9moknkjauga7k74.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnaxsl9moknkjauga7k74.jpg" alt="AI-generated illustration. Without a standard plug, every app needs a custom cable for every tool." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;There are three moving parts, and they map straight onto the USB-C picture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The AI app (the laptop):&lt;/strong&gt; Cursor, Claude Desktop, VS Code, or your own app. In MCP terms this is the &lt;strong&gt;host&lt;/strong&gt;, and it runs an &lt;strong&gt;MCP client&lt;/strong&gt; inside it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The MCP server (the device):&lt;/strong&gt; a small program that wraps a tool—like GitHub, Postgres, or Figma—and speaks MCP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The protocol (the cable):&lt;/strong&gt; plain JSON messages going back and forth.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you plug in an MCP server, the conversation is surprisingly simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Discovery:&lt;/strong&gt; the AI app asks, &lt;em&gt;"What can you do?"&lt;/em&gt; The server replies with a list of actions and the inputs each one expects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tool call:&lt;/strong&gt; when you ask a question, the model picks a tool and sends a JSON request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result:&lt;/strong&gt; the server runs the real work (queries Postgres, calls GitHub's API) and hands the result back to the model.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt; &lt;span class="n"&gt;asks&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;MCP&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"tool"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"get_issue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"arguments"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"repo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"bitlyst"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"number"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt; &lt;span class="n"&gt;Server&lt;/span&gt; &lt;span class="n"&gt;calls&lt;/span&gt; &lt;span class="n"&gt;GitHub&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;sends&lt;/span&gt; &lt;span class="n"&gt;back&lt;/span&gt; &lt;span class="n"&gt;plain&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Fix dark mode flash"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"state"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"open"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An MCP server can expose three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tools:&lt;/strong&gt; actions the model can run, like &lt;code&gt;create_issue&lt;/code&gt; or &lt;code&gt;run_query&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resources:&lt;/strong&gt; read-only data the model can look at, like a file or a database schema.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompts:&lt;/strong&gt; reusable prompt templates the server offers up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In day-to-day use, &lt;strong&gt;tools&lt;/strong&gt; are 90% of what you'll see.&lt;/p&gt;

&lt;h2&gt;
  
  
  In practice
&lt;/h2&gt;

&lt;p&gt;Connecting a server is usually a few lines in your editor's config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"github"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://api.githubcopilot.com/mcp/"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once it's plugged in, the AI can discover and use it without you writing any glue code.&lt;/p&gt;

&lt;p&gt;Two honest things to keep in mind before you plug in twenty servers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every tool costs context.&lt;/strong&gt; The AI has to read the list of available tools so it knows what it can call. Too many servers means a crowded prompt and slower, noisier answers. Only keep the ones you actually use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MCP is a plug, not a lock.&lt;/strong&gt; It doesn't make a tool safe by itself. Treat an MCP server like any other code with access to your accounts: only connect servers you trust, and review write actions before approving them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"MCP, or Model Context Protocol, is a standard way for AI apps to connect to external tools and data—like USB-C for AI. Instead of every AI app writing custom integrations for every service, a service exposes one MCP server with its tools, resources, and prompts, and any MCP-compatible client can discover and call them over JSON messages. The trade-off is that every connected tool adds to the model's context, and you still have to manage permissions carefully."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Short answer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What is MCP?&lt;/td&gt;
&lt;td&gt;A standard protocol between AI apps and external tools&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The mental model&lt;/td&gt;
&lt;td&gt;USB-C for AI: one plug instead of custom cables per app&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who runs what?&lt;/td&gt;
&lt;td&gt;AI app = client; tool wrapper = MCP server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What can a server expose?&lt;/td&gt;
&lt;td&gt;Tools (actions), Resources (read-only data), Prompts (templates)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What's the catch?&lt;/td&gt;
&lt;td&gt;Too many tools crowd the context window; trust only servers you vet&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Local vs. remote MCP servers
&lt;/h3&gt;

&lt;p&gt;MCP speaks JSON-RPC 2.0, and it can travel over two main transports:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;stdio&lt;/code&gt; (local):&lt;/strong&gt; your AI app starts the MCP server as a child process on your machine and talks to it over standard input and output. Great for local files, local databases, or CLI tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streamable HTTP (remote):&lt;/strong&gt; the MCP server runs on the web behind a normal URL, with OAuth or a bearer token for auth. Great for team services like GitHub, Linear, or Sentry—no local install needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why not just give the AI a REST API?
&lt;/h3&gt;

&lt;p&gt;You can, and people did. The catch is that every REST API has its own auth flow, its own docs format, and its own way of describing endpoints. You end up writing a custom wrapper for each API in every AI app.&lt;/p&gt;

&lt;p&gt;MCP standardizes &lt;strong&gt;discovery&lt;/strong&gt;: every server answers &lt;code&gt;tools/list&lt;/code&gt; in the exact same JSON Schema format, so the AI app can plug in a brand-new tool at runtime without a single line of app-specific code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://modelcontextprotocol.io" rel="noopener noreferrer"&gt;Model Context Protocol official docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/mcp-explained" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>systemdesign</category>
      <category>api</category>
    </item>
    <item>
      <title>Is AI Really Faster and Cheaper for Building Products?</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Wed, 02 Dec 2026 11:20:33 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/is-ai-really-faster-and-cheaper-for-building-products-1d18</link>
      <guid>https://dev.to/mohsenfallahnjd/is-ai-really-faster-and-cheaper-for-building-products-1d18</guid>
      <description>&lt;p&gt;Every week there's a post about someone building a whole SaaS app in a weekend with AI. It's easy to look at that and ask: &lt;strong&gt;is AI really a faster and cheaper way to build a product?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The honest answer is &lt;strong&gt;yes—and no.&lt;/strong&gt; It depends on whether you're talking about &lt;em&gt;getting to a demo&lt;/em&gt; or &lt;em&gt;running a real product six months later&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where AI really is faster and cheaper
&lt;/h2&gt;

&lt;p&gt;Let's give credit where it's due. For the first stretch of a product, AI is a massive speed-up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prototypes and demos.&lt;/strong&gt; Turning a rough idea into a clickable screen you can show users used to take days. Now it takes an afternoon.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boilerplate and glue code.&lt;/strong&gt; Forms, CRUD endpoints, config files, migrations, tests for obvious cases—AI eats through the boring parts so you don't have to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unfamiliar ground.&lt;/strong&gt; Need a quick script in a language you barely use, or a first pass at a new API? AI gets you past the blank-page problem in minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your goal is to test whether anyone even wants your idea, AI is genuinely cheaper and faster than anything we had before.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hidden bill: the last 20%
&lt;/h2&gt;

&lt;p&gt;So where does the "cheaper" story start to crack? Right after the demo works.&lt;/p&gt;

&lt;p&gt;Picture an &lt;strong&gt;iceberg&lt;/strong&gt;. The shiny tip above the water is the UI and the happy path—the part AI generates in twenty minutes. Everything below the water is what makes a real product survive: permissions, edge cases, data integrity, error handling, performance, and changing a feature without breaking three others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffu1k5ueqbqnr5aaiznd8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffu1k5ueqbqnr5aaiznd8.jpg" alt="AI-generated illustration. AI builds the tip above the water fast; the real cost lives underneath." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When teams let AI write huge chunks of code without someone steering the design, three costs show up fast:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Review fatigue.&lt;/strong&gt; Generating 500 lines takes ten seconds. Reading 500 lines carefully enough to spot a subtle auth bug or race condition still takes a human brain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code nobody understands.&lt;/strong&gt; When a bug hits production at 2 a.m., "the AI wrote that file" doesn't fix it. You still pay the time to understand it—you just pay it later, with interest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drift and duplication.&lt;/strong&gt; AI is great at solving the prompt in front of it, not at remembering the quiet rules of your whole codebase. Left unchecked, you end up with three slightly different ways to do the same thing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's why &lt;a href="https://bitlyst.blog/p/ai-is-your-junior-developer" rel="noopener noreferrer"&gt;AI works best as a junior developer&lt;/a&gt;: fast hands, endless energy, still needs a senior to own the architecture and say "no, don't do it that way."&lt;/p&gt;

&lt;h2&gt;
  
  
  Typing was never the real bottleneck
&lt;/h2&gt;

&lt;p&gt;This is the part most hype posts miss. Building a product was never mostly about how fast fingers hit a keyboard.&lt;/p&gt;

&lt;p&gt;The hard parts have always been:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Figuring out what to build&lt;/strong&gt; (and what &lt;em&gt;not&lt;/em&gt; to build).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keeping the system simple&lt;/strong&gt; as requirements change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Knowing it's right&lt;/strong&gt; before real users and real money touch it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI makes &lt;em&gt;typing&lt;/em&gt; nearly free. It does not make &lt;em&gt;thinking&lt;/em&gt;, &lt;em&gt;verifying&lt;/em&gt;, or &lt;em&gt;owning the outcome&lt;/em&gt; free.&lt;/p&gt;

&lt;h2&gt;
  
  
  So when is AI actually cheaper?
&lt;/h2&gt;

&lt;p&gt;You get the real speed-up—without the hangover—when you pair AI with clear guardrails:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use it aggressively for discovery.&lt;/strong&gt; Prototype fast, throw away what doesn't stick, and don't fall in love with throwaway code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep changes small.&lt;/strong&gt; Asking AI for one well-scoped function or component with a test is fast to review and safe to ship. Asking it to "build the whole billing system" is how you buy a month of debugging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep a human owning the design.&lt;/strong&gt; Someone on the team still needs to understand how the pieces fit together and hold the line on simplicity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AI really can make building a product faster and cheaper. It just doesn't replace engineering—it rewards the teams who already know how to do it well.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/is-ai-really-faster-and-cheaper" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>engineering</category>
      <category>product</category>
      <category>career</category>
    </item>
    <item>
      <title>How to Build an MCP Server in 30 Lines of TypeScript</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Wed, 02 Dec 2026 11:20:33 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/how-to-build-an-mcp-server-in-30-lines-of-typescript-5f5b</link>
      <guid>https://dev.to/mohsenfallahnjd/how-to-build-an-mcp-server-in-30-lines-of-typescript-5f5b</guid>
      <description>&lt;p&gt;If you read &lt;a href="https://bitlyst.blog/p/mcp-explained" rel="noopener noreferrer"&gt;what MCP is&lt;/a&gt;, you know it's the USB-C plug between AI apps and your tools. So how hard is it to build your own MCP server?&lt;/p&gt;

&lt;p&gt;Honestly, it's much smaller than it sounds. You don't need a web framework or a big setup. A working MCP server fits in about &lt;strong&gt;30 lines of TypeScript&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole idea in one picture
&lt;/h2&gt;

&lt;p&gt;Think of an MCP server like a &lt;strong&gt;tiny coffee shop counter&lt;/strong&gt;. You only need three things to open the window:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A shop name&lt;/strong&gt; (create the server).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A menu&lt;/strong&gt; (register your tools: what each one is called, what inputs it takes, and what it does).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A counter window&lt;/strong&gt; (connect a transport so the AI app can pass orders in and get answers back).&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Building an MCP server is just three steps: create the server, register your tools with their input shapes, and connect a transport.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once those three pieces are in place, Cursor, Claude Desktop, or VS Code can plug right in and call your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Install the SDK
&lt;/h2&gt;

&lt;p&gt;Start a folder and install the official TypeScript SDK plus &lt;code&gt;zod&lt;/code&gt; (for describing tool inputs):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init -y
npm install @modelcontextprotocol/sdk zod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Write the server
&lt;/h2&gt;

&lt;p&gt;Here is a complete local MCP server with one tool, &lt;code&gt;word_count&lt;/code&gt;, that counts words in any text the AI sends it:&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="nc"&gt;McpServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@modelcontextprotocol/sdk/server/mcp.js"&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="nc"&gt;StdioServerTransport&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@modelcontextprotocol/sdk/server/stdio.js"&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="n"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"zod"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// 1. Open the shop&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;server&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;McpServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"my-tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1.0.0"&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;// 2. Put a tool on the menu&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;registerTool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="s2"&gt;"word_count"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Count the words in a piece of text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;inputSchema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;text&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&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="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;s&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="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;content&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="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${count} words`&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// 3. Open the counter window (local stdio)&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StdioServerTransport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look how plain that is. The SDK handles all the JSON-RPC wiring and discovery for you. When the AI app connects, it asks for the menu, sees &lt;code&gt;word_count&lt;/code&gt; and its &lt;code&gt;text&lt;/code&gt; input, and knows when to call it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvrit5wzgu42z8252sfks.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvrit5wzgu42z8252sfks.jpg" alt="AI-generated illustration. Define the tool, describe its inputs, and connect the transport—the SDK handles the rest." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Plug it into your AI editor
&lt;/h2&gt;

&lt;p&gt;Point Cursor, Claude Desktop, or VS Code at your script in its MCP config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"my-tools"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"npx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tsx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/absolute/path/to/server.ts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart or reload your MCP servers, then ask your AI: &lt;em&gt;"How many words are in this paragraph?"&lt;/em&gt; It will pick your &lt;code&gt;word_count&lt;/code&gt; tool and run it.&lt;/p&gt;

&lt;h2&gt;
  
  
  In practice: the one bug everyone hits first
&lt;/h2&gt;

&lt;p&gt;If you build a local (&lt;code&gt;stdio&lt;/code&gt;) MCP server, remember one golden rule: &lt;strong&gt;never call &lt;code&gt;console.log()&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why? Because &lt;code&gt;stdio&lt;/code&gt; uses standard output (&lt;code&gt;stdout&lt;/code&gt;) as the wire between your server and the AI app. One stray &lt;code&gt;console.log("debug")&lt;/code&gt; breaks the JSON stream and crashes the connection. Log to &lt;code&gt;console.error()&lt;/code&gt; instead—that goes to &lt;code&gt;stderr&lt;/code&gt;, which stays out of the protocol's way.&lt;/p&gt;

&lt;p&gt;Also, write clear &lt;code&gt;description&lt;/code&gt; fields. The AI model reads those descriptions to decide &lt;em&gt;when&lt;/em&gt; to call your tool. A vague description means the model either ignores your tool or calls it at the wrong time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"Building an MCP server takes three steps with the official SDK: instantiate an &lt;code&gt;McpServer&lt;/code&gt;, register your tools with a description, a schema for the inputs, and a handler function, then connect a transport—usually &lt;code&gt;StdioServerTransport&lt;/code&gt; for local tools or Streamable HTTP for remote ones. The main gotcha with local servers is never writing logs to &lt;code&gt;stdout&lt;/code&gt;, because &lt;code&gt;stdout&lt;/code&gt; carries the JSON-RPC messages."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;What you write&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Create the server&lt;/td&gt;
&lt;td&gt;&lt;code&gt;new McpServer({ name, version })&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add a tool&lt;/td&gt;
&lt;td&gt;&lt;code&gt;server.registerTool(name, { description, inputSchema }, handler)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connect locally&lt;/td&gt;
&lt;td&gt;&lt;code&gt;await server.connect(new StdioServerTransport())&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Log safely&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;console.error()&lt;/code&gt;, never &lt;code&gt;console.log()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wire to editor&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;command&lt;/code&gt; + &lt;code&gt;args&lt;/code&gt; under &lt;code&gt;mcpServers&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing without restarting your editor
&lt;/h3&gt;

&lt;p&gt;Anthropic maintains an official &lt;strong&gt;MCP Inspector&lt;/strong&gt; CLI that gives you a local web UI to list your tools and call them by hand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx @modelcontextprotocol/inspector npx tsx server.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's the fastest way to check your schemas and responses before plugging the server into Cursor or Claude.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to switch from &lt;code&gt;stdio&lt;/code&gt; to HTTP
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;stdio&lt;/code&gt; is great when the tool runs on your own laptop (reading local files, querying a local database, wrapping a CLI). When you want a whole team to use the same server without installing anything locally—like an internal docs search or a company API—host it behind &lt;strong&gt;Streamable HTTP&lt;/strong&gt; with OAuth or a bearer token instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://modelcontextprotocol.io/docs/develop/build-server" rel="noopener noreferrer"&gt;Build an MCP Server (official guide)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/modelcontextprotocol/typescript-sdk" rel="noopener noreferrer"&gt;MCP TypeScript SDK on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/how-to-build-an-mcp-server" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>typescript</category>
      <category>node</category>
    </item>
    <item>
      <title>AsyncLocalStorage: A Name Badge for Every Request</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Tue, 01 Dec 2026 08:10:40 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/asynclocalstorage-a-name-badge-for-every-request-lm4</link>
      <guid>https://dev.to/mohsenfallahnjd/asynclocalstorage-a-name-badge-for-every-request-lm4</guid>
      <description>&lt;p&gt;Have you ever wondered how &lt;code&gt;cookies()&lt;/code&gt; in Next.js knows &lt;em&gt;which&lt;/em&gt; user's cookies to give you? You never pass it the request. You call it from deep inside some component, and somehow it knows.&lt;/p&gt;

&lt;p&gt;Your server is handling hundreds of requests at the same time. So how does it keep them apart? The answer is &lt;strong&gt;AsyncLocalStorage&lt;/strong&gt;, a small Node.js tool with a big job.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole idea in one picture
&lt;/h2&gt;

&lt;p&gt;Think of a big conference. At the door, every guest gets a &lt;strong&gt;name badge&lt;/strong&gt;. Wherever they go after that, any room, any talk, any coffee line, anyone can glance at the badge and know who they are. Nobody has to walk next to them shouting their name.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AsyncLocalStorage pins a name badge on a request at the start. Any code that runs for that request, even after an &lt;code&gt;await&lt;/code&gt; or a timer, can read the badge.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the whole idea. The rest is how Node keeps the badges from getting mixed up.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem it solves
&lt;/h2&gt;

&lt;p&gt;Say you want every log line to include the request ID. Without a badge, you have to pass it by hand into every function:&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&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="n"&gt;requestId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;loadUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// pass it&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;loadOrders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// pass it again&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ...and loadUser passes it on, and so on, forever&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's like carrying someone's name on a sign behind them all day. It works, but every function has to take the sign and hand it on, even if it doesn't care.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxuzz6s9rjuouuoxacmz1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxuzz6s9rjuouuoxacmz1.jpg" alt="AI-generated illustration. Passing it by hand works, until the tenth function that has to carry it." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;You create one storage, put the badge on at the start of a request with &lt;code&gt;run()&lt;/code&gt;, and read it anywhere with &lt;code&gt;getStore()&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&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;from&lt;/span&gt; &lt;span class="s2"&gt;"node:async_hooks"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;als&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="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="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&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="sb"&gt;`[${als.getStore()?.requestId}] ${msg}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadUser&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;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"loaded user"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no requestId passed in!&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;const&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"a1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b2"&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="n"&gt;als&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;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="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;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;loadUser&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"done"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[a1] start
[b2] start
[a1] loaded user
[a1] done
[b2] loaded user
[b2] done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both requests run at the same time, but &lt;strong&gt;each one keeps its own badge&lt;/strong&gt;, even across the &lt;code&gt;await&lt;/code&gt; and the timer. Outside &lt;code&gt;run()&lt;/code&gt;, &lt;code&gt;getStore()&lt;/code&gt; gives you &lt;code&gt;undefined&lt;/code&gt;. There's no badge if you never came through the door.&lt;/p&gt;

&lt;p&gt;How does Node pull this off? Every time your code schedules something for later (a promise, a &lt;code&gt;setTimeout&lt;/code&gt;, a network call), Node &lt;strong&gt;copies the current badge onto it&lt;/strong&gt;. When that work runs later, the badge is put back on. That's why it survives every hop.&lt;/p&gt;

&lt;h2&gt;
  
  
  In practice
&lt;/h2&gt;

&lt;p&gt;You probably use it already without knowing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next.js&lt;/strong&gt; runs each request inside AsyncLocalStorage. That's how &lt;code&gt;cookies()&lt;/code&gt;, &lt;code&gt;headers()&lt;/code&gt; and &lt;code&gt;after()&lt;/code&gt; know which request they belong to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging and tracing&lt;/strong&gt; tools (like OpenTelemetry) use it to stamp every log and span with the right request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your own code&lt;/strong&gt; can use it for request IDs, the current user, or a per-request database transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One honest catch: since Node copies the badge onto every timer and promise, &lt;strong&gt;a long timer keeps the whole request in memory&lt;/strong&gt; until it fires. That's exactly what caused a 5 GB leak in &lt;a href="https://bitlyst.blog/p/nextjs-ssr-memory-leak-timer" rel="noopener noreferrer"&gt;the timer that kept every request&lt;/a&gt;. Keep what you put in the store small, and be careful with long timers during a request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"AsyncLocalStorage is Node's way to keep per-request data without passing it through every function. You call &lt;code&gt;run(store, fn)&lt;/code&gt; at the start of a request, and anything inside it, even after &lt;code&gt;await&lt;/code&gt; or a &lt;code&gt;setTimeout&lt;/code&gt;, can read the same data with &lt;code&gt;getStore()&lt;/code&gt;. Node does this by copying the current context onto every async task it schedules. Next.js uses it for &lt;code&gt;cookies()&lt;/code&gt; and &lt;code&gt;headers()&lt;/code&gt;. The catch is that pending timers keep that context alive, so long timers can hold requests in memory."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Short answer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What is it?&lt;/td&gt;
&lt;td&gt;Per-request data that follows async code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Put the badge on&lt;/td&gt;
&lt;td&gt;&lt;code&gt;als.run(store, fn)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read the badge&lt;/td&gt;
&lt;td&gt;&lt;code&gt;als.getStore()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outside &lt;code&gt;run()&lt;/code&gt;?&lt;/td&gt;
&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who uses it?&lt;/td&gt;
&lt;td&gt;Next.js &lt;code&gt;cookies()&lt;/code&gt; / &lt;code&gt;headers()&lt;/code&gt;, logging, tracing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The catch&lt;/td&gt;
&lt;td&gt;Pending timers and promises keep the store alive&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why not a global variable?
&lt;/h3&gt;

&lt;p&gt;A global works when there's one request at a time. Node handles many at once on a single thread: request A hits an &lt;code&gt;await&lt;/code&gt;, request B runs, then A continues. A global would hold B's data by then. AsyncLocalStorage gives each chain of async work its own value.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;run()&lt;/code&gt; vs &lt;code&gt;enterWith()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;run(store, fn)&lt;/code&gt; puts the badge on only for &lt;code&gt;fn&lt;/code&gt; and everything it schedules, then takes it off. &lt;code&gt;enterWith(store)&lt;/code&gt; puts it on for the rest of the current synchronous code and has no clear end, so it's easy to leak into places you didn't expect. Prefer &lt;code&gt;run()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beyond Node
&lt;/h3&gt;

&lt;p&gt;Deno, Bun and Cloudflare Workers support &lt;code&gt;AsyncLocalStorage&lt;/code&gt; too. There's also a TC39 proposal, &lt;a href="https://github.com/tc39/proposal-async-context" rel="noopener noreferrer"&gt;AsyncContext&lt;/a&gt;, to add the same idea to JavaScript itself, including browsers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/api/async_context.html#class-asynclocalstorage" rel="noopener noreferrer"&gt;Node.js docs: AsyncLocalStorage&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/asynclocalstorage-explained" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>nextjs</category>
      <category>async</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Next.js Memory Leak: The Timer That Kept Every Request</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Mon, 30 Nov 2026 14:24:56 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/nextjs-memory-leak-the-timer-that-kept-every-request-1de6</link>
      <guid>https://dev.to/mohsenfallahnjd/nextjs-memory-leak-the-timer-that-kept-every-request-1de6</guid>
      <description>&lt;p&gt;Picture a Next.js server sitting at &lt;strong&gt;5 GB of memory&lt;/strong&gt;. It isn't crashing and it isn't spiking. It sits there, day after day. The garbage collector runs, and nothing comes back.&lt;/p&gt;

&lt;p&gt;That's what Rohit Kashyap's team at Zepto saw, and &lt;a href="https://rohitpotato.xyz/thoughts/ssr-requests-still-alive" rel="noopener noreferrer"&gt;his write-up&lt;/a&gt; is a great detective story. It's also long and heavy. Here's the same story in plain words: how they noticed it, how they measured it, what caused it, and how they fixed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole idea in one picture
&lt;/h2&gt;

&lt;p&gt;Think of a cloakroom at a party. Every guest (a request) hands over a coat. When the guest leaves, the coat should go too.&lt;/p&gt;

&lt;p&gt;Now picture someone pinning a &lt;strong&gt;5-minute timer ticket&lt;/strong&gt; to every coat, and a house rule: &lt;em&gt;you can't clear a coat while its ticket is still ticking.&lt;/em&gt; Guests leave, but the coats pile up for five minutes each. With lots of guests, the rack is always full.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any &lt;code&gt;setTimeout&lt;/code&gt; created while a request is being rendered keeps that whole request in memory until the timer fires.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the bug. Everything else is how they found the ticket and took it off.&lt;/p&gt;

&lt;h2&gt;
  
  
  How they noticed: memory that never goes down
&lt;/h2&gt;

&lt;p&gt;First, a quick reminder of how memory gets freed in JavaScript. The garbage collector doesn't hunt for "unused" things. It starts from a few &lt;strong&gt;roots&lt;/strong&gt; (globals, the call stack, pending timers) and keeps everything it can still reach. Everything else gets thrown away. (More on that in &lt;a href="https://bitlyst.blog/p/how-garbage-collection-works" rel="noopener noreferrer"&gt;how garbage collection works&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;So when memory stays high even after the garbage collector runs, it means one thing: &lt;strong&gt;something still points at that memory.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;They measured this on a production build (not &lt;code&gt;next dev&lt;/code&gt;, which is full of dev-only stuff) and forced a garbage collection before each measurement, so only memory that's really stuck would show:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;After…&lt;/th&gt;
&lt;th&gt;Heap after forced GC&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Warm-up (56 requests)&lt;/td&gt;
&lt;td&gt;112 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;+280 requests&lt;/td&gt;
&lt;td&gt;312 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;~1,400 requests&lt;/td&gt;
&lt;td&gt;934 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;About &lt;strong&gt;1 MB kept per request&lt;/strong&gt;, and none of it came back. That's the cloakroom filling up.&lt;/p&gt;

&lt;h2&gt;
  
  
  How they found it: follow the string back
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;heap snapshot&lt;/strong&gt; is a photo of everything in memory and who points at whom. They took one before the load and one after, and compared them.&lt;/p&gt;

&lt;p&gt;One line gave it away: &lt;code&gt;ServerResponse 336&lt;/code&gt;. They had sent exactly 336 requests, and &lt;strong&gt;every response was still alive&lt;/strong&gt;, minutes after it finished.&lt;/p&gt;

&lt;p&gt;The usual tool, the "biggest objects" view, didn't help, because nothing was big. It was hundreds of small coats, not one giant one. So they asked a different question: &lt;em&gt;what chain of references, starting from a root, still reaches this response?&lt;/em&gt; They wrote a small script to walk the snapshot from the roots and print that chain.&lt;/p&gt;

&lt;p&gt;Every chain looked the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pending 5-minute timer        ← a GC root
  → react-query Query
  → QueryClient
  → the request's context
  → ServerResponse            ← the finished request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzkp5tcrms3o82wuik3ld.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzkp5tcrms3o82wuik3ld.jpg" alt="AI-generated illustration. The bug wasn't in the parcels. It was at the other end of the string." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a timer kept the request
&lt;/h2&gt;

&lt;p&gt;Three small things added up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;react-query v3 starts a timer for every query.&lt;/strong&gt; In the browser that's useful: "if nobody uses this data for 5 minutes, clear it." Normally a component subscribes and cancels it, but that happens in &lt;code&gt;useEffect&lt;/code&gt;, and &lt;strong&gt;effects never run on the server&lt;/strong&gt;. So during server rendering, the timer ticks for 2 to 5 minutes for nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node pins the request to the timer.&lt;/strong&gt; Next.js runs each request inside &lt;code&gt;AsyncLocalStorage&lt;/code&gt;. That's what makes &lt;code&gt;cookies()&lt;/code&gt; and &lt;code&gt;headers()&lt;/code&gt; work. Every timer you create copies that request context, in case the callback needs it later. Node can't know if it will, so it keeps it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pending timers are roots.&lt;/strong&gt; Anything a waiting timer can reach can't be freed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So: one timer per query, each holding a whole request (response, buffers, data) for minutes. Under real traffic, that's a full rack all the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: don't start the timer on the server
&lt;/h2&gt;

&lt;p&gt;react-query skips the timer when the cache time is &lt;code&gt;Infinity&lt;/code&gt;. On the server, each &lt;code&gt;QueryClient&lt;/code&gt; lives for one request and is thrown away after it anyway, so "never expire" really means "expire with the request":&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;class&lt;/span&gt; &lt;span class="nc"&gt;ServerQueryCache&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;QueryCache&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&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="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cacheTime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;Infinity&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;queryClient&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;QueryClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;queryCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;isServer&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;ServerQueryCache&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="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser keeps its normal cache. Only the server changes.&lt;/p&gt;

&lt;p&gt;The result: the local heap stayed flat (70 MB → 84 MB after about a thousand requests), and in production, &lt;strong&gt;memory per pod dropped from about 4.8 GiB to 1.2 GiB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One honest warning from the article: their autoscaler was scaling on memory. Once memory dropped, fewer pods ran and latency went up. If you fix a memory problem, keep an eye on your scaling for a few days.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"On a Node server, any timer created during a request keeps that request's context alive until it fires, because AsyncLocalStorage attaches the context to the timer and pending timers are GC roots. In one real case, react-query started a 5-minute cache timer per query during SSR, so every finished request stayed in memory for minutes. They found it by diffing heap snapshots after a forced GC and following retainer paths from the roots, and fixed it by setting the server cache time to Infinity so the timer never starts."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;What to do&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Notice&lt;/td&gt;
&lt;td&gt;Memory stays high &lt;strong&gt;after&lt;/strong&gt; a forced GC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Measure&lt;/td&gt;
&lt;td&gt;Production build, force GC, then take a heap snapshot&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compare&lt;/td&gt;
&lt;td&gt;Diff two snapshots: what count grows with requests?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Trace&lt;/td&gt;
&lt;td&gt;Follow the reference chain back to a GC root&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Usual suspect&lt;/td&gt;
&lt;td&gt;Timers, long promises, caches created during a request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;This fix&lt;/td&gt;
&lt;td&gt;Server &lt;code&gt;cacheTime: Infinity&lt;/code&gt;, so no timer starts&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why upgrading react-query isn't enough
&lt;/h3&gt;

&lt;p&gt;react-query v4 and v5 default the cache time (&lt;code&gt;gcTime&lt;/code&gt; in v5) to &lt;code&gt;Infinity&lt;/code&gt; on the server. But if your hooks pass their own finite value, a real timer still starts during SSR. The article points to &lt;a href="https://github.com/TanStack/query/issues/11320" rel="noopener noreferrer"&gt;TanStack/query#11320&lt;/a&gt; and notes that, as of November 2026, the fix wasn't merged. If you set &lt;code&gt;gcTime&lt;/code&gt; per hook, you still need a guard like the one above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the "biggest objects" view missed it
&lt;/h3&gt;

&lt;p&gt;A dominator tree shows who &lt;strong&gt;alone&lt;/strong&gt; owns a big chunk of memory. Here, hundreds of small, separate references each held one request. No single object was big, so nothing stood out. Shortest paths from the GC roots (a breadth-first search over the snapshot, skipping weak references) found them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Taking snapshots safely
&lt;/h3&gt;

&lt;p&gt;Snapshots of a leaking server get huge. The author's first script joined all the chunks into one string and hit the maximum string size. Stream the chunks straight to a file instead, and call &lt;code&gt;HeapProfiler.collectGarbage&lt;/code&gt; (twice) before taking the snapshot.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other timers to watch for
&lt;/h3&gt;

&lt;p&gt;The same thing happens with any library written for the browser that sets timers: cache eviction, polling, debouncing, retries. If it runs during SSR, it can pin the request.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/nextjs-ssr-memory-leak-timer" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>node</category>
      <category>memory</category>
      <category>performance</category>
    </item>
    <item>
      <title>Database Indexes: The Back Page of the Book</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Sun, 29 Nov 2026 12:52:46 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/database-indexes-the-back-page-of-the-book-2f23</link>
      <guid>https://dev.to/mohsenfallahnjd/database-indexes-the-back-page-of-the-book-2f23</guid>
      <description>&lt;p&gt;Picture this: your app works great with 100 users. A year later you have 2 million, and the login page suddenly takes three seconds. You didn't change any code. So what happened?&lt;/p&gt;

&lt;p&gt;Very often, the answer is a missing &lt;strong&gt;database index&lt;/strong&gt;. Let's see what that is, in plain words.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole idea in one picture
&lt;/h2&gt;

&lt;p&gt;Think about a big cookbook. You want the recipe for pancakes. You have two choices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start at page 1 and flip through every page until you find it.&lt;/li&gt;
&lt;li&gt;Go to the &lt;strong&gt;index at the back&lt;/strong&gt;, find "Pancakes → page 212", and jump straight there.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;A database index is the index at the back of the book. It tells the database exactly where to look, so it doesn't have to read every row.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it. Everything else in this post is just details on top of that picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;When you run a query like this:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;the database needs to find the matching row. Without an index, it does option 1 from the cookbook: it reads &lt;strong&gt;every single row&lt;/strong&gt; and checks the email. This is called a &lt;strong&gt;full table scan&lt;/strong&gt;. With 100 rows, nobody notices. With 2 million rows, you notice.&lt;/p&gt;

&lt;p&gt;Now you add an index:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The database builds a separate, small list: every email, &lt;strong&gt;kept in sorted order&lt;/strong&gt;, each one with a pointer to its row. Like the cookbook index, sorted from A to Z.&lt;/p&gt;

&lt;p&gt;Because the list is sorted, the database can find a value the way you find a word in a dictionary: open the middle, see if you went too far, cut the rest in half, repeat. For a million rows that's only about 20 steps, because you can halve a million only about 20 times before one row is left. Compare that with checking a million rows one by one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fde8w0b10taqwd5aza0xu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fde8w0b10taqwd5aza0xu.jpg" alt="AI-generated illustration. Same question, same data. The only difference is whether someone kept a sorted list." width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(If someone asks what the list really looks like inside: it's usually a &lt;strong&gt;B-tree&lt;/strong&gt;, a sorted tree that stays balanced as you add rows. You don't need more than that name for most conversations.)&lt;/p&gt;

&lt;h2&gt;
  
  
  The catch: indexes aren't free
&lt;/h2&gt;

&lt;p&gt;If indexes make reads faster, why not add one to every column? Go back to the cookbook.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It takes pages.&lt;/strong&gt; The index at the back of the book uses paper. A database index uses disk space and memory too.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It needs upkeep.&lt;/strong&gt; Every time the author adds a recipe, someone has to update the index. Every time you &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt; a row, the database has to update &lt;strong&gt;every index&lt;/strong&gt; on that table as well. So writes get a bit slower.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the rule is simple: &lt;strong&gt;index the columns you search, filter, join or sort by often.&lt;/strong&gt; Don't index things you never look up.&lt;/p&gt;

&lt;h2&gt;
  
  
  In practice
&lt;/h2&gt;

&lt;p&gt;Good candidates for an index:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Columns in &lt;code&gt;WHERE&lt;/code&gt;: &lt;code&gt;email&lt;/code&gt;, &lt;code&gt;user_id&lt;/code&gt;, &lt;code&gt;status&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Columns you &lt;code&gt;JOIN&lt;/code&gt; on: foreign keys like &lt;code&gt;orders.user_id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Columns you &lt;code&gt;ORDER BY&lt;/code&gt; on big tables: &lt;code&gt;created_at&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most databases create an index for your &lt;strong&gt;primary key&lt;/strong&gt; automatically, and for &lt;code&gt;UNIQUE&lt;/code&gt; columns too, so &lt;code&gt;WHERE id = 42&lt;/code&gt; is already fast.&lt;/p&gt;

&lt;p&gt;Not sure if a query uses an index? Ask the database. Put &lt;code&gt;EXPLAIN&lt;/code&gt; in front of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'sara@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- "Seq Scan on users"    ← reading every row (no index used)&lt;/span&gt;
&lt;span class="c1"&gt;-- "Index Scan using idx_users_email" ← jumping straight there&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How often will this bite you? Honestly, &lt;strong&gt;almost every growing app hits it at least once.&lt;/strong&gt; The good news: it's usually a one-line fix, and the slow query shows you exactly where it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say it in an interview
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"An index is like the index at the back of a book. The database keeps a sorted copy of one or more columns, usually as a B-tree, with pointers to the rows. So instead of scanning the whole table, it can find a value in a few steps. The trade-off is extra storage and slower writes, because every insert or update also has to update the index. So I index the columns I filter, join and sort by, and check with &lt;code&gt;EXPLAIN&lt;/code&gt;."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Short answer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What is an index?&lt;/td&gt;
&lt;td&gt;A sorted list of column values with pointers to rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why is it fast?&lt;/td&gt;
&lt;td&gt;Sorted data can be searched by halving, not row by row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No index means?&lt;/td&gt;
&lt;td&gt;Full table scan: read every row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What does it cost?&lt;/td&gt;
&lt;td&gt;Disk space, and slower &lt;code&gt;INSERT&lt;/code&gt; / &lt;code&gt;UPDATE&lt;/code&gt; / &lt;code&gt;DELETE&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What to index?&lt;/td&gt;
&lt;td&gt;Columns in &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;JOIN&lt;/code&gt; and &lt;code&gt;ORDER BY&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How to check?&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;EXPLAIN&lt;/code&gt; your query&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Composite indexes and the "phone book" rule
&lt;/h3&gt;

&lt;p&gt;An index can cover more than one column:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_users_last_first&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_name&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;Think of a phone book sorted by last name, then first name. It helps when you search by &lt;code&gt;last_name&lt;/code&gt;, or by &lt;code&gt;last_name&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;first_name&lt;/code&gt;. It does not help when you search only by &lt;code&gt;first_name&lt;/code&gt;, because the first names are scattered all over the book. Column order matters: put the column you always filter by first.&lt;/p&gt;

&lt;h3&gt;
  
  
  When the database ignores your index
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The query wraps the column in a function.&lt;/strong&gt; &lt;code&gt;WHERE LOWER(email) = '...'&lt;/code&gt; can't use a plain index on &lt;code&gt;email&lt;/code&gt;. Many databases let you index the expression itself (&lt;code&gt;CREATE INDEX ... ON users (LOWER(email))&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The filter matches most of the table.&lt;/strong&gt; If &lt;code&gt;status = 'active'&lt;/code&gt; is true for 95% of rows, reading the whole table is cheaper than jumping back and forth. The query planner decides this for you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leading wildcards.&lt;/strong&gt; &lt;code&gt;LIKE '%gmail.com'&lt;/code&gt; can't use the sorted order, for the same reason you can't look up words by their last letters in a dictionary.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://use-the-index-luke.com/" rel="noopener noreferrer"&gt;Use The Index, Luke&lt;/a&gt;: a free, friendly book about SQL indexing.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/indexes.html" rel="noopener noreferrer"&gt;PostgreSQL docs: Indexes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/database-indexes-explained" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>performance</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>any vs unknown vs never in TypeScript, Simply</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Fri, 27 Nov 2026 10:13:45 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/any-vs-unknown-vs-never-in-typescript-simply-17ik</link>
      <guid>https://dev.to/mohsenfallahnjd/any-vs-unknown-vs-never-in-typescript-simply-17ik</guid>
      <description>&lt;p&gt;&lt;code&gt;JSON.parse&lt;/code&gt; returns &lt;code&gt;any&lt;/code&gt;. So does a bare &lt;code&gt;catch (e)&lt;/code&gt; in older configs. Both quietly turn off TypeScript for everything they touch, and the bug shows up at runtime instead.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;any&lt;/code&gt;, &lt;code&gt;unknown&lt;/code&gt; and &lt;code&gt;never&lt;/code&gt; sound alike, but they mean three very different things.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Think of airport security.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;any&lt;/code&gt;&lt;/strong&gt; walks around the scanner. Nothing is checked, and anything goes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;unknown&lt;/code&gt;&lt;/strong&gt; is a sealed bag on the belt. You can hold it, but you can't open it until you scan it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;never&lt;/code&gt;&lt;/strong&gt; is a gate nothing can pass through. If code reaches it, something is wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;any&lt;/code&gt; switches the checker off.&lt;/strong&gt; You can call anything on it, and TypeScript says nothing.&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'"hi"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// compiles fine, crashes at runtime&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Worse, it spreads. Pass &lt;code&gt;data&lt;/code&gt; into a typed function and the result is often &lt;code&gt;any&lt;/code&gt; too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;unknown&lt;/code&gt; says "check before use".&lt;/strong&gt; You can store it and pass it around, but you can't use it until you prove what it is. That proof is called &lt;strong&gt;narrowing&lt;/strong&gt;: a &lt;code&gt;typeof&lt;/code&gt;, &lt;code&gt;instanceof&lt;/code&gt; or other check that tells TypeScript the real type.&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'"hi"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&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;// error: 'data' is of type 'unknown'&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;typeof&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&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;// fine: scanned, it's a string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk8aj7iclib4lm0dlu8xk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk8aj7iclib4lm0dlu8xk.jpg" alt="A hand inspects a sealed box with a magnifying glass, next to a tray where each shape sits in its own slot" width="800" height="420"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;unknown isn't less safe than a real type. It's a real type you haven't opened yet.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;never&lt;/code&gt; says "this cannot happen".&lt;/strong&gt; No value has type &lt;code&gt;never&lt;/code&gt;. You see it in two places: a function that never returns (it always throws or loops forever), and a branch TypeScript has proven impossible.&lt;/p&gt;
&lt;h2&gt;
  
  
  In practice
&lt;/h2&gt;

&lt;p&gt;The most useful trick is the &lt;strong&gt;exhaustive check&lt;/strong&gt;. Picture a &lt;code&gt;switch&lt;/code&gt; over every shape:&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="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"square"&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;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&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;case&lt;/span&gt; &lt;span class="s2"&gt;"square"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&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="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// only compiles if every case is handled&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;"triangle"&lt;/code&gt; to &lt;code&gt;Shape&lt;/code&gt; and forget the case: &lt;code&gt;s&lt;/code&gt; is now &lt;code&gt;"triangle"&lt;/code&gt; in the &lt;code&gt;default&lt;/code&gt; branch, not &lt;code&gt;never&lt;/code&gt;, so the build fails and points at the line you forgot.&lt;/p&gt;

&lt;p&gt;Simple rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data from outside (API responses, &lt;code&gt;JSON.parse&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt; errors, user input): type it as &lt;strong&gt;&lt;code&gt;unknown&lt;/code&gt;&lt;/strong&gt; and check it, or validate it with a library such as Zod.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;any&lt;/code&gt;&lt;/strong&gt; only as a short-lived escape hatch, for example while migrating JavaScript. Leave a comment saying why.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;never&lt;/code&gt;&lt;/strong&gt; for exhaustive checks and functions that always throw.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you enable &lt;code&gt;strict&lt;/code&gt;, &lt;code&gt;catch (e)&lt;/code&gt; gives you &lt;code&gt;unknown&lt;/code&gt; already (via &lt;code&gt;useUnknownInCatchVariables&lt;/code&gt;), which is a good sign of which one TypeScript itself prefers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Say it in an interview:&lt;/strong&gt; "&lt;code&gt;any&lt;/code&gt; turns type checking off for that value, so mistakes reach runtime. &lt;code&gt;unknown&lt;/code&gt; also accepts any value, but you have to narrow it with a check before you can use it, so it's the safe choice for outside data. &lt;code&gt;never&lt;/code&gt; is the type with no values: it marks code that can't be reached, and I use it in a &lt;code&gt;switch&lt;/code&gt; default to make the compiler catch missing cases."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Airport picture&lt;/th&gt;
&lt;th&gt;Can you use it directly?&lt;/th&gt;
&lt;th&gt;Use for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;any&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Skips the scanner&lt;/td&gt;
&lt;td&gt;Yes, nothing is checked&lt;/td&gt;
&lt;td&gt;Temporary escape hatch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unknown&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sealed bag&lt;/td&gt;
&lt;td&gt;No, narrow it first&lt;/td&gt;
&lt;td&gt;Outside data, errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;never&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Gate nothing passes&lt;/td&gt;
&lt;td&gt;There's no value to use&lt;/td&gt;
&lt;td&gt;Exhaustive checks, always-throw functions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; reach for &lt;code&gt;unknown&lt;/code&gt; where you'd type &lt;code&gt;any&lt;/code&gt;, and let &lt;code&gt;never&lt;/code&gt; tell you when you forgot a case.&lt;/p&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Top type and bottom type
&lt;/h3&gt;

&lt;p&gt;In type-theory terms, &lt;code&gt;unknown&lt;/code&gt; is the &lt;strong&gt;top type&lt;/strong&gt;: every type can be assigned to it. &lt;code&gt;never&lt;/code&gt; is the &lt;strong&gt;bottom type&lt;/strong&gt;: it can be assigned to every type, and nothing (except &lt;code&gt;never&lt;/code&gt;) can be assigned to it. &lt;code&gt;any&lt;/code&gt; breaks the rules both ways, which is why it's an escape hatch rather than a real place in the hierarchy.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;never&lt;/code&gt; in unions and conditional types
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;string | never&lt;/code&gt; is just &lt;code&gt;string&lt;/code&gt;; &lt;code&gt;never&lt;/code&gt; disappears from unions. That's why conditional types use it to filter: &lt;code&gt;type NonNull&amp;lt;T&amp;gt; = T extends null ? never : T&lt;/code&gt; drops &lt;code&gt;null&lt;/code&gt; from a union.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linting &lt;code&gt;any&lt;/code&gt; away
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;@typescript-eslint/no-explicit-any&lt;/code&gt; flags written &lt;code&gt;any&lt;/code&gt;, and the &lt;code&gt;no-unsafe-*&lt;/code&gt; rules catch &lt;code&gt;any&lt;/code&gt; that sneaks in from libraries. Together they keep the escape hatch from becoming the default.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/typescript-any-unknown-never" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>types</category>
      <category>interview</category>
    </item>
    <item>
      <title>Sessions vs JWT: Where Does Your Login Actually Live?</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Fri, 27 Nov 2026 10:13:15 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/sessions-vs-jwt-where-does-your-login-actually-live-37n9</link>
      <guid>https://dev.to/mohsenfallahnjd/sessions-vs-jwt-where-does-your-login-actually-live-37n9</guid>
      <description>&lt;p&gt;You log in once, and the next fifty requests still know who you are. HTTP forgets everything between requests, so something has to carry your login along.&lt;/p&gt;

&lt;p&gt;There are two classic answers: a &lt;strong&gt;session&lt;/strong&gt; or a &lt;strong&gt;JWT&lt;/strong&gt;. The difference is one question: &lt;strong&gt;who keeps the proof?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A session is a coat-check ticket. A JWT is a signed festival wristband.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With a coat check, the server keeps your coat (your login data) and gives you a numbered ticket. The ticket means nothing on its own. With a wristband, you carry the proof yourself. The guard at the door doesn't look anything up; they check the seal.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Sessions.&lt;/strong&gt; You log in, and the server saves a record like &lt;code&gt;{ id: "a8f3", userId: 7 }&lt;/code&gt; in a database or Redis. It sends back only the ID, usually in a cookie. On every request, the server looks up &lt;code&gt;a8f3&lt;/code&gt; to find out who you are.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JWT&lt;/strong&gt; (JSON Web Token). You log in, and the server writes your details into a token, such as &lt;code&gt;{ userId: 7, role: "admin", exp: ... }&lt;/code&gt;, and &lt;strong&gt;signs&lt;/strong&gt; it with a secret key. The signature is the seal: change one letter and the check fails. On every request, the server checks the signature and reads the data straight from the token. No lookup.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Session (ticket)&lt;/th&gt;
&lt;th&gt;JWT (wristband)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Where the login data lives&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;In the token you carry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Each request&lt;/td&gt;
&lt;td&gt;Look up the ID&lt;/td&gt;
&lt;td&gt;Check the signature&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Log out / ban a user&lt;/td&gt;
&lt;td&gt;Delete the record. Done.&lt;/td&gt;
&lt;td&gt;Hard: the token is valid until it expires&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scaling many servers&lt;/td&gt;
&lt;td&gt;All need the same store&lt;/td&gt;
&lt;td&gt;Any server with the key can check it&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  In practice
&lt;/h2&gt;

&lt;p&gt;The big trade-off is &lt;strong&gt;revoking&lt;/strong&gt;. Delete a session, and that user is out on the next request. A JWT has no record to delete, so a stolen or outdated token keeps working until its &lt;code&gt;exp&lt;/code&gt; time. If you just removed someone's admin role, their old token still says &lt;code&gt;"admin"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmw0o46umi3pd9m6m896y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmw0o46umi3pd9m6m896y.jpg" alt="A guard with scissors stands by torn tickets while a person with a gold wristband walks past him" width="800" height="420"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The guard can tear up any ticket. He can't cut a wristband he never kept a record of.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That's why most JWT setups use &lt;strong&gt;short-lived access tokens&lt;/strong&gt; (minutes) plus a &lt;strong&gt;refresh token&lt;/strong&gt; stored on the server. And once you store refresh tokens to make logout work, you've brought back a server-side record: a session by another name.&lt;/p&gt;

&lt;p&gt;So, which one?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A normal web app with one backend:&lt;/strong&gt; sessions in an &lt;a href="https://bitlyst.blog/p/httpOnly-cookies-guide" rel="noopener noreferrer"&gt;&lt;code&gt;httpOnly&lt;/code&gt; cookie&lt;/a&gt;. Simple, easy to revoke.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many services or third parties that must check identity without calling you:&lt;/strong&gt; JWTs, with short expiry.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two myths to drop: a JWT is &lt;strong&gt;signed, not encrypted&lt;/strong&gt;, so anyone can read what's inside. Never put secrets in it. And JWT vs session is not the same choice as cookie vs &lt;code&gt;localStorage&lt;/code&gt;: a JWT can live in a cookie too, and where it lives decides your &lt;a href="https://bitlyst.blog/p/xss-vs-csrf" rel="noopener noreferrer"&gt;XSS and CSRF&lt;/a&gt; risks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Say it in an interview:&lt;/strong&gt; "With sessions, the server stores the login and the client holds only an ID, so every request needs a lookup, but logout and bans are instant. A JWT is a signed token that carries the user data itself, so any server can verify it without a lookup, but you can't easily revoke it before it expires. For a typical web app I use sessions in an httpOnly cookie; I reach for short-lived JWTs when several services need to verify identity on their own."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Session = coat-check ticket:&lt;/strong&gt; server keeps the data, you hold an ID.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JWT = signed wristband:&lt;/strong&gt; you carry the data, the server checks the seal.&lt;/li&gt;
&lt;li&gt;Sessions are &lt;strong&gt;easy to revoke&lt;/strong&gt;; JWTs live &lt;strong&gt;until they expire&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;JWTs are &lt;strong&gt;signed, not encrypted&lt;/strong&gt;: readable by anyone.&lt;/li&gt;
&lt;li&gt;Default for web apps: &lt;strong&gt;session + &lt;code&gt;httpOnly&lt;/code&gt; cookie&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; sessions trust a lookup, JWTs trust a signature. Pick the one whose weakness you can live with.&lt;/p&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What a JWT looks like
&lt;/h3&gt;

&lt;p&gt;Three Base64URL parts joined by dots: &lt;code&gt;header.payload.signature&lt;/code&gt;. The header names the algorithm, the payload holds the claims (&lt;code&gt;sub&lt;/code&gt;, &lt;code&gt;exp&lt;/code&gt;, your own fields), and the signature covers both. Paste one into a decoder and you can read the payload without any key. That's the "signed, not encrypted" point in action.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ways to revoke a JWT anyway
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Keep access tokens short (5 to 15 minutes is common) so a stolen one dies soon.&lt;/li&gt;
&lt;li&gt;Keep a blocklist of revoked token IDs (&lt;code&gt;jti&lt;/code&gt;). That's a lookup on every request again.&lt;/li&gt;
&lt;li&gt;Store a per-user "token version" and bump it on logout or password change.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these trades some of the "no lookup" benefit for control.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signing algorithms
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;HS256&lt;/code&gt; uses one shared secret to sign and to check, so every checker can also create tokens. &lt;code&gt;RS256&lt;/code&gt; and &lt;code&gt;ES256&lt;/code&gt; sign with a private key and check with a public key, which suits many services that only need to verify. Always pin the expected algorithm on the server; never trust the &lt;code&gt;alg&lt;/code&gt; field in the token alone.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/sessions-vs-jwt" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>security</category>
      <category>jwt</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>async vs defer: Why Script Order Breaks Your Page</title>
      <dc:creator>Mohsen Fallahnejad</dc:creator>
      <pubDate>Fri, 27 Nov 2026 10:13:15 +0000</pubDate>
      <link>https://dev.to/mohsenfallahnjd/async-vs-defer-why-script-order-breaks-your-page-fok</link>
      <guid>https://dev.to/mohsenfallahnjd/async-vs-defer-why-script-order-breaks-your-page-fok</guid>
      <description>&lt;p&gt;You add &lt;code&gt;async&lt;/code&gt; to two script tags to speed up the page. Now it breaks one load in ten with &lt;code&gt;$ is not defined&lt;/code&gt;. The plugin ran before the library it needs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; vs &lt;code&gt;defer&lt;/code&gt; is a question about &lt;strong&gt;when&lt;/strong&gt; a script runs, not how it downloads.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Both download in the background. &lt;code&gt;async&lt;/code&gt; runs the moment it lands. &lt;code&gt;defer&lt;/code&gt; waits in line until the HTML is done.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Picture food deliveries arriving while you set a table. &lt;code&gt;async&lt;/code&gt; serves each dish the second it comes through the door, in whatever order the drivers arrive. &lt;code&gt;defer&lt;/code&gt; puts every dish on a shelf and serves them in menu order once the table is fully set.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;The browser reads your HTML top to bottom and builds the page as it goes. That's called &lt;strong&gt;parsing&lt;/strong&gt;. A script tag can interrupt it in three ways:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tag&lt;/th&gt;
&lt;th&gt;Download&lt;/th&gt;
&lt;th&gt;Runs&lt;/th&gt;
&lt;th&gt;Order kept?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stops parsing to download&lt;/td&gt;
&lt;td&gt;Right away&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;script async&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;In the background&lt;/td&gt;
&lt;td&gt;As soon as it arrives, pausing parsing&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;script defer&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;In the background&lt;/td&gt;
&lt;td&gt;After the HTML is parsed, before &lt;code&gt;DOMContentLoaded&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A plain &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; is the worst case: the page stops building until the file is downloaded &lt;strong&gt;and&lt;/strong&gt; run. That's why old advice said "put scripts at the bottom of &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;".&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; fixes the download, but it still runs whenever it arrives. That can be mid-page, and two &lt;code&gt;async&lt;/code&gt; scripts run in whichever order the network delivers them.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;defer&lt;/code&gt; fixes both. It never pauses the page, and deferred scripts run in the order they appear in the HTML.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F25i84gczzmikiocmtbxg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F25i84gczzmikiocmtbxg.jpg" alt="Three ribbons: one cut by a block, one dented by a block dropped mid-way, one unbroken with gold blocks waiting at its end" width="800" height="420"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Plain, async, defer. Only the last one lets the page finish in one piece and still keeps the scripts in line.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  In practice
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;&lt;code&gt;defer&lt;/code&gt;&lt;/strong&gt; for your own app code, and for anything that depends on another script or on the DOM. It's the safe default.&lt;/p&gt;

&lt;p&gt;Use &lt;strong&gt;&lt;code&gt;async&lt;/code&gt;&lt;/strong&gt; for scripts that stand alone and don't care about order: analytics, ads, a chat widget. If it would be fine for it to run first or last, it can be &lt;code&gt;async&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/lib.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/plugin.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt; &lt;span class="c"&gt;&amp;lt;!-- always after lib.js --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://analytics.example.com/a.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two quick facts help here. &lt;strong&gt;Module scripts&lt;/strong&gt; (&lt;code&gt;type="module"&lt;/code&gt;) are deferred by default. And both attributes only work on scripts with a &lt;code&gt;src&lt;/code&gt;; on an inline script, they do nothing.&lt;/p&gt;

&lt;p&gt;How often will you hit this? Frameworks like Next.js already load their own bundles the right way. You meet it when you add a third-party snippet by hand, and that's exactly where the "works on my machine" order bug comes from.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Say it in an interview:&lt;/strong&gt; "Both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; download the script without blocking the HTML parser. &lt;code&gt;async&lt;/code&gt; runs as soon as it's downloaded, in any order, so it's for independent scripts like analytics. &lt;code&gt;defer&lt;/code&gt; waits until the HTML is parsed and runs scripts in document order, so it's the default for app code that depends on the DOM or on other scripts."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Plain script:&lt;/strong&gt; stops the page, downloads, runs. Avoid in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;async&lt;/code&gt;:&lt;/strong&gt; serve the moment it lands. Any order. For independent scripts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;defer&lt;/code&gt;:&lt;/strong&gt; wait until the table is set, then serve in menu order. For app code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;type="module"&lt;/code&gt;:&lt;/strong&gt; deferred by default.&lt;/li&gt;
&lt;li&gt;Inline scripts ignore both attributes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; if order matters, use &lt;code&gt;defer&lt;/code&gt;. If it doesn't, &lt;code&gt;async&lt;/code&gt; is fine.&lt;/p&gt;




&lt;h2&gt;
  
  
  Going deeper
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Advanced and optional. Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Both attributes at once
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;script async defer&amp;gt;&lt;/code&gt; acts as &lt;code&gt;async&lt;/code&gt; in every modern browser. It was a trick for very old browsers that only knew &lt;code&gt;defer&lt;/code&gt;. You don't need it today.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;async&lt;/code&gt; on a module
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;script type="module" async&amp;gt;&lt;/code&gt; drops the wait-for-HTML rule: it runs as soon as it and its imports are ready, like a normal &lt;code&gt;async&lt;/code&gt; script.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scripts added from JavaScript
&lt;/h3&gt;

&lt;p&gt;A script created with &lt;code&gt;document.createElement("script")&lt;/code&gt; acts as &lt;code&gt;async&lt;/code&gt; by default. Set &lt;code&gt;script.async = false&lt;/code&gt; to make dynamically added scripts run in insertion order.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://html.spec.whatwg.org/multipage/scripting.html#attr-script-async" rel="noopener noreferrer"&gt;HTML spec diagram for script loading&lt;/a&gt; shows all the cases on one timeline.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bitlyst.blog/p/async-vs-defer-script" rel="noopener noreferrer"&gt;Bitlyst&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>performance</category>
      <category>browser</category>
    </item>
  </channel>
</rss>
