<?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: Alessio Michelini</title>
    <description>The latest articles on DEV Community by Alessio Michelini (@darkmavis1980).</description>
    <link>https://dev.to/darkmavis1980</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%2F309908%2Fc0e39e91-06f1-465e-98eb-899089f7a611.jpeg</url>
      <title>DEV Community: Alessio Michelini</title>
      <link>https://dev.to/darkmavis1980</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/darkmavis1980"/>
    <language>en</language>
    <item>
      <title>Deploying to a home server on a private Tailscale network from GitHub Actions</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Mon, 23 Nov 2026 10:22:17 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/deploying-to-a-home-server-on-a-private-tailscale-network-from-github-actions-n2b</link>
      <guid>https://dev.to/darkmavis1980/deploying-to-a-home-server-on-a-private-tailscale-network-from-github-actions-n2b</guid>
      <description>&lt;p&gt;I wanted to set up a QA environment for my personal site. &lt;br&gt;
Every push to &lt;code&gt;main&lt;/code&gt; now deploys a copy of the site to my home server. That server is not open to the internet. GitHub Actions reaches it through Tailscale, deploys over SSH, and the QA copy is served on a real domain through a Cloudflare Tunnel that was already running there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also to be crystal clear, this is not the best solution possible, this is a solution that works for my case&lt;/strong&gt;, where I don't want to spend crazy money in hosting, and I have already a self hosted solution that I can use for QA, but I also use for other things.&lt;/p&gt;

&lt;p&gt;Production is unchanged. It's a separate VPS, deployed when I publish a GitHub release. What I wanted was a place to look at &lt;code&gt;main&lt;/code&gt; after every merged PR, before it goes anywhere near production, without opening a single port on my home network.&lt;/p&gt;

&lt;p&gt;The first real deploys taught me more than the design did, so most of this post is about what went wrong and how each failure stopped safely.&lt;/p&gt;
&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Piece&lt;/th&gt;
&lt;th&gt;What it is&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The site&lt;/td&gt;
&lt;td&gt;A static Vite + React site, built into an nginx Docker image with a &lt;code&gt;Dockerfile&lt;/code&gt; and &lt;code&gt;compose.yaml&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Production&lt;/td&gt;
&lt;td&gt;A separate VPS, deployed on GitHub releases (out of scope here)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The home server&lt;/td&gt;
&lt;td&gt;An Ubuntu box running about 40 other containers: self-hosted apps, databases, a reverse proxy. Already on my tailnet.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The code&lt;/td&gt;
&lt;td&gt;A private GitHub repo, where Actions runs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serving&lt;/td&gt;
&lt;td&gt;A &lt;code&gt;cloudflared&lt;/code&gt; tunnel already running on the server for the other apps&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the examples I'll call the server &lt;code&gt;qa-box&lt;/code&gt;, the tailnet &lt;code&gt;tailXXXX.ts.net&lt;/code&gt;, the deploy user &lt;code&gt;deploy&lt;/code&gt;, the app folder &lt;code&gt;/srv/apps/my-site&lt;/code&gt;, and the server's LAN IP &lt;code&gt;192.168.1.50&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;QA gets its own hostname on the existing tunnel, behind Cloudflare Access. Opening it asks for an email address first, so only addresses I allow get in, and search engines can't index a second copy of my site.&lt;/p&gt;
&lt;h2&gt;
  
  
  The design, and what I rejected
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Docker Compose on the server, from a git clone
&lt;/h3&gt;

&lt;p&gt;QA runs the same image I get from a local &lt;code&gt;docker compose up&lt;/code&gt;. The server keeps a git clone of the repo in &lt;code&gt;/srv/apps/my-site&lt;/code&gt;, checks out the commit being deployed, and builds it with Compose.&lt;/p&gt;

&lt;p&gt;Because the box is shared, isolation matters. Each app gets its own compose project name (&lt;code&gt;-p&lt;/code&gt;) and its own host port. A deploy of this app can only touch containers in its own project. It can't replace or remove anything else.&lt;/p&gt;

&lt;p&gt;Two alternatives I rejected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Build with &lt;code&gt;npm run build&lt;/code&gt; and serve with the host's nginx, like production.&lt;/strong&gt; That needs Node on the server and a hand-made nginx site on a box that already has a reverse proxy and 40 containers. More moving parts on a shared machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build in CI and &lt;code&gt;rsync&lt;/code&gt; the &lt;code&gt;dist/&lt;/code&gt; folder.&lt;/strong&gt; Simple, but QA wouldn't test the Docker image. Half the point of QA is to catch a broken image before production does.
### Tailscale with workload identity, not a stored secret&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The runner joins the tailnet with &lt;code&gt;tailscale/github-action@v4&lt;/code&gt; using workload identity federation (OIDC). The job asks GitHub for an OIDC token (&lt;code&gt;permissions: id-token: write&lt;/code&gt;), and Tailscale trusts tokens that match a credential I set up in the admin console. The action only needs &lt;code&gt;oauth-client-id&lt;/code&gt; and &lt;code&gt;audience&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The result: no long-lived Tailscale secret stored in GitHub. The runner joins as &lt;code&gt;tag:ci&lt;/code&gt;, as an ephemeral node, and it's removed after the job.&lt;/p&gt;
&lt;h3&gt;
  
  
  Plain SSH with a dedicated deploy key, not Tailscale SSH
&lt;/h3&gt;

&lt;p&gt;Tailscale SSH would have been neat, but it needs the destination to be tagged. Tagging the shared server would change who owns it in the tailnet and change the access rules for everything else on it. I didn't want a QA deploy to change how 40 other apps are reached.&lt;/p&gt;

&lt;p&gt;So the runner uses normal SSH with a key made only for deploys. Tailscale's job is narrower: let &lt;code&gt;tag:ci&lt;/code&gt; reach &lt;strong&gt;only &lt;code&gt;tcp:22&lt;/code&gt; on that one machine&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  All settings in a GitHub Environment
&lt;/h3&gt;

&lt;p&gt;At the start I had a question I couldn't answer: on a server shared with other apps, where do I say which folder belongs to this app? The answer turned out to be a GitHub Environment variable, validated before it's used.&lt;/p&gt;

&lt;p&gt;Everything lives in an Environment called &lt;code&gt;qa&lt;/code&gt;, limited to the &lt;code&gt;main&lt;/code&gt; branch:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Kind&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_HOST&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;variable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;qa-box.tailXXXX.ts.net&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_SSH_PORT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;variable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;22&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_SSH_USER&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;variable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;deploy&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_APP_PATH&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;variable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/srv/apps/my-site&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_HTTP_PORT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;variable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;7500&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_COMPOSE_PROJECT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;variable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;my-site-qa&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_SSH_PRIVATE_KEY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;secret&lt;/td&gt;
&lt;td&gt;private half of the deploy key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QA_SSH_KNOWN_HOSTS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;secret&lt;/td&gt;
&lt;td&gt;the server's host key line&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TS_OAUTH_CLIENT_ID&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;secret&lt;/td&gt;
&lt;td&gt;Tailscale trust credential ID&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TS_AUDIENCE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;secret&lt;/td&gt;
&lt;td&gt;Tailscale audience value&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Stream the deploy script from the runner
&lt;/h3&gt;

&lt;p&gt;The deploy script lives in the repo, at &lt;code&gt;scripts/deploy-qa.sh&lt;/code&gt;. The runner doesn't copy it to the server. It streams it over SSH:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh deploy@qa-box &lt;span class="s1"&gt;'bash -s -- &amp;lt;args&amp;gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; scripts/deploy-qa.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server always runs the script from the commit being deployed, and nothing needs installing there first. If I change the script in a PR, the next deploy uses the new version.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Tailscale policy
&lt;/h2&gt;

&lt;p&gt;This is the part of the tailnet policy that matters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="nl"&gt;"tagOwners"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"tag:ci"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"autogroup:admin"&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="nl"&gt;"hosts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"qa-box"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"100.x.y.z"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nl"&gt;"grants"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="c1"&gt;// GitHub Actions runners may reach only SSH on the QA server.&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tag:ci"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"dst"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"qa-box"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"ip"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"tcp:22"&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="nl"&gt;"tests"&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="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"tag:ci"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"accept"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"qa-box:22"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"deny"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"qa-box:80"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"qa-box:7500"&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;A few things worth explaining.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check for the default allow-all rule first.&lt;/strong&gt; A new tailnet ships with a rule like &lt;code&gt;"src": ["*"], "dst": ["*"]&lt;/code&gt;. If yours still has it, a tagged CI node can reach &lt;strong&gt;every device&lt;/strong&gt; on your tailnet, and the narrow grant above adds nothing. Narrow the broad rule to &lt;code&gt;autogroup:member&lt;/code&gt;, so it covers people's devices and not tagged nodes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;tests&lt;/code&gt; block runs when you save the policy.&lt;/strong&gt; If a later edit lets &lt;code&gt;tag:ci&lt;/code&gt; reach port 80 or the QA port, or stops it reaching SSH, Tailscale refuses to save. It's a cheap way to make the intent stick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I used a &lt;code&gt;hosts&lt;/code&gt; alias instead of a tag.&lt;/strong&gt; &lt;code&gt;qa-box&lt;/code&gt; is just a name for the server's Tailscale IP. The server keeps its existing owner and every rule that already applied to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  The OIDC subject trap
&lt;/h3&gt;

&lt;p&gt;This one is easy to get wrong. When a job uses &lt;code&gt;environment: qa&lt;/code&gt;, the subject of GitHub's OIDC token is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;repo:&amp;lt;owner&amp;gt;/&amp;lt;repo&amp;gt;:environment:qa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is &lt;strong&gt;not&lt;/strong&gt; &lt;code&gt;repo:&amp;lt;owner&amp;gt;/&amp;lt;repo&amp;gt;:ref:refs/heads/main&lt;/code&gt;, even though the job only runs on &lt;code&gt;main&lt;/code&gt;. The Tailscale trust credential has to match the subject exactly, or the token is rejected. The credential also needs the Auth Keys write scope and the &lt;code&gt;tag:ci&lt;/code&gt; tag, so it can create the ephemeral node.&lt;/p&gt;

&lt;h2&gt;
  
  
  The GitHub Actions job
&lt;/h2&gt;

&lt;p&gt;This is the final version of the job. It runs after the &lt;code&gt;build&lt;/code&gt; job (lint, typecheck, build) passes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;deploy-qa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to QA&lt;/span&gt;
  &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build&lt;/span&gt;                      &lt;span class="c1"&gt;# only after lint/typecheck/build pass
&lt;/span&gt;  &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;github.ref == 'refs/heads/main'&lt;/span&gt;
  &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
  &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;qa&lt;/span&gt;
  &lt;span class="na"&gt;concurrency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;group&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;qa-deploy&lt;/span&gt;
    &lt;span class="na"&gt;cancel-in-progress&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;       &lt;span class="c1"&gt;# a second merge waits, then deploys the newer commit
&lt;/span&gt;  &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;read&lt;/span&gt;
    &lt;span class="na"&gt;id-token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;                 &lt;span class="c1"&gt;# Tailscale workload identity federation (OIDC)
&lt;/span&gt;  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;QA_HOST&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_HOST }}&lt;/span&gt;
    &lt;span class="na"&gt;QA_SSH_PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_SSH_PORT }}&lt;/span&gt;
    &lt;span class="na"&gt;QA_SSH_USER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_SSH_USER }}&lt;/span&gt;
    &lt;span class="na"&gt;QA_APP_PATH&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_APP_PATH }}&lt;/span&gt;
    &lt;span class="na"&gt;QA_HTTP_PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_HTTP_PORT }}&lt;/span&gt;
    &lt;span class="na"&gt;QA_COMPOSE_PROJECT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_COMPOSE_PROJECT }}&lt;/span&gt;
    &lt;span class="na"&gt;DEPLOY_SHA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.sha }}&lt;/span&gt;
  &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v7&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;fetch-depth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;              &lt;span class="c1"&gt;# full history, so the push below is not shallow
&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Check QA settings&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;fail() { echo "::error::$1"; exit 1; }&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$QA_HOST"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^[A-Za-z0-9.-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="s"&gt;+$&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="s"&gt;fail "QA_HOST is missing or invalid"&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$QA_SSH_PORT"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^[0-9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;$&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="s"&gt;fail "QA_SSH_PORT is missing or invalid"&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$QA_SSH_USER"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^[a-z_&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;a-z0-9_-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;$&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="s"&gt;fail "QA_SSH_USER is missing or invalid"&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$QA_APP_PATH"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^/[A-Za-z0-9._/-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="s"&gt;+$&lt;/span&gt; &lt;span class="nv"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="s2"&gt;"$QA_APP_PATH"&lt;/span&gt; &lt;span class="kt"&gt;!=&lt;/span&gt; &lt;span class="nv"&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="s"&gt;fail "QA_APP_PATH is missing or invalid"&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$QA_HTTP_PORT"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^[0-9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;$&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="s"&gt;fail "QA_HTTP_PORT is missing or invalid"&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$QA_COMPOSE_PROJECT"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^[a-z0-9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;a-z0-9_-&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;$&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="s"&gt;fail "QA_COMPOSE_PROJECT is missing or invalid"&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$DEPLOY_SHA"&lt;/span&gt; &lt;span class="s"&gt;=~&lt;/span&gt; &lt;span class="s"&gt;^[0-9a-f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&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="s"&gt;fail "Unexpected commit SHA"&lt;/span&gt;

    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Join the tailnet&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tailscale/github-action@v4&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;oauth-client-id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.TS_OAUTH_CLIENT_ID }}&lt;/span&gt;
        &lt;span class="na"&gt;audience&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.TS_AUDIENCE }}&lt;/span&gt;
        &lt;span class="na"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;tag:ci&lt;/span&gt;
        &lt;span class="na"&gt;ping&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ vars.QA_HOST }}&lt;/span&gt;   &lt;span class="c1"&gt;# wait until the server can see the new node
&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up SSH&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;SSH_KEY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.QA_SSH_PRIVATE_KEY }}&lt;/span&gt;
        &lt;span class="na"&gt;KNOWN_HOSTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.QA_SSH_KNOWN_HOSTS }}&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;install -m 700 -d ~/.ssh&lt;/span&gt;
        &lt;span class="s"&gt;printf '%s\n' "$SSH_KEY" &amp;gt; ~/.ssh/qa_deploy&lt;/span&gt;
        &lt;span class="s"&gt;chmod 600 ~/.ssh/qa_deploy&lt;/span&gt;
        &lt;span class="s"&gt;printf '%s\n' "$KNOWN_HOSTS" &amp;gt; ~/.ssh/known_hosts&lt;/span&gt;

    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Push the commit to QA&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;GIT_SSH_COMMAND="ssh -i $HOME/.ssh/qa_deploy -p $QA_SSH_PORT -o StrictHostKeyChecking=yes -o BatchMode=yes" \&lt;/span&gt;
          &lt;span class="s"&gt;git push --force "$QA_SSH_USER@$QA_HOST:$QA_APP_PATH" "${DEPLOY_SHA}:refs/qa/deploy"&lt;/span&gt;

    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;deploy&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;ssh -i ~/.ssh/qa_deploy -p "$QA_SSH_PORT" -o StrictHostKeyChecking=yes -o BatchMode=yes \&lt;/span&gt;
          &lt;span class="s2"&gt;"$QA_SSH_USER@$QA_HOST"&lt;/span&gt; &lt;span class="s"&gt;\&lt;/span&gt;
          &lt;span class="s2"&gt;"bash -s -- '$QA_APP_PATH' '$DEPLOY_SHA' '$QA_HTTP_PORT' '$QA_COMPOSE_PROJECT'"&lt;/span&gt; &lt;span class="s"&gt;\&lt;/span&gt;
          &lt;span class="s"&gt;&amp;lt; scripts/deploy-qa.sh&lt;/span&gt;

    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Show QA logs on failure&lt;/span&gt;
      &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;failure() &amp;amp;&amp;amp; steps.deploy.outcome == 'failure'&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;ssh -i ~/.ssh/qa_deploy -p "$QA_SSH_PORT" -o StrictHostKeyChecking=yes -o BatchMode=yes \&lt;/span&gt;
          &lt;span class="s2"&gt;"$QA_SSH_USER@$QA_HOST"&lt;/span&gt; &lt;span class="s"&gt;\&lt;/span&gt;
          &lt;span class="s2"&gt;"cd '$QA_APP_PATH' &amp;amp;&amp;amp; docker compose -p '$QA_COMPOSE_PROJECT' logs --tail 100"&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Remove SSH key&lt;/span&gt;
      &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always()&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rm -rf ~/.ssh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;concurrency&lt;/code&gt; group means two merges close together never deploy at the same time. The second run waits for the first, then deploys its own, newer commit. The "Show QA logs" step only runs when the deploy step itself failed, so a failed deploy shows the container's last 100 log lines right in the job output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keep &lt;code&gt;${{ }}&lt;/code&gt; out of &lt;code&gt;run:&lt;/code&gt; blocks
&lt;/h3&gt;

&lt;p&gt;There isn't a single &lt;code&gt;${{ }}&lt;/code&gt; inside a &lt;code&gt;run:&lt;/code&gt; block. Every GitHub value goes into &lt;code&gt;env:&lt;/code&gt; first and is used as &lt;code&gt;"$VAR"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The reason: GitHub replaces &lt;code&gt;${{ }}&lt;/code&gt; by pasting the value into the script text &lt;strong&gt;before&lt;/strong&gt; the shell runs it. If a value contains a quote, a semicolon or &lt;code&gt;$(...)&lt;/code&gt;, it stops being data and becomes part of the script. Through &lt;code&gt;env:&lt;/code&gt;, the value reaches the shell as a variable, and quoting keeps it as one string.&lt;/p&gt;

&lt;p&gt;On top of that, every value is checked against a strict regex in the "Check QA settings" step. The deploy script runs the same checks again on the server. Being careful twice costs a few lines.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check host keys
&lt;/h3&gt;

&lt;p&gt;Every SSH call uses &lt;code&gt;StrictHostKeyChecking=yes&lt;/code&gt;, with the server's key in a &lt;code&gt;known_hosts&lt;/code&gt; secret. Plenty of examples online use &lt;code&gt;StrictHostKeyChecking=no&lt;/code&gt; to make the error go away. That turns off the one check that proves you're talking to your server.&lt;/p&gt;

&lt;p&gt;My production workflow had the same problem. I fixed it there afterwards.&lt;/p&gt;

&lt;h2&gt;
  
  
  The deploy script
&lt;/h2&gt;

&lt;p&gt;These are the key parts of &lt;code&gt;scripts/deploy-qa.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# Run on the QA server. The server hosts other apps: touch only &amp;lt;app_path&amp;gt; and &amp;lt;compose_project&amp;gt;.&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -euo pipefail
&lt;span class="nv"&gt;app_path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="nv"&gt;sha&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="nv"&gt;host_port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$3&lt;/span&gt; &lt;span class="nv"&gt;project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$4&lt;/span&gt;
&lt;span class="c"&gt;# ...same regex checks as the runner...&lt;/span&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"$app_path"&lt;/span&gt;

&lt;span class="c"&gt;# The workflow pushes the commit to refs/qa/deploy first; fetch origin only if it is still missing.&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; ! git cat-file -e &lt;span class="s2"&gt;"$sha^{commit}"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  git fetch --prune origin
  git cat-file -e &lt;span class="s2"&gt;"$sha^{commit}"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Commit $sha not found after fetch"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 67&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# Stop before touching anything if another project or container holds the port.&lt;/span&gt;
&lt;span class="nv"&gt;others&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;docker ps --filter &lt;span class="s2"&gt;"publish=$host_port"&lt;/span&gt; --format &lt;span class="s1"&gt;'{{.Label "com.docker.compose.project"}}|{{.Names}}'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
  awk -F&lt;span class="s1"&gt;'|'&lt;/span&gt; -v &lt;span class="nv"&gt;p&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$project"&lt;/span&gt; &lt;span class="s1"&gt;'$1 != p { print $2 " (project " ($1 == "" ? "none" : $1) ")" }'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -n &lt;span class="s2"&gt;"$others"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Port $host_port is already used by: $others"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="nb"&gt;exit&lt;/span&gt; 68
&lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# Build exactly the commit: drop local edits, keep an optional .env.&lt;/span&gt;
git reset --hard --quiet
git clean -fdx --quiet -e .env
git checkout --quiet --detach &lt;span class="s2"&gt;"$sha"&lt;/span&gt;

&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;HOST_PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$host_port&lt;/span&gt;
docker compose -p &lt;span class="s2"&gt;"$project"&lt;/span&gt; up -d --build --wait --wait-timeout 120 --remove-orphans
curl -fsS -o /dev/null &lt;span class="s2"&gt;"http://127.0.0.1:$host_port/"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"QA is up: $(git rev-parse --short HEAD) on port $host_port"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the port line in &lt;code&gt;compose.yaml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"${HOST_BIND:-0.0.0.0}:${HOST_PORT:-7500}:80"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With no env vars set, local &lt;code&gt;docker compose up&lt;/code&gt; still works and the site is on port 7500. The deploy script sets &lt;code&gt;HOST_PORT&lt;/code&gt; from the Environment.&lt;/p&gt;

&lt;p&gt;A few things about the script:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It only touches its own folder and its own compose project.&lt;/strong&gt; &lt;code&gt;git reset&lt;/code&gt; and &lt;code&gt;git clean&lt;/code&gt; run inside &lt;code&gt;/srv/apps/my-site&lt;/code&gt;. Compose runs with &lt;code&gt;-p "$project"&lt;/code&gt;, so &lt;code&gt;--remove-orphans&lt;/code&gt; can only remove orphans of this project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The port check runs before anything changes.&lt;/strong&gt; If any container outside this project already publishes the port, the script stops with exit code 68 and names the container. More on why this exists below.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It builds exactly the commit.&lt;/strong&gt; Local edits are dropped, untracked files are removed, and an optional &lt;code&gt;.env&lt;/code&gt; survives. The checkout is detached at the SHA, not at a branch that might have moved.
### Failing safely with &lt;code&gt;--wait&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;--wait&lt;/code&gt; makes Compose wait until the container's &lt;code&gt;HEALTHCHECK&lt;/code&gt; (defined in the image) reports healthy. If the new image fails to build, or the new container never becomes healthy within 120 seconds, the command fails and the job fails.&lt;/p&gt;

&lt;p&gt;The important part is what happens to the running site. Compose only replaces the old container once the new image is built. A broken build never gets as far as stopping the old container, so &lt;strong&gt;the old container keeps serving&lt;/strong&gt;. The &lt;code&gt;curl&lt;/code&gt; at the end is a last check that something actually answers on the port.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing it before the first real deploy
&lt;/h3&gt;

&lt;p&gt;I tested the script locally before letting CI run it against the server:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bad inputs were refused.&lt;/strong&gt; &lt;code&gt;'/tmp/x; echo pwned'&lt;/code&gt; as the path, &lt;code&gt;HEAD&lt;/code&gt; as the SHA, and port &lt;code&gt;80&lt;/code&gt; all failed the checks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A deploy to a throwaway clone with a local edit&lt;/strong&gt; dropped the edit but kept &lt;code&gt;.env&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A commit with &lt;code&gt;RUN false&lt;/code&gt; in the Dockerfile&lt;/strong&gt; made the deploy fail, while the old container kept answering 200.
## What went wrong on the first real deploys&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The plan had assumed four things that turned out to be wrong. Each failure stopped safely, and the running QA container was never touched. Here they are in the order I hit them.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;QA_HOST is missing or invalid&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;I had forgotten to set one variable. The "Check QA settings" step stopped the job before it even joined the tailnet.&lt;/p&gt;

&lt;p&gt;This is why the validation step comes first. The job failed in 6 seconds with a message that said exactly what was wrong. Without it, the same mistake would have surfaced later as a confusing SSH or Tailscale error with an empty hostname somewhere in it.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. A fingerprint is not a &lt;code&gt;known_hosts&lt;/code&gt; line
&lt;/h3&gt;

&lt;p&gt;The next run got onto the tailnet and failed at SSH:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No ED25519 host key is known for qa-box.tailXXXX.ts.net and you have requested strict checking.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;QA_SSH_KNOWN_HOSTS&lt;/code&gt; secret didn't hold a key line. I had pasted the key's &lt;strong&gt;fingerprint&lt;/strong&gt;, which looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;256 SHA256:... (ED25519)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;known_hosts&lt;/code&gt; wants the full public key line, the kind &lt;code&gt;ssh-keyscan&lt;/code&gt; prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;qa-box.tailXXXX.ts.net ssh-ed25519 AAAA...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's a second catch. The name at the start of the line must match &lt;code&gt;QA_HOST&lt;/code&gt; exactly. With MagicDNS, &lt;code&gt;qa-box&lt;/code&gt; and &lt;code&gt;qa-box.tailXXXX.ts.net&lt;/code&gt; both resolve to the same machine, but to SSH they're different entries. If &lt;code&gt;QA_HOST&lt;/code&gt; is the full name, the line must start with the full name.&lt;/p&gt;

&lt;p&gt;The two key pairs are easy to mix up:&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;Server's ID card&lt;/th&gt;
&lt;th&gt;Runner's door key&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Proves&lt;/td&gt;
&lt;td&gt;"I really am the server"&lt;/td&gt;
&lt;td&gt;"I'm allowed in"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Comes from&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;/etc/ssh/ssh_host_ed25519_key.pub&lt;/code&gt; on the server&lt;/td&gt;
&lt;td&gt;a new key pair made only for deploys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GitHub secret&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;QA_SSH_KNOWN_HOSTS&lt;/code&gt; (public line)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;QA_SSH_PRIVATE_KEY&lt;/code&gt; (private half)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Other half&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;server's &lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To build the &lt;code&gt;known_hosts&lt;/code&gt; line, run &lt;code&gt;ssh-keyscan&lt;/code&gt; from a machine on the tailnet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keyscan -t ed25519 qa-box.tailXXXX.ts.net
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then check that the fingerprint matches &lt;strong&gt;on the server itself&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# On the server&lt;/span&gt;
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ssh-keyscan&lt;/code&gt; trusts whatever answers on that name and port. Comparing against the file on the server is what makes the line trustworthy.&lt;/p&gt;

&lt;p&gt;For the door key, make a new key pair just for deploys. Don't reuse your laptop's personal key. If the deploy key ever leaks, you remove one line from the deploy user's &lt;code&gt;authorized_keys&lt;/code&gt; and nothing else changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Three problems I found by looking at the server
&lt;/h3&gt;

&lt;p&gt;Before the next attempt, I SSHed into the server and looked at how things were actually wired. I found three more problems, all of the "worked on my machine" kind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;cloudflared&lt;/code&gt; runs in a container, not on the host.&lt;/strong&gt; My plan bound QA to &lt;code&gt;127.0.0.1&lt;/code&gt;, so it would only be reachable through the tunnel. But &lt;code&gt;cloudflared&lt;/code&gt; on this box runs as a Docker container in its own network. Inside a container, &lt;code&gt;127.0.0.1&lt;/code&gt; is the container itself, not the host. The tunnel would have got a 502.&lt;/p&gt;

&lt;p&gt;Every other app on the server listens on &lt;code&gt;0.0.0.0:&amp;lt;port&amp;gt;&lt;/code&gt;, and the tunnel reaches them through the host's LAN IP. I switched QA to the same pattern: it binds to &lt;code&gt;0.0.0.0:7500&lt;/code&gt;, and the tunnel's route for the QA hostname points at &lt;code&gt;http://192.168.1.50:7500&lt;/code&gt;. The trade-off is that QA is also reachable on my LAN. For a home network I accepted that. That's also why the bind address is a variable (&lt;code&gt;HOST_BIND&lt;/code&gt;) in &lt;code&gt;compose.yaml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The port was already taken.&lt;/strong&gt; Weeks earlier I had started a copy of the same site by hand, from the same folder, under a different compose project name. It was sitting on the port QA wanted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The server couldn't get the commit.&lt;/strong&gt; The plan had the server's clone run &lt;code&gt;git fetch&lt;/code&gt; to get the commit being deployed. But the GitHub repo is private, and the server has no GitHub credentials. A &lt;code&gt;git fetch&lt;/code&gt; would never have found the commit.&lt;/p&gt;

&lt;p&gt;I didn't want to add a GitHub key to the server just for this. The runner already has an SSH connection to the server and the full repo. So it now pushes the exact commit to a private ref before deploying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push --force &lt;span class="s2"&gt;"$QA_SSH_USER@$QA_HOST:$QA_APP_PATH"&lt;/span&gt; &lt;span class="s2"&gt;"${DEPLOY_SHA}:refs/qa/deploy"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ref lives under &lt;code&gt;refs/qa/&lt;/code&gt;, so it never shows up as a branch and never conflicts with your real branches. It needs &lt;code&gt;fetch-depth: 0&lt;/code&gt; on checkout, because git won't push from a shallow clone into a repository that doesn't already have the missing history. The script still falls back to &lt;code&gt;git fetch origin&lt;/code&gt; if the commit is missing. The server never needs credentials for GitHub.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A small zsh trap while testing locally.&lt;/strong&gt; I first wrote the refspec as &lt;code&gt;"$SHA:refs/qa/deploy"&lt;/code&gt;. In zsh, that lost its &lt;code&gt;:r&lt;/code&gt;. &lt;code&gt;:r&lt;/code&gt; is a zsh history modifier ("remove extension"), so zsh read &lt;code&gt;$SHA:r&lt;/code&gt; as one expansion and left &lt;code&gt;efs/qa/deploy&lt;/code&gt; behind it. Bash on the runner doesn't do this, but &lt;code&gt;"${SHA}:refs/qa/deploy"&lt;/code&gt; is safe in both. Braces around variables next to a colon are a good habit anyway.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;code&gt;Bind for 0.0.0.0:7500 failed: port is already allocated&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;After the first good deploy, I renamed the compose project in the GitHub Environment and re-ran the job. That was a mistake.&lt;/p&gt;

&lt;p&gt;Compose doesn't know a project was renamed. It saw a brand-new project and tried to start a second container on the same port, next to the old one. The site stayed up, because the old container kept serving, but the deploy failed after a full image build.&lt;/p&gt;

&lt;p&gt;The fix was to put the old name back and remove the half-made project (&lt;code&gt;docker compose -p &amp;lt;new-name&amp;gt; down&lt;/code&gt;). It also led to the early port check in the script. Now, if anything outside the current project holds the port, the script stops before building and says which container is in the way. It refuses in 0 seconds instead of failing after a build.&lt;/p&gt;

&lt;p&gt;The lesson for me: the compose project name is part of the app's identity on the server, like its folder and its port. Changing it is a migration, not a setting tweak.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. "This workflow run cannot be retried"
&lt;/h3&gt;

&lt;p&gt;GitHub showed me this message during the fixes. It's also worth knowing what a re-run does: it uses the &lt;strong&gt;workflow file from the original commit&lt;/strong&gt;, not the latest one. So after fixing the workflow itself, re-running an old failed run tests the old workflow.&lt;/p&gt;

&lt;p&gt;What you want is a new run on the latest commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gh workflow run CI --ref main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Look at the server before writing the plan.&lt;/strong&gt; &lt;code&gt;docker ps&lt;/code&gt;, the networks containers are on, the ports they publish, and where &lt;code&gt;cloudflared&lt;/code&gt; runs. Three of my four wrong assumptions would have shown up there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat isolation on a shared server as my job.&lt;/strong&gt; One folder, one compose project name, one port per app. And never a global &lt;code&gt;docker system prune&lt;/code&gt; on a box with 40 other containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Restrict the CI node on the tailnet from day one.&lt;/strong&gt; Tag it, grant one host and one port, and write policy &lt;code&gt;tests&lt;/code&gt; that prove it. Then check the tailnet doesn't still have the default allow-all rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remember that GitHub Environments change the OIDC subject.&lt;/strong&gt; With &lt;code&gt;environment: qa&lt;/code&gt;, the subject ends in &lt;code&gt;environment:qa&lt;/code&gt;, not the branch ref. The trust credential must match exactly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put the key line in &lt;code&gt;known_hosts&lt;/code&gt;, not the fingerprint.&lt;/strong&gt; And make the hostname at the start of the line match &lt;code&gt;QA_HOST&lt;/code&gt; exactly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate every input before it reaches a shell, and keep &lt;code&gt;${{ }}&lt;/code&gt; out of &lt;code&gt;run:&lt;/code&gt;.&lt;/strong&gt; Validating first also gives you fast, readable failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design deploys to fail safely.&lt;/strong&gt; Through every failure in this post, the old QA container kept serving. That's what made it fine to learn by breaking things.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>tailscale</category>
      <category>devops</category>
      <category>docker</category>
    </item>
    <item>
      <title>nginx2root on Ubuntu: how I checked and mitigated it without upgrading nginx</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Sat, 21 Nov 2026 00:26:54 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/nginx2root-on-ubuntu-how-i-checked-and-mitigated-it-without-upgrading-nginx-58de</link>
      <guid>https://dev.to/darkmavis1980/nginx2root-on-ubuntu-how-i-checked-and-mitigated-it-without-upgrading-nginx-58de</guid>
      <description>&lt;p&gt;The nginx2root bugs went public on 18 November 2026. Like most people I thought "fine, I'll just upgrade nginx." Then I looked at my server.&lt;/p&gt;

&lt;p&gt;It runs Ubuntu's packaged nginx, and on Ubuntu "just upgrade" wasn't an option yet. This post covers what I did instead on 20 November. I checked whether I was exposed, turned off the vulnerable features, nearly panicked over a Cloudflare false alarm, and blocked the kernel module that turns a compromised worker into root.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update, 21 November 2026: the fix is out.&lt;/strong&gt; Ubuntu published &lt;a href="https://ubuntu.com/security/notices/USN-9159-1" rel="noopener noreferrer"&gt;USN-9159-1&lt;/a&gt; on 19 November, covering all five nginx2root CVEs. On 26.04 LTS the fixed version is &lt;code&gt;1.28.3-2ubuntu1.13&lt;/code&gt;. If that's available to you, skip to the update section: upgrade, &lt;strong&gt;restart&lt;/strong&gt;, and re-enable HTTP/2. The mitigation steps are still useful if you're on a distro without a fix yet, can't upgrade straight away, or just want to know what was exposed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What nginx2root is (the short version)
&lt;/h2&gt;

&lt;p&gt;My primary source is Cloudflare's write-up: &lt;strong&gt;&lt;a href="https://blog.cloudflare.com/nginx2root/" rel="noopener noreferrer"&gt;nginx2root&lt;/a&gt;&lt;/strong&gt;. Read it. The summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;nginx2root is five memory-corruption bugs in nginx: four use-after-free bugs and one integer overflow in HTTP/2 flow control. An unauthenticated attacker can crash a worker or, according to Xint's research, run code as the worker.&lt;/li&gt;
&lt;li&gt;Xint also found a separate use-after-free in the Linux kernel's SCTP code that can turn that foothold into root.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;CVE&lt;/th&gt;
&lt;th&gt;Affected configuration&lt;/th&gt;
&lt;th&gt;First affected release&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CVE-2026-123007&lt;/td&gt;
&lt;td&gt;HTTP/2 enabled&lt;/td&gt;
&lt;td&gt;1.9.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CVE-2026-123633&lt;/td&gt;
&lt;td&gt;HTTP/2 with &lt;code&gt;proxy_pass&lt;/code&gt;, worker low on &lt;code&gt;worker_connections&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;1.19.7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CVE-2026-121810&lt;/td&gt;
&lt;td&gt;HTTP/3 (QUIC) with &lt;code&gt;proxy_pass&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;1.25.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CVE-2026-123634&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;resolver&lt;/code&gt; + &lt;code&gt;proxy_pass&lt;/code&gt; with variables + a &lt;code&gt;mirror&lt;/code&gt; or SSI subrequest&lt;/td&gt;
&lt;td&gt;0.6.18&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CVE-2026-113425&lt;/td&gt;
&lt;td&gt;Mail proxy with &lt;code&gt;proxy on&lt;/code&gt; and &lt;code&gt;auth_http&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;0.5.33&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Fixed upstream releases are nginx Open Source &lt;strong&gt;1.30.7&lt;/strong&gt; or &lt;strong&gt;1.31.9&lt;/strong&gt;, and nginx Plus &lt;strong&gt;37.0.8.1&lt;/strong&gt; or &lt;strong&gt;37.3.0.1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The one that matters for most of us is the first row. CVE-2026-123007 needs nothing more than HTTP/2 being on. If you serve HTTPS from nginx and switched on HTTP/2 years ago because every performance guide said to, you're in scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I couldn't "just upgrade"
&lt;/h2&gt;

&lt;p&gt;My server runs the nginx package from Ubuntu 26.04 LTS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ nginx -v
nginx version: nginx/1.28.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That doesn't tell you much, for two reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Ubuntu backports security fixes without changing the upstream version number.&lt;/strong&gt; A patched Ubuntu nginx will still say 1.28.3. &lt;code&gt;nginx -v&lt;/code&gt; can't tell you whether you're patched; the package revision and changelog can.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There was no fixed package at the time.&lt;/strong&gt; As of 18 November 2026, Canonical's and Debian's security trackers listed every supported release as vulnerable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I could have switched to the nginx.org packages, but that's a bigger change than I wanted to make in a hurry on a server that otherwise just works. So the plan was to mitigate now and upgrade as soon as Ubuntu shipped a fix.&lt;/p&gt;

&lt;p&gt;Here's how to check where your own server stands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Refresh package lists, then compare "Installed" with "Candidate"&lt;/span&gt;
sudo apt update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-cache policy nginx

&lt;span class="c"&gt;# Show the exact installed revisions of every nginx package&lt;/span&gt;
dpkg-query -W &lt;span class="s1"&gt;'nginx*'&lt;/span&gt;

&lt;span class="c"&gt;# Does the *candidate* version's changelog mention the fix? (see the warning below)&lt;/span&gt;
apt changelog nginx 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'CVE-2026-12(3007|3633)'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Careful with that last one.&lt;/strong&gt; &lt;code&gt;apt changelog&lt;/code&gt; downloads the changelog of the version apt &lt;em&gt;would&lt;/em&gt; install, not the one you have. A match means a fixed package is available, not that you're running it. I nearly misread it this way myself once the fix shipped. To know whether you're patched, compare the installed version with the fixed version listed in the Ubuntu notice for your release:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 26.04 LTS example: prints "patched" only if the installed nginx is the fixed version or newer&lt;/span&gt;
dpkg --compare-versions &lt;span class="s2"&gt;"$(dpkg-query -W -f='${Version}' nginx)"&lt;/span&gt; ge 1.28.3-2ubuntu1.13 \
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; patched &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"NOT patched"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I did this on 20 November I didn't have a fixed package yet, so I moved on to mitigation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Find where HTTP/2 and HTTP/3 are enabled
&lt;/h2&gt;

&lt;p&gt;Don't just grep &lt;code&gt;/etc/nginx&lt;/code&gt;. &lt;code&gt;nginx -T&lt;/code&gt; dumps the full configuration nginx actually loads, with every &lt;code&gt;include&lt;/code&gt;d file, so you see the real picture. This awk prints each match along with the file it came from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# List every listen line with http2/quic, and every "http2 on" / "http3 on", with its source file&lt;/span&gt;
sudo nginx -T 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;|&lt;/span&gt; awk &lt;span class="s1"&gt;'
  /^# configuration file/ { f = $4 }
  /^[[:space:]]*(listen[^#;]*[[:space:]](http2|quic)|http[23][[:space:]]+on)/ { print f, $0 }'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quicker alternative that greps the files directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Find HTTP/2 enabled on listen lines or via the http2 directive&lt;/span&gt;
grep -rnE &lt;span class="s1"&gt;'^\s*(listen\b[^#;]*\bhttp2\b|http2\s+on\b)'&lt;/span&gt; /etc/nginx/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTTP/2 can be switched on in two ways, so you need to look for both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;listen 443 ssl http2;&lt;/code&gt; is the older style. It has been deprecated since 1.25.1, but &lt;strong&gt;it still works on 1.28&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http2 on;&lt;/code&gt; is the newer directive (1.25.1+). It's off by default, so it only matters if you set it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2: Turn HTTP/2 off
&lt;/h2&gt;

&lt;p&gt;I did two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Removed &lt;code&gt;http2&lt;/code&gt; from every &lt;code&gt;listen&lt;/code&gt; line.&lt;/strong&gt; A &lt;code&gt;listen ... http2&lt;/code&gt; still turns HTTP/2 on even if &lt;code&gt;http2 off;&lt;/code&gt; is set elsewhere, so this part isn't optional.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Added &lt;code&gt;http2 off;&lt;/code&gt; to the &lt;code&gt;http { }&lt;/code&gt; block&lt;/strong&gt; in the main &lt;code&gt;nginx.conf&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more thing: a &lt;code&gt;server&lt;/code&gt; block with its own &lt;code&gt;http2 on;&lt;/code&gt; overrides the &lt;code&gt;http&lt;/code&gt;-level setting. If step 1 found any of those, change them to &lt;code&gt;off&lt;/code&gt; or delete them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Back up the whole config first&lt;/span&gt;
sudo cp -a /etc/nginx /etc/nginx.bak-&lt;span class="nv"&gt;$(&lt;/span&gt;date +%F&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Strip " http2" from every listen line that has it&lt;/span&gt;
sudo grep -rlE &lt;span class="s1"&gt;'^\s*listen\b[^#;]*\bhttp2\b'&lt;/span&gt; /etc/nginx/ \
  &lt;span class="o"&gt;|&lt;/span&gt; xargs -r sudo sed -i -E &lt;span class="s1"&gt;'/^\s*listen\b/ s/\s+http2\b//'&lt;/span&gt;

&lt;span class="c"&gt;# Test the config, then restart (not reload) so existing HTTP/2 connections close&lt;/span&gt;
sudo nginx -t &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run the step 1 command again. It should print nothing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Verify it (and the Cloudflare false alarm)
&lt;/h2&gt;

&lt;p&gt;My first check went against my public hostname:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ curl -s -o /dev/null --http2-prior-knowledge -w &lt;span class="s1"&gt;'%{http_version}\n'&lt;/span&gt; https://www.alessiomichelini.com/
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still HTTP/2? I had a brief panic. Then I checked who was actually answering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ curl -sI https://www.alessiomichelini.com/ &lt;span class="o"&gt;|&lt;/span&gt; grep -iE &lt;span class="s1"&gt;'^(server|cf-ray)'&lt;/span&gt;
server: cloudflare
cf-ray: a5db45a6cf2ffea0-AMS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The site is proxied by Cloudflare, so that &lt;code&gt;2&lt;/code&gt; came from &lt;strong&gt;Cloudflare's edge&lt;/strong&gt;, not from my nginx. Visitors talk HTTP/2 to Cloudflare, and Cloudflare opens its own connection to my origin.&lt;/p&gt;

&lt;p&gt;To test the origin itself, run curl on the server with &lt;code&gt;--resolve&lt;/code&gt;. That sends the request to localhost but keeps the right hostname for SNI and the certificate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Ask the local nginx directly, offering HTTP/2 via ALPN&lt;/span&gt;
$ curl -sk -o /dev/null --http2 -w &lt;span class="s1"&gt;'%{http_version}\n'&lt;/span&gt; \
    --resolve www.alessiomichelini.com:443:127.0.0.1 https://www.alessiomichelini.com/
1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The origin only speaks HTTP/1.1 now, so it's mitigated, and visitors still get HTTP/2 from Cloudflare.&lt;/p&gt;

&lt;p&gt;Being behind Cloudflare is a real layer of protection here, with caveats. The Cloudflare post explains that the HTTP/2 and HTTP/3 attacks don't reach nginx when your origin is only reachable through Cloudflare (Tunnel, IP allowlisting, or Authenticated Origin Pulls). It still recommends patching. It also says the proxy doesn't protect you from the mail proxy bug or the resolver bug, or on DNS-only (grey-cloud) hostnames. And if anyone can connect to your origin IP directly, none of that protection applies.&lt;/p&gt;

&lt;p&gt;So it's worth checking whether your origin answers directly. Run this from &lt;strong&gt;outside&lt;/strong&gt; your network:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Hit the origin IP directly, bypassing Cloudflare&lt;/span&gt;
curl -sk -o /dev/null -m 5 -w &lt;span class="s1"&gt;'%{http_code}\n'&lt;/span&gt; \
  --resolve www.example.com:443:YOUR_SERVER_PUBLIC_IP https://www.example.com/
&lt;span class="c"&gt;# timeout / 000 = only reachable via Cloudflare&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Check for the bugs that don't need HTTP/2
&lt;/h2&gt;

&lt;p&gt;Turning off HTTP/2 covers CVE-2026-123007 and CVE-2026-123633. The other three depend on other features, so look for those too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# HTTP/3 (QUIC) listeners or "http3 on" (CVE-2026-121810)&lt;/span&gt;
sudo nginx -T 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;|&lt;/span&gt; grep -nE &lt;span class="s1"&gt;'^\s*(listen\b[^#;]*\bquic\b|http3\s+on\b)'&lt;/span&gt;

&lt;span class="c"&gt;# Ingredients for CVE-2026-123634: resolver, mirror, SSI&lt;/span&gt;
sudo nginx -T 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;|&lt;/span&gt; grep -nE &lt;span class="s1"&gt;'^\s*(resolver|mirror|ssi on)\b'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you find any, Cloudflare's suggested mitigations are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP/3:&lt;/strong&gt; remove the QUIC listeners or set &lt;code&gt;http3 off;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;resolver + variable &lt;code&gt;proxy_pass&lt;/code&gt; + &lt;code&gt;mirror&lt;/code&gt;/SSI:&lt;/strong&gt; point &lt;code&gt;proxy_pass&lt;/code&gt; at fixed upstream groups.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mail proxy:&lt;/strong&gt; bind mail listeners to trusted networks. This only matters if you actually have a &lt;code&gt;mail { }&lt;/code&gt; block.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither command found anything on my server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Block the SCTP kernel module (the step to root)
&lt;/h2&gt;

&lt;p&gt;This is the step people skip, and it's cheap. The nginx bugs give an attacker code execution as the nginx worker. The SCTP kernel bug is what turns that into root. If you don't use SCTP (and almost nobody running a web server does), stop the module from loading.&lt;/p&gt;

&lt;p&gt;The rule has to go in a file under &lt;code&gt;/etc/modprobe.d/&lt;/code&gt; whose name ends in &lt;code&gt;.conf&lt;/code&gt; (any name works):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Make any attempt to load sctp run /bin/false instead&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'install sctp /bin/false'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sudo tee /etc/modprobe.d/disable-sctp.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then verify it. Here's my real output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ modprobe -n -v sctp
insmod /lib/modules/7.0.0-44-generic/kernel/net/ipv4/udp_tunnel.ko.zst
insmod /lib/modules/7.0.0-44-generic/kernel/net/ipv6/ip6_udp_tunnel.ko.zst
install /bin/false
$ lsmod &lt;span class="o"&gt;|&lt;/span&gt; grep sctp
$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to read that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;install /bin/false&lt;/code&gt; means the rule is active.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;insmod&lt;/code&gt; lines are SCTP's helper dependencies. &lt;code&gt;-n&lt;/code&gt; is a &lt;strong&gt;dry run&lt;/strong&gt;, so nothing was actually loaded, and those lines are harmless.&lt;/li&gt;
&lt;li&gt;Empty &lt;code&gt;lsmod&lt;/code&gt; output means SCTP isn't loaded right now, so no reboot is needed. If it is loaded, the rule won't unload it: run &lt;code&gt;sudo modprobe -r sctp&lt;/code&gt; or reboot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cloudflare's post also notes that this stopgap doesn't help if SCTP is built into the kernel instead of being a module. The real fix is your distribution's kernel update once it ships.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gotchas worth knowing
&lt;/h2&gt;

&lt;p&gt;These are the things that would have caught me out:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;nginx -v&lt;/code&gt; lies (sort of) on Ubuntu.&lt;/strong&gt; Distro backports keep the upstream version number. Check the package revision, not the version string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;apt changelog&lt;/code&gt; shows the candidate, not what's installed.&lt;/strong&gt; A CVE match there means a fix is &lt;em&gt;available&lt;/em&gt;. Compare &lt;code&gt;apt-cache policy nginx&lt;/code&gt; (or &lt;code&gt;dpkg --compare-versions&lt;/code&gt;) against the fixed version in the Ubuntu notice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;listen ... http2&lt;/code&gt; still enables HTTP/2 on 1.28,&lt;/strong&gt; even though it's deprecated. Adding &lt;code&gt;http2 off;&lt;/code&gt; isn't enough; remove it from the &lt;code&gt;listen&lt;/code&gt; lines too.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reload isn't enough.&lt;/strong&gt; Restart, so existing HTTP/2 connections are closed. The same goes for the upgrade: a running nginx keeps the old binary until it restarts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behind Cloudflare, a public curl tests the edge, not your origin.&lt;/strong&gt; Use &lt;code&gt;--resolve host:443:127.0.0.1&lt;/code&gt; on the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;--http2-prior-knowledge&lt;/code&gt; is meant for plain &lt;code&gt;http://&lt;/code&gt;.&lt;/strong&gt; For &lt;code&gt;https://&lt;/code&gt; use &lt;code&gt;--http2&lt;/code&gt;, which negotiates via ALPN.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloudflare only protects you if the origin isn't directly reachable.&lt;/strong&gt; That means Tunnel, an IP allowlist, or Authenticated Origin Pulls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The SCTP block is cheap insurance&lt;/strong&gt; against the nginx-worker-to-root chain. One line, no reboot if the module isn't loaded.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Cloudflare post also suggests watching your nginx error log for &lt;code&gt;worker process ... exited on signal 11&lt;/code&gt; while you're unpatched, since crashing workers are the first symptom of these bugs being poked at.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update: the fix is out, now patch properly
&lt;/h2&gt;

&lt;p&gt;Ubuntu published &lt;a href="https://ubuntu.com/security/notices/USN-9159-1" rel="noopener noreferrer"&gt;USN-9159-1&lt;/a&gt; on 19 November 2026. It fixes all five nginx2root CVEs (plus a few unrelated nginx ones). On Ubuntu 26.04 LTS the fixed version is &lt;code&gt;1.28.3-2ubuntu1.13&lt;/code&gt; for &lt;code&gt;nginx&lt;/code&gt;, &lt;code&gt;nginx-core&lt;/code&gt;, &lt;code&gt;nginx-full&lt;/code&gt;, &lt;code&gt;nginx-light&lt;/code&gt; and &lt;code&gt;nginx-extras&lt;/code&gt;. Other releases have their own version numbers, so look yours up in the notice.&lt;/p&gt;

&lt;p&gt;The mitigation was always a stopgap, so here's how to finish the job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Upgrade the nginx packages (skip if unattended-upgrades already did)&lt;/span&gt;
sudo apt update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; sudo apt install --only-upgrade nginx nginx-common nginx-core

&lt;span class="c"&gt;# 2. Confirm the installed version is the fixed one (26.04 example)&lt;/span&gt;
dpkg --compare-versions &lt;span class="s2"&gt;"$(dpkg-query -W -f='${Version}' nginx)"&lt;/span&gt; ge 1.28.3-2ubuntu1.13 \
  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; patched &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"NOT patched"&lt;/span&gt;

&lt;span class="c"&gt;# 3. Restart so the running nginx actually uses the patched binary&lt;/span&gt;
sudo nginx -t &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3 matters even if unattended-upgrades installed the package for you. The Ubuntu notice only says a standard system update makes the necessary changes, so don't assume nginx was restarted; restart it yourself.&lt;/p&gt;

&lt;p&gt;Once you're patched, turn HTTP/2 back on. Since I'd already stripped &lt;code&gt;http2&lt;/code&gt; from every &lt;code&gt;listen&lt;/code&gt; line, the cleanest way was to flip the one directive in the &lt;code&gt;http { }&lt;/code&gt; block of &lt;code&gt;nginx.conf&lt;/code&gt; from &lt;code&gt;http2 off;&lt;/code&gt; to &lt;code&gt;http2 on;&lt;/code&gt;, then restart and rerun the origin check from the verify step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Should now print 2 again: the origin speaks HTTP/2&lt;/span&gt;
curl -sk -o /dev/null --http2 -w &lt;span class="s1"&gt;'%{http_version}\n'&lt;/span&gt; \
  --resolve www.alessiomichelini.com:443:127.0.0.1 https://www.alessiomichelini.com/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Keep the SCTP block.&lt;/strong&gt; It works around the &lt;em&gt;kernel&lt;/em&gt; bug, which an nginx update doesn't touch. Leave &lt;code&gt;/etc/modprobe.d/disable-sctp.conf&lt;/code&gt; in place until you've rebooted into a kernel that fixes it (and if you never use SCTP, there's no harm in keeping it for good).&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;p&gt;Copy this into your notes or ticket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If a fixed package is available:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`apt-cache policy nginx`&lt;/span&gt;: Installed = the fixed version for your release (26.04: 1.28.3-2ubuntu1.13)
&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`nginx -t &amp;amp;&amp;amp; systemctl restart nginx`&lt;/span&gt; after the upgrade
&lt;span class="k"&gt;- &lt;/span&gt;[ ] HTTP/2 re-enabled if you'd turned it off; origin test with &lt;span class="sb"&gt;`--resolve ...127.0.0.1`&lt;/span&gt; prints &lt;span class="sb"&gt;`2`&lt;/span&gt;
&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`/etc/modprobe.d/disable-sctp.conf`&lt;/span&gt; in place until a fixed kernel is installed and booted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;If you can't patch yet:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`nginx -T`&lt;/span&gt; grep shows no &lt;span class="sb"&gt;`http2`&lt;/span&gt; / &lt;span class="sb"&gt;`quic`&lt;/span&gt; / &lt;span class="sb"&gt;`http2 on`&lt;/span&gt; / &lt;span class="sb"&gt;`http3 on`&lt;/span&gt;
&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`http2 off;`&lt;/span&gt; in &lt;span class="sb"&gt;`http { }`&lt;/span&gt;, &lt;span class="sb"&gt;`http2`&lt;/span&gt; removed from all &lt;span class="sb"&gt;`listen`&lt;/span&gt; lines
&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`nginx -t &amp;amp;&amp;amp; systemctl restart nginx`&lt;/span&gt;
&lt;span class="k"&gt;- &lt;/span&gt;[ ] Origin test with &lt;span class="sb"&gt;`--resolve ...127.0.0.1`&lt;/span&gt; prints &lt;span class="sb"&gt;`1.1`&lt;/span&gt;
&lt;span class="k"&gt;- &lt;/span&gt;[ ] Origin not reachable directly (if behind a CDN)
&lt;span class="k"&gt;- &lt;/span&gt;[ ] No risky &lt;span class="sb"&gt;`resolver`&lt;/span&gt; + variable &lt;span class="sb"&gt;`proxy_pass`&lt;/span&gt; + &lt;span class="sb"&gt;`mirror`&lt;/span&gt;/SSI combo
&lt;span class="k"&gt;- &lt;/span&gt;[ ] &lt;span class="sb"&gt;`/etc/modprobe.d/disable-sctp.conf`&lt;/span&gt; in place; &lt;span class="sb"&gt;`lsmod | grep sctp`&lt;/span&gt; empty
&lt;span class="k"&gt;- &lt;/span&gt;[ ] Check the installed version (not &lt;span class="sb"&gt;`apt changelog`&lt;/span&gt;) against your distro's notice, then upgrade, restart and re-enable HTTP/2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're on a distro nginx that still has no fixed package, I hope the mitigation steps save you some time. If you're on Ubuntu, upgrade and restart today.&lt;/p&gt;

</description>
      <category>nginx</category>
      <category>security</category>
      <category>linux</category>
      <category>devops</category>
    </item>
    <item>
      <title>You should stop using high effort for everything in Claude Code</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Thu, 29 Oct 2026 14:34:03 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/you-should-stop-using-high-effort-for-everything-in-claude-code-11j2</link>
      <guid>https://dev.to/darkmavis1980/you-should-stop-using-high-effort-for-everything-in-claude-code-11j2</guid>
      <description>&lt;p&gt;I've noticed something among developers I know who use Claude Code: a lot of them set the effort level to &lt;code&gt;high&lt;/code&gt;, &lt;code&gt;xhigh&lt;/code&gt;, or even &lt;code&gt;max&lt;/code&gt;, and just leave it there, forever.&lt;/p&gt;

&lt;p&gt;The reasoning is understandable: "more effort means better code, right?".&lt;/p&gt;

&lt;p&gt;Well, not exactly.&lt;/p&gt;

&lt;p&gt;In this article I want to share the workflow I use when I build a new feature, or even an entire new project, where I spend a lot of effort (and tokens) when it matters, and very little when it doesn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is effort anyway?
&lt;/h2&gt;

&lt;p&gt;If you haven't played with it yet, effort is a setting in Claude Code that tells the model roughly how much work you want it to put into a task. You can change it at any time with the &lt;code&gt;/effort&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/effort low
/effort medium
/effort high
/effort xhigh
/effort max
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, &lt;code&gt;high&lt;/code&gt; is not the top of the scale! With Opus 5.5 you also get &lt;code&gt;xhigh&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt;, and from what I see these are the levels a lot of people actually use, as "if &lt;code&gt;high&lt;/code&gt; is good, &lt;code&gt;max&lt;/code&gt; must be better".&lt;/p&gt;

&lt;p&gt;But every step up costs more. Taking &lt;code&gt;medium&lt;/code&gt; (the default) as the baseline, this is roughly how many tokens each level consumes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Effort&lt;/th&gt;
&lt;th&gt;Tokens compared to &lt;code&gt;medium&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;low&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;~0.7x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;medium&lt;/code&gt; (default)&lt;/td&gt;
&lt;td&gt;1x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;high&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;~1.5x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;xhigh&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;~2.5x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;max&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;~5.2x&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So if you leave Claude Code on &lt;code&gt;max&lt;/code&gt; for everything, you are spending about five times the tokens of the default, and more than seven times what the same task would cost on &lt;code&gt;low&lt;/code&gt;. And the extra time adds up just as quickly.&lt;/p&gt;

&lt;p&gt;Higher effort doesn't make the model "smarter". What it does is make Claude spend more time verifying its own work, checking edge cases, and making its own judgement calls. Lower effort gives you a quicker, simpler result that you are expected to iterate on.&lt;/p&gt;

&lt;p&gt;Think about it like asking a colleague to do something. If you give them a full day, they will try to deliver the most complete version they can, and they will make a lot of decisions on your behalf along the way. If you give them an hour, they will deliver something that does the job and expect you to give feedback.&lt;/p&gt;

&lt;p&gt;Neither is wrong, it depends on what you need at that moment.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with "max effort all the time"
&lt;/h2&gt;

&lt;p&gt;When you leave effort on &lt;code&gt;high&lt;/code&gt;, &lt;code&gt;xhigh&lt;/code&gt;, or &lt;code&gt;max&lt;/code&gt; for everything, a few things happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You burn a lot more tokens, even for tasks like renaming a variable or adding a field to a form.&lt;/li&gt;
&lt;li&gt;You wait longer for every single response.&lt;/li&gt;
&lt;li&gt;Claude makes more assumptions for you, which is great when you want it to work autonomously, but not so great when you want to stay in control of what's being built.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last point is the one that surprised me the most. More effort doesn't automatically mean more of what &lt;em&gt;you&lt;/em&gt; wanted, it often means more of what &lt;em&gt;Claude thinks&lt;/em&gt; you wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  My workflow: plan high, execute lower
&lt;/h2&gt;

&lt;p&gt;So here's what I do instead. I split the work into two very different phases, and I use a different effort level (and sometimes a different model) for each of them.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    A[Idea] --&amp;gt; B[Plan&amp;lt;br/&amp;gt;Opus 5.5, high effort]
    B --&amp;gt; C[Plan + ADRs]
    C --&amp;gt; D[Execute step N&amp;lt;br/&amp;gt;Opus or Sonnet 5.5, low/medium]
    D --&amp;gt; E{Review}
    E -- needs changes --&amp;gt; D
    E -- looks good --&amp;gt; F[Verify&amp;lt;br/&amp;gt;high effort]
    F --&amp;gt; G{More steps?}
    G -- yes --&amp;gt; D
    G -- no --&amp;gt; H[Done]&lt;/code&gt;&lt;/pre&gt;



&lt;h3&gt;
  
  
  Step 1: Plan with high effort
&lt;/h3&gt;

&lt;p&gt;The planning phase is where the hard thinking happens: what are we building, how do the pieces fit together, what are the trade-offs, what could go wrong.&lt;/p&gt;

&lt;p&gt;This is exactly where I want the model to think hard, so I use Opus 5.5 with &lt;code&gt;high&lt;/code&gt; effort:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;claude &lt;span class="nt"&gt;--model&lt;/span&gt; opus

&lt;span class="c"&gt;# Then inside the session&lt;/span&gt;
/effort high
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also ask Claude to &lt;strong&gt;interview me&lt;/strong&gt; about the feature before writing the plan. This is a small trick that makes a huge difference, as it forces me to answer the questions I didn't think about, and Claude doesn't have to guess.&lt;/p&gt;

&lt;p&gt;To do that, I use the &lt;a href="https://github.com/mattpocock/skills/blob/main/skills/productivity/grilling/SKILL.md" rel="noopener noreferrer"&gt;grilling skill&lt;/a&gt; from Matt Pocock. If you don't know what skills are, they are just markdown files with instructions that Claude Code loads when they are relevant to what you are doing.&lt;/p&gt;

&lt;p&gt;What I like about this skill is how structured the interview is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Claude maps every decision as a tree, where each decision unlocks the ones that depend on it.&lt;/li&gt;
&lt;li&gt;It asks the questions in rounds, only the ones that can be answered &lt;em&gt;now&lt;/em&gt;, without guessing answers you haven't given yet.&lt;/li&gt;
&lt;li&gt;Each question comes numbered and with a recommended answer, so most of the time you can just say "yes" and move on.&lt;/li&gt;
&lt;li&gt;Facts it can find by itself, like what's in your codebase, it looks up on its own, and it only asks &lt;em&gt;you&lt;/em&gt; about decisions.&lt;/li&gt;
&lt;li&gt;It doesn't start doing anything until you both agree there's nothing left to decide.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To install it, just follow the &lt;a href="https://github.com/mattpocock/skills/tree/main#installation-30-second-setup" rel="noopener noreferrer"&gt;installation steps&lt;/a&gt; in the repository, it takes less than a minute.&lt;/p&gt;

&lt;p&gt;Then in the planning session I just ask Claude to grill me:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I want to add bookmarks to my blog app, so logged-in users can save
articles and see them in a "Saved" page.
Grill me on this before writing any plan. Once we agree, write a plan
split into small, independent steps.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a round of questions will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❓ Q1 - Anonymous users: Should users who are not logged in see the
bookmark button?

➡️ Show it, but redirect to the login page when clicked.

❓ Q2 - Storage: Should bookmarks be stored server-side, or is
localStorage enough?

➡️ Server-side, so bookmarks follow the user across devices.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can take a while, and with &lt;code&gt;high&lt;/code&gt; effort it does use a good amount of tokens, but it's the best place to spend them, as every question answered here is one less assumption Claude has to make later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Write everything down
&lt;/h3&gt;

&lt;p&gt;The output of the planning session is not code, it's documents. I normally end up with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
|-- docs
|   |-- adr
|   |   |-- 001-store-bookmarks-in-postgres.md
|   |   `-- 002-use-swr-for-client-fetching.md
|   `-- plans
|       `-- bookmarks.md
`-- CLAUDE.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The plan, &lt;code&gt;docs/plans/bookmarks.md&lt;/code&gt;, is split into steps, where each step is small enough to be done in a single session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gh"&gt;# Bookmarks feature&lt;/span&gt;

&lt;span class="gu"&gt;## Step 1: Database&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; Create a &lt;span class="sb"&gt;`bookmarks`&lt;/span&gt; table (user_id, article_id, created_at)
&lt;span class="p"&gt;-&lt;/span&gt; Add a unique constraint on (user_id, article_id)
&lt;span class="p"&gt;-&lt;/span&gt; Write the migration and the rollback

&lt;span class="gu"&gt;## Step 2: API endpoints&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; POST /api/bookmarks
&lt;span class="p"&gt;-&lt;/span&gt; DELETE /api/bookmarks/:articleId
&lt;span class="p"&gt;-&lt;/span&gt; GET /api/bookmarks

&lt;span class="gu"&gt;## Step 3: UI&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; Bookmark button on the article page
&lt;span class="p"&gt;-&lt;/span&gt; "Saved" page listing the bookmarks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the decisions that need a "why", like choosing a library or a database design, I also ask Claude to write an &lt;strong&gt;ADR&lt;/strong&gt; (Architecture Decision Record). An ADR is just a short markdown file that explains what we decided, why, and what alternatives we considered:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gh"&gt;# ADR-002: Use SWR for client-side fetching&lt;/span&gt;

&lt;span class="gu"&gt;## Status&lt;/span&gt;
Accepted

&lt;span class="gu"&gt;## Context&lt;/span&gt;
The "Saved" page needs to fetch the bookmarks list and refresh it
when a bookmark is added or removed.

&lt;span class="gu"&gt;## Decision&lt;/span&gt;
Use the SWR hook with Suspense, as we already do in other pages.

&lt;span class="gu"&gt;## Consequences&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; Caching and revalidation for free
&lt;span class="p"&gt;-&lt;/span&gt; One more dependency in the frontend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you want to know more about SWR and Suspense, I wrote about it in &lt;a href="https://dev.to/darkmavis1980/using-swr-hook-with-suspense-in-react-18-1clk"&gt;Using SWR hook with Suspense in React 18&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And finally, things that are important to remember, but that don't really belong in the plan (like "the staging database uses a different port" or "we use &lt;code&gt;pnpm&lt;/code&gt;, not &lt;code&gt;npm&lt;/code&gt;"), go into Claude's memory, like the &lt;code&gt;CLAUDE.md&lt;/code&gt; file. This way they are available in every session, without cluttering the plan.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Execute one step at a time, with low or medium effort
&lt;/h3&gt;

&lt;p&gt;Now comes the part where most people keep the effort on &lt;code&gt;high&lt;/code&gt;, and where I do the opposite.&lt;/p&gt;

&lt;p&gt;For each step of the plan, I start a &lt;strong&gt;new session&lt;/strong&gt;, point it to the plan, and lower the effort to &lt;code&gt;low&lt;/code&gt; or &lt;code&gt;medium&lt;/code&gt;, depending on how big the step is.&lt;/p&gt;

&lt;p&gt;I can keep using Opus 5.5 for this, but for the more straightforward steps I often switch to a cheaper model like Sonnet 5.5:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Stay on Opus 5.5...&lt;/span&gt;
claude &lt;span class="nt"&gt;--model&lt;/span&gt; opus

&lt;span class="c"&gt;# ...or switch to Sonnet 5.5 for simpler steps&lt;/span&gt;
claude &lt;span class="nt"&gt;--model&lt;/span&gt; sonnet

&lt;span class="c"&gt;# Then inside the session&lt;/span&gt;
/effort low     &lt;span class="c"&gt;# small, well-defined steps&lt;/span&gt;
/effort medium  &lt;span class="c"&gt;# bigger steps, or ones touching more files&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Read docs/plans/bookmarks.md and the ADRs in docs/adr.
Implement Step 2 only. Don't touch anything outside that step.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why does this work? Because all the hard decisions were &lt;strong&gt;already made&lt;/strong&gt; during the planning phase. Claude doesn't need to think hard about how to design the API, it's written right there in the plan. It just needs to execute it.&lt;/p&gt;

&lt;p&gt;Starting a new session for each step also has another advantage: the context is clean. No leftovers from previous steps, no long conversation history to drag along, just the plan, the ADRs, and the code.&lt;/p&gt;

&lt;p&gt;If a step turns out to be trickier than expected, I just bump the effort up, or switch back to Opus. No big deal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Review, then verify with high effort
&lt;/h3&gt;

&lt;p&gt;Once a step is done, I review the code myself, to make sure Claude got the gist of what I wanted. If something is off, I iterate, still on the same lower effort.&lt;/p&gt;

&lt;p&gt;When I'm happy with it, I switch back to &lt;code&gt;high&lt;/code&gt; effort and ask Claude to verify the work: run the tests, look for edge cases, check that nothing else broke.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Review the changes for Step 2 against docs/plans/bookmarks.md.
Write tests for the edge cases (duplicate bookmarks, deleting a
bookmark that doesn't exist, unauthenticated requests) and run them.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the extra effort really pays off, as hunting for edge cases is exactly the kind of work where thinking longer finds more problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is this just my opinion?
&lt;/h2&gt;

&lt;p&gt;Partly, yes! But it turns out Anthropic recently published a post on the claude.dev blog, &lt;a href="https://claude.dev/blog/spending-your-effort/" rel="noopener noreferrer"&gt;Using Claude Code: Spending your effort&lt;/a&gt;, where they tested the same tasks at different effort levels, and a few findings match this workflow pretty well.&lt;/p&gt;

&lt;p&gt;The most interesting one for me: when they gave Claude a vague request (like "build a fitness tracker app"), the effort level changed the result dramatically. But when they first had Claude interview them and produce a detailed spec, then implemented that spec at different effort levels, &lt;strong&gt;the results were very similar&lt;/strong&gt;, just at very different speeds.&lt;/p&gt;

&lt;p&gt;That's the whole point of planning first: a detailed plan removes most of the guesswork, and when there's no guesswork, you don't need to pay for extra thinking.&lt;/p&gt;

&lt;p&gt;The loop they recommend is also very close to what I described above: get interviewed about the spec, implement on low effort, review and iterate, then verify and test on high effort.&lt;/p&gt;

&lt;p&gt;To be fair, the same post also shows that higher effort &lt;em&gt;does&lt;/em&gt; produce better results on hard problems, especially when there are a lot of hidden edge cases. So it's not that high effort is bad, it's that it's not free, and it's not always the thing that makes the difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  When you should crank up the effort
&lt;/h2&gt;

&lt;p&gt;As with everything, this is not a rule you should apply blindly. There are cases where I go straight to &lt;code&gt;high&lt;/code&gt;, or even &lt;code&gt;max&lt;/code&gt;, during execution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bugs in an existing codebase.&lt;/strong&gt; When you don't fully know why something is breaking, you want Claude to reproduce the problem first and verify the fix, not just patch the first thing that looks suspicious.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security-sensitive code.&lt;/strong&gt; Things like input sanitisation, authentication, or permissions are full of edge cases, and missing one of them is expensive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance work.&lt;/strong&gt; Claude needs to measure, test with bigger inputs, and iterate, not just write something that looks fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fully autonomous tasks.&lt;/strong&gt; If you are not going to be in the loop to review each step, then you want Claude to do the verification on its own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a rule of thumb, I start with &lt;code&gt;high&lt;/code&gt;, and I move to &lt;code&gt;xhigh&lt;/code&gt; or &lt;code&gt;max&lt;/code&gt; only when the task is really hard, or when Claude is going to work on its own for a long time without me checking in. Those are the cases where all that extra verification actually pays for itself.&lt;/p&gt;

&lt;p&gt;And on the other side, there are cases where even planning on &lt;code&gt;high&lt;/code&gt; is overkill: a typo fix, a small refactor, or a one-off script. Just do it on &lt;code&gt;low&lt;/code&gt; and move on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;The effort level is not a "quality" knob you set once and forget, it's a budget you spend depending on the task.&lt;/p&gt;

&lt;p&gt;So what I'd suggest is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Plan&lt;/strong&gt; with a strong model and high effort, and let Claude grill you until there's nothing left to decide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write it down&lt;/strong&gt;: a plan split into steps, ADRs for the important decisions, and memory for everything else.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute&lt;/strong&gt; each step in a fresh session, with low or medium effort, and maybe a cheaper model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify&lt;/strong&gt; with high effort, where finding edge cases actually matters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You'll spend fewer tokens, wait less for each response, and in my experience, you'll stay much more in control of what's being built.&lt;/p&gt;

&lt;p&gt;How do you use effort levels in Claude Code? I'd love to hear if you have a different approach, so let me know in the comments!&lt;/p&gt;

</description>
      <category>ai</category>
      <category>claude</category>
      <category>productivity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>You should use Map instead of object literals for lookup tables</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Thu, 22 Oct 2026 11:04:30 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/you-should-use-map-instead-of-object-literals-for-lookup-tables-2fmb</link>
      <guid>https://dev.to/darkmavis1980/you-should-use-map-instead-of-object-literals-for-lookup-tables-2fmb</guid>
      <description>&lt;p&gt;A while ago I wrote an article about &lt;a href="https://dev.to/darkmavis1980/using-object-literals-instead-of-switch-statements-in-javascript-kdp"&gt;using object literals instead of switch statements&lt;/a&gt;, and I still think the idea behind it is a good one: map an input to an output, skip the long list of &lt;code&gt;case&lt;/code&gt; and &lt;code&gt;break&lt;/code&gt;, and keep your code short and readable.&lt;/p&gt;

&lt;p&gt;But there's a catch I didn't cover back then, and a reader (thanks &lt;a href="https://dev.to/dariomannu"&gt;Dario&lt;/a&gt;!) pointed it out in the comments: plain objects don't only contain the keys &lt;em&gt;you&lt;/em&gt; put in them.&lt;/p&gt;

&lt;p&gt;So in this article I want to show you what the problem is, and why today I'd reach for a &lt;code&gt;Map&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick recap
&lt;/h2&gt;

&lt;p&gt;In the previous article we had something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getAnimalSound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;woof&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;cow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;moo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;unknown sound&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;sounds&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// 'woof'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tiger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 'unknown sound'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean, short, and it works. Or at least it looks like it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what's the problem?
&lt;/h2&gt;

&lt;p&gt;Every object you create with &lt;code&gt;{}&lt;/code&gt; inherits from &lt;code&gt;Object.prototype&lt;/code&gt;. This means that when you read &lt;code&gt;sounds[animal]&lt;/code&gt;, if JavaScript doesn't find the key on the object itself, it goes up the prototype chain and looks there too.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;Object.prototype&lt;/code&gt; has quite a few properties on it, like &lt;code&gt;constructor&lt;/code&gt;, &lt;code&gt;toString&lt;/code&gt;, &lt;code&gt;valueOf&lt;/code&gt;, &lt;code&gt;hasOwnProperty&lt;/code&gt; and &lt;code&gt;__proto__&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see what happens if we pass one of those names to our function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// [Function: Object]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;    &lt;span class="c1"&gt;// [Function: toString]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;__proto__&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// [Object: null prototype] {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;None of those return &lt;code&gt;'unknown sound'&lt;/code&gt;. Because the value we get back is &lt;em&gt;truthy&lt;/em&gt;, the &lt;code&gt;|| sounds.default&lt;/code&gt; fallback never kicks in, and we return something completely unexpected.&lt;/p&gt;

&lt;p&gt;Now, you might think "who would ever pass &lt;code&gt;constructor&lt;/code&gt; as an animal?". And you are probably right, until that value comes from a query string, a form field, or the body of an API request. Then anybody can pass whatever they want.&lt;/p&gt;

&lt;h3&gt;
  
  
  It gets worse with functions
&lt;/h3&gt;

&lt;p&gt;In the previous article we also used object literals to dispatch commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;executeCommand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello there!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;farewell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Goodbye!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unknown command&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commands&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;commands&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try a few "commands":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;executeCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// nothing happens, it silently calls Object()&lt;/span&gt;
&lt;span class="nf"&gt;executeCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// nothing happens either&lt;/span&gt;
&lt;span class="nf"&gt;executeCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: Cannot convert undefined or null to object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first two fail silently, which is the worst kind of bug, because you don't even know something went wrong. The last one crashes your function.&lt;/p&gt;

&lt;h3&gt;
  
  
  And with nested objects
&lt;/h3&gt;

&lt;p&gt;The state machine example is my favourite one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stateTransitions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;idle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleStateTransition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;stateTransitions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;]?.[&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;handleStateTransition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 'Object'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;handleStateTransition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;    &lt;span class="c1"&gt;// [Function: toString]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, your component is now in the &lt;code&gt;'Object'&lt;/code&gt; state, or worse, its state is a function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
To be clear, this is &lt;strong&gt;not&lt;/strong&gt; prototype pollution. Prototype pollution happens when an attacker manages to &lt;em&gt;write&lt;/em&gt; to &lt;code&gt;Object.prototype&lt;/code&gt;, for example with &lt;code&gt;obj[key] = value&lt;/code&gt; where &lt;code&gt;key&lt;/code&gt; is &lt;code&gt;__proto__&lt;/code&gt;. Here we are only &lt;em&gt;reading&lt;/em&gt;, but it's still a real bug, and it's the reason why security linters like &lt;code&gt;eslint-plugin-security&lt;/code&gt; flag &lt;code&gt;obj[variable]&lt;/code&gt; lookups.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Enter &lt;code&gt;Map&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;Map&lt;/code&gt; is a built-in collection of key/value pairs, and the important difference is that &lt;code&gt;map.get(key)&lt;/code&gt; only returns entries that you explicitly added. There's no prototype chain to walk through, so &lt;code&gt;constructor&lt;/code&gt; is just a key like any other, and since you never added it, you get &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's rewrite our first example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;woof&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;moo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getAnimalSound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;unknown sound&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;         &lt;span class="c1"&gt;// 'woof'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tiger&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;       &lt;span class="c1"&gt;// 'unknown sound'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 'unknown sound' ✅&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getAnimalSound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;__proto__&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;   &lt;span class="c1"&gt;// 'unknown sound' ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might have noticed a few other small changes, and they are not random:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Map&lt;/code&gt; is defined &lt;strong&gt;outside&lt;/strong&gt; the function, so we don't rebuild it every time the function is called.&lt;/li&gt;
&lt;li&gt;The fallback is no longer stored as a &lt;code&gt;default&lt;/code&gt; key, so nobody can "select" it by passing &lt;code&gt;'default'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I'm using &lt;code&gt;??&lt;/code&gt; (nullish coalescing) instead of &lt;code&gt;||&lt;/code&gt;. With &lt;code&gt;||&lt;/code&gt;, a valid value like &lt;code&gt;0&lt;/code&gt; or an empty string would be replaced by the fallback. With &lt;code&gt;??&lt;/code&gt;, the fallback is used only when the value is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;&lt;br&gt;
If you already have an object and you don't want to rewrite it as an array of arrays, you can convert it with &lt;code&gt;new Map(Object.entries(myObject))&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Dispatching functions
&lt;/h3&gt;

&lt;p&gt;The same works for our commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;greet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello there!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;farewell&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Goodbye!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;time&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Current time: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;unknownCommand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unknown command&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;executeCommand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;commands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="nx"&gt;unknownCommand&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt;

&lt;span class="nf"&gt;executeCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;greet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// 'Hello there!'&lt;/span&gt;
&lt;span class="nf"&gt;executeCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'Unknown command' ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with parameters, like the calculator from the previous article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;operations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;add&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;subtract&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;multiply&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;divide&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cannot divide by zero&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;calculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;operations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unknown operation&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;calculator&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;add&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;     &lt;span class="c1"&gt;// 8&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;calculator&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;valueOf&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 'Unknown operation' ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The state machine
&lt;/h3&gt;

&lt;p&gt;For nested lookups you can nest &lt;code&gt;Map&lt;/code&gt;s too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stateTransitions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]])],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cancel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]])],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;refresh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clear&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]])],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;retry&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clear&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]])],&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleStateTransition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;stateTransitions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;)?.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;handleStateTransition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// 'loading'&lt;/span&gt;
&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;handleStateTransition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 'loaded'&lt;/span&gt;
&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;handleStateTransition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;toString&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'loaded' (unchanged) ✅&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll be honest, this one is a bit less pretty than the object version. We'll come back to that in a moment.&lt;/p&gt;

&lt;h2&gt;
  
  
  A bonus with TypeScript
&lt;/h2&gt;

&lt;p&gt;If you use TypeScript, &lt;code&gt;Map&lt;/code&gt; has another nice side effect: &lt;code&gt;map.get()&lt;/code&gt; always returns &lt;code&gt;T | undefined&lt;/code&gt;, so the compiler forces you to handle the missing case.&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="nx"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;woof&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;moo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getAnimalSound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;unknown sound&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// without ?? TypeScript will complain&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When you don't need a &lt;code&gt;Map&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;As always, I don't want you to take this as "never use objects again". There are cases where a plain object is perfectly fine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When the keys never come from outside your code.&lt;/strong&gt; If the key is a value you control, for example a TypeScript literal union that you pass around internally, the prototype issue can't really happen. In that case an object with &lt;code&gt;satisfies&lt;/code&gt; gives you great type checking:&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="nx"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;woof&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;meow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;cow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;moo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;satisfies&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just remember that TypeScript types disappear at runtime: if the value comes from a request or a form, you still need to validate it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you only need a quick safety check.&lt;/strong&gt; If you want to keep the object syntax, &lt;code&gt;Object.hasOwn()&lt;/code&gt; checks only the object's own properties, ignoring the prototype chain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getAnimalSound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;unknown sound&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is also a good option for deeply nested tables like the state machine, where nested &lt;code&gt;Map&lt;/code&gt;s get a bit noisy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you need to serialise the data.&lt;/strong&gt; &lt;code&gt;JSON.stringify(new Map([['a', 1]]))&lt;/code&gt; returns &lt;code&gt;{}&lt;/code&gt;, not what you expect. If your lookup table needs to be sent over the wire or saved as JSON, an object is easier to work with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When &lt;code&gt;switch&lt;/code&gt; is simply the better tool.&lt;/strong&gt; As I mentioned in the &lt;a href="https://dev.to/darkmavis1980/using-object-literals-instead-of-switch-statements-in-javascript-kdp"&gt;previous article&lt;/a&gt;, if your conditions involve ranges, complex expressions or intentional fall-through, a &lt;code&gt;switch&lt;/code&gt; is still the right choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Object literals are still a great idea to replace long &lt;code&gt;switch&lt;/code&gt; statements, but when the key comes from user input, a plain object can return things you never put in it, like &lt;code&gt;constructor&lt;/code&gt; or &lt;code&gt;toString&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Map&lt;/code&gt; fixes that by design: it only knows the keys you gave it, it works nicely with &lt;code&gt;??&lt;/code&gt;, and with TypeScript it forces you to handle the missing case.&lt;/p&gt;

&lt;p&gt;So my rule of thumb today is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the key comes from outside your code, use a &lt;code&gt;Map&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the key is fully under your control, an object is fine, and &lt;code&gt;Object.hasOwn()&lt;/code&gt; is a cheap safety net if you want one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this helped you to understand the hidden gotcha of object lookups, and as always any comments are welcome!&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;Parts of this article were created with the help of AI.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Stop writing raw SQL in your migrations (most of the time)</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Fri, 28 Aug 2026 13:28:34 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/stop-writing-raw-sql-in-your-migrations-most-of-the-time-i1o</link>
      <guid>https://dev.to/darkmavis1980/stop-writing-raw-sql-in-your-migrations-most-of-the-time-i1o</guid>
      <description>&lt;p&gt;Something I keep seeing across teams, especially with people who are getting comfortable with tools like &lt;a href="https://github.com/db-migrate/node-db-migrate" rel="noopener noreferrer"&gt;db-migrate&lt;/a&gt; or &lt;a href="https://kysely.dev/" rel="noopener noreferrer"&gt;Kysely&lt;/a&gt;, is a very specific habit: they reach for raw SQL for &lt;em&gt;everything&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Need to add a column? Raw &lt;code&gt;ALTER TABLE&lt;/code&gt;. Need to create a table? Raw &lt;code&gt;CREATE TABLE&lt;/code&gt;. Need to add an index? You guessed it, another raw query.&lt;/p&gt;

&lt;p&gt;And look, raw SQL is not evil, and I'm not here to tell you to never use it. But when your migration tool gives you a proper schema builder and you skip it to write raw strings for a simple column addition, you're throwing away a lot of the things that make those tools worth using in the first place.&lt;/p&gt;

&lt;p&gt;Let me explain why.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, what does "raw" even look like?
&lt;/h2&gt;

&lt;p&gt;Let's say you want to add a simple &lt;code&gt;email&lt;/code&gt; column to a &lt;code&gt;users&lt;/code&gt; table. With Kysely, the raw approach tends to look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;sql&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kysely&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&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;up&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="s2"&gt;`ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;down&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="s2"&gt;`ALTER TABLE users DROP COLUMN email`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&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;Or with db-migrate, people often do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runSql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;down&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runSql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ALTER TABLE users DROP COLUMN email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works. The migration runs, the column appears, everyone goes home happy.&lt;/p&gt;

&lt;p&gt;But the same thing with the builder looks like this in Kysely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&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;up&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alterTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;varchar(255)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notNull&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;down&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alterTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dropColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&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;And with db-migrate you don't even need to write SQL at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;notNull&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;down&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance the raw version might even look shorter, so &lt;em&gt;why should you care?&lt;/em&gt; Let me give you a few reasons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason 1: you lose portability
&lt;/h2&gt;

&lt;p&gt;This is the big one, and it's the one that bites people the hardest.&lt;/p&gt;

&lt;p&gt;When you write &lt;code&gt;VARCHAR(255)&lt;/code&gt;, &lt;code&gt;SERIAL&lt;/code&gt;, &lt;code&gt;AUTO_INCREMENT&lt;/code&gt;, &lt;code&gt;BOOLEAN&lt;/code&gt;, or &lt;code&gt;NOW()&lt;/code&gt; directly in a string, you're hardcoding the dialect of one specific database. The moment you switch, or the moment you run your tests against a different engine than production, things start to break in annoying ways.&lt;/p&gt;

&lt;p&gt;If you've read some of my other posts, you'll know I have a soft spot for this problem. In the &lt;a href="https://dev.to/darkmavis1980/the-returning-clause-in-sql-11k2"&gt;RETURNING clause post&lt;/a&gt; I got bitten by exactly this: &lt;code&gt;RETURNING&lt;/code&gt; works great on PostgreSQL, but MySQL/MariaDB only partially support it. Raw SQL assumes you know every quirk of your target engine, forever.&lt;/p&gt;

&lt;p&gt;The whole point of the schema builder is that it knows the dialect for you. You say &lt;code&gt;addColumn('active', 'boolean')&lt;/code&gt; and Kysely (or db-migrate) figures out whether that becomes a real &lt;code&gt;BOOLEAN&lt;/code&gt;, a &lt;code&gt;TINYINT(1)&lt;/code&gt;, or whatever the engine expects. You describe the &lt;em&gt;intent&lt;/em&gt;, the tool handles the &lt;em&gt;syntax&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason 2: your &lt;code&gt;down&lt;/code&gt; migrations get fragile
&lt;/h2&gt;

&lt;p&gt;Here's a subtle one. When you use the builder, the tool understands the structure of the change you're making, and reversing it is often trivial and consistent.&lt;/p&gt;

&lt;p&gt;When you write raw SQL, &lt;em&gt;you&lt;/em&gt; are responsible for writing a &lt;code&gt;down&lt;/code&gt; that perfectly undoes the &lt;code&gt;up&lt;/code&gt;, and it's very easy to get lazy or to make a mistake. I've seen plenty of migrations where the &lt;code&gt;up&lt;/code&gt; is a carefully crafted raw query and the &lt;code&gt;down&lt;/code&gt; is just... empty, or wrong, because nobody wanted to hand-write the reverse.&lt;/p&gt;

&lt;p&gt;A broken &lt;code&gt;down&lt;/code&gt; migration is one of those things you don't notice until the worst possible moment: you need to roll back in production and suddenly discover your escape hatch doesn't work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reason 3: you skip the safety net
&lt;/h2&gt;

&lt;p&gt;Builder methods are typed and validated. In Kysely especially, you get autocomplete and, if you're using TypeScript, the compiler will shout at you when you try to do something that doesn't make sense.&lt;/p&gt;

&lt;p&gt;This is the same argument I made in my post about &lt;a href="https://dev.to/darkmavis1980/how-to-define-objects-with-type-definitions-in-jsdoc-comments-23n1"&gt;defining objects with type definitions in JSDoc&lt;/a&gt; and the one on &lt;a href="https://dev.to/darkmavis1980/always-abstract-nested-object-in-typescript-4pp5"&gt;abstracting nested types in TypeScript&lt;/a&gt;: giving your tools enough information to help you is almost always worth it. A raw string is completely opaque. Your editor can't check it, can't complete it, and can't warn you that you typed &lt;code&gt;VARHCAR&lt;/code&gt; instead of &lt;code&gt;VARCHAR&lt;/code&gt;. You'll only find out at runtime, when the migration blows up.&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;// Typo city. Nothing catches this until it runs.&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;sql&lt;/span&gt;&lt;span class="s2"&gt;`ALTER TABLE users ADD COLUMN emial VARHCAR(255)`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// The builder won't let 'emial' silently become a problem,&lt;/span&gt;
&lt;span class="c1"&gt;// and the column type is a known value, not a free-form string.&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alterTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;varchar(255)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reason 4: readability and consistency
&lt;/h2&gt;

&lt;p&gt;If half your migrations are raw SQL and the other half use the builder, reading through your migration history becomes a chore. Every file speaks a slightly different language and the reader has to context-switch constantly.&lt;/p&gt;

&lt;p&gt;I made basically the same point in &lt;a href="https://dev.to/darkmavis1980/why-you-shouldnt-mix-es-modules-and-commonjs-import-statements-56fc"&gt;why you shouldn't mix ES modules and CommonJS&lt;/a&gt;: pick one style and stick with it. Consistency isn't just aesthetic, it genuinely lowers the mental cost for the next person who has to touch the code (and that next person is very often &lt;em&gt;you&lt;/em&gt;, six months from now, with no memory of what you were thinking).&lt;/p&gt;

&lt;h2&gt;
  
  
  So when &lt;em&gt;should&lt;/em&gt; you use raw SQL?
&lt;/h2&gt;

&lt;p&gt;Now, here's the important part, because I don't want you to walk away thinking "raw SQL bad, builder good, the end". That's not the point at all.&lt;/p&gt;

&lt;p&gt;Raw SQL is the right tool when &lt;strong&gt;the ORM or migration library simply doesn't give you a way to express what you need.&lt;/strong&gt; And that happens more often than you'd think, especially once you go past basic table shapes. Some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database-specific features&lt;/strong&gt; that the builder doesn't wrap, like PostgreSQL extensions (&lt;code&gt;CREATE EXTENSION "uuid-ossp"&lt;/code&gt;), custom types with &lt;code&gt;CREATE TYPE ... AS ENUM&lt;/code&gt;, or generated columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complex data migrations&lt;/strong&gt;, where you're not just changing the schema but also transforming existing rows, for example backfilling a new column from old data with a &lt;code&gt;CASE&lt;/code&gt; statement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrent index creation&lt;/strong&gt; in Postgres (&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;), which the builder might not support because it has special transaction requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Triggers, functions, materialized views&lt;/strong&gt;, and other things that are inherently SQL and have no clean builder equivalent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In those cases, reaching for raw SQL isn't a shortcut, it's the correct answer. The builder abstraction has a ceiling, and when you hit it, you drop down a level. That's a totally healthy thing to do.&lt;/p&gt;

&lt;p&gt;The rule I try to follow is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use the builder for anything the builder can express. Drop to raw SQL only when the builder can't do the job.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  A quick reality check
&lt;/h2&gt;

&lt;p&gt;To be fair, there are a couple of arguments on the other side, and I want to acknowledge them rather than pretend they don't exist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"I know SQL better than the builder's API."&lt;/strong&gt; Fair. If you're comfortable with SQL, the builder can feel like an extra layer between you and the thing you already know how to write. But the builder isn't there to teach you SQL, it's there to keep your migrations portable, reversible and checkable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"My app is tied to one database and always will be."&lt;/strong&gt; Also fair, and if that's genuinely true, the portability argument loses some weight. But the readability, the safer &lt;code&gt;down&lt;/code&gt; migrations, and the type safety still stand. And "we'll never change database" has a funny way of not aging well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So it's not black and white. It's about defaults. My default is the builder, and raw SQL is the deliberate exception, not the reflex.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Writing raw SQL for a simple &lt;code&gt;ALTER TABLE&lt;/code&gt; is a bit like using &lt;code&gt;parseInt&lt;/code&gt; when &lt;a href="https://dev.to/darkmavis1980/you-should-stop-using-parseint-nbf"&gt;&lt;code&gt;Number&lt;/code&gt; would do the job better&lt;/a&gt;: it works, but you're reaching for the wrong tool out of habit, and you're quietly giving up things you'd actually like to keep.&lt;/p&gt;

&lt;p&gt;Let the builder handle the boring, portable, reversible stuff. Save raw SQL for the moments where the ORM genuinely can't help you, and it'll feel like a precision tool instead of a crutch.&lt;/p&gt;

&lt;p&gt;And as always, this is just how I like to do it, not the one and only truth. If you've got a different take, I'd love to hear it in the comments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER&lt;/strong&gt;&lt;br&gt;
I used AI to help me write this article and fix grammar issues.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>node</category>
    </item>
    <item>
      <title>Why Mermaid is the Best Way to Document Your Architecture in the AI Era</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Wed, 25 Feb 2026 11:49:40 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/why-mermaid-is-the-best-way-to-document-your-architecture-in-the-ai-era-2dgb</link>
      <guid>https://dev.to/darkmavis1980/why-mermaid-is-the-best-way-to-document-your-architecture-in-the-ai-era-2dgb</guid>
      <description>&lt;p&gt;Creating documentation for your project was always a fundamental skill for me, long before LLMs even existed.&lt;br&gt;
Being able to quickly read how to setup a project or what the project is about it's something that every repository should have.&lt;br&gt;
And in the LLMs era, where you need to also tell a model how your project works, without it having to waste tokens reading through your code and figuring things out on its own, it's even more important.&lt;/p&gt;

&lt;p&gt;But what changed is how the documentation is consumed. Is no longer for humans-only to be understood, but it's also massively important  for machines.&lt;/p&gt;

&lt;p&gt;One of the most important aspect we need to document about a project is often the architecture or any flows of how that piece of software you wrote works.&lt;/p&gt;

&lt;p&gt;And what's the easiest way to graphically show that? Diagrams.&lt;/p&gt;

&lt;p&gt;In the past a common way to do it was using ASCII, so you might have something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────┐        ┌─────────────┐        ┌─────────────┐
│   Browser   │ ──────▶│   Backend   │ ──────▶│  Database   │
└─────────────┘        └─────────────┘        └─────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple to read, fairly universal to show it anywhere, it doesn't need any special plugin or software to be read, it just works.&lt;/p&gt;

&lt;p&gt;And often an LLM, when you ask to create a diagram, will use just that, as it's easier for a human to read.&lt;/p&gt;

&lt;p&gt;But this doesn't mean that, while is effective to humans, is also effective to AI models.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Mermaid.js
&lt;/h2&gt;

&lt;p&gt;Another way to represent the exact same diagram above, is to use a format called mermaid.&lt;br&gt;
It's a very simple format that is supported by Github, Gitlab, Gitea, etc... and that diagram will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flowchart LR
    Browser --&amp;gt; Backend --&amp;gt; Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, still fairly easy to read, maybe less fancy than the ASCII, but it will render beautifully iin a browser where that format is supported.&lt;br&gt;
You can also zoom in, it scales up to whatever resolution you want, and many other advantages, and the only real disadvantage is the fact that you &lt;strong&gt;need something to render it&lt;/strong&gt;, which is the mermaid renderer.&lt;/p&gt;

&lt;p&gt;Another issue is that when you have complex diagram, if you don't render it with mermaid library, it becomes hard to read for humans. Something like this for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flowchart TD
    User["👤 User"] --&amp;gt; Browser["🌐 Browser"]
    Browser --&amp;gt; CDN["CDN\n(Static Assets)"]
    Browser --&amp;gt; LB["Load Balancer"]

    LB --&amp;gt; API1["API Server 1"]
    LB --&amp;gt; API2["API Server 2"]

    API1 &amp;amp; API2 --&amp;gt; Cache["Redis Cache"]
    API1 &amp;amp; API2 --&amp;gt; DB[("PostgreSQL")]
    API1 &amp;amp; API2 --&amp;gt; Queue["Message Queue\n(SQS / RabbitMQ)"]

    Queue --&amp;gt; Worker["Background Worker"]
    Worker --&amp;gt; DB

    API1 &amp;amp; API2 --&amp;gt; Auth["Auth Service\n(OAuth / JWT)"]
    API1 &amp;amp; API2 --&amp;gt; ThirdParty["Third Party APIs\n(Email, Payments)"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can still understand what's going on, but for a human is harder to read.&lt;/p&gt;

&lt;p&gt;So at this point, if you are a human, you could argue against one or the other.&lt;br&gt;
But that's the key: if you are human.&lt;/p&gt;

&lt;p&gt;If you are an LLM, it's a completely different story.&lt;/p&gt;

&lt;h2&gt;
  
  
  But it's not just about AI
&lt;/h2&gt;

&lt;p&gt;Before we even get to the AI angle, mermaid has some very practical advantages for humans too that are worth mentioning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It lives in your codebase.&lt;/strong&gt; Because mermaid diagrams are just text, they live right in your markdown files alongside your code. This means they go through the same version control process as everything else — you can review changes in a PR, revert them, and see exactly who changed what and when. Try doing that with a PNG exported from Lucidchart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diffs are meaningful.&lt;/strong&gt; Following on from the above, when somebody updates a diagram, the diff in your PR will show exactly what changed in the flow, not just "image.png was modified". This makes code reviews a lot more useful and transparent.&lt;br&gt;
No extra tooling required. With traditional diagramming tools like Figma, Draw.io or Lucidchart, you need to open a separate application, make your changes, export an image, and then commit it. With mermaid, you just edit the text directly in your markdown file. Your editor, GitHub, and most modern documentation platforms will handle the rendering for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It forces you to think in structure.&lt;/strong&gt; Writing a diagram in mermaid syntax, rather than dragging boxes around a canvas, naturally encourages you to think about the relationships and flow of your system more carefully. It's a bit like the difference between writing pseudocode and drawing boxes freehand — the structured format tends to produce clearer thinking.&lt;/p&gt;

&lt;h2&gt;
  
  
  LLMs love mermaid
&lt;/h2&gt;

&lt;p&gt;LLMs love mermaid format as it has a lot of key advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Models are trained to understand mermaid format quite extensively&lt;/li&gt;
&lt;li&gt;They are more compact and easier to follow the flow&lt;/li&gt;
&lt;li&gt;It's more efficient in terms of tokens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Essentially reading an ASCII diagram for an LLM is a waste of tokens, and it's harder to follow what the diagram is trying to explain, because of extra noise added by the decorative part of the diagram.&lt;br&gt;
Mermaid explains how a diagram works more efficiently.&lt;/p&gt;

&lt;p&gt;And where often LLMs have to digest massive amounts of tokens, having an efficient way to digest that information is fundamental.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mermaid uses way less tokens
&lt;/h3&gt;

&lt;p&gt;So let's compare the same diagram we showed above, and both format, and see what's the difference in tokens:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagram using ASCII&lt;/strong&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.amazonaws.com%2Fuploads%2Farticles%2Fuyft9b87kv5usoobvfgf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuyft9b87kv5usoobvfgf.png" alt="Diagram using ASCII" width="728" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diagram using mermaid&lt;/strong&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.amazonaws.com%2Fuploads%2Farticles%2Fzjizhz4s609xrmgug4kt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjizhz4s609xrmgug4kt.png" alt="Diagram using mermaid" width="763" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see above, the difference in tokens is massive: 55 tokens for the ASCII versus 10 of the mermaid, and this is a very simple diagram.&lt;/p&gt;

&lt;p&gt;But again, what it also matters, apart from the economy of tokens, is the fact that mermaid will be easier to understand for LLMs with way less chances to hallucinate.&lt;/p&gt;

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

&lt;p&gt;Documentation has always been important, but in the AI era, we need to think about two audiences: humans and machines. While ASCII diagrams are universal and need no special tooling to be read, they are noisy and inefficient for LLMs to process.&lt;/p&gt;

&lt;p&gt;Mermaid gives you the best of both worlds. It's still readable by humans, it renders beautifully on platforms like GitHub and GitLab, and most importantly, it communicates the same information to an LLM in a fraction of the tokens, with less risk of misinterpretation.&lt;/p&gt;

&lt;p&gt;So next time you're documenting your architecture in a &lt;code&gt;README.md&lt;/code&gt; or a &lt;code&gt;CLAUDE.md&lt;/code&gt; file, skip the ASCII art and reach for Mermaid instead. Your teammates will appreciate it, and your AI tools will thank you for it too.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>llm</category>
      <category>productivity</category>
      <category>tooling</category>
    </item>
    <item>
      <title>When and How to Use LRU Cache in Node.js Backend Projects</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Fri, 30 Jan 2026 14:09:36 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/when-and-how-to-use-lru-cache-in-nodejs-backend-projects-42c8</link>
      <guid>https://dev.to/darkmavis1980/when-and-how-to-use-lru-cache-in-nodejs-backend-projects-42c8</guid>
      <description>&lt;p&gt;If you've been working with Node.js for a while, you've probably encountered situations where your application makes repeated requests to fetch the same data, whether from a database, an external API, or some expensive computation. And if you're like me, you've probably thought "there must be a better way to handle this."&lt;/p&gt;

&lt;p&gt;Well, there is, and it's called caching. But not just any caching—today we're going to talk about LRU (Least Recently Used) caching and when it makes sense to use it in your Node.js applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an LRU Cache?
&lt;/h2&gt;

&lt;p&gt;Before we jump into the code, let's understand what an LRU cache actually is.&lt;/p&gt;

&lt;p&gt;An LRU cache is a data structure that stores a limited number of items and automatically removes the least recently used items when it reaches its capacity. Think of it like a small notebook where you keep notes, but you only have space for, let's say, 100 pages. When you need to add a new note and the notebook is full, you remove the oldest note you haven't looked at in a while.&lt;/p&gt;

&lt;p&gt;The beauty of LRU caching is that it keeps frequently accessed data readily available while automatically discarding data that hasn't been used recently, all without you having to manage it manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should you use LRU Cache?
&lt;/h2&gt;

&lt;p&gt;Let's be practical here. You &lt;strong&gt;should&lt;/strong&gt; use LRU caching when:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. You're making repeated expensive operations
&lt;/h3&gt;

&lt;p&gt;If you're querying a database for the same data multiple times, or calling an external API that returns relatively static data, caching can save you a lot of time and resources.&lt;/p&gt;

&lt;p&gt;For example, let's say you have an e-commerce application where product details don't change very often:&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;// Without caching - hits the database every time&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getProduct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM products WHERE id = ?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;product&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 1000 users are viewing the same product, you're making 1000 database calls for the exact same data. That's wasteful.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Your data doesn't change frequently
&lt;/h3&gt;

&lt;p&gt;LRU cache works best with data that's relatively static or changes infrequently. Things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User profile information&lt;/li&gt;
&lt;li&gt;Product catalogs&lt;/li&gt;
&lt;li&gt;Configuration settings&lt;/li&gt;
&lt;li&gt;API responses that update every few minutes/hours&lt;/li&gt;
&lt;li&gt;Computed results from heavy calculations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. You have memory to spare
&lt;/h3&gt;

&lt;p&gt;Remember, caching means storing data in memory. If your application is already running tight on memory, adding a cache might not be the best idea.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. You need to limit memory usage
&lt;/h3&gt;

&lt;p&gt;Unlike simple object caching where you might accidentally cache unlimited items and run out of memory, LRU cache has a built-in size limit. When it reaches capacity, it automatically evicts the least recently used items.&lt;/p&gt;

&lt;h2&gt;
  
  
  When you shouldn't use LRU Cache
&lt;/h2&gt;

&lt;p&gt;Just as important as knowing when to use it, is knowing when &lt;strong&gt;not&lt;/strong&gt; to use it:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. You're running multiple Node.js processes
&lt;/h3&gt;

&lt;p&gt;This is probably the most important limitation to understand. LRU cache stores data in memory &lt;strong&gt;within a single Node.js process&lt;/strong&gt;. If you're running multiple instances of your application (which you should be doing in production for reliability and performance), each process will have its own separate cache.&lt;/p&gt;

&lt;p&gt;Let me explain why this is a problem with a practical example.&lt;/p&gt;

&lt;p&gt;Let's say you have 3 Node.js processes running behind a load balancer:&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;// Process 1, 2, and 3 are all running this code&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM users WHERE id = ?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="nx"&gt;userCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now here's what happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User makes a request that hits Process 1 → cache miss, fetches from DB, caches it&lt;/li&gt;
&lt;li&gt;User makes another request that hits Process 2 → cache miss again! (different process, different cache)&lt;/li&gt;
&lt;li&gt;User makes another request that hits Process 3 → cache miss again!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So you're not really caching anything effectively across your application. You're just caching per process, which might give you some benefit, but nowhere near what you'd expect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Even worse&lt;/strong&gt;, if you update data in one process and invalidate the cache there, the other processes still have stale data:&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;// Update happens in Process 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UPDATE users SET ? WHERE id = ?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;updates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="nx"&gt;userCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Only clears cache in Process 1!&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// Process 2 and 3 still have the old cached data!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means LRU cache in Node.js is really only suitable for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small projects running a single Node.js process&lt;/li&gt;
&lt;li&gt;Development environments&lt;/li&gt;
&lt;li&gt;Simple scripts or tools that don't need to scale&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For production applications that need to scale horizontally, you should use a proper distributed cache like Redis or Memcached instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Your data changes constantly
&lt;/h3&gt;

&lt;p&gt;If you're caching data that updates every second, you're going to have stale data problems. For example, caching real-time stock prices or live sports scores wouldn't make much sense.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Your data is already fast to retrieve
&lt;/h3&gt;

&lt;p&gt;If your operation takes 5 milliseconds, adding a cache might actually slow things down due to the overhead of cache management. Don't optimize what doesn't need optimizing.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. You need guaranteed fresh data
&lt;/h3&gt;

&lt;p&gt;If you're building a banking application where every transaction must reflect the absolute current state, caching might introduce dangerous inconsistencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Your data is unique per request
&lt;/h3&gt;

&lt;p&gt;If every request requires different data that's never reused, caching won't help you. For example, caching user-specific data that's only accessed once doesn't make sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's see it in action
&lt;/h2&gt;

&lt;p&gt;Now that we understand the theory, let's build something practical. We'll use the &lt;code&gt;lru-cache&lt;/code&gt; package, which is the most popular LRU cache implementation for Node.js.&lt;/p&gt;

&lt;p&gt;First, install the package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;lru-cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A simple example
&lt;/h3&gt;

&lt;p&gt;Let's say we have a function that fetches user data from a database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;LRUCache&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lru-cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./database&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// your database connection&lt;/span&gt;

&lt;span class="c1"&gt;// Create a cache that holds up to 500 items&lt;/span&gt;
&lt;span class="c1"&gt;// Each item expires after 5 minutes&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 5 minutes in milliseconds&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&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="c1"&gt;// Check if we have it in cache&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cache hit!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Not in cache, fetch from database&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cache miss, fetching from DB...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SELECT * FROM users WHERE id = ?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// Store in cache for future requests&lt;/span&gt;
  &lt;span class="nx"&gt;userCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Fetches from DB&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns from cache!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A more complex real-world example
&lt;/h3&gt;

&lt;p&gt;Let's build a more practical example with an API that fetches weather data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;LRUCache&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lru-cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WeatherService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Cache weather data for each city&lt;/span&gt;
    &lt;span class="c1"&gt;// Max 100 cities, each cached for 30 minutes&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="c1"&gt;// Optional: what to do when an item is evicted&lt;/span&gt;
      &lt;span class="na"&gt;dispose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Evicting weather data for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Create a cache key&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cacheKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Try to get from cache&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&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="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;fromCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Not in cache, fetch from API&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`https://api.weatherapi.com/v1/current.json`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;WEATHER_API_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;city&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="nx"&gt;weatherData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;temp_c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;fetchedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="c1"&gt;// Store in cache&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;weatherData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;weatherData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;fromCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error fetching weather:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to fetch weather data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Clear cache for a specific city&lt;/span&gt;
  &lt;span class="nf"&gt;clearCity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Clear entire cache&lt;/span&gt;
  &lt;span class="nf"&gt;clearAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Get cache statistics&lt;/span&gt;
  &lt;span class="nf"&gt;getStats&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="na"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;max&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;WeatherService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's how you'd use it in your Express application:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;weatherService&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;WeatherService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/weather/:city&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;weather&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;weatherService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;weather&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/weather/cache/stats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;weatherService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStats&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/weather/cache/:city&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;weatherService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clearCity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Cache cleared for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Server running on port 3000&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when you call &lt;code&gt;/weather/london&lt;/code&gt; multiple times within 30 minutes, only the first request will hit the external API. The rest will be served from cache, making your application faster and reducing API costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced features you should know about
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;lru-cache&lt;/code&gt; package has some really useful features:&lt;/p&gt;

&lt;h3&gt;
  
  
  Size-based eviction
&lt;/h3&gt;

&lt;p&gt;Instead of limiting by number of items, you can limit by memory size:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maxSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// total size in arbitrary units&lt;/span&gt;
  &lt;span class="na"&gt;sizeCalculation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Calculate size of each item&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&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;h3&gt;
  
  
  Update TTL on access
&lt;/h3&gt;

&lt;p&gt;Keep frequently accessed items in cache longer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;updateAgeOnGet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;// Reset TTL when item is accessed&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Custom disposal
&lt;/h3&gt;

&lt;p&gt;Do cleanup when items are evicted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;dispose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reason&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="c1"&gt;// Clean up resources, log evictions, etc.&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reason&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;evict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; was evicted due to cache being full`&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;h2&gt;
  
  
  Some gotchas to watch out for
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Cache invalidation is hard
&lt;/h3&gt;

&lt;p&gt;The hardest problem in computer science is knowing when to invalidate your cache. If you cache user data and the user updates their profile, you need to clear that cache entry:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updateUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UPDATE users SET ? WHERE id = ?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;updates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="c1"&gt;// Don't forget to invalidate the cache!&lt;/span&gt;
  &lt;span class="nx"&gt;userCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Don't cache sensitive data carelessly
&lt;/h3&gt;

&lt;p&gt;Be careful about caching sensitive information. If you're caching user data, make sure you're not exposing one user's data to another:&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;// Bad: using just productId as key&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cacheKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Better: include user context if permissions matter&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cacheKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Memory leaks
&lt;/h3&gt;

&lt;p&gt;While LRU cache automatically manages size, you still need to be careful about what you cache. Don't cache huge objects or you'll run out of memory quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring the impact
&lt;/h2&gt;

&lt;p&gt;You should always measure whether caching is actually helping. Here's a simple way to track hit rates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CachedService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;hits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;misses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;misses&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getHitRate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;misses&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to migrate to Redis (or another distributed cache)
&lt;/h2&gt;

&lt;p&gt;Now, here's the thing: LRU cache is great for getting started, but as your application grows, you'll hit its limitations pretty quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signs you need to move to Redis
&lt;/h3&gt;

&lt;p&gt;You should consider migrating to Redis when:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You're running multiple Node.js processes&lt;/strong&gt; - This is the big one. As soon as you need to scale horizontally, in-memory caching becomes problematic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You need cache consistency across instances&lt;/strong&gt; - If one process updates data, all processes need to see the updated cache.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Your cache needs to survive restarts&lt;/strong&gt; - LRU cache is ephemeral. When your process restarts, the cache is gone. Redis persists data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You need more advanced features&lt;/strong&gt; - Redis offers pub/sub, sorted sets, lists, and many other data structures that LRU cache doesn't have.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The migration is simpler than you think
&lt;/h3&gt;

&lt;p&gt;The good news is that if you've been using LRU cache with a good abstraction layer (like the &lt;code&gt;CachedService&lt;/code&gt; examples above), migrating to Redis is straightforward.&lt;/p&gt;

&lt;p&gt;Here's how the same weather service would look with Redis:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WeatherService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redisClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REDIS_HOST&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REDIS_PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;6379&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redisClient&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="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cacheKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`weather:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Try to get from Redis&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redisClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&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="nx"&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="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;fromCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Not in cache, fetch from API&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`https://api.weatherapi.com/v1/current.json`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;WEATHER_API_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;city&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="nx"&gt;weatherData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;temp_c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;humidity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;fetchedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="c1"&gt;// Store in Redis with 30 minute expiration&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redisClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setEx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;cacheKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;1800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 30 minutes in seconds&lt;/span&gt;
        &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;weatherData&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="nx"&gt;weatherData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;fromCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error fetching weather:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to fetch weather data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;clearCity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;city&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redisClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;del&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`weather:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the interface is almost identical. The main difference is that Redis operations are asynchronous (they return promises), while LRU cache operations are synchronous.&lt;/p&gt;

&lt;h3&gt;
  
  
  My recommendation
&lt;/h3&gt;

&lt;p&gt;Start with LRU cache if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're building a prototype or small project&lt;/li&gt;
&lt;li&gt;You're running a single Node.js process&lt;/li&gt;
&lt;li&gt;You want to get something working quickly without infrastructure complexity&lt;/li&gt;
&lt;li&gt;You're just learning about caching concepts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Move to Redis when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to scale to multiple processes or servers&lt;/li&gt;
&lt;li&gt;You need cache persistence&lt;/li&gt;
&lt;li&gt;You need cache sharing across different services&lt;/li&gt;
&lt;li&gt;Your project is going to production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't feel bad about starting with LRU cache and migrating later. It's a perfectly valid approach, and the patterns you learn with LRU cache transfer directly to Redis.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measuring the impact
&lt;/h2&gt;

&lt;p&gt;You should always measure whether caching is actually helping. Here's a simple way to track hit rates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CachedService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LRUCache&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;hits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;misses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;misses&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getHitRate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;misses&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;LRU caching is a powerful tool that can significantly improve your application's performance when used correctly. But it's important to understand its limitations, especially around scaling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The key takeaway: LRU cache is perfect for small projects or single Node.js processes, but becomes problematic at scale.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's a quick decision tree:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Use it for expensive operations that return the same data repeatedly&lt;/li&gt;
&lt;li&gt;✅ Use it for relatively static data that doesn't change often&lt;/li&gt;
&lt;li&gt;✅ Use it when you need automatic memory management&lt;/li&gt;
&lt;li&gt;✅ Use it for prototypes and small projects with a single process&lt;/li&gt;
&lt;li&gt;✅ Use it to learn caching patterns before moving to Redis&lt;/li&gt;
&lt;li&gt;❌ Don't use it for data that changes constantly&lt;/li&gt;
&lt;li&gt;❌ Don't use it when operations are already fast&lt;/li&gt;
&lt;li&gt;❌ Don't use it for unique, one-time data&lt;/li&gt;
&lt;li&gt;❌ Don't use it when you have multiple Node.js processes (use Redis instead)&lt;/li&gt;
&lt;li&gt;❌ Don't use it when you need cache consistency across instances&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The reality is that most production Node.js applications run multiple processes for reliability and performance. As soon as you spin up a second instance, LRU cache loses much of its effectiveness because each process has its own isolated cache. This leads to cache misses, stale data, and inconsistent behavior.&lt;/p&gt;

&lt;p&gt;That said, LRU cache is still valuable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's a great learning tool to understand caching concepts&lt;/li&gt;
&lt;li&gt;It's perfect for CLI tools, scripts, or single-process applications&lt;/li&gt;
&lt;li&gt;It can be useful as a secondary cache layer in front of Redis&lt;/li&gt;
&lt;li&gt;It's excellent for local development and testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember, caching is an optimization technique. As with all optimizations, measure first, optimize second. Don't add complexity to your application unless you have data showing it's actually helping.&lt;/p&gt;

&lt;p&gt;And when you're ready to scale beyond a single process, the patterns you learned with LRU cache will transfer directly to Redis. The migration path is straightforward, and you'll be glad you started with something simple to understand the fundamentals.&lt;/p&gt;

&lt;p&gt;Finally, always remember the two hardest things in computer science: cache invalidation and naming things. Make sure you have a clear strategy for both, especially when dealing with distributed systems!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;DISCLAIMER&lt;/strong&gt;&lt;br&gt;
I used AI to help me out to write this article, mostly for grammar and to create quick samples code.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Redis Commander with Docker and Apple Silicon</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Sat, 18 Oct 2025 09:52:33 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/redis-commander-with-docker-and-apple-silicon-2a5m</link>
      <guid>https://dev.to/darkmavis1980/redis-commander-with-docker-and-apple-silicon-2a5m</guid>
      <description>&lt;p&gt;If you are here, it's probably because you either updated version of Docker Desktop or Rancher Desktop (or any application that runs Docker containers) and all the sudden a container, in my case &lt;a href="https://github.com/joeferner/redis-commander" rel="noopener noreferrer"&gt;redis-commander&lt;/a&gt; and you can't figure out why.&lt;br&gt;
I want to be clear, that could be other exotic issues, but the majority of times, and this is not specific to Redis Commander to be fair, the reason is because that specific image was build with a different platform.&lt;/p&gt;

&lt;p&gt;If you look at the &lt;a href="https://github.com/joeferner/redis-commander/blob/master/Dockerfile" rel="noopener noreferrer"&gt;Dockerfile&lt;/a&gt; in that specific project, you will see that it start with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;FROM alpine:3.21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if you look at what alpine is, it's just a minimal linux image, with basic support, but by default is built using the &lt;code&gt;amd64&lt;/code&gt; architecture.&lt;br&gt;
And to be clear, there's nothing wrong about it, it's just the default CPU architecture as it's the most common.&lt;/p&gt;

&lt;p&gt;But there's the issue if you have an Apple Silicon like me, like any M* processors from Apple.&lt;/p&gt;

&lt;p&gt;And when you start docker with the default image like the Alpine one, it will try to run a container that was build from a different architecture than the one your computer is running. Is like to ask to a Japanese person, that doesn't speak any english, to read an english book. He knows what a book is, he knows that's something written there, but it can't understand it.&lt;/p&gt;

&lt;p&gt;But don't worry, there's a simple solution. In docker you can just tell that, that specific image, it's written in a different architecture, so it can run it properly, using the &lt;code&gt;platform&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;So if you want to run Redis Commander with Docker (or any image that has the same issue) with Apple Silicon, you should running like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--name&lt;/span&gt; redis-commander &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--hostname&lt;/span&gt; redis-commander &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--platform&lt;/span&gt; linux/amd64 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--restart&lt;/span&gt; always &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;REDIS_HOSTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;local&lt;/span&gt;:redis:6379 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-p&lt;/span&gt; 8081:8081 &lt;span class="se"&gt;\&lt;/span&gt;
  ghcr.io/joeferner/redis-commander:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;--platform linux/amd64&lt;/code&gt;, that's the line that tells Docker to run it with that specific platform, and not the native one of your computer.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>docker</category>
      <category>linux</category>
    </item>
    <item>
      <title>Template Literals: Why Indentation Matters (Especially for AI Prompts)</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Thu, 14 Aug 2025 10:18:57 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/template-literals-why-indentation-matters-especially-for-ai-prompts-3jl4</link>
      <guid>https://dev.to/darkmavis1980/template-literals-why-indentation-matters-especially-for-ai-prompts-3jl4</guid>
      <description>&lt;p&gt;I've heard this argument quite a few times recently, especially in code related to prompt engineering, and sometimes the most trivial things can stir up quite a conversation between developers.&lt;/p&gt;

&lt;p&gt;You might see some code like this:&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="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello world
                      This is some multiline text
                      More text
`&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="c1"&gt;// some stuff here&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;Many developers think that's the right way to write a multiline string, as everything is indented according to the rest of the code. But there are two major issues with this approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is a template literal
&lt;/h2&gt;

&lt;p&gt;While at first glance the indentation might seem to make sense, when using a template literal (the string wrapped with backticks), you don't need to follow the rest of the code's indentation. That's because when that variable is rendered, it will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello world
              This is some multiline text
              More text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, that doesn't look correct at all — the extra spaces or tabs are taken into account and become part of the actual string content.&lt;/p&gt;

&lt;p&gt;Instead, according to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#multi-line_strings" rel="noopener noreferrer"&gt;language specifications&lt;/a&gt;, you should write the same code like this:&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="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello world
This is some multiline text
More text
`&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="c1"&gt;// some stuff here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Empty spaces count as tokens
&lt;/h2&gt;

&lt;p&gt;Another issue with the original code, especially when dealing with AI prompts, is that extra spaces or tabs count as tokens, which then translates to higher costs.&lt;/p&gt;

&lt;p&gt;It might seem like a small thing, but when dealing with large amounts of text, it can start to make a real difference in your AI usage costs.&lt;/p&gt;

&lt;p&gt;You can test this yourself by using &lt;a href="https://platform.openai.com/tokenizer" rel="noopener noreferrer"&gt;OpenAI's tokenizer tool&lt;/a&gt;. The same text we used above, without the extra spaces, generates 11 tokens, while the one with the extra spaces generates 14 tokens.&lt;/p&gt;

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

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

&lt;h2&gt;
  
  
  The bottom line
&lt;/h2&gt;

&lt;p&gt;When working with template literals, especially for prompt engineering, always remember that what you see in your code editor is exactly what gets sent to the AI model. Those extra spaces and tabs aren't just visual clutter — they're actual content that affects both the quality of your prompts and your costs.&lt;/p&gt;

&lt;p&gt;Keep your template literals clean and properly formatted, and your future self (and your budget) will thank you!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; AI was used to reformat this article and correct errors&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>ai</category>
      <category>promptengineering</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Secure your .env with dotenvx</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Mon, 07 Jul 2025 10:07:24 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/secure-your-env-with-dotenvx-3b67</link>
      <guid>https://dev.to/darkmavis1980/secure-your-env-with-dotenvx-3b67</guid>
      <description>&lt;p&gt;It's a fairly common practice to run a project locally and use a &lt;code&gt;.env&lt;/code&gt; file to load environmental variables from it. As long it's for local development, that's absolutely fine, then when running the code in any live or test environments you should fallback to actual system variables, never, ever, use &lt;code&gt;.env&lt;/code&gt; files in those cases.&lt;/p&gt;

&lt;p&gt;But while the above is generally a standard and acceptable practice, it has a very simple issue.&lt;/p&gt;

&lt;p&gt;Variables that represents secrets, are not so secret in your local environment, and in the era of AI coding tools, you might expose keys that could be potentially be end up in some LLM and eventually exposed to the internet if you are not careful enough.&lt;/p&gt;

&lt;p&gt;I get it, it's a bit of an extreme example, but it could theoretically happen!&lt;/p&gt;

&lt;p&gt;In Node.js, but also in other languages, you often use a variation of a &lt;code&gt;dotenv&lt;/code&gt; package to load those secrets in your code, but now there's a better alternative: &lt;a href="https://dotenvx.com/docs/install" rel="noopener noreferrer"&gt;dotenvx&lt;/a&gt;!&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# As a dependency of your project&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; @dotenvx/dotenvx &lt;span class="nt"&gt;--save&lt;/span&gt;

&lt;span class="c"&gt;# globally&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; @dotenvx/dotenvx

&lt;span class="c"&gt;# More ways can be found at https://dotenvx.com/docs/install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Some key differences
&lt;/h2&gt;

&lt;p&gt;What it differs from the other non-x package, is the fact that the latter can encrypt your secrets, using a public and private keys. &lt;/p&gt;

&lt;p&gt;And instead of being a Node.js package exclusively, you can use it to run a large number of languages, as you can run it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; &amp;lt;whatever&amp;gt;

&lt;span class="c"&gt;# example&lt;/span&gt;
dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; node main.js

&lt;span class="c"&gt;# or Python&lt;/span&gt;
dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; python3 main.py

&lt;span class="c"&gt;# or Rust&lt;/span&gt;
dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; cargo run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get the gist, you can run pretty much (almost) anything with it.&lt;/p&gt;

&lt;p&gt;Another advantage is that you don't really need to add it as a dependency in your project, unlike &lt;code&gt;dotenv&lt;/code&gt;, you can just run it with npx to run your command and inject the environmental variables, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"HELLO=World"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .env
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"console.log('Hello ' + process.env.HELLO)"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; index.js

node index.js
&lt;span class="c"&gt;# Hello undefined&lt;/span&gt;

npx @dotenvx/dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; node index.js
&lt;span class="c"&gt;# Hello World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you prefer, you can use it as a drop-in replacement for &lt;code&gt;dotenv&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="c1"&gt;// Before&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// After&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@dotenvx/dotenvx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;One thing I want to clarify, you don't encrypt the entire &lt;code&gt;.env&lt;/code&gt; file, you just encrypt variable by variable, and you don't need to encrypt them all, just the ones you want to keep secure.&lt;/p&gt;

&lt;p&gt;So stuff that are not secrets, but just configuration settings, don't really need to be encrypted.&lt;/p&gt;

&lt;p&gt;But let's set it in action to better understand how it works.&lt;/p&gt;

&lt;p&gt;Let's create a simple &lt;code&gt;.env&lt;/code&gt; with a few variables and normally you might have something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PROJECT_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Steve"&lt;/span&gt;
&lt;span class="nv"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1234
&lt;span class="nv"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"supersecret"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we don't really care if &lt;code&gt;PROJECT_NAME&lt;/code&gt; or if the &lt;code&gt;PORT&lt;/code&gt; are left in clear, but we really want to protect that secret. &lt;/p&gt;

&lt;p&gt;What do you need to do is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx @dotenvx/dotenvx &lt;span class="nb"&gt;set &lt;/span&gt;SECRET_KEY &lt;span class="s2"&gt;"supersecret"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above a few things happened.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;.env.keys&lt;/code&gt; created
&lt;/h3&gt;

&lt;p&gt;A new file &lt;code&gt;.env.keys&lt;/code&gt; is created. This create the private key that it will be used to encrypt/decrypt our secrets. This file &lt;strong&gt;must be added to .gitignore&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A public key is added
&lt;/h3&gt;

&lt;p&gt;On top of your &lt;code&gt;.env&lt;/code&gt; file you will see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#/-------------------[DOTENV_PUBLIC_KEY]--------------------/&lt;/span&gt;
&lt;span class="c"&gt;#/ public-key encryption for .env files                    /&lt;/span&gt;
&lt;span class="c"&gt;#/ [how it works](https://dotenvx.com/encryption)          /&lt;/span&gt;
&lt;span class="c"&gt;#/----------------------------------------------------------/&lt;/span&gt;

&lt;span class="nv"&gt;DOTENV_PUBLIC_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"0246dd40acee998b5bbc850c507dfaca0a49245c4da6eb9b0c9ae4219e5d83d292"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the public key used in conjunction with the secret one, that &lt;code&gt;dotenvx&lt;/code&gt; will use to decrypt the secrets when using &lt;code&gt;dotenvx run -- command&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your secret key is now encrypted
&lt;/h3&gt;

&lt;p&gt;Where before you had a clear string for your secret, now you have the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PROJECT_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Steve"&lt;/span&gt;
&lt;span class="nv"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"1234"&lt;/span&gt;
&lt;span class="nv"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"encrypted:BMQJJ2bEACUIPad/jtRIyDi6m0yBYhlcUBFREgvBCTYuLajcCtyJ3hB0PLMnBDY4CWxHphnuCAgobqyzL+LucTV8TAukr8rIPIr8D8me4LIfQu8WHqYj5DXicDKE9U35pOHbFWDbBE3iv2JR"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, only the &lt;code&gt;SECRET_KEY&lt;/code&gt; variable is encrypted, while the other variables are still in clear text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with multiple environments
&lt;/h2&gt;

&lt;p&gt;One really nice feature of &lt;code&gt;dotenvx&lt;/code&gt; is how it handles different environments. You can have separate &lt;code&gt;.env&lt;/code&gt; files for different environments and encrypt secrets differently for each one.&lt;/p&gt;

&lt;p&gt;For example, you might have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt; for local development&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env.production&lt;/code&gt; for production secrets&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env.staging&lt;/code&gt; for staging environment
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Encrypt secrets for different environments&lt;/span&gt;
npx @dotenvx/dotenvx &lt;span class="nb"&gt;set &lt;/span&gt;SECRET_KEY &lt;span class="s2"&gt;"local-secret"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; .env
npx @dotenvx/dotenvx &lt;span class="nb"&gt;set &lt;/span&gt;SECRET_KEY &lt;span class="s2"&gt;"prod-secret"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; .env.production
npx @dotenvx/dotenvx &lt;span class="nb"&gt;set &lt;/span&gt;SECRET_KEY &lt;span class="s2"&gt;"staging-secret"&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; .env.staging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can run your application with the appropriate environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Local development&lt;/span&gt;
npx @dotenvx/dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; node app.js

&lt;span class="c"&gt;# Production&lt;/span&gt;
npx @dotenvx/dotenvx run &lt;span class="nt"&gt;-f&lt;/span&gt; .env.production &lt;span class="nt"&gt;--&lt;/span&gt; node app.js

&lt;span class="c"&gt;# Staging&lt;/span&gt;
npx @dotenvx/dotenvx run &lt;span class="nt"&gt;-f&lt;/span&gt; .env.staging &lt;span class="nt"&gt;--&lt;/span&gt; node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A real-world example
&lt;/h2&gt;

&lt;p&gt;Let's say you're building an Express.js application that connects to a database and uses an API key. Your &lt;code&gt;app.js&lt;/code&gt; might look like this:&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;// app.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@dotenvx/dotenvx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Load environment variables&lt;/span&gt;
&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// Never log secrets in real applications!&lt;/span&gt;
    &lt;span class="na"&gt;hasDbUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hasApiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;API_KEY&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Your &lt;code&gt;.env&lt;/code&gt; file would contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;NODE_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"development"&lt;/span&gt;
&lt;span class="nv"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3000
&lt;span class="nv"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"encrypted:BK2Jsd9f8h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3jk2h3"&lt;/span&gt;
&lt;span class="nv"&gt;API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"encrypted:AL9Kme4g7j2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1m2l1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Important security considerations
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;dotenvx&lt;/code&gt; is much more secure than plain &lt;code&gt;.env&lt;/code&gt; files, there are still some best practices to follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Always add &lt;code&gt;.env.keys&lt;/code&gt; to your &lt;code&gt;.gitignore&lt;/code&gt;&lt;/strong&gt; - This file contains your private key and should never be committed to version control.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Share the &lt;code&gt;.env.keys&lt;/code&gt; file securely&lt;/strong&gt; - When working in a team, you'll need to share the private key securely (through encrypted channels, password managers, or secure key management systems).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rotate your keys regularly&lt;/strong&gt; - Just like any other secret, consider rotating your encryption keys periodically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Still use proper environment variables in production&lt;/strong&gt; - Even with &lt;code&gt;dotenvx&lt;/code&gt;, you should still use proper environment variables or secret management systems in production environments.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What about CI/CD?
&lt;/h2&gt;

&lt;p&gt;In your CI/CD pipeline, you can use &lt;code&gt;dotenvx&lt;/code&gt; by either:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Storing the private key as a secret&lt;/strong&gt; in your CI/CD system and creating the &lt;code&gt;.env.keys&lt;/code&gt; file at runtime&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Using a dedicated &lt;code&gt;.env.ci&lt;/code&gt; file&lt;/strong&gt; with encrypted secrets specific to your CI environment
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# In your CI/CD pipeline&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$DOTENV_PRIVATE_KEY&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .env.keys
npx @dotenvx/dotenvx run &lt;span class="nt"&gt;--&lt;/span&gt; npm &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;As you can see, &lt;code&gt;dotenvx&lt;/code&gt; is a much better and more secure alternative to traditional &lt;code&gt;.env&lt;/code&gt; files. It provides encryption for sensitive data while maintaining the simplicity of environment files, works across multiple programming languages, and offers flexible deployment options.&lt;/p&gt;

&lt;p&gt;The fact that you can selectively encrypt only the secrets you care about, while leaving configuration values in plain text, makes it practical for real-world use.&lt;/p&gt;

&lt;p&gt;I highly recommend you check out the project site at &lt;a href="https://dotenvx.com/" rel="noopener noreferrer"&gt;https://dotenvx.com/&lt;/a&gt; and give it a try in your next project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; AI was used to reformat this article and correct errors&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>dotenv</category>
      <category>programming</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Setting up DocMost with Amazon SES SMTP using Docker Compose</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Mon, 09 Jun 2025 23:03:14 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/setting-up-docmost-with-amazon-ses-smtp-using-docker-compose-5m7</link>
      <guid>https://dev.to/darkmavis1980/setting-up-docmost-with-amazon-ses-smtp-using-docker-compose-5m7</guid>
      <description>&lt;p&gt;If you're setting up DocMost with Docker and need to configure email functionality, you'll probably want to use a reliable SMTP service. Amazon SES (Simple Email Service) is an excellent choice for this, as it's cost-effective, reliable, and integrates well with other AWS services.&lt;/p&gt;

&lt;p&gt;In this guide, I'll walk you through the entire process of setting up Amazon SES as your SMTP provider for DocMost running in Docker containers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'll need
&lt;/h2&gt;

&lt;p&gt;Before we start, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An AWS account with appropriate permissions&lt;/li&gt;
&lt;li&gt;Docker and Docker Compose installed&lt;/li&gt;
&lt;li&gt;A domain you own (for email sending verification)&lt;/li&gt;
&lt;li&gt;Basic familiarity with AWS console&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Setting up Amazon SES
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Navigate to Amazon SES
&lt;/h3&gt;

&lt;p&gt;First, log into your AWS console and navigate to Amazon SES. You can find it by searching for "SES" in the services search bar, or under the "Customer Engagement" section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify your email domain
&lt;/h3&gt;

&lt;p&gt;Before you can send emails through SES, you need to verify your domain or email address.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the SES console, go to &lt;strong&gt;Configuration&lt;/strong&gt; → &lt;strong&gt;Verified identities&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create identity&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Domain&lt;/strong&gt; if you want to send from any email address on your domain, or &lt;strong&gt;Email address&lt;/strong&gt; for a specific email&lt;/li&gt;
&lt;li&gt;Enter your domain name (e.g., &lt;code&gt;example.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Follow the verification process by adding the required DNS records to your domain&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you're just testing, you can verify a single email address instead, but for production use, verifying the entire domain is recommended.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Request production access (if needed)
&lt;/h3&gt;

&lt;p&gt;By default, SES starts in "sandbox mode," which means you can only send emails to verified email addresses. For production use, you'll need to request production access:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the SES console, go to &lt;strong&gt;Account dashboard&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If you see "Your account is in the Amazon SES sandbox," click &lt;strong&gt;Request production access&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Fill out the form explaining your use case&lt;/li&gt;
&lt;li&gt;Wait for approval (usually takes 24-48 hours)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 2: Create SMTP credentials
&lt;/h2&gt;

&lt;p&gt;Now we need to create the actual SMTP credentials that DocMost will use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generate SMTP credentials
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;In the SES console, go to &lt;strong&gt;Configuration&lt;/strong&gt; → &lt;strong&gt;SMTP settings&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create SMTP credentials&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Enter a username for the credentials (e.g., &lt;code&gt;docmost-smtp&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create user&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Important&lt;/strong&gt;: Download the credentials immediately! You won't be able to see the password again&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your SMTP credentials will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SMTP Username: AKIAIOSFODNN7EXAMPLE
SMTP Password: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Note the SMTP endpoint
&lt;/h3&gt;

&lt;p&gt;On the same SMTP settings page, you'll see the SMTP endpoint for your AWS region. It will look like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;email-smtp.us-east-1.amazonaws.com&lt;/code&gt; (for US East)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;email-smtp.eu-west-1.amazonaws.com&lt;/code&gt; (for EU West)&lt;/li&gt;
&lt;li&gt;And so on for other regions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The port will typically be &lt;code&gt;587&lt;/code&gt; for STARTTLS or &lt;code&gt;465&lt;/code&gt; for SSL/TLS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Configure your Docker Compose file
&lt;/h2&gt;

&lt;p&gt;Now we need to configure DocMost to use these credentials. Here's how your &lt;code&gt;docker-compose.yml&lt;/code&gt; should look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.8'&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;docmost&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docmost/docmost:latest&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;redis&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;APP_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;http://localhost:3000"&lt;/span&gt;
      &lt;span class="na"&gt;APP_SECRET&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your-secret-key-here"&lt;/span&gt;
      &lt;span class="na"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgresql://docmost:STRONG_DB_PASSWORD@db:5432/docmost"&lt;/span&gt;
      &lt;span class="na"&gt;REDIS_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis://redis:6379"&lt;/span&gt;

      &lt;span class="c1"&gt;# Email configuration&lt;/span&gt;
      &lt;span class="na"&gt;MAIL_DRIVER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;smtp&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_HOST&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;email-smtp.us-east-1.amazonaws.com&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_PORT&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;587&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AKIAIOSFODNN7EXAMPLE&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_SECURE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;true"&lt;/span&gt;
      &lt;span class="na"&gt;MAIL_FROM_ADDRESS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;hello@yourdomain.com&lt;/span&gt;
      &lt;span class="na"&gt;MAIL_FROM_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Docmost&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3000:3000"&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;

  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:15-alpine&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docmost&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docmost&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;STRONG_DB_PASSWORD&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db_data:/var/lib/postgresql/data&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;

  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:7-alpine&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db_data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using environment variables (recommended)
&lt;/h3&gt;

&lt;p&gt;Instead of hardcoding credentials in your &lt;code&gt;docker-compose.yml&lt;/code&gt;, it's much better to use a &lt;code&gt;.env&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in the same directory as your &lt;code&gt;docker-compose.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# App configuration
APP_URL=http://localhost:3000
APP_SECRET=your-secret-key-here
DATABASE_URL=postgresql://docmost:STRONG_DB_PASSWORD@db:5432/docmost
REDIS_URL=redis://redis:6379

# Email configuration
MAIL_DRIVER=smtp
SMTP_HOST=email-smtp.us-east-1.amazonaws.com
SMTP_PORT=587
SMTP_USERNAME=AKIAIOSFODNN7EXAMPLE
SMTP_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
SMTP_SECURE=true
MAIL_FROM_ADDRESS=hello@yourdomain.com
MAIL_FROM_NAME=Docmost

# Database
POSTGRES_DB=docmost
POSTGRES_USER=docmost
POSTGRES_PASSWORD=STRONG_DB_PASSWORD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then update your &lt;code&gt;docker-compose.yml&lt;/code&gt; to use these variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.8'&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;docmost&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docmost/docmost:latest&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;redis&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;APP_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${APP_URL}&lt;/span&gt;
      &lt;span class="na"&gt;APP_SECRET&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${APP_SECRET}&lt;/span&gt;
      &lt;span class="na"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DATABASE_URL}&lt;/span&gt;
      &lt;span class="na"&gt;REDIS_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${REDIS_URL}&lt;/span&gt;
      &lt;span class="na"&gt;MAIL_DRIVER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${MAIL_DRIVER}&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_HOST&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${SMTP_HOST}&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_PORT&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${SMTP_PORT}&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${SMTP_USERNAME}&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${SMTP_PASSWORD}&lt;/span&gt;
      &lt;span class="na"&gt;SMTP_SECURE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${SMTP_SECURE}&lt;/span&gt;
      &lt;span class="na"&gt;MAIL_FROM_ADDRESS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${MAIL_FROM_ADDRESS}&lt;/span&gt;
      &lt;span class="na"&gt;MAIL_FROM_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${MAIL_FROM_NAME}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3000:3000"&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;

  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:15-alpine&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${POSTGRES_DB}&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${POSTGRES_USER}&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${POSTGRES_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db_data:/var/lib/postgresql/data&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;

  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:7-alpine&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db_data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Understanding the configuration
&lt;/h2&gt;

&lt;p&gt;Let me explain what each environment variable does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MAIL_DRIVER&lt;/strong&gt;: Set to &lt;code&gt;smtp&lt;/code&gt; to use SMTP for sending emails&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP_HOST&lt;/strong&gt;: The Amazon SES SMTP endpoint for your region&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP_PORT&lt;/strong&gt;: Usually &lt;code&gt;587&lt;/code&gt; for STARTTLS or &lt;code&gt;465&lt;/code&gt; for SSL/TLS&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP_USERNAME&lt;/strong&gt;: The SMTP username you got from SES&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP_PASSWORD&lt;/strong&gt;: The SMTP password you got from SES&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP_SECURE&lt;/strong&gt;: Set to &lt;code&gt;true&lt;/code&gt; to enable TLS encryption&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MAIL_FROM_ADDRESS&lt;/strong&gt;: The email address emails will be sent from (must be verified in SES)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MAIL_FROM_NAME&lt;/strong&gt;: The display name for outgoing emails&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 5: Start your containers
&lt;/h2&gt;

&lt;p&gt;Now you can start your DocMost instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up &lt;span class="nt"&gt;-d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the logs to make sure everything started correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose logs docmost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6: Test email functionality
&lt;/h2&gt;

&lt;p&gt;Once DocMost is running, you can test the email functionality by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating a new user account (this should send a welcome email)&lt;/li&gt;
&lt;li&gt;Using the password reset feature&lt;/li&gt;
&lt;li&gt;Inviting other users to your workspace&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If emails aren't being sent, check the DocMost logs for any SMTP-related error messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Troubleshooting common issues
&lt;/h2&gt;

&lt;h3&gt;
  
  
  "Username/Password incorrect" errors
&lt;/h3&gt;

&lt;p&gt;Double-check that you're using the SMTP credentials (not your AWS console credentials) and that you've copied them correctly from the SES console.&lt;/p&gt;

&lt;h3&gt;
  
  
  "Email address not verified" errors
&lt;/h3&gt;

&lt;p&gt;Make sure the email address in &lt;code&gt;MAIL_FROM_ADDRESS&lt;/code&gt; is either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An email address you've verified individually in SES, or&lt;/li&gt;
&lt;li&gt;An email address on a domain you've verified in SES&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Connection timeout errors
&lt;/h3&gt;

&lt;p&gt;Check that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your SMTP_HOST matches your AWS region&lt;/li&gt;
&lt;li&gt;Your SMTP_PORT is correct (587 or 465)&lt;/li&gt;
&lt;li&gt;Your network allows outbound connections on the SMTP port&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Still in sandbox mode
&lt;/h3&gt;

&lt;p&gt;If you're trying to send emails to unverified addresses and getting bounces, you might still be in SES sandbox mode. Either verify the recipient email addresses or request production access.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security considerations
&lt;/h2&gt;

&lt;p&gt;A few important security notes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Never commit credentials to version control&lt;/strong&gt;: Always use &lt;code&gt;.env&lt;/code&gt; files and add them to your &lt;code&gt;.gitignore&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use IAM roles when possible&lt;/strong&gt;: If you're running on EC2, consider using IAM roles instead of SMTP credentials&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rotate credentials regularly&lt;/strong&gt;: SES SMTP passwords don't expire, but it's good practice to rotate them periodically&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor your usage&lt;/strong&gt;: Keep an eye on your SES usage and set up billing alerts&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Setting up DocMost with Amazon SES provides you with a reliable, scalable email solution. The SMTP configuration is straightforward once you have your SES credentials, and using Docker Compose with environment variables keeps your setup clean and secure.&lt;/p&gt;

&lt;p&gt;The main thing to remember is that SES requires domain or email verification before you can send emails, and you'll need to request production access if you want to send to unverified email addresses.&lt;/p&gt;

&lt;p&gt;Once everything is configured, DocMost will be able to send welcome emails, password reset emails, and notifications to keep your team informed and engaged with your documentation platform.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: AI was used to reformat this article and correct errors&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>aws</category>
      <category>ses</category>
      <category>docker</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding MCP Servers Across Different Platforms: Claude Desktop vs VS Code vs Cursor</title>
      <dc:creator>Alessio Michelini</dc:creator>
      <pubDate>Mon, 28 Apr 2025 14:15:57 +0000</pubDate>
      <link>https://dev.to/darkmavis1980/understanding-mcp-servers-across-different-platforms-claude-desktop-vs-vs-code-vs-cursor-4opk</link>
      <guid>https://dev.to/darkmavis1980/understanding-mcp-servers-across-different-platforms-claude-desktop-vs-vs-code-vs-cursor-4opk</guid>
      <description>&lt;p&gt;When working with AI assistants like Claude, you may need to set up Model Context Protocol (MCP) servers to extend the capabilities of your AI tools. However, the configuration can vary significantly between different platforms. In this article, I'll break down how to configure MCP servers across Claude Desktop, VS Code, and Cursor, highlighting the key differences and providing practical examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are MCP Servers?
&lt;/h2&gt;

&lt;p&gt;Model Context Protocol (MCP) servers allow AI assistants to interact with external systems and tools. They act as intermediaries that enable AI models to access data, execute code, or perform actions outside their default capabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration Differences Between Platforms
&lt;/h2&gt;

&lt;p&gt;While Claude Desktop, Cursor, and VS Code all support MCP servers, the syntax and capabilities differ between them.&lt;/p&gt;

&lt;h3&gt;
  
  
  VS Code Configuration
&lt;/h3&gt;

&lt;p&gt;In VS Code, MCP servers need to be configured with this specific format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"servers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Your&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;servers&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;configuration&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;goes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Claude Desktop and Cursor Configuration
&lt;/h3&gt;

&lt;p&gt;For Claude Desktop and Cursor, the format is slightly different:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Your&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;servers&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;configuration&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;goes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Remote Servers: Platform Limitations
&lt;/h2&gt;

&lt;p&gt;One important distinction is how different platforms handle remote servers. While Cursor supports remote servers through SSE (Server-Sent Events), Claude Desktop has limitations.&lt;/p&gt;

&lt;p&gt;For example, this configuration works in Cursor but &lt;strong&gt;not&lt;/strong&gt; in Claude Desktop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"photos"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"transport"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:3001/sse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"TRANSPORT"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sse"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Solutions for Claude Desktop
&lt;/h2&gt;

&lt;p&gt;If you're using Claude Desktop, you have several alternatives to work around these limitations:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Local Command Execution via stdio
&lt;/h3&gt;

&lt;p&gt;The most straightforward way to get MCP servers working in Claude Desktop is to execute them locally using stdio:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"photos"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"/path/to/your/server.js"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Using NPM Packages
&lt;/h3&gt;

&lt;p&gt;Another option is to create and publish an NPM package, then invoke it with &lt;code&gt;npx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"github"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"-y"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"@modelcontextprotocol/server-github"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"GITHUB_PERSONAL_ACCESS_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;YOUR-TOKEN&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Docker Containers
&lt;/h3&gt;

&lt;p&gt;For more complex setups, you can use Docker containers (requires Docker to be installed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"github"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"docker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"run"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"-i"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--rm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"-e"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"GITHUB_PERSONAL_ACCESS_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"ghcr.io/github/github-mcp-server"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"GITHUB_PERSONAL_ACCESS_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;YOUR-TOKEN&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;p&gt;Based on the available options, here are some recommendations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For cross-platform compatibility&lt;/strong&gt;: If you need your configuration to work across all platforms, stick with the local command execution approach.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For simplicity&lt;/strong&gt;: Using published NPM packages with &lt;code&gt;npx&lt;/code&gt; offers a clean solution if you don't want to manage local server files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;For isolation and dependencies&lt;/strong&gt;: Docker containers provide the best isolation and dependency management but require Docker to be installed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Understanding the differences in MCP server configurations across platforms is crucial for effectively extending your AI assistants' capabilities. While Claude Desktop has some limitations regarding remote servers, multiple workarounds are available through local execution, NPM packages, or Docker containers.&lt;/p&gt;

&lt;p&gt;By selecting the right approach for your specific needs, you can ensure your MCP servers work consistently across Claude Desktop, VS Code, and Cursor, enhancing your AI-assisted development workflow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: AI was used to reformat this article and correct errors&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>mcp</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
