<?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: Kazu</title>
    <description>The latest articles on DEV Community by Kazu (@shinagawa-web).</description>
    <link>https://dev.to/shinagawa-web</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%2F3422958%2F561728f2-3289-4079-9cba-cc1c855c8b68.png</url>
      <title>DEV Community: Kazu</title>
      <link>https://dev.to/shinagawa-web</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shinagawa-web"/>
    <language>en</language>
    <item>
      <title>When malware runs through automated dependency updates</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Tue, 24 Nov 2026 13:04:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/when-malware-runs-through-automated-dependency-updates-4a11</link>
      <guid>https://dev.to/shinagawa-web/when-malware-runs-through-automated-dependency-updates-4a11</guid>
      <description>&lt;p&gt;Dependabot and Renovate PRs get merged automatically once CI passes. AI agents are left to update lockfiles. There are reported cases where setups like these, which pull in new dependency versions automatically, pulled in a version that contained malware.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;You cannot tell whether a version contains malware from how its version number changes. In past cases, the malicious versions were published as patch or minor releases, the same as ordinary updates.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; Limiting auto-merge to patch and minor does not keep them out.&lt;/p&gt;

&lt;p&gt;This article lays out where such malware runs and what it does, then looks at settings that reduce the damage and at the logs you need to notice that it ran.&lt;/p&gt;

&lt;h2&gt;
  
  
  How new versions get pulled in automatically
&lt;/h2&gt;

&lt;p&gt;New dependency versions get pulled in automatically in three ways.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auto-merging Dependabot or Renovate PRs&lt;/li&gt;
&lt;li&gt;A person or an AI agent bumping the lockfile with &lt;code&gt;uv lock --upgrade&lt;/code&gt; or &lt;code&gt;npm update&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Referencing GitHub Actions or container images by tag. What a tag points to can be changed later&lt;sup id="fnref3"&gt;3&lt;/sup&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Auto-merge has settings that wait a number of days after a release before updating (Dependabot's cooldown,&lt;sup id="fnref4"&gt;4&lt;/sup&gt; Renovate's minimumReleaseAge&lt;sup id="fnref5"&gt;5&lt;/sup&gt;). They do not check whether a version contains malware. They only mean that if the version is removed from the registry during the wait, you never pull it in. Lockfile updates and tag references are not covered by these settings. There is a limit to what can be stopped before it comes in, so you need settings that reduce the damage after it does, and logs for noticing that it ran.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where malware runs and what it does
&lt;/h2&gt;

&lt;p&gt;Malware does not run only in production, and the damage is not limited to exfiltration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where it runs
&lt;/h3&gt;

&lt;p&gt;Malware that runs at install time (npm &lt;code&gt;preinstall&lt;/code&gt;, &lt;code&gt;postinstall&lt;/code&gt;, and so on) runs wherever dependencies are installed: CI, Docker builds, and local machines.&lt;sup id="fnref6"&gt;6&lt;/sup&gt; Malware that runs when the package is loaded or when Python starts also runs in production.&lt;sup id="fnref7"&gt;7&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Exfiltration
&lt;/h4&gt;

&lt;p&gt;It can steal the credentials present where it runs:&lt;sup id="fnref8"&gt;8&lt;/sup&gt; in CI, tokens in environment variables and secrets passed to Docker builds;&lt;sup id="fnref9"&gt;9&lt;/sup&gt; on local machines, credential files on disk;&lt;sup id="fnref10"&gt;10&lt;/sup&gt; in production, credentials passed to the process and credentials available from the cloud metadata service.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cryptocurrency mining
&lt;/h4&gt;

&lt;p&gt;The malware uses the CPU where it runs to mine cryptocurrency.&lt;sup id="fnref11"&gt;11&lt;/sup&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Deleting and encrypting files
&lt;/h4&gt;

&lt;p&gt;The malware deletes&lt;sup id="fnref12"&gt;12&lt;/sup&gt; or encrypts files it can write to. Ransomware that encrypts files and demands payment has so far appeared in fake packages with lookalike names.&lt;sup id="fnref13"&gt;13&lt;/sup&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Stopping processes
&lt;/h4&gt;

&lt;p&gt;A process that loads the dependency stops responding, for example because of an infinite loop.&lt;sup id="fnref14"&gt;14&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Settings that reduce the damage
&lt;/h2&gt;

&lt;p&gt;The settings below assume CI runs on GitHub Actions and production runs in Docker containers. Local machines are not covered, because each developer's environment is different.&lt;/p&gt;

&lt;h3&gt;
  
  
  Turn off install scripts
&lt;/h3&gt;

&lt;p&gt;Install-time malware does not run if install scripts are turned off when CI and Docker builds install dependencies. With npm, add &lt;code&gt;--ignore-scripts&lt;/code&gt;.&lt;sup id="fnref15"&gt;15&lt;/sup&gt; Since v10, pnpm does not run them by default.&lt;sup id="fnref16"&gt;16&lt;/sup&gt; This does not stop malware that runs at load time or at startup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reduce what can be exfiltrated
&lt;/h3&gt;

&lt;p&gt;In GitHub Actions, reduce the credentials passed to jobs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limit &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; to the permissions it needs, using &lt;code&gt;permissions&lt;/code&gt; on the workflow or job&lt;sup id="fnref17"&gt;17&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;Put deploy secrets in an environment. Environment secrets are passed only to jobs that use that environment&lt;sup id="fnref18"&gt;18&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the cloud with OIDC, so no long-lived credentials sit in secrets.&lt;sup id="fnref19"&gt;19&lt;/sup&gt; Publishing to npm with trusted publishing also needs no long-lived token&lt;sup id="fnref20"&gt;20&lt;/sup&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Docker builds, do not pass secrets through &lt;code&gt;ARG&lt;/code&gt;. Use &lt;code&gt;RUN --mount=type=secret&lt;/code&gt; to pass them only to the &lt;code&gt;RUN&lt;/code&gt; that uses them.&lt;sup id="fnref9"&gt;9&lt;/sup&gt; In production, pass a container only the environment variables it uses through &lt;code&gt;docker run&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limit mining
&lt;/h3&gt;

&lt;p&gt;Set a CPU limit with &lt;code&gt;docker run --cpus&lt;/code&gt;. By default there is no limit; with one, the container is throttled when it goes over.&lt;sup id="fnref21"&gt;21&lt;/sup&gt; The mining does not stop, but CPU is left for the other containers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prepare for deletion and encryption
&lt;/h3&gt;

&lt;p&gt;Switch to a non-root user with &lt;code&gt;USER&lt;/code&gt; in the Dockerfile, and make the root filesystem read-only with &lt;code&gt;docker run --read-only&lt;/code&gt;. Put the paths that need writes on volumes,&lt;sup id="fnref22"&gt;22&lt;/sup&gt; and make sure they can be restored from backup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prepare for stopped processes
&lt;/h3&gt;

&lt;p&gt;With a &lt;code&gt;HEALTHCHECK&lt;/code&gt; in the Dockerfile, a container that does not respond becomes &lt;code&gt;unhealthy&lt;/code&gt;.&lt;sup id="fnref23"&gt;23&lt;/sup&gt; Only the status changes, so make the deploy procedure stop the old container only after the container running the new version becomes &lt;code&gt;healthy&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logs for noticing that it ran
&lt;/h2&gt;

&lt;p&gt;What Docker keeps is the container's stdout and stderr (&lt;code&gt;docker logs&lt;/code&gt;) and events such as container start and exit (&lt;code&gt;docker events&lt;/code&gt;).&lt;sup id="fnref24"&gt;24&lt;/sup&gt; To notice DNS queries, outbound connections, and file changes, you need something separate that records them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Noticing exfiltration
&lt;/h3&gt;

&lt;p&gt;Exfiltration does not require starting a new process, so it does not necessarily show up in a record of process starts. Record DNS queries and outbound connections in a form that shows which container they came from.&lt;/p&gt;

&lt;p&gt;Records of stolen credentials being used are left where they were used. For cloud credentials, check the cloud's API call logs. For an npm token, check the list of published versions. For a database password, check the database's connection log.&lt;/p&gt;

&lt;h3&gt;
  
  
  Noticing mining
&lt;/h3&gt;

&lt;p&gt;Check that CPU usage per container is being recorded. With a limit in place, usage sits at the limit and does not come down.&lt;/p&gt;

&lt;h3&gt;
  
  
  Noticing deletion and encryption
&lt;/h3&gt;

&lt;p&gt;Record which processes started and which files they modified.&lt;/p&gt;

&lt;h3&gt;
  
  
  Noticing stopped processes
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker events&lt;/code&gt; shows container starts and exits and changes in &lt;code&gt;health_status&lt;/code&gt;. Without &lt;code&gt;--since&lt;/code&gt;, though, it only shows events from that point on, and there is a cap on how many past events it returns, so ship the events elsewhere and keep them.&lt;sup id="fnref25"&gt;25&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Which version ran where
&lt;/h3&gt;

&lt;p&gt;When you see a malware report, check where that version has been running in your environment, and since when. GitHub Actions workflow run logs are kept for 90 days by default.&lt;sup id="fnref26"&gt;26&lt;/sup&gt; Keep the deploy times, along with the commit and the image digest for each deploy, for longer than that. If images are referenced by digest, what is running is pinned.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;I think the size of the damage is decided by what you put where the malware runs and what you allow there. If you keep pulling in dependencies automatically, I would cut the credentials passed to jobs and the permissions given to containers before lengthening the cooldown.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://blog.gitguardian.com/renovate-dependabot-the-new-malware-delivery-system/" rel="noopener noreferrer"&gt;GitGuardian&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Patch releases: chalk and debug (&lt;a href="https://www.aikido.dev/blog/npm-debug-and-chalk-packages-compromised" rel="noopener noreferrer"&gt;Aikido&lt;/a&gt;), LiteLLM 1.82.7 and 1.82.8 (&lt;a href="https://github.com/BerriAI/litellm/issues/24518" rel="noopener noreferrer"&gt;litellm#24518&lt;/a&gt;). Minor release: LiteLLM 1.107.0 (&lt;a href="https://blog.flatt.tech/entry/litellm_2nd_compromise" rel="noopener noreferrer"&gt;Flatt Security&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;For GitHub Actions, the &lt;a href="https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions" rel="noopener noreferrer"&gt;Secure use reference&lt;/a&gt; says action tags can be moved or deleted. A container image referenced by digest is pinned to the same image (the &lt;code&gt;docker image pull&lt;/code&gt; &lt;a href="https://docs.docker.com/reference/cli/docker/image/pull/" rel="noopener noreferrer"&gt;reference&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;&lt;a href="https://docs.github.com/en/code-security/dependabot/working-with-dependabot/dependabot-options-reference" rel="noopener noreferrer"&gt;Dependabot options reference&lt;/a&gt;. Since 2026-07-14, a 3-day cooldown applies even when none is configured (&lt;a href="https://github.blog/changelog/2026-07-14-dependabot-version-updates-introduce-default-package-cooldown/" rel="noopener noreferrer"&gt;changelog&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;Renovate's &lt;a href="https://docs.renovatebot.com/configuration-options/#minimumreleaseage" rel="noopener noreferrer"&gt;minimumReleaseAge&lt;/a&gt;. It does not apply unless configured.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;One example is Shai-Hulud 2.0, which ran in &lt;code&gt;preinstall&lt;/code&gt; (&lt;a href="https://securitylabs.datadoghq.com/articles/shai-hulud-2.0-npm-worm/" rel="noopener noreferrer"&gt;Datadog Security Labs&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;One example is LiteLLM 1.82.8, which used a &lt;code&gt;.pth&lt;/code&gt; file and ran every time Python started (&lt;a href="https://github.com/BerriAI/litellm/issues/24512" rel="noopener noreferrer"&gt;litellm#24512&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;LiteLLM 1.82.8 collected environment variables with &lt;code&gt;printenv&lt;/code&gt; and sent them out (&lt;a href="https://github.com/BerriAI/litellm/issues/24512" rel="noopener noreferrer"&gt;litellm#24512&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;Secrets mounted with &lt;code&gt;RUN --mount=type=secret&lt;/code&gt; are placed at &lt;code&gt;/run/secrets/&amp;lt;id&amp;gt;&lt;/code&gt; only for the duration of that &lt;code&gt;RUN&lt;/code&gt;, so install-time malware running in that &lt;code&gt;RUN&lt;/code&gt; can read them too (Docker documentation, &lt;a href="https://docs.docker.com/build/building/secrets/" rel="noopener noreferrer"&gt;Build secrets&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;Shai-Hulud 2.0 collected credential files on disk, such as gcloud's &lt;code&gt;application_default_credentials.json&lt;/code&gt;, and used the npm token in &lt;code&gt;.npmrc&lt;/code&gt; and the GitHub credentials on disk (&lt;a href="https://securitylabs.datadoghq.com/articles/shai-hulud-2.0-npm-worm/" rel="noopener noreferrer"&gt;Datadog Security Labs&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn11"&gt;
&lt;p&gt;The malware in ultralytics ran XMRig, a cryptocurrency miner (&lt;a href="https://blog.pypi.org/posts/2024-12-11-ultralytics-attack-analysis/" rel="noopener noreferrer"&gt;PyPI Blog&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn12"&gt;
&lt;p&gt;Shai-Hulud 2.0 deleted files in the home directory when it could not steal anything (&lt;a href="https://securitylabs.datadoghq.com/articles/shai-hulud-2.0-npm-worm/" rel="noopener noreferrer"&gt;Datadog Security Labs&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn13"&gt;
&lt;p&gt;&lt;a href="https://socket.dev/blog/black-basta-dependency-confusion-ambitions-and-ransomware-in-open-source-ecosystems" rel="noopener noreferrer"&gt;Socket&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn14"&gt;
&lt;p&gt;The maintainer of colors published a version that enters an infinite loop when loaded (&lt;a href="https://snyk.io/blog/open-source-maintainer-pulls-the-plug-on-npm-packages-colors-and-faker-now-what/" rel="noopener noreferrer"&gt;Snyk&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn15"&gt;
&lt;p&gt;&lt;code&gt;ignore-scripts&lt;/code&gt; in npm's &lt;a href="https://docs.npmjs.com/cli/v11/using-npm/config#ignore-scripts" rel="noopener noreferrer"&gt;config&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn16"&gt;
&lt;p&gt;pnpm &lt;a href="https://pnpm.io/10.x/settings" rel="noopener noreferrer"&gt;Settings&lt;/a&gt;. A gap that let install scripts run for dependencies fetched from git was fixed in 10.26.0 (&lt;a href="https://github.com/pnpm/pnpm/security/advisories/GHSA-379q-355j-w6rj" rel="noopener noreferrer"&gt;GHSA-379q-355j-w6rj&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn17"&gt;
&lt;p&gt;&lt;a href="https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication" rel="noopener noreferrer"&gt;Use GITHUB_TOKEN for authentication in workflows&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn18"&gt;
&lt;p&gt;&lt;a href="https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-deployments/managing-environments-for-deployment" rel="noopener noreferrer"&gt;Managing environments for deployment&lt;/a&gt;. If you set required reviewers, the job cannot read the secrets until it is approved.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn19"&gt;
&lt;p&gt;&lt;a href="https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/about-security-hardening-with-openid-connect" rel="noopener noreferrer"&gt;About security hardening with OpenID Connect&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn20"&gt;
&lt;p&gt;npm &lt;a href="https://docs.npmjs.com/trusted-publishers" rel="noopener noreferrer"&gt;Trusted publishing&lt;/a&gt;. It applies to GitHub-hosted runners.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn21"&gt;
&lt;p&gt;Docker documentation, &lt;a href="https://docs.docker.com/engine/containers/resource_constraints/" rel="noopener noreferrer"&gt;Resource constraints&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn22"&gt;
&lt;p&gt;&lt;a href="https://docs.docker.com/reference/dockerfile/#user" rel="noopener noreferrer"&gt;USER&lt;/a&gt; in the Dockerfile reference and &lt;a href="https://docs.docker.com/reference/cli/docker/container/run/" rel="noopener noreferrer"&gt;--read-only&lt;/a&gt; in the &lt;code&gt;docker run&lt;/code&gt; reference.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn23"&gt;
&lt;p&gt;&lt;a href="https://docs.docker.com/reference/dockerfile/#healthcheck" rel="noopener noreferrer"&gt;HEALTHCHECK&lt;/a&gt; in the Dockerfile reference. The status changes and a &lt;code&gt;health_status&lt;/code&gt; event is emitted. The reference does not say that the container is stopped or restarted.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn24"&gt;
&lt;p&gt;Docker documentation, &lt;a href="https://docs.docker.com/engine/logging/" rel="noopener noreferrer"&gt;View container logs&lt;/a&gt;, and the &lt;code&gt;docker events&lt;/code&gt; &lt;a href="https://docs.docker.com/reference/cli/docker/system/events/" rel="noopener noreferrer"&gt;reference&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn25"&gt;
&lt;p&gt;The &lt;code&gt;docker events&lt;/code&gt; &lt;a href="https://docs.docker.com/reference/cli/docker/system/events/" rel="noopener noreferrer"&gt;reference&lt;/a&gt;. When looking back, it returns at most the 256 most recent events.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn26"&gt;
&lt;p&gt;&lt;a href="https://docs.github.com/en/organizations/managing-organization-settings/configuring-the-retention-period-for-github-actions-artifacts-and-logs-in-your-organization" rel="noopener noreferrer"&gt;Configuring the retention period for GitHub Actions artifacts and logs in your organization&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>dependabot</category>
      <category>security</category>
      <category>devops</category>
      <category>docker</category>
    </item>
    <item>
      <title>Cluster Autoscaler not scaling down nodes: finding the reason in its logs</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Fri, 20 Nov 2026 13:06:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/cluster-autoscaler-not-scaling-down-nodes-finding-the-reason-in-its-logs-1742</link>
      <guid>https://dev.to/shinagawa-web/cluster-autoscaler-not-scaling-down-nodes-finding-the-reason-in-its-logs-1742</guid>
      <description>&lt;p&gt;To cut node costs, you lowered the requests on your Pods and deployed. Requests are the amount of CPU and memory reserved for each container in a Pod. Allocatable is the amount of a node's CPU and memory that can be assigned to Pods. When you check it yourself, for the resource you lowered, the sum of requests on every node is below half of allocatable.&lt;/p&gt;

&lt;p&gt;Cluster Autoscaler (CA) removes a node when the sum of requests on it falls below half of allocatable.&lt;sup id="fnref1"&gt;1&lt;/sup&gt; Yet an hour later the node count hasn't changed, and the CA logs have no error lines (lines starting with &lt;code&gt;E&lt;/code&gt;). The bill doesn't go down until nodes are removed.&lt;/p&gt;

&lt;p&gt;CA logs, for each node, the reason it decided not to remove it. The line it prints tells you the reason and where to fix it.&lt;/p&gt;

&lt;p&gt;I reproduced this on a test cluster, triggered each reason for not removing a node one at a time, and checked which line CA logs for each.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the CA arguments
&lt;/h2&gt;

&lt;p&gt;Before reading the logs, look at &lt;code&gt;--v&lt;/code&gt; and &lt;code&gt;--unremovable-node-recheck-timeout&lt;/code&gt; in the configuration you installed CA with (Helm values or a manifest).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--v&lt;/code&gt; sets how much detail CA writes to its log. Below 4, a node whose sum of requests is at or above half of allocatable in CA's calculation gets no line giving that reason.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--unremovable-node-recheck-timeout&lt;/code&gt; (default 5 minutes) is how long CA waits before re-evaluating a node it decided it can't remove because the Pods on it can't be moved to other nodes. During that time, no line appears for that node.&lt;sup id="fnref3"&gt;3&lt;/sup&gt; Set &lt;code&gt;--since&lt;/code&gt; in the command in the next section longer than this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Log lines and reasons
&lt;/h2&gt;

&lt;p&gt;Extract from the CA logs the lines that give the reasons for not removing nodes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl logs -n &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;CA namespace&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; deploy/&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;CA Deployment name&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --since=10m \
  &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'node group min size reached|marked as no scale down|above the scale-down utilization threshold|cannot be removed:|is not suitable for removal:'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Match each line to the table below by node name. CA checks a node against the table from the first row down and logs only the line for the first reason that matches.&lt;sup id="fnref4"&gt;4&lt;/sup&gt; To evict, in the table, means to move the Pods on a node to other nodes before removing the node. A node group is a set of nodes whose count CA scales up and down; on EKS it corresponds to an Auto Scaling group.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Reason&lt;/th&gt;
&lt;th&gt;String to grep&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The node is not managed by CA&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The node count has reached the node group's minimum size&lt;/td&gt;
&lt;td&gt;&lt;code&gt;node group min size reached&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The node has the &lt;code&gt;scale-down-disabled&lt;/code&gt; annotation&lt;/td&gt;
&lt;td&gt;&lt;code&gt;marked as no scale down&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;In CA's calculation, the sum of requests is at or above half of allocatable&lt;/td&gt;
&lt;td&gt;&lt;code&gt;above the scale-down utilization threshold&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A Pod can't be evicted&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cannot be removed:&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The Pods have nowhere to go&lt;/td&gt;
&lt;td&gt;&lt;code&gt;is not suitable for removal:&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the output of the command above, a node that gets no line at all while other nodes do is either not managed by CA or a node CA has decided to remove. A node CA has decided to remove gets a &lt;code&gt;may be removed&lt;/code&gt; line, so grep the CA logs for that string as well. If that doesn't appear either, the node is not managed by CA.&lt;/p&gt;

&lt;h2&gt;
  
  
  How each reason happens
&lt;/h2&gt;

&lt;p&gt;As the previous section says, CA logs only the line for the first reason that matches a node. So after you fix the reason shown, if the same node also matches another reason or another Pod, that line appears next and the node still isn't removed. The top three reasons in the table have nothing to do with requests, so they already applied before you lowered requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  The node is not managed by CA
&lt;/h3&gt;

&lt;p&gt;A node that isn't in a node group is not evaluated by CA.&lt;sup id="fnref5"&gt;5&lt;/sup&gt; To fix this, let CA find the node group. The AWS example manifest uses &lt;code&gt;--node-group-auto-discovery&lt;/code&gt; to look for Auto Scaling groups tagged with &lt;code&gt;k8s.io/cluster-autoscaler/enabled&lt;/code&gt; and &lt;code&gt;k8s.io/cluster-autoscaler/&amp;lt;cluster name&amp;gt;&lt;/code&gt;, so add these tags to the groups you want CA to manage.&lt;sup id="fnref6"&gt;6&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The node count has reached the node group's minimum size
&lt;/h3&gt;

&lt;p&gt;The line has the form &lt;code&gt;Skipping &amp;lt;node name&amp;gt; - node group min size reached (current: N, min: M)&lt;/code&gt;, with the current count and the minimum size.&lt;sup id="fnref5"&gt;5&lt;/sup&gt; If you want fewer nodes, lower the minimum size of the Auto Scaling group or its equivalent.&lt;/p&gt;

&lt;h3&gt;
  
  
  The node has the &lt;code&gt;scale-down-disabled&lt;/code&gt; annotation
&lt;/h3&gt;

&lt;p&gt;If a node has &lt;code&gt;cluster-autoscaler.kubernetes.io/scale-down-disabled: "true"&lt;/code&gt;, CA doesn't remove it.&lt;sup id="fnref7"&gt;7&lt;/sup&gt; If the node is fine to remove, delete this annotation.&lt;/p&gt;

&lt;h3&gt;
  
  
  In CA's calculation, the sum of requests is at or above half of allocatable
&lt;/h3&gt;

&lt;p&gt;The value you checked can be below half while CA's calculation puts it at or above half. The line shows which resource was at what percentage in CA's calculation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node &amp;lt;node name&amp;gt; unremovable: cpu requested (53.75% of allocatable) is above the scale-down utilization threshold
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One cause of the gap is which resource you lowered. CA divides the sum of requests by allocatable separately for CPU and for memory, and checks whether the higher of the two is at or above half.&lt;sup id="fnref8"&gt;8&lt;/sup&gt; If you lowered only memory and CPU is at or above half, CA doesn't remove the node.&lt;/p&gt;

&lt;p&gt;The other is sidecars. The requests CA adds up include the requests of sidecars (initContainers with &lt;code&gt;restartPolicy: Always&lt;/code&gt;). In kube-state-metrics, &lt;code&gt;kube_pod_container_resource_requests&lt;/code&gt; covers only regular containers, and sidecars appear separately in &lt;code&gt;kube_pod_init_container_resource_requests&lt;/code&gt;.&lt;sup id="fnref9"&gt;9&lt;/sup&gt; So the following PromQL leaves out the sidecars.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sum by (node) (kube_pod_container_resource_requests{resource="cpu"})
  / on (node) kube_node_status_allocatable{resource="cpu"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this, review the requests for the resource shown in the log, including the sidecars.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Pod can't be evicted
&lt;/h3&gt;

&lt;p&gt;The line has the form below. After &lt;code&gt;cannot be removed:&lt;/code&gt; come the reason eviction was blocked and the name of the Pod blocking it.&lt;sup id="fnref10"&gt;10&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node &amp;lt;node name&amp;gt; cannot be removed: &amp;lt;namespace&amp;gt;/&amp;lt;Pod name&amp;gt; is not replicated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pods that aren't recreated
&lt;/h4&gt;

&lt;p&gt;If you see &lt;code&gt;is not replicated&lt;/code&gt;, the blocker is a Pod that was not created by a ReplicaSet (which a Deployment creates), a StatefulSet, or a Job.&lt;sup id="fnref11"&gt;11&lt;/sup&gt; This covers Pods created directly and Pods created by custom controllers (such as CRD operators). Change them to be created from a Deployment or similar.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pods involving PDBs
&lt;/h4&gt;

&lt;p&gt;If you see &lt;code&gt;non-pdb-assigned kube-system pod present&lt;/code&gt;, the blocker is a kube-system Pod without a PDB. A PDB (PodDisruptionBudget) is an object that sets how many Pods may be stopped at the same time during a drain or a CA eviction. kube-system holds many Pods the whole cluster relies on, such as DNS, so CA doesn't evict a kube-system Pod unless a PDB sets how many of them may be stopped. Since CA 1.33, CA evicts such a Pod without a PDB once the Pod is more than an hour old.&lt;sup id="fnref12"&gt;12&lt;/sup&gt; Add a PDB to that Pod. A Pod with an emptyDir, however, stops at the &lt;code&gt;pod with local storage present&lt;/code&gt; line once it has a PDB, and the node is not removed no matter how long you wait. In that case, you also need the fix under "Pods with an annotation or a volume" below.&lt;/p&gt;

&lt;p&gt;If you see &lt;code&gt;not enough pod disruption budget to move&lt;/code&gt;, the blocker is a Pod whose PDB allows no evictions.&lt;sup id="fnref13"&gt;13&lt;/sup&gt; If &lt;code&gt;kube_poddisruptionbudget_status_pod_disruptions_allowed&lt;/code&gt; in kube-state-metrics is 0, that PDB is the cause. If it is 1 or more and this line still appears, CA has decided to remove another node carrying Pods covered by the same PDB, and that node has used up the allowed count. Once that node is removed, the allowed count comes back. A PDB with &lt;code&gt;minAvailable: 1&lt;/code&gt; on a Deployment with replicas 1 keeps the allowed count at 0 at all times. Set replicas to 2 or more and change the PDB to &lt;code&gt;maxUnavailable: 1&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pods with an annotation or a volume
&lt;/h4&gt;

&lt;p&gt;If you see &lt;code&gt;pod annotated as not safe to evict present&lt;/code&gt;, the blocker is a Pod marked so that CA won't evict it.&lt;sup id="fnref14"&gt;14&lt;/sup&gt; This is set with the Pod annotation &lt;code&gt;cluster-autoscaler.kubernetes.io/safe-to-evict: "false"&lt;/code&gt;, to protect Pods that would cause trouble if stopped partway through their work. If the Pod no longer needs that protection, remove the annotation.&lt;/p&gt;

&lt;p&gt;If you see &lt;code&gt;pod with local storage present&lt;/code&gt;, the blocker is a Pod that uses an emptyDir (other than &lt;code&gt;medium: Memory&lt;/code&gt;) or a hostPath.&lt;sup id="fnref15"&gt;15&lt;/sup&gt; The contents of these volumes are lost when the node is removed, so CA doesn't evict the Pod. If losing the contents is fine, tell CA by putting the volume names in the Pod annotation &lt;code&gt;cluster-autoscaler.kubernetes.io/safe-to-evict-local-volumes&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Pods have nowhere to go
&lt;/h3&gt;

&lt;p&gt;The line has the form below. M is the number of Pods to move off the node, and N is how many were placed before a Pod that doesn't fit came up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node &amp;lt;node name&amp;gt; is not suitable for removal: can reschedule only N out of M pods
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CA checks whether the node's Pods fit on the remaining nodes, leaving that node out, using the same conditions as the scheduler.&lt;sup id="fnref16"&gt;16&lt;/sup&gt; Besides free capacity, the conditions include rules on how Pods are placed relative to each other. One of these rules puts only one Pod of the same Deployment on each node, so that losing one node doesn't stop every replica. When the number of replicas equals the number of nodes, whichever node is removed, the remaining nodes have no place for that Pod.&lt;/p&gt;

&lt;p&gt;To find the Pod that didn't fit, grep the CA logs for &lt;code&gt;failed to find place for&lt;/code&gt;; the Pod name is on that line.&lt;sup id="fnref17"&gt;17&lt;/sup&gt; CA stops trying as soon as one Pod doesn't fit, so this line shows only that one Pod. To fix this, see whether the rule can be changed from "must be followed" to "followed when possible".&lt;sup id="fnref18"&gt;18&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;The node group's minimum size, &lt;code&gt;scale-down-disabled&lt;/code&gt;, &lt;code&gt;safe-to-evict: "false"&lt;/code&gt;, and a "must be followed" placement rule can each be a setting someone added to protect something that losing a node would disrupt. Before changing one, I want to ask the person who added it what it was protecting.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was not checked
&lt;/h2&gt;

&lt;p&gt;This article assumes a CA you installed in the cluster yourself. The managed CA on GKE or AKS does not run inside the cluster, so you can't get its logs with &lt;code&gt;kubectl logs&lt;/code&gt;. I did not run a managed CA. I ran only CA 1.36.1 and, to compare how kube-system Pods are handled, 1.32.1, and I used kwok as the cloud provider. I did not run the process that finds AWS Auto Scaling groups by their tags.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;The threshold is &lt;code&gt;--scale-down-utilization-threshold&lt;/code&gt;, and the default is 0.5 (&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/config/flags/flags.go#L76-L90" rel="noopener noreferrer"&gt;argument definitions in CA 1.36.1&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;On a GitHub Actions ubuntu-24.04 runner, I ran kwok 0.8.0 virtual nodes on top of kind (Kubernetes 1.36.4) and ran CA 1.36.1 with the kwok cloud provider. The CA arguments match the defaults of Helm chart 9.59.0, with only the time arguments shortened. The scripts and results are in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/2b13b32/cluster-autoscaler-nodes-not-removed" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt; and the &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/39911680617" rel="noopener noreferrer"&gt;GitHub Actions run&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/core/scaledown/planner/planner.go#L326-L335" rel="noopener noreferrer"&gt;planner.go&lt;/a&gt; and &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/core/scaledown/eligibility/eligibility.go#L82-L104" rel="noopener noreferrer"&gt;eligibility.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;The checks in CA 1.36.1 for &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/processors/nodes/pre_filtering_processor.go#L54-L67" rel="noopener noreferrer"&gt;management and minimum size&lt;/a&gt;, &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/core/scaledown/eligibility/eligibility.go#L108-L168" rel="noopener noreferrer"&gt;the annotation and the sum of requests&lt;/a&gt;, and &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/cluster.go#L149-L165" rel="noopener noreferrer"&gt;eviction and destinations&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/processors/nodes/pre_filtering_processor.go#L54-L67" rel="noopener noreferrer"&gt;pre_filtering_processor.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;The &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml#L124-L182" rel="noopener noreferrer"&gt;AWS example manifest&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/core/scaledown/eligibility/eligibility.go#L108-L168" rel="noopener noreferrer"&gt;eligibility.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/utilization/info.go#L72-L135" rel="noopener noreferrer"&gt;info.go&lt;/a&gt; and &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/utils/pod/pod.go#L88-L96" rel="noopener noreferrer"&gt;pod.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;kube-state-metrics &lt;a href="https://github.com/kubernetes/kube-state-metrics/blob/main/docs/metrics/workload/pod-metrics.md" rel="noopener noreferrer"&gt;Pod metrics&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drain.go#L64-L84" rel="noopener noreferrer"&gt;drain.go&lt;/a&gt; and &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drainability/rules/rules.go#L68-L118" rel="noopener noreferrer"&gt;rules.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn11"&gt;
&lt;p&gt;The &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drainability/rules/replicated/rule.go#L46-L67" rel="noopener noreferrer"&gt;replicated rule&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn12"&gt;
&lt;p&gt;The &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drainability/rules/system/rule.go#L49-L62" rel="noopener noreferrer"&gt;system rule&lt;/a&gt; in CA 1.36.1. The one-hour check was added in &lt;a href="https://github.com/kubernetes/autoscaler/pull/7914" rel="noopener noreferrer"&gt;kubernetes/autoscaler#7914&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn13"&gt;
&lt;p&gt;The &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drainability/rules/pdb/rule.go#L45" rel="noopener noreferrer"&gt;pdb rule&lt;/a&gt; in CA 1.36.1, and the kube-state-metrics &lt;a href="https://github.com/kubernetes/kube-state-metrics/blob/main/docs/metrics/policy/poddisruptionbudget-metrics.md" rel="noopener noreferrer"&gt;PDB metrics&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn14"&gt;
&lt;p&gt;The &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drainability/rules/notsafetoevict/rule.go#L44" rel="noopener noreferrer"&gt;notsafetoevict rule&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn15"&gt;
&lt;p&gt;The &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/drainability/rules/localstorage/rule.go#L44" rel="noopener noreferrer"&gt;localstorage rule&lt;/a&gt; and &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/utils/drain/drain.go#L123-L147" rel="noopener noreferrer"&gt;the volumes it applies to&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn16"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/cluster.go#L149-L165" rel="noopener noreferrer"&gt;cluster.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn17"&gt;
&lt;p&gt;This line appears when &lt;code&gt;--v&lt;/code&gt; is 4 or higher. &lt;a href="https://github.com/kubernetes/autoscaler/blob/cluster-autoscaler-1.36.1/cluster-autoscaler/simulator/scheduling/hinting_simulator.go#L117-L127" rel="noopener noreferrer"&gt;hinting_simulator.go&lt;/a&gt; in CA 1.36.1.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn18"&gt;
&lt;p&gt;The rule goes in the Pod's podAntiAffinity. "Must be followed" is &lt;code&gt;requiredDuringSchedulingIgnoredDuringExecution&lt;/code&gt;, and "followed when possible" is &lt;code&gt;preferredDuringSchedulingIgnoredDuringExecution&lt;/code&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>kubernetes</category>
      <category>aws</category>
      <category>devops</category>
      <category>sre</category>
    </item>
    <item>
      <title>node-exporter under Pod Security Baseline reports the Pod's network, not the node's</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:02:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/node-exporter-under-pod-security-baseline-reports-the-pods-network-not-the-nodes-h25</link>
      <guid>https://dev.to/shinagawa-web/node-exporter-under-pod-security-baseline-reports-the-pods-network-not-the-nodes-h25</guid>
      <description>&lt;p&gt;If you apply the Pod Security Admission Baseline label with &lt;code&gt;--dry-run=server&lt;/code&gt; before enforcing it on the namespace that runs node-exporter (a namespace is the unit Kubernetes uses to partition resources), node-exporter's Pod shows up as a violation.&lt;/p&gt;

&lt;p&gt;For node-exporter installed from the prometheus-node-exporter Helm chart with default settings, the warning looks like this.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl label --dry-run=server --overwrite ns ne-base pod-security.kubernetes.io/enforce=baseline
Warning: existing pods in namespace "ne-base" violate the new PodSecurity enforce level "baseline:latest"
Warning: prometheus-node-exporter-rrrlc: host namespaces, hostPath volumes, hostPort
namespace/ne-base labeled (server dry run)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;host namespaces&lt;/code&gt;, &lt;code&gt;hostPath volumes&lt;/code&gt;, and &lt;code&gt;hostPort&lt;/code&gt;, listed after the Pod name, are the settings Baseline does not allow. Remove all three from the DaemonSet and the dry-run no longer warns about node-exporter's Pod.&lt;/p&gt;

&lt;p&gt;With the three removed, though, some of node-exporter's network metrics keep their names but report the node-exporter Pod's values instead of the node's. Filesystem values also come from the container's mounts instead of the node's disks.&lt;/p&gt;

&lt;p&gt;For example, even when the node's conntrack table (the list of connections the kernel is tracking) is above the threshold, the existing alert on its usage does not fire.&lt;/p&gt;

&lt;p&gt;The sections below take each item in the warning and describe what node-exporter reads through that setting and which metrics stop being node values when it is removed.&lt;/p&gt;

&lt;p&gt;Next, for environments that have already removed them and run under Baseline, a section shows how to tell from the DaemonSet's settings which metrics are no longer node values. The last section describes how to keep the items and place node-exporter's namespace outside Baseline.&lt;/p&gt;

&lt;p&gt;The two setups, one with the items removed to run inside Baseline and one with node-exporter's namespace moved outside Baseline, look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzjr56b6quzmgcvwvwmqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzjr56b6quzmgcvwvwmqw.png" alt="Setup with the items removed, running inside Baseline" width="800" height="738"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm3kaaf41q0buhaaeebp8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm3kaaf41q0buhaaeebp8.png" alt="Setup with node-exporter's namespace moved outside Baseline" width="800" height="738"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening
&lt;/h2&gt;

&lt;p&gt;This section goes through the three items in the warning in order, covering what node-exporter reads through each setting and how to remove it. It then shows, for each removal, which values stop being node values and what that does to alerts.&lt;/p&gt;

&lt;h3&gt;
  
  
  host namespaces
&lt;/h3&gt;

&lt;p&gt;Baseline's &lt;code&gt;host namespaces&lt;/code&gt; forbids &lt;code&gt;hostNetwork&lt;/code&gt;, &lt;code&gt;hostPID&lt;/code&gt;, and &lt;code&gt;hostIPC&lt;/code&gt;.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; The chart enables &lt;code&gt;hostNetwork&lt;/code&gt; and &lt;code&gt;hostPID&lt;/code&gt; by default.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hostNetwork&lt;/code&gt; makes the Pod use the node's network (the node's IP addresses and interfaces) as is. The traffic and connection values node-exporter reads are then the node's.&lt;/p&gt;

&lt;p&gt;Even when read from the node's &lt;code&gt;/proc&lt;/code&gt; mounted into the Pod, these values come from the network namespace of the process that reads them.&lt;sup id="fnref3"&gt;3&lt;/sup&gt; So removing &lt;code&gt;hostNetwork&lt;/code&gt; turns them into the Pod's values.&lt;/p&gt;

&lt;p&gt;Values that exist once for the whole kernel (socket memory, the conntrack limit) and interface attributes read from &lt;code&gt;/sys&lt;/code&gt; stay as the node's.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hostPID&lt;/code&gt; lets the Pod see every process on the node. node-exporter reads node values from the node's &lt;code&gt;/proc&lt;/code&gt; mount, so &lt;code&gt;hostPID&lt;/code&gt; does not change where it reads from.&lt;sup id="fnref4"&gt;4&lt;/sup&gt; Removing it changes none of the values in the table shown later.&lt;/p&gt;

&lt;p&gt;To clear &lt;code&gt;host namespaces&lt;/code&gt; from the warning, remove both. In the settings passed to the chart (values), write:&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;hostNetwork&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="na"&gt;hostPID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  hostPath volumes
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;hostPath volumes&lt;/code&gt; mounts a directory on the node into the Pod. By default the chart mounts the node's &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;/sys&lt;/code&gt; and the node's root filesystem.&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;node-exporter reads kernel values from the mounted &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;/sys&lt;/code&gt;. From the root filesystem it reads per-mount-point values and the disks' udev information (&lt;code&gt;/run/udev/data&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Only the root filesystem mount can be removed through values.&lt;sup id="fnref6"&gt;6&lt;/sup&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;hostRootFsMount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The values &lt;code&gt;hostProcFsMount&lt;/code&gt; and &lt;code&gt;hostSysFsMount&lt;/code&gt; only have a mountPropagation setting, so the &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;/sys&lt;/code&gt; mounts cannot be removed and stay. Removing the root mount therefore still leaves &lt;code&gt;hostPath volumes&lt;/code&gt; in the warning.&lt;/p&gt;

&lt;h3&gt;
  
  
  hostPort
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;hostPort&lt;/code&gt; binds a port on the node's IP to a port on the Pod.&lt;/p&gt;

&lt;p&gt;A Pod with &lt;code&gt;hostNetwork&lt;/code&gt; enabled listens directly on the node's IP, so Kubernetes gives that Pod a &lt;code&gt;hostPort&lt;/code&gt; with the same number as the container port.&lt;sup id="fnref7"&gt;7&lt;/sup&gt; That is why &lt;code&gt;hostPort&lt;/code&gt; appears in the warning even though the values have no &lt;code&gt;hostPort&lt;/code&gt; setting.&lt;/p&gt;

&lt;p&gt;node-exporter listens on this port on the node's IP, and Prometheus scrapes metrics from there.&lt;/p&gt;

&lt;p&gt;Removing &lt;code&gt;hostNetwork&lt;/code&gt; clears &lt;code&gt;hostPort&lt;/code&gt; from the warning. node-exporter then listens on the Pod's IP, and Prometheus scrapes the Pod's IP.&lt;/p&gt;

&lt;h3&gt;
  
  
  Metrics after removal
&lt;/h3&gt;

&lt;p&gt;The table columns refer to these four states.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Default: the chart's default settings&lt;/li&gt;
&lt;li&gt;hostNetwork: only &lt;code&gt;hostNetwork&lt;/code&gt; set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Root: only the root filesystem mount removed through values&lt;/li&gt;
&lt;li&gt;Baseline: &lt;code&gt;hostNetwork&lt;/code&gt;, &lt;code&gt;hostPID&lt;/code&gt;, and the root filesystem mount removed through values, with the &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;/sys&lt;/code&gt; mounts also removed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the table, "Node" means the node's value and "Pod" means the node-exporter Pod's value. The &lt;code&gt;nodename&lt;/code&gt; and &lt;code&gt;instance&lt;/code&gt; rows show whether the label holds the node's or the Pod's name, and the node's or the Pod's IP.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Default&lt;/th&gt;
&lt;th&gt;hostNetwork&lt;/th&gt;
&lt;th&gt;Root&lt;/th&gt;
&lt;th&gt;Baseline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU, memory, disk&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network traffic&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connections, sockets&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Socket memory&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;conntrack count&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;conntrack limit&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interface attributes&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Pod&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Filesystem&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Partial errors&lt;/td&gt;
&lt;td&gt;Container mounts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;udev labels&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Node&lt;/td&gt;
&lt;td&gt;Empty labels&lt;/td&gt;
&lt;td&gt;Empty labels&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nodename&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Node name&lt;/td&gt;
&lt;td&gt;Pod name&lt;/td&gt;
&lt;td&gt;Node name&lt;/td&gt;
&lt;td&gt;Pod name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;instance&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Node IP&lt;/td&gt;
&lt;td&gt;Pod IP&lt;/td&gt;
&lt;td&gt;Node IP&lt;/td&gt;
&lt;td&gt;Pod IP&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These are the main metrics checked for each row.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Row&lt;/th&gt;
&lt;th&gt;Main metrics&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU, memory, disk&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;node_cpu_seconds_total&lt;/code&gt;, &lt;code&gt;node_memory_MemTotal_bytes&lt;/code&gt;, &lt;code&gt;node_disk_read_bytes_total&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network traffic&lt;/td&gt;
&lt;td&gt;&lt;code&gt;node_network_receive_bytes_total&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connections, sockets&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;node_netstat_Tcp_CurrEstab&lt;/code&gt;, &lt;code&gt;node_sockstat_TCP_inuse&lt;/code&gt;, &lt;code&gt;node_sockstat_sockets_used&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Socket memory&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;node_sockstat_TCP_alloc&lt;/code&gt;, &lt;code&gt;node_sockstat_UDP_mem&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;conntrack count and limit&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;node_nf_conntrack_entries&lt;/code&gt;, &lt;code&gt;node_nf_conntrack_entries_limit&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Interface attributes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;node_network_info&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Filesystem&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;node_filesystem_device_error&lt;/code&gt;, &lt;code&gt;node_filesystem_size_bytes&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;udev labels&lt;/td&gt;
&lt;td&gt;Labels such as &lt;code&gt;model&lt;/code&gt; and &lt;code&gt;serial&lt;/code&gt; on &lt;code&gt;node_disk_info&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nodename&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The &lt;code&gt;nodename&lt;/code&gt; label on &lt;code&gt;node_uname_info&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;instance&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The address Prometheus scraped, which it attaches to every metric&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following three cell terms mean these states.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Cell term&lt;/th&gt;
&lt;th&gt;State&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Partial errors&lt;/td&gt;
&lt;td&gt;The node's mount points are listed, but for mount points other than &lt;code&gt;/&lt;/code&gt;, such as &lt;code&gt;/boot&lt;/code&gt; and &lt;code&gt;/run&lt;/code&gt;, &lt;code&gt;node_filesystem_device_error&lt;/code&gt; is 1 and &lt;code&gt;node_filesystem_size_bytes&lt;/code&gt; is missing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Container mounts&lt;/td&gt;
&lt;td&gt;Instead of the node's mount points, only the container's own mounts such as &lt;code&gt;/etc/hosts&lt;/code&gt; are listed, and &lt;code&gt;/&lt;/code&gt; is missing as well&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty labels&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;node_disk_info&lt;/code&gt; is present, but labels such as &lt;code&gt;model&lt;/code&gt; and &lt;code&gt;serial&lt;/code&gt; are empty&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Alerts built on rows that become Pod values evaluate the Pod's values, not the node's. For example, the kube-prometheus-stack alert on conntrack usage uses this expression.&lt;sup id="fnref8"&gt;8&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight prometheus"&gt;&lt;code&gt;&lt;span class="na"&gt;(node_nf_conntrack_entries{job&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"node-exporter"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;node_nf_conntrack_entries_limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.75&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;hostNetwork&lt;/code&gt; removed, the count becomes the Pod's value, 0, while the limit stays the node's value.&lt;sup id="fnref9"&gt;9&lt;/sup&gt; Even with the node's table filled past the threshold, the expression returns nothing and the alert does not fire.&lt;/p&gt;

&lt;h2&gt;
  
  
  Telling which state your environment is in
&lt;/h2&gt;

&lt;p&gt;You can tell which state in the previous section's table your node-exporter is in from two things in the DaemonSet's Pod settings: &lt;code&gt;hostNetwork&lt;/code&gt; and the &lt;code&gt;path&lt;/code&gt; of the hostPath volumes.&lt;/p&gt;

&lt;p&gt;Print the DaemonSet as YAML with the following command and look under &lt;code&gt;spec.template.spec&lt;/code&gt;. Replace the namespace and DaemonSet name with your own.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl get daemonset -n ne-base prometheus-node-exporter -o yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a DaemonSet installed with the chart defaults, the relevant lines look like this (&lt;code&gt;...&lt;/code&gt; marks omitted lines).&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="nn"&gt;...
&lt;/span&gt;      &lt;span class="na"&gt;hostNetwork&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
      &lt;span class="na"&gt;hostPID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
      &lt;span class="nn"&gt;...
&lt;/span&gt;      &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;hostPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/proc&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&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;proc&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;hostPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/sys&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&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;sys&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;hostPath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&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;root&lt;/span&gt;
      &lt;span class="nn"&gt;...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A removed setting shows up as a missing line here. Which lines are missing decides which column of the previous section's table applies. Whether &lt;code&gt;hostPID: true&lt;/code&gt; is present affects no column, so you can ignore it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Lines missing from the DaemonSet&lt;/th&gt;
&lt;th&gt;Column in the previous section's table&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;hostNetwork: true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;hostNetwork&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;path: /&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Root&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;hostNetwork: true&lt;/code&gt; and &lt;code&gt;path: /&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Both hostNetwork and Root&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The two above plus &lt;code&gt;path: /proc&lt;/code&gt; and &lt;code&gt;path: /sys&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Baseline&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When both columns apply, every row that is not a node value in either the hostNetwork column or the Root column is no longer a node value. Combinations not in this table cannot be judged from the previous section's table.&lt;/p&gt;

&lt;p&gt;In the column that applies, the rows whose cell is anything other than "Node", "Node name", or "Node IP" are the ones that are no longer node values. Which metrics those rows cover is in the row-to-metric table in the previous section.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do
&lt;/h2&gt;

&lt;p&gt;node-exporter that uses the settings in the warning cannot run in a namespace where Baseline is enforced.&lt;/p&gt;

&lt;p&gt;PSA sets the enforced level per namespace through a label and cannot exempt individual Pods.&lt;sup id="fnref10"&gt;10&lt;/sup&gt; So put node-exporter alone in a separate namespace and place no restrictions on that namespace.&lt;/p&gt;

&lt;p&gt;The level that restricts nothing is called privileged. Create the namespace for node-exporter first and give it the privileged label.&lt;sup id="fnref11"&gt;11&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl create namespace monitoring-ne
$ kubectl label ns monitoring-ne pod-security.kubernetes.io/enforce=privileged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have already removed items, restore the chart defaults when you move node-exporter into this namespace. For items removed through values, delete those values. For the &lt;code&gt;/proc&lt;/code&gt; and &lt;code&gt;/sys&lt;/code&gt; mounts, revert the changes you made outside the chart.&lt;/p&gt;

&lt;p&gt;If you install the prometheus-node-exporter chart on its own, delete the current release with &lt;code&gt;helm uninstall&lt;/code&gt; and reinstall it with &lt;code&gt;-n monitoring-ne&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;kube-prometheus-stack puts node-exporter in the same namespace as Prometheus.&lt;/p&gt;

&lt;p&gt;Pass the name of the namespace you created to &lt;code&gt;prometheus-node-exporter.namespaceOverride&lt;/code&gt; in values and apply it with &lt;code&gt;helm upgrade&lt;/code&gt;. This separates node-exporter's namespace from Prometheus's.&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;prometheus-node-exporter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;namespaceOverride&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;monitoring-ne&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;node-exporter's Pod then runs in &lt;code&gt;monitoring-ne&lt;/code&gt;.&lt;sup id="fnref12"&gt;12&lt;/sup&gt; If kube-prometheus-stack is installed in &lt;code&gt;monitoring&lt;/code&gt;, a Baseline dry-run on &lt;code&gt;monitoring&lt;/code&gt; shows no warning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl label --dry-run=server --overwrite ns monitoring pod-security.kubernetes.io/enforce=baseline
namespace/monitoring labeled &lt;span class="o"&gt;(&lt;/span&gt;server dry run&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the move, check the DaemonSet with the method from the previous section and confirm that all the default lines are present.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;monitoring-ne&lt;/code&gt;, where node-exporter now runs, remains an exception to Baseline.&lt;/p&gt;

&lt;p&gt;In this namespace, any Pod can use every setting Baseline forbids, including ones that were not in the warning (privileged containers, for example). Anyone who can create Pods there can run a Pod that reaches the node's network, processes, and directories.&lt;/p&gt;

&lt;p&gt;So keep only node-exporter in &lt;code&gt;monitoring-ne&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The two setups compare as follows.&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;Remove the items and run under Baseline&lt;/th&gt;
&lt;th&gt;Place node-exporter's namespace outside Baseline&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Network-related values&lt;/td&gt;
&lt;td&gt;Pod's values&lt;/td&gt;
&lt;td&gt;Node's values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;conntrack usage alert&lt;/td&gt;
&lt;td&gt;Does not fire when over the threshold&lt;/td&gt;
&lt;td&gt;Fires when over the threshold&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Filesystem values&lt;/td&gt;
&lt;td&gt;Container mounts&lt;/td&gt;
&lt;td&gt;Node's mount points&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scope Baseline does not cover&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;node-exporter's namespace&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you can restrict who can create Pods, and resources that create Pods such as Deployments, in &lt;code&gt;monitoring-ne&lt;/code&gt;, you can place node-exporter's namespace outside Baseline.&lt;/p&gt;

&lt;p&gt;In the setup with the items removed, the metric names stay the same, so on a dashboard it is hard to notice that the values have become the Pod's. I would rather keep one namespace as an exception to Baseline than have alerts stop firing without anyone knowing.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Verified on k3s v1.37.1+k3s1, Linux 6.17, and prometheus-node-exporter chart 4.60.0 (node_exporter v1.12.1). The code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/ce851718140e563e61097a0ba6a42b45ad565f5d/node-exporter-baseline-pod-values" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt; and the results are in the &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/40298121891" rel="noopener noreferrer"&gt;GitHub Actions run&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;The "Host Namespaces" control under Baseline in &lt;a href="https://kubernetes.io/docs/concepts/security/pod-security-standards/" rel="noopener noreferrer"&gt;Pod Security Standards&lt;/a&gt;. Sharing the node's namespaces lets a Pod reach the node's network, processes, and shared memory, which breaks the isolation between the Pod and the node, so it is forbidden.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://github.com/torvalds/linux/blob/v6.8/fs/proc/proc_net.c#L414" rel="noopener noreferrer"&gt;&lt;code&gt;/proc/net&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/torvalds/linux/blob/v6.8/net/sysctl_net.c#L33" rel="noopener noreferrer"&gt;&lt;code&gt;/proc/sys/net&lt;/code&gt;&lt;/a&gt; show the contents of the reading process's network namespace.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;Reading the node's process information needs only one of hostPID or a &lt;code&gt;/proc&lt;/code&gt; mount. The chart uses both, so with hostPID removed it can still read through the &lt;code&gt;/proc&lt;/code&gt; mount (&lt;a href="https://github.com/prometheus-community/helm-charts/issues/5507#issuecomment-2779256004" rel="noopener noreferrer"&gt;helm-charts #5507&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;a href="https://github.com/prometheus-community/helm-charts/blob/62669c198365d3cd3a0802396336946004808510/charts/prometheus-node-exporter/templates/daemonset.yaml#L341-L353" rel="noopener noreferrer"&gt;The chart's DaemonSet template&lt;/a&gt;. The paths to read from are passed in &lt;a href="https://github.com/prometheus-community/helm-charts/blob/62669c198365d3cd3a0802396336946004808510/charts/prometheus-node-exporter/templates/daemonset.yaml#L102-L108" rel="noopener noreferrer"&gt;args&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;&lt;a href="https://github.com/prometheus-community/helm-charts/blob/62669c198365d3cd3a0802396336946004808510/charts/prometheus-node-exporter/values.yaml#L388-L417" rel="noopener noreferrer"&gt;The chart's values&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/kubernetes/blob/cdb9495daf1450cba9ddc7fa91b3adee51b8a077/pkg/apis/core/v1/defaults.go#L164-L217" rel="noopener noreferrer"&gt;Kubernetes defaulting for Pods&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;The expression in kube-prometheus-stack's &lt;a href="https://github.com/prometheus-community/helm-charts/blob/86e724d5dd4cb73275410f211d1cedfb77f67764/charts/kube-prometheus-stack/templates/prometheus/rules-1.14/node-exporter.yaml#L433" rel="noopener noreferrer"&gt;alerting rules&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;node-exporter &lt;a href="https://github.com/prometheus/node_exporter/blob/v1.12.1/collector/conntrack_linux.go#L108-L120" rel="noopener noreferrer"&gt;reads the count and the limit from &lt;code&gt;/proc/sys/net/netfilter&lt;/code&gt;&lt;/a&gt;. In Linux, &lt;a href="https://github.com/torvalds/linux/blob/v6.8/net/netfilter/nf_conntrack_standalone.c#L1078-L1106" rel="noopener noreferrer"&gt;the count is separate per network namespace and the limit is not&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;&lt;a href="https://kubernetes.io/docs/concepts/security/pod-security-admission/#exemptions" rel="noopener noreferrer"&gt;Pod Security Admission exemptions&lt;/a&gt; are set by username, RuntimeClass, or namespace. There is no per-Pod setting.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn11"&gt;
&lt;p&gt;If the warn and audit labels are also set to baseline, set them to privileged as well. When labeling all namespaces at once, leave this namespace out.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn12"&gt;
&lt;p&gt;In kube-prometheus-stack chart 92.11.0, &lt;code&gt;namespaceOverride&lt;/code&gt; appears in &lt;a href="https://github.com/prometheus-community/helm-charts/blob/62669c198365d3cd3a0802396336946004808510/charts/prometheus-node-exporter/templates/_helpers.tpl#L131-L152" rel="noopener noreferrer"&gt;the node-exporter chart's template&lt;/a&gt; and &lt;a href="https://github.com/prometheus-community/helm-charts/blob/86e724d5dd4cb73275410f211d1cedfb77f67764/charts/kube-prometheus-stack/values.yaml#L2891" rel="noopener noreferrer"&gt;kube-prometheus-stack's values&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>monitoring</category>
      <category>security</category>
    </item>
    <item>
      <title>nginx proxy_cache shows another user's name</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Fri, 13 Nov 2026 13:03:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/nginx-proxycache-shows-another-users-name-3k77</link>
      <guid>https://dev.to/shinagawa-web/nginx-proxycache-shows-another-users-name-3k77</guid>
      <description>&lt;p&gt;Product pages are cached with nginx's &lt;code&gt;proxy_cache&lt;/code&gt;. With &lt;code&gt;proxy_cache&lt;/code&gt;, nginx stores a response from the application server and, until it expires, returns that response to requests for the same URL. A product page has the same content no matter who opens it, so serving it from the cache cuts the requests that reach the application server.&lt;/p&gt;

&lt;p&gt;Then a change goes in that shows the logged-in user's name on the page. Now a product page with one user's name in it is returned to everyone until it expires, including users who are not logged in. Below, a page that shows a user's name other than the viewer's own is called "someone else's page." If the stored response was for a user who is not logged in, logged-in users get a page with no name. That one is called "the logged-out page."&lt;/p&gt;

&lt;p&gt;When a request carries the cookie issued at login, the application server identifies the user from its value and shows that user's name. nginx does not look at the request's cookies, so it returns the stored response no matter whom it was made for.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant A as Logged-in user A
    participant B as User B, not logged in
    participant N as nginx
    participant S as Application server
    A-&amp;gt;&amp;gt;N: Product page (with cookie)
    N-&amp;gt;&amp;gt;S: The cache entry has expired, so forward it with the cookie
    S--&amp;gt;&amp;gt;N: Page with A's name
    Note over N: Store it in the cache (cookie not checked)
    N--&amp;gt;&amp;gt;A: Page with A's name
    B-&amp;gt;&amp;gt;N: Same product page (no cookie)
    Note over N: Same URL, so return it from the cache
    N--&amp;gt;&amp;gt;B: Page with A's name (someone else's page)&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;I reproduced this on nginx 1.30.6 and measured how often someone else's page is returned and what each fix does.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening
&lt;/h2&gt;

&lt;p&gt;nginx decides whether to store a response in the cache from the response headers, and looks up the stored response to return by URL. Whom the stored response is for is decided by the first request after the cache entry expires.&lt;/p&gt;

&lt;h3&gt;
  
  
  Headers that decide whether a response is cached
&lt;/h3&gt;

&lt;p&gt;Product page responses with a name in them get cached too. nginx decides whether to store a response mainly from the following response headers&lt;sup id="fnref1"&gt;1&lt;/sup&gt;. &lt;code&gt;Set-Cookie&lt;/code&gt; is the response header that makes the browser store a cookie. &lt;code&gt;private&lt;/code&gt; is a value saying the response is meant for one user, asking shared caches not to store it.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart LR
    Q1["Set-Cookie"] -- present --&amp;gt; N["Not stored"]
    Q1 -- absent --&amp;gt; Q2["Cache-Control has&amp;lt;br&amp;gt;private, no-cache,&amp;lt;br&amp;gt;or no-store"]
    Q2 -- no --&amp;gt; Y["Stored"]
    Q2 -- yes --&amp;gt; N&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;If only the login response issues the cookie, product page responses carry no &lt;code&gt;Set-Cookie&lt;/code&gt;. If the change that adds the name leaves &lt;code&gt;Cache-Control&lt;/code&gt; as it was, the chart ends at "Stored."&lt;/p&gt;

&lt;h3&gt;
  
  
  The cache key that looks up stored responses
&lt;/h3&gt;

&lt;p&gt;nginx builds a string called the cache key from the request and uses it to look up a stored response. The default cache key is &lt;code&gt;$scheme$proxy_host$request_uri&lt;/code&gt;, which is made from the URL&lt;sup id="fnref2"&gt;2&lt;/sup&gt;. So as long as the URL is the same, requests with different cookies get the same response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Whose response gets stored
&lt;/h3&gt;

&lt;p&gt;The stored response goes to everyone, so whether users see someone else's page or the logged-out page depends on whose response is stored. What gets stored is the response to the first request that arrives after the entry expires.&lt;/p&gt;

&lt;h2&gt;
  
  
  Measurement
&lt;/h2&gt;

&lt;p&gt;I measured how often someone else's page is returned without a fix, with a client, nginx, and the application server connected as follows.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart LR
    L["Client"] -- "500 requests/s&amp;lt;br&amp;gt;10% logged in" --&amp;gt; N["nginx&amp;lt;br&amp;gt;caches for 10 s"]
    N -- "only when the cache can't serve it" --&amp;gt; S["Application server&amp;lt;br&amp;gt;/items/1 to /items/100"]&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;For each request, the client picks one product page from &lt;code&gt;/items/1&lt;/code&gt; to &lt;code&gt;/items/100&lt;/code&gt;, and it sends 500 requests per second for 120 seconds. 10% of the requests come from logged-in users and the rest from users who are not logged in&lt;/li&gt;
&lt;li&gt;With the config below, nginx caches product page (&lt;code&gt;/items/&lt;/code&gt;) responses for 10 seconds&lt;/li&gt;
&lt;li&gt;The application server shows the user's name when a cookie is present, and a login link when it is not. It sets neither &lt;code&gt;Set-Cookie&lt;/code&gt; nor &lt;code&gt;Cache-Control&lt;/code&gt; on its responses
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="na"&gt;proxy_cache_path /var/cache/nginx/pages keys_zone&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;10m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;proxy_pass&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//app:8080;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_cache&lt;/span&gt; &lt;span class="n"&gt;pages&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_cache_valid&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="mi"&gt;10s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application server embeds in each page the user it built the page for. The client compares that user with the one who sent the request and counts whom each returned page was for&lt;sup id="fnref3"&gt;3&lt;/sup&gt;. The correct page is one with your own name if you are logged in, and one with a login link if you are not.&lt;/p&gt;

&lt;p&gt;Without a fix, about 1 in 10 pages is someone else's page, whether or not the user is logged in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check whether your setup is affected
&lt;/h2&gt;

&lt;p&gt;Send two requests in a row to the same URL: one as a logged-in user, then one as a user who is not logged in. If the second page shows the first user's name, your setup returns someone else's page.&lt;/p&gt;

&lt;p&gt;To the end of the URL, append a parameter that the application server does not read, set to a value not used before. The default cache key is made from the URL including the query string, so requests to this URL never get a response stored by other users' requests.&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 -H &lt;span class="s1"&gt;'Cookie: &amp;lt;name&amp;gt;=&amp;lt;value&amp;gt;'&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;product page URL&amp;gt;?check=&amp;lt;unused value&amp;gt;'&lt;/span&gt;
curl -s &lt;span class="s1"&gt;'&amp;lt;product page URL&amp;gt;?check=&amp;lt;unused value&amp;gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;Cookie&lt;/code&gt;, use the name and value of the cookie the browser received at login.&lt;/p&gt;

&lt;p&gt;If the second response shows the first user's name, the first response was stored in the cache and returned to the second request for the same URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixes
&lt;/h2&gt;

&lt;p&gt;Each option below removes both someone else's page and the logged-out page, and sends almost all requests from logged-in users to the application server. "Wrong page" is the share of returned pages that are not the correct page (someone else's page and the logged-out page). "Forwarded" is the number of requests nginx forwarded to the application server.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Wrong page&lt;/th&gt;
&lt;th&gt;Forwarded (/s)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Do nothing&lt;/td&gt;
&lt;td&gt;about 20%&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Skip the cache when logged in&lt;/td&gt;
&lt;td&gt;0%&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add &lt;code&gt;Vary: Cookie&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;0%&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cookie in the cache key&lt;/td&gt;
&lt;td&gt;0%&lt;/td&gt;
&lt;td&gt;58&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Skip the cache when logged in
&lt;/h3&gt;

&lt;p&gt;Add these two lines to the location where &lt;code&gt;proxy_cache&lt;/code&gt; is set&lt;sup id="fnref4"&gt;4&lt;/sup&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;proxy_cache_bypass&lt;/span&gt; $&lt;span class="n"&gt;cookie_session&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;proxy_no_cache&lt;/span&gt; $&lt;span class="n"&gt;cookie_session&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$cookie_session&lt;/code&gt; is the value of the &lt;code&gt;session&lt;/code&gt; cookie. Replace &lt;code&gt;session&lt;/code&gt; with the name of your own cookie that is first issued at login. If you use the name of a cookie that is issued before login, nobody uses the cache.&lt;/p&gt;

&lt;p&gt;Requests from logged-in users are forwarded instead of being served from the cache, and their responses are not stored. Only responses for users who are not logged in remain in the cache.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Vary: Cookie
&lt;/h3&gt;

&lt;p&gt;The application server adds &lt;code&gt;Vary: Cookie&lt;/code&gt; to every response. &lt;code&gt;Vary&lt;/code&gt; is a response header that names the request headers that change the content for the same URL. nginx stores and returns a separate response for each cookie value&lt;sup id="fnref5"&gt;5&lt;/sup&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Put the cookie in the cache key
&lt;/h3&gt;

&lt;p&gt;Append the cookie value to the end of nginx's cache key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;proxy_cache_key&lt;/span&gt; $&lt;span class="n"&gt;scheme&lt;/span&gt;$&lt;span class="n"&gt;proxy_host&lt;/span&gt;$&lt;span class="n"&gt;request_uri&lt;/span&gt;$&lt;span class="n"&gt;cookie_session&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Requests from logged-in users get a separate cache key for each user, and requests from users who are not logged in keep the URL-only cache key.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which to choose
&lt;/h3&gt;

&lt;p&gt;The wrong-page rate and the forwarded count are about the same for all three options, so choose by what you can change and what ends up stored in the cache.&lt;/p&gt;

&lt;p&gt;If you can change the nginx config, skip the cache when logged in. Putting the cookie in the cache key also takes only an nginx config change, but the forwarded count stays the same while a page for each logged-in user piles up in the cache.&lt;/p&gt;

&lt;p&gt;If you can change only the application server, add &lt;code&gt;Vary: Cookie&lt;/code&gt;. nginx splits on the value of the whole Cookie header, so on a site that issues other cookies, such as for analytics, pages for users who are not logged in are also stored separately for each value of those cookies.&lt;/p&gt;

&lt;p&gt;Someone else's page is returned even when neither the nginx config nor the application server has a mistake in it. Before a change that adds per-user content to a page goes in, I want to check whether nginx caches that page.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://github.com/nginx/nginx/blob/release-1.30.6/src/http/ngx_http_upstream.c#L5065" rel="noopener noreferrer"&gt;&lt;code&gt;ngx_http_upstream_process_set_cookie()&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/nginx/nginx/blob/release-1.30.6/src/http/ngx_http_upstream.c#L5109-L5140" rel="noopener noreferrer"&gt;&lt;code&gt;ngx_http_upstream_process_cache_control()&lt;/code&gt;&lt;/a&gt; in the nginx 1.30.6 source.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;The nginx docs on &lt;a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key" rel="noopener noreferrer"&gt;&lt;code&gt;proxy_cache_key&lt;/code&gt;&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;Run on a 4-core GitHub Actions runner. The code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/6a4048567113f60ad16bd9f0cdfcf5e7a1799cb0/nginx-cache-shows-other-users" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;, and the results are in the &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/39693236322" rel="noopener noreferrer"&gt;CI run&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;The nginx docs on &lt;a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass" rel="noopener noreferrer"&gt;&lt;code&gt;proxy_cache_bypass&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_no_cache" rel="noopener noreferrer"&gt;&lt;code&gt;proxy_no_cache&lt;/code&gt;&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;The nginx docs on &lt;a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid" rel="noopener noreferrer"&gt;&lt;code&gt;proxy_cache_valid&lt;/code&gt;&lt;/a&gt;. They state that a response with &lt;code&gt;Vary&lt;/code&gt; is cached taking the corresponding request header fields into account.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>nginx</category>
      <category>devops</category>
      <category>webdev</category>
      <category>caching</category>
    </item>
    <item>
      <title>Telling apart 3 reasons MySQL isn't using your index</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Tue, 10 Nov 2026 13:03:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/telling-apart-3-reasons-mysql-isnt-using-your-index-3hi8</link>
      <guid>https://dev.to/shinagawa-web/telling-apart-3-reasons-mysql-isnt-using-your-index-3hi8</guid>
      <description>&lt;p&gt;You are reading the slow query log in CloudWatch Logs or a similar tool, and one query has a &lt;code&gt;Rows_examined&lt;/code&gt; (rows read) about equal to the table's row count, or larger, while its &lt;code&gt;Rows_sent&lt;/code&gt; (rows returned) is only a few. &lt;code&gt;SHOW INDEX&lt;/code&gt; shows an index on the column used in the condition. Running EXPLAIN on the same query in a staging environment restored from a backup gives &lt;code&gt;ALL&lt;/code&gt; in the &lt;code&gt;type&lt;/code&gt; column: the whole table is read.&lt;/p&gt;

&lt;p&gt;Of the reasons MySQL can't use an index, I cover the following three.&lt;/p&gt;

&lt;p&gt;The first is a query that passes a datetime column (&lt;code&gt;created_at&lt;/code&gt;) through &lt;code&gt;DATE()&lt;/code&gt; to filter by date. I call this the "function" case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="kt"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2010-01-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second is a query on a member number made only of digits (&lt;code&gt;code&lt;/code&gt;), stored in a VARCHAR column and searched with an unquoted number. I call this "type mismatch".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000042&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third joins two tables on the member number, where the column's character set is utf8mb4 on one side (&lt;code&gt;members.code&lt;/code&gt;) and utf8mb3 on the other (&lt;code&gt;b_mb3_unicode.member_code&lt;/code&gt;). I call this "character set mismatch". utf8mb4 and utf8mb3 are both MySQL character sets for UTF-8; one stores a character in up to 4 bytes, the other in up to 3. Writing &lt;code&gt;utf8&lt;/code&gt; as the character set gives you utf8mb3.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;b_mb3_unicode&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;member_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;account_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function case is visible in the query. Type mismatch and character set mismatch are not. In both, the condition is a plain equality, and the difference lies in the columns' types and character sets.&lt;/p&gt;

&lt;p&gt;I set up tables and queries that trigger each of the three causes on MySQL 8.4 and 8.0, and checked the output of EXPLAIN and &lt;code&gt;SHOW WARNINGS&lt;/code&gt;, along with &lt;code&gt;Rows_examined&lt;/code&gt; in the slow query log.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mapping EXPLAIN output to the cause
&lt;/h2&gt;

&lt;p&gt;The three are told apart by &lt;code&gt;possible_keys&lt;/code&gt; in EXPLAIN, by warning 1739 from a &lt;code&gt;SHOW WARNINGS&lt;/code&gt; run right after it, and by what the column named in 1739 is compared with.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;possible_keys&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;possible_keys&lt;/code&gt; lists the indexes the optimizer picked as candidates. In a join, look at the row for the table whose &lt;code&gt;type&lt;/code&gt; is &lt;code&gt;ALL&lt;/code&gt;. Of the three, only the function case gives NULL in &lt;code&gt;possible_keys&lt;/code&gt;. When a condition passes a column through a function, that column's index is not even a candidate.&lt;/p&gt;

&lt;p&gt;In type mismatch and character set mismatch, the index does appear in &lt;code&gt;possible_keys&lt;/code&gt;. In these two, warning 1739 shows that the index can't be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Warning 1739
&lt;/h3&gt;

&lt;p&gt;MySQL emits warning 1739 when it decides it can't use an index because of a type or collation conversion. Running the query and then &lt;code&gt;SHOW WARNINGS&lt;/code&gt; does not show it, and the slow query log does not record it.&lt;sup id="fnref3"&gt;3&lt;/sup&gt; Run EXPLAIN, then &lt;code&gt;SHOW WARNINGS&lt;/code&gt; right after it in the same session.&lt;/p&gt;

&lt;p&gt;Below, the VARCHAR column &lt;code&gt;code&lt;/code&gt; is compared with a number, and &lt;code&gt;SHOW WARNINGS&lt;/code&gt; returns two 1739 rows. &lt;code&gt;ref&lt;/code&gt; and &lt;code&gt;range&lt;/code&gt; are names of access methods that find rows through an index, and both rows can appear for the same index. The final 1003 row is the query after MySQL rewrote it; I shortened the column list to &lt;code&gt;...&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000042&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+-------------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;select_type&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;partitions&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;possible_keys&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;key_len&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;rows&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;filtered&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Extra&lt;/span&gt;       &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+-------------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="no"&gt;SIMPLE&lt;/span&gt;      &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;       &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;idx_code&lt;/span&gt;      &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;298796&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mf"&gt;10.00&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;Using&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+-------------+&lt;/span&gt;

&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="no"&gt;WARNINGS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+---------+------+-------------------------------------------------------------------------------------------------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Level&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Code&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;                                                                                         &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+---------+------+-------------------------------------------------------------------------------------------------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Warning&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1739&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Cannot&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;access&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="s1"&gt;'idx_code'&lt;/span&gt; &lt;span class="n"&gt;due&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="n"&gt;collation&lt;/span&gt; &lt;span class="n"&gt;conversion&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="s1"&gt;'code'&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Warning&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1739&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Cannot&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="n"&gt;access&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="s1"&gt;'idx_code'&lt;/span&gt; &lt;span class="n"&gt;due&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="k"&gt;or&lt;/span&gt; &lt;span class="n"&gt;collation&lt;/span&gt; &lt;span class="n"&gt;conversion&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="s1"&gt;'code'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Note&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1003&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="cm"&gt;/* select#1 */&lt;/span&gt; &lt;span class="k"&gt;select&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;from&lt;/span&gt; &lt;span class="sb"&gt;`lab`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="sb"&gt;`members`&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`lab`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="sb"&gt;`members`&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="sb"&gt;`code`&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000042&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;+---------+------+-------------------------------------------------------------------------------------------------+&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What the column is compared with
&lt;/h3&gt;

&lt;p&gt;The 1739 message reads "type or collation conversion" for both type mismatch and character set mismatch. To separate them, find the column named after &lt;code&gt;on field&lt;/code&gt; and look at what the query compares it with. In the example above, &lt;code&gt;code&lt;/code&gt; is compared with the unquoted number &lt;code&gt;1000042&lt;/code&gt;, so it is a type mismatch. If the other side is a column, compare the two columns' character sets with &lt;code&gt;SHOW CREATE TABLE&lt;/code&gt;. If they differ, it is a character set mismatch.&lt;/p&gt;

&lt;p&gt;Each cause shows up as follows.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;&lt;code&gt;possible_keys&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;1739&lt;/th&gt;
&lt;th&gt;Compared with&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Function&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Type mismatch&lt;/td&gt;
&lt;td&gt;Listed&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Numeric type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Character set mismatch&lt;/td&gt;
&lt;td&gt;Listed&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Column with a different charset&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  How each cause happens
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="kt"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2010-01-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;# Query_time: 0.047168  Lock_time: 0.000003 Rows_sent: 48  Rows_examined: 300000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The index holds the values of &lt;code&gt;created_at&lt;/code&gt;, not the values of &lt;code&gt;DATE(created_at)&lt;/code&gt;. MySQL can't use the index to find the rows where &lt;code&gt;DATE(created_at)&lt;/code&gt; is &lt;code&gt;'2010-01-01'&lt;/code&gt;, so it passes every row's &lt;code&gt;created_at&lt;/code&gt; through &lt;code&gt;DATE()&lt;/code&gt; and compares the result.&lt;/p&gt;

&lt;p&gt;To fix it, rewrite the condition as the range &lt;code&gt;created_at &amp;gt;= '2010-01-01' AND created_at &amp;lt; '2010-01-02'&lt;/code&gt;. It returns the same rows, and the index can find them. To fix it without changing the query, add a functional index on &lt;code&gt;DATE(created_at)&lt;/code&gt;.&lt;sup id="fnref4"&gt;4&lt;/sup&gt; Either way, the number of rows read equals the number of rows returned.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type mismatch
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000042&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;# Query_time: 0.078791  Lock_time: 0.000002 Rows_sent: 1  Rows_examined: 300000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the string column &lt;code&gt;code&lt;/code&gt; is compared with the number &lt;code&gt;1000042&lt;/code&gt;, MySQL converts the values of &lt;code&gt;code&lt;/code&gt; to numbers and compares those. &lt;code&gt;'1'&lt;/code&gt;, &lt;code&gt;' 1'&lt;/code&gt;, and &lt;code&gt;'00001'&lt;/code&gt; all become the number 1. The index orders &lt;code&gt;code&lt;/code&gt; as strings, so the rows matching the number 1000042 are not in one place in the index. MySQL therefore reads every row.&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;This happens when application code passes the value as a number.&lt;/p&gt;

&lt;p&gt;To fix it, pass the value as a string (&lt;code&gt;code = '1000042'&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Character set mismatch
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;b_mb3_unicode&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;member_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;account_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;# Query_time: 0.945879  Lock_time: 0.000003 Rows_sent: 5  Rows_examined: 600000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;b.member_code&lt;/code&gt; (utf8mb3) is compared with &lt;code&gt;a.code&lt;/code&gt; (utf8mb4), MySQL converts the values of &lt;code&gt;b.member_code&lt;/code&gt; to utf8mb4 and compares those. b's index is ordered by the utf8mb3 values, so it can't be used to find the rows.&lt;sup id="fnref5"&gt;5&lt;/sup&gt; The index on &lt;code&gt;code&lt;/code&gt; in a is usable, so MySQL reads all of b and, for each row of b, looks up a through the index. b also has 300,000 rows. The 300,000 rows read from b plus the 300,000 looked up in a make &lt;code&gt;Rows_examined&lt;/code&gt; 600,000.&lt;/p&gt;

&lt;p&gt;A character set is set per table and per column, and whatever was specified when the table was created stays. With nothing specified, it is the database default at that time.&lt;sup id="fnref6"&gt;6&lt;/sup&gt; If you used to create tables in utf8 (utf8mb3) and at some point switched to utf8mb4, both kinds of tables end up side by side in the same database. Converting an existing table to utf8mb4 requires rebuilding it, and writes are blocked during the rebuild.&lt;sup id="fnref7"&gt;7&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;To fix it, convert the utf8mb3 columns to utf8mb4 during a window when you can stop writes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking on staging
&lt;/h2&gt;

&lt;p&gt;Restore a production backup onto a staging server running the same MySQL version as production. Run EXPLAIN on the statement from the slow query log as-is, then run &lt;code&gt;SHOW WARNINGS&lt;/code&gt;, and you get the same verdict as in production.&lt;/p&gt;

&lt;p&gt;To read the slow query log outside production, have it written to a file (&lt;code&gt;log_output&lt;/code&gt; set to &lt;code&gt;FILE&lt;/code&gt;) and shipped to CloudWatch Logs. Aurora MySQL 3 defaults to &lt;code&gt;FILE&lt;/code&gt;.&lt;sup id="fnref8"&gt;8&lt;/sup&gt; RDS for MySQL defaults to &lt;code&gt;TABLE&lt;/code&gt;, which puts the log in the &lt;code&gt;mysql.slow_log&lt;/code&gt; table in production, so reading it means running queries against production.&lt;sup id="fnref9"&gt;9&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;The slow query log keeps statements with their values embedded. Numbers stay unquoted, so running the logged statement as-is compares values with the same types as in production. Queries run as server-side prepared statements are also logged with their values filled in.&lt;sup id="fnref10"&gt;10&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Whether an index can be used depends on the query's expressions and the columns' types and character sets, not on the table's row count or statistics. Below is EXPLAIN for the type mismatch query &lt;code&gt;SELECT * FROM members WHERE code = 1000042&lt;/code&gt;, on the original table and on the table just after restoring it with mysqldump. The top row is the original table and the bottom row the restored one. &lt;code&gt;rows&lt;/code&gt; (the estimated row count) differs, but &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;possible_keys&lt;/code&gt;, and &lt;code&gt;key&lt;/code&gt; are the same. So is warning 1739 in &lt;code&gt;SHOW WARNINGS&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;| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
|  1 | SIMPLE      | members | NULL       | ALL  | idx_code      | NULL | NULL    | NULL | 298796 |    10.00 | Using where |
|  1 | SIMPLE      | members | NULL       | ALL  | idx_code      | NULL | NULL    | NULL |     10 |    10.00 | Using where |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;mysqldump writes a column's character set into CREATE TABLE when it differs from the table default. So after the restore, the columns have the same character sets as in production. Below is part of the CREATE TABLE in the dump: the table default is utf8mb4, and the &lt;code&gt;member_code&lt;/code&gt; column is utf8mb3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="sb"&gt;`member_code`&lt;/span&gt; &lt;span class="kt"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;CHARACTER&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;utf8mb3&lt;/span&gt; &lt;span class="k"&gt;COLLATE&lt;/span&gt; &lt;span class="n"&gt;utf8mb3_unicode_ci&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;ENGINE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;InnoDB&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="no"&gt;CHARSET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;utf8mb4&lt;/span&gt; &lt;span class="k"&gt;COLLATE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;utf8mb4_unicode_ci&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.4/en/charset-unicode-utf8mb3.html" rel="noopener noreferrer"&gt;The utf8mb3 Character Set&lt;/a&gt; in the MySQL 8.4 Reference Manual.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Verified with MySQL 8.4.11 and 8.0.46 in Docker on a GitHub Actions ubuntu-24.04 runner. The 300,000-row table &lt;code&gt;members&lt;/code&gt; (utf8mb4, utf8mb4_unicode_ci) has columns such as a VARCHAR member number, a datetime, and an email address, each with its own index. As join partners, I created two tables with an indexed column holding the same values as the member number in &lt;code&gt;members&lt;/code&gt;: one with collation utf8mb4_unicode_ci and one with utf8mb3_unicode_ci. The scripts and results are in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/2be18600e0d6e4f54cf62e999b44d2fed519042e/mysql-index-unusable" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt; and the &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/39756681883" rel="noopener noreferrer"&gt;GitHub Actions run&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;In the MySQL 8.4.11 source, 1739 is emitted only during EXPLAIN and when &lt;code&gt;sql_safe_updates&lt;/code&gt; is enabled (&lt;a href="https://github.com/mysql/mysql-server/blob/mysql-8.4.11/sql/sql_optimizer.cc#L7154-L7167" rel="noopener noreferrer"&gt;ref check&lt;/a&gt;, &lt;a href="https://github.com/mysql/mysql-server/blob/mysql-8.4.11/sql/range_optimizer/range_analysis.cc#L109-L119" rel="noopener noreferrer"&gt;range check&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;Functional Key Parts in &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/create-index.html" rel="noopener noreferrer"&gt;CREATE INDEX&lt;/a&gt; in the MySQL 8.4 Reference Manual. A functional index is used when the expression in the query matches the expression in the index.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.4/en/mysql-indexes.html" rel="noopener noreferrer"&gt;How MySQL Uses Indexes&lt;/a&gt; in the MySQL 8.4 Reference Manual. It states that an index can't be used when a string column is compared with a number, or when columns with different character sets are compared.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.4/en/charset-syntax.html" rel="noopener noreferrer"&gt;Specifying Character Sets and Collations&lt;/a&gt; in the MySQL 8.4 Reference Manual.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-operations.html" rel="noopener noreferrer"&gt;Online DDL Operations&lt;/a&gt; in the MySQL 8.4 Reference Manual. Converting the character set rebuilds the table and does not permit concurrent DML.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_LogAccess.MySQL.LogFileSize.html" rel="noopener noreferrer"&gt;Overview of Aurora MySQL database logs&lt;/a&gt; in the Amazon Aurora User Guide. The default &lt;code&gt;log_output&lt;/code&gt; for Aurora MySQL 2 and 3 is &lt;code&gt;FILE&lt;/code&gt;, and shipping to CloudWatch Logs requires &lt;code&gt;FILE&lt;/code&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_LogAccess.MySQL.LogFileSize.html" rel="noopener noreferrer"&gt;Overview of RDS for MySQL database logs&lt;/a&gt; in the Amazon RDS User Guide. The default &lt;code&gt;log_output&lt;/code&gt; is &lt;code&gt;TABLE&lt;/code&gt;, and slow queries are written to the &lt;code&gt;mysql.slow_log&lt;/code&gt; table.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;The code in the MySQL 8.4.11 source that fills a prepared statement's values into the statement text for logging (&lt;a href="https://github.com/mysql/mysql-server/blob/mysql-8.4.11/sql/sql_prepare.cc#L756-L758" rel="noopener noreferrer"&gt;sql_prepare.cc&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>mysql</category>
      <category>database</category>
      <category>performance</category>
      <category>devops</category>
    </item>
    <item>
      <title>Negative stock and lost updates in a Postgres transaction</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Sun, 08 Nov 2026 13:03:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/negative-stock-and-lost-updates-in-a-postgres-transaction-5hb</link>
      <guid>https://dev.to/shinagawa-web/negative-stock-and-lost-updates-in-a-postgres-transaction-5hb</guid>
      <description>&lt;p&gt;Stock goes negative even though the code is wrapped in a transaction. Or a stocktake finds less physical stock than the system shows. Nothing appears in the error log. Tests that process one order at a time in sequence never show the symptom.&lt;/p&gt;

&lt;p&gt;I measured two patterns that cause this symptom under PostgreSQL 16's default isolation level (Read Committed).&lt;sup id="fnref1"&gt;1&lt;/sup&gt; The symptom shows up differently in each pattern, and the fix that works is different too.&lt;/p&gt;

&lt;h2&gt;
  
  
  A transaction guarantees it does not stop halfway
&lt;/h2&gt;

&lt;p&gt;Wrapping statements in BEGIN and COMMIT guarantees that either all of them take effect or none of them do. Even if the server crashes partway through, you do not end up with a partial result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&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;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- if it crashes before COMMIT, neither items nor orders is changed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whether the transaction stays separate from other transactions is decided by a different setting. That setting is the isolation level, and at the default level, changes from other transactions can leak in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens under concurrent execution
&lt;/h2&gt;

&lt;p&gt;UPDATE takes a write lock (a row lock) on the target row, even if you do not ask for one explicitly. The lock is not released until commit or rollback.&lt;/p&gt;

&lt;p&gt;When another transaction tries to UPDATE the same row, it waits until the preceding UPDATE commits or rolls back.&lt;/p&gt;

&lt;p&gt;Once the wait ends, the later UPDATE sees the latest committed value at that point.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; How the UPDATE is written decides what happens next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Environment
&lt;/h3&gt;

&lt;p&gt;I measured on PostgreSQL 16 at Read Committed, in a single environment on GitHub Actions CI. One PostgreSQL 16 instance has two tables, &lt;code&gt;items&lt;/code&gt; (stock) and &lt;code&gt;orders&lt;/code&gt;. &lt;code&gt;items&lt;/code&gt; has one row, and all 50 concurrent clients update that same row.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditions
&lt;/h3&gt;

&lt;p&gt;Multiple clients run a "check the stock, then place an order" operation at the same time. I measured two patterns that differ only in how the UPDATE is written.&lt;/p&gt;

&lt;p&gt;When the app reads a value with SELECT, processes it on the app side (validation, external API calls, business logic calculations, and so on), and then runs UPDATE, another transaction can SELECT the same row during that processing. Several transactions end up reading the same value and going on to UPDATE with it. The conditions are 50 concurrent clients, an initial stock of 20, and a 0.1s sleep between SELECT and UPDATE. The sleep stands in for that application processing time. Each pattern was run 5 times.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern A: delta update
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- if stock &amp;lt;= 0, ROLLBACK and stop&lt;/span&gt;
&lt;span class="c1"&gt;-- (sleep 0.1s)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The UPDATE is written as &lt;code&gt;SET stock = stock - 1&lt;/code&gt;. The database does the subtraction when the UPDATE runs.&lt;/p&gt;

&lt;p&gt;Column definitions (the same for the tables that follow):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Column&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;stock_sum&lt;/td&gt;
&lt;td&gt;Total stock after processing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;decremented&lt;/td&gt;
&lt;td&gt;Initial total stock minus stock_sum. The total number actually decremented&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;orders_ok&lt;/td&gt;
&lt;td&gt;Number of successful orders&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;lost_decrements&lt;/td&gt;
&lt;td&gt;The difference between orders_ok and decremented. Above 0 means decrements were lost&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;negative_rows&lt;/td&gt;
&lt;td&gt;Number of rows with negative stock&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Results (identical in all 5 runs):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stock_sum&lt;/th&gt;
&lt;th&gt;decremented&lt;/th&gt;
&lt;th&gt;orders_ok&lt;/th&gt;
&lt;th&gt;lost_decrements&lt;/th&gt;
&lt;th&gt;negative_rows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;-30&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;decremented is 50 and orders_ok is 50. All 50 orders went through, and the subtraction ran correctly 50 times. lost_decrements=0 means zero decrements were lost, so the number of subtractions is correct. The problem is that negative_rows is 1. The stock is at -30.&lt;/p&gt;

&lt;p&gt;The 50 transactions proceed as follows.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1&lt;/th&gt;
&lt;th&gt;T2&lt;/th&gt;
&lt;th&gt;T3–T49&lt;/th&gt;
&lt;th&gt;T50&lt;/th&gt;
&lt;th&gt;stock&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;UPDATE stock - 1 (acquires lock)&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;COMMIT&lt;/td&gt;
&lt;td&gt;UPDATE stock - 1 (acquires lock)&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3–49&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;COMMIT (time 3)&lt;/td&gt;
&lt;td&gt;UPDATE stock - 1 and COMMIT, one at a time (T49 runs only the UPDATE)&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;18 → -28&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;T49: COMMIT&lt;/td&gt;
&lt;td&gt;UPDATE stock - 1 (acquires lock)&lt;/td&gt;
&lt;td&gt;-29&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;51&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;COMMIT&lt;/td&gt;
&lt;td&gt;-30&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The subtraction itself works correctly. The problem is when the stock check happens. The check uses the value read at SELECT time, so orders keep going through even after the stock reaches 0. The system accepts more orders than it has stock for, and orders that cannot ship pile up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern B: absolute write-back
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- store v = 20 in an app variable&lt;/span&gt;
&lt;span class="c1"&gt;-- if v &amp;lt;= 0, ROLLBACK and stop&lt;/span&gt;
&lt;span class="c1"&gt;-- (sleep 0.1s)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;new_stock&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- set the result of v-1 as a fixed value&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The UPDATE is written as &lt;code&gt;SET stock = :new_stock&lt;/code&gt;. It writes a fixed value that the app calculated at SELECT time, not the current stock value. This is the only difference from pattern A.&lt;/p&gt;

&lt;p&gt;Results (5 runs):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;run&lt;/th&gt;
&lt;th&gt;stock_sum&lt;/th&gt;
&lt;th&gt;decremented&lt;/th&gt;
&lt;th&gt;orders_ok&lt;/th&gt;
&lt;th&gt;lost_decrements&lt;/th&gt;
&lt;th&gt;negative_rows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;49&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;49&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;49&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;49&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;49&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;orders_ok is 50, so all 50 orders went through. But decremented is 1: the stock went down by only 1. lost_decrements is 49, so 49 decrements were lost. negative_rows is 0, so the stock did not go negative.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:new_stock&lt;/code&gt; is an app variable. When the wait ends, the UPDATE does not look at the current stock value. It uses the fixed value the app calculated at SELECT time.&lt;/p&gt;

&lt;p&gt;A later write overwrites the earlier one, so the final value is whatever the last thread to commit wrote. Every thread that read stock=20 writes 19.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1&lt;/th&gt;
&lt;th&gt;T2&lt;/th&gt;
&lt;th&gt;T3&lt;/th&gt;
&lt;th&gt;stock&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;SELECT → 20, sleep&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;UPDATE SET 19 (acquires lock)&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;COMMIT&lt;/td&gt;
&lt;td&gt;UPDATE SET 19 (acquires lock)&lt;/td&gt;
&lt;td&gt;waits&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;COMMIT&lt;/td&gt;
&lt;td&gt;UPDATE SET 19 (acquires lock)&lt;/td&gt;
&lt;td&gt;19 (unchanged)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;COMMIT&lt;/td&gt;
&lt;td&gt;19 (unchanged)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All three wrote &lt;code&gt;SET stock = 19&lt;/code&gt;, but the T2 and T3 commits do not change stock, because it was already 19. The same thing happens with 50 concurrent clients: there is only one effective decrement (20→19), and the other 49 are lost. If a thread that ran its SELECT after the first commit is in the mix, that thread reads 19 and writes 18. If that thread commits last, the final value is 18. In a separate measurement under the same conditions, all five runs ended at 18.&lt;sup id="fnref4"&gt;4&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;The orders succeed and the stock stays positive. There is no constraint violation and no error. In this measurement, 50 orders went through against a stock of 20, which means the system accepted a large number of orders it cannot ship. The gap between the stock count and actual sales is first detected at stocktake.&lt;/p&gt;

&lt;p&gt;What happens here is that a preceding transaction's update is overwritten by a later write and disappears. This is called a lost update.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing the two patterns
&lt;/h2&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;Pattern A (delta update)&lt;/th&gt;
&lt;th&gt;Pattern B (absolute write-back)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SET clause&lt;/td&gt;
&lt;td&gt;&lt;code&gt;stock - 1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;v - 1&lt;/code&gt; (app variable)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Row lock wait&lt;/td&gt;
&lt;td&gt;Occurs&lt;/td&gt;
&lt;td&gt;Occurs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stock goes negative&lt;/td&gt;
&lt;td&gt;Yes (negative_rows=1)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Decrements lost&lt;/td&gt;
&lt;td&gt;No (lost_decrements=0)&lt;/td&gt;
&lt;td&gt;Yes (lost_decrements=49)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can tell which pattern a piece of code is by looking at whether the UPDATE uses &lt;code&gt;stock - 1&lt;/code&gt; (a column reference) or &lt;code&gt;v - 1&lt;/code&gt; (an app variable).&lt;/p&gt;

&lt;p&gt;Through an ORM, the code does not tell you which one it is. The usual ORM style of loading an entity, changing a property, and saving it issues an absolute UPDATE by default, so it becomes pattern B even when the code is meant as a delta update. Every ORM has an API that issues a delta update (Django's &lt;code&gt;F()&lt;/code&gt;, Rails' &lt;code&gt;update_counters&lt;/code&gt;, Prisma's &lt;code&gt;decrement&lt;/code&gt;, and others).&lt;sup id="fnref5"&gt;5&lt;/sup&gt; With an ORM, you need to log the SQL it issues and check it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixes
&lt;/h2&gt;

&lt;p&gt;Once you know the type, choose a fix. A and B have different root causes, so the fixes that work are different. A fix that works for A does not necessarily work for B, and a fix that works for both costs more. Below are three fixes: CAS with measured results, and conditional UPDATE and a CHECK constraint with their behavior by design.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional UPDATE
&lt;/h3&gt;

&lt;p&gt;This is a fix for the negative-stock symptom (delta update). Adding &lt;code&gt;AND stock &amp;gt; 0&lt;/code&gt; to the UPDATE stops it from running once the stock reaches 0. If the stock has run out by the time the lock wait ends, the UPDATE finishes with no matching row.&lt;sup id="fnref6"&gt;6&lt;/sup&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For orders placed after the stock runs out, the UPDATE updates nothing. PostgreSQL returns the number of updated rows (rowcount) as 0, so the app can detect this and reject the order.&lt;/p&gt;

&lt;p&gt;The branch on the app side looks like this (pseudocode):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- an updated row count of 0 means out of stock&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="n"&gt;rowcount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
  &lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;RETURN&lt;/span&gt; &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'out of stock'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For an order quantity of N, it becomes &lt;code&gt;SET stock = stock - :qty WHERE ... AND stock &amp;gt;= :qty&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This fix only works for delta updates (&lt;code&gt;stock - 1&lt;/code&gt;). Adding the same condition to an absolute write-back does not help, because multiple threads still overwrite each other with the same fixed value.&lt;/p&gt;

&lt;h3&gt;
  
  
  CAS (Compare-And-Swap)
&lt;/h3&gt;

&lt;p&gt;This fix works for both symptoms: stock going negative, and orders going through while the stock does not go down. It costs more to implement, and in return it works regardless of how the UPDATE is written.&lt;/p&gt;

&lt;p&gt;Let &lt;code&gt;v&lt;/code&gt; be the stock count read at SELECT time. At UPDATE time, if the stock is still &lt;code&gt;v&lt;/code&gt;, nobody has cut in, so the update is safe. This check is written as a condition in the WHERE clause.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- store this value in v&lt;/span&gt;
&lt;span class="c1"&gt;-- if stock &amp;lt;= 0, ROLLBACK and stop&lt;/span&gt;
&lt;span class="c1"&gt;-- compute new_stock = v - 1&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;new_stock&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparing on the business value (the stock count) is a simplification for the test. If the WHERE clause compares only the stock count, it cannot detect a change to other columns that the SELECT also read and the decision depended on. Also, if the stock returns to the same value, as in 20→19→20 (restocking or a cancellation), &lt;code&gt;stock = :v&lt;/code&gt; still passes and misses the update that happened in between. With a &lt;code&gt;version&lt;/code&gt; column (an integer incremented on every update), you can detect a conflict no matter which column changed, including when a value returns to where it was.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- store the version value in v&lt;/span&gt;
&lt;span class="c1"&gt;-- if stock &amp;lt;= 0, ROLLBACK and stop&lt;/span&gt;
&lt;span class="c1"&gt;-- compute new_stock = stock - 1&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;new_stock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An updated row count of 0 means another transaction changed the value away from &lt;code&gt;v&lt;/code&gt; first. This operation is called CAS (Compare-And-Swap). A caller that gets an updated row count of 0 can start over from SELECT (retry), or pass the conflict on to its own caller.&lt;/p&gt;

&lt;p&gt;The test used an implementation that retries when the updated row count is 0. Retries were immediate, with no retry limit, no wait, and no timeout, and continued until the thread found the stock out. Concurrency, initial stock, and the sleep are the same as in A and B.&lt;/p&gt;

&lt;p&gt;Results (5 runs):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;run&lt;/th&gt;
&lt;th&gt;stock_sum&lt;/th&gt;
&lt;th&gt;decremented&lt;/th&gt;
&lt;th&gt;orders_ok&lt;/th&gt;
&lt;th&gt;lost_decrements&lt;/th&gt;
&lt;th&gt;negative_rows&lt;/th&gt;
&lt;th&gt;retries&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;148&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;150&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;146&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;146&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;144&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Neither negative stock nor lost decrements occur. retries is high, 144 to 150, because 50 threads go after the same row at the same time, and the stock has often changed by the time a thread runs its UPDATE, which forces a retry. orders_ok is 20 because there are only 20 in stock. The other 30 orders are correctly rejected as out of stock after repeated retries. This fix only holds once the retry path (error handling, a retry limit, a timeout) is implemented as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  CHECK constraint (stock &amp;gt;= 0)
&lt;/h3&gt;

&lt;p&gt;With a &lt;code&gt;CHECK (stock &amp;gt;= 0)&lt;/code&gt; constraint added to the items table (&lt;code&gt;ALTER TABLE items ADD CONSTRAINT stock_nonneg CHECK (stock &amp;gt;= 0)&lt;/code&gt;), a pattern A transaction ends with a constraint violation error as soon as the &lt;code&gt;stock - 1&lt;/code&gt; that runs after the lock wait would take the stock below 0.&lt;sup id="fnref7"&gt;7&lt;/sup&gt; The "nothing in the error log" symptom turns into an immediate error. With 50 concurrent clients against a stock of 20, the constraint should, by design, let the first 20 succeed and end the remaining 30 with a constraint violation.&lt;/p&gt;

&lt;p&gt;It does not work for pattern B. B never makes the stock negative, so the constraint never fires. The gap between the stock count and actual sales remains.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixes by pattern
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Fixes that work&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A: delta update (&lt;code&gt;stock - 1&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Stock goes negative&lt;/td&gt;
&lt;td&gt;CHECK constraint / conditional UPDATE / CAS&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B: absolute write-back (&lt;code&gt;:new_stock&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Orders go through but stock does not go down&lt;/td&gt;
&lt;td&gt;CAS only (conditional UPDATE and CHECK constraint do not work)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If B's code can be rewritten in the &lt;code&gt;stock - 1&lt;/code&gt; form, adding &lt;code&gt;AND stock &amp;gt; 0&lt;/code&gt; is enough (the same fix as the conditional UPDATE). If you want to fix B as it is, or if there is complex app-side processing between SELECT and UPDATE, you need CAS (with retries).&lt;/p&gt;

&lt;h3&gt;
  
  
  Other fixes
&lt;/h3&gt;

&lt;p&gt;You can also take the row lock at SELECT time with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, but it raises concerns about lock contention, deadlocks, throughput, and more, so I cover it in a &lt;a href="https://zenn.dev/shinagawa_web/articles/select-for-update-throughput-ceiling" rel="noopener noreferrer"&gt;separate article&lt;/a&gt;. Raising the isolation level to REPEATABLE READ or higher turns concurrent writes in both A and B into "could not serialize access due to concurrent update" errors.&lt;sup id="fnref8"&gt;8&lt;/sup&gt; Data no longer ends up wrong without anyone noticing, but you need error handling and retries. That option, including its throughput impact, is also covered in a separate article.&lt;/p&gt;

&lt;h2&gt;
  
  
  A query to tell the type apart in a test environment
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;:keys&lt;/code&gt; is the number of rows in the target table, and &lt;code&gt;:init_stock&lt;/code&gt; is the initial stock per row. For one row with an initial stock of 20, use &lt;code&gt;:keys = 1&lt;/code&gt; and &lt;code&gt;:init_stock = 20&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                                          &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;stock_sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;init_stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;decremented&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM orders)                       AS orders_ok,
  (SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;init_stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;lost_decrements&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE stock &amp;lt; 0)                   AS negative_rows
FROM items;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;negative_rows&lt;/code&gt; is positive, it is type A (the stock has gone negative). If &lt;code&gt;lost_decrements&lt;/code&gt; is positive, it is type B (decrements have been lost). If both are 0, either concurrency is low or the requests are spread across many rows.&lt;/p&gt;

&lt;p&gt;This query is for telling the type apart when you reproduce the symptom in a test environment. It is not suited to production. In production, &lt;code&gt;:init_stock&lt;/code&gt; (the initial stock per row) often cannot be reconstructed without the restocking history, and the query counts every row in &lt;code&gt;orders&lt;/code&gt;, so past orders get mixed in unless you limit the time range. In production, the clue for type A is an alert that watches for rows with &lt;code&gt;stock &amp;lt; 0&lt;/code&gt;, and the clue for type B is comparing the order count with the stock decrease over a limited time range.&lt;/p&gt;

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

&lt;p&gt;How the symptom shows up depends on whether the UPDATE uses a column reference (&lt;code&gt;stock - 1&lt;/code&gt;) or an absolute value computed in an app variable. Stock goes negative with the former (pattern A), and decrements are lost with the latter (pattern B).&lt;/p&gt;

&lt;p&gt;A query that collects &lt;code&gt;negative_rows&lt;/code&gt; and &lt;code&gt;lost_decrements&lt;/code&gt; together tells you which type you have in your own environment.&lt;/p&gt;

&lt;p&gt;The conditional UPDATE does not work for B. If B's code can be rewritten in the &lt;code&gt;stock - 1&lt;/code&gt; form, you can move the subtraction to the database to get A's form and then add the conditional UPDATE, and the CAS retry path is no longer needed. When there is complex app-side processing between SELECT and UPDATE, or when multiple rows or columns have to be updated consistently, the code cannot be moved to A's form, so you need CAS.&lt;/p&gt;

&lt;p&gt;If the app's decision depends on the stock count and you need to guarantee that the value does not change between the check and the update, &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; is an option. Measurements including its throughput impact are in a &lt;a href="https://zenn.dev/shinagawa_web/articles/select-for-update-throughput-ceiling" rel="noopener noreferrer"&gt;separate article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you decrement stock through an ORM, I want to check the log at least once for the UPDATE it issues. If the right-hand side of the SET clause is a value such as &lt;code&gt;19&lt;/code&gt; or &lt;code&gt;$1&lt;/code&gt;, rather than a subtraction from the column such as &lt;code&gt;stock - 1&lt;/code&gt;, that update is pattern B.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/16/transaction-iso.html#XACT-READ-COMMITTED" rel="noopener noreferrer"&gt;Read Committed&lt;/a&gt; section of the PostgreSQL 16 documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/16/transaction-iso.html#XACT-READ-COMMITTED" rel="noopener noreferrer"&gt;Read Committed&lt;/a&gt; section of the PostgreSQL 16 documentation. A later update waits for the earlier update to commit or roll back, and if the earlier one committed, the later one operates on the updated version of the row.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;The test results are &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/37888379271" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and the code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/d883157/read-committed-lost-update-patterns" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;&lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/37887998690/job/113682237124" rel="noopener noreferrer"&gt;Test results&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;Django's &lt;a href="https://docs.djangoproject.com/en/5.2/ref/models/expressions/#f-expressions" rel="noopener noreferrer"&gt;&lt;code&gt;F()&lt;/code&gt;&lt;/a&gt;, Rails' &lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-update_counters" rel="noopener noreferrer"&gt;&lt;code&gt;update_counters&lt;/code&gt;&lt;/a&gt;, and Prisma's &lt;a href="https://www.prisma.io/docs/orm/reference/prisma-client-reference#atomic-number-operations" rel="noopener noreferrer"&gt;atomic number operations&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/16/transaction-iso.html#XACT-READ-COMMITTED" rel="noopener noreferrer"&gt;Read Committed&lt;/a&gt; section of the PostgreSQL 16 documentation. When the wait ends, the WHERE clause is re-evaluated against the updated row.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/16/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS" rel="noopener noreferrer"&gt;CHECK constraints&lt;/a&gt; section of the PostgreSQL 16 documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/16/transaction-iso.html#XACT-REPEATABLE-READ" rel="noopener noreferrer"&gt;Repeatable Read&lt;/a&gt; section of the PostgreSQL 16 documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>backend</category>
      <category>programming</category>
    </item>
    <item>
      <title>After upgrading from Java 8, G1 stopped returning memory</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Fri, 06 Nov 2026 13:02:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/after-upgrading-from-java-8-g1-stopped-returning-memory-38n</link>
      <guid>https://dev.to/shinagawa-web/after-upgrading-from-java-8-g1-stopped-returning-memory-38n</guid>
      <description>&lt;p&gt;When you move from Java 8 to any of Java 11 through 25, the GC the JVM picks when you don't specify one (the default GC) changes from Parallel to G1. A GC is the mechanism that collects the memory of objects no longer in use. Parallel and G1 are names of two kinds of GC.&lt;/p&gt;

&lt;p&gt;With this change, container memory (cgroup &lt;code&gt;memory.current&lt;/code&gt;) no longer drops after a load peak passes.&lt;/p&gt;

&lt;p&gt;I ran the same program under the same load on each version and compared how much container memory changed after the peak&lt;sup id="fnref1"&gt;1&lt;/sup&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Change after the peak&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java 8&lt;/td&gt;
&lt;td&gt;−234.6 to −227.6MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 11&lt;/td&gt;
&lt;td&gt;0.0 to +2.9MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 25&lt;/td&gt;
&lt;td&gt;+2.3 to +2.7MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In this article, the committed heap is the amount of heap the JVM has taken from the OS, and shrinking means reducing the committed heap and returning that memory to the OS. Memory stays high not because of a leak, but because G1 does not shrink the committed heap while load continues.&lt;/p&gt;

&lt;p&gt;You can tell whether memory that stays high after a peak is a leak or the default GC's behavior from the JVM metrics and GC logs you already collect, without touching production. If you still want it to drop, add a setting to G1 or switch the GC back to Parallel.&lt;/p&gt;

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

&lt;p&gt;Whether memory drops after a peak depends not on whether the GC ran, but on whether it shrank the committed heap.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collecting vs. shrinking
&lt;/h3&gt;

&lt;p&gt;The GC collects objects that are no longer in use and creates free space inside the heap. Collecting reduces the used heap, meaning the part of the committed heap that objects occupy. Whether the freed space is returned to the OS to shrink the committed heap is decided separately from collecting.&lt;/p&gt;

&lt;h3&gt;
  
  
  When a GC happens
&lt;/h3&gt;

&lt;p&gt;The JVM splits the heap into a young generation and an old generation. Most new objects are placed in the young generation, and most of them become unused there. When the young generation fills up, a GC that collects only the young generation (young GC) runs. While the program keeps allocating, the young generation fills up again and again, so young GCs keep coming.&lt;/p&gt;

&lt;p&gt;Objects that survive several young GCs are moved to the old generation. When the old generation fills up, the whole heap has to be collected. A GC that stops the program and collects the whole heap is called a full GC&lt;sup id="fnref2"&gt;2&lt;/sup&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two ways to pause
&lt;/h3&gt;

&lt;p&gt;While the GC collects, the program is stopped, and requests that arrive in the meantime wait. GC designs differ in how they pause.&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;Pause all at once&lt;/th&gt;
&lt;th&gt;Pause in small pieces&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;How it collects&lt;/td&gt;
&lt;td&gt;Stops the program and collects in one go&lt;/td&gt;
&lt;td&gt;Splits collection into pieces and runs part of it concurrently with the program&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single pause&lt;/td&gt;
&lt;td&gt;Long&lt;/td&gt;
&lt;td&gt;Short&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Work the program gets done (throughput)&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Somewhat lower, since the GC uses CPU&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When Oracle's guide recommends it&lt;/td&gt;
&lt;td&gt;Pauses of 1 second or longer are acceptable&lt;/td&gt;
&lt;td&gt;Response time matters more than throughput&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GC&lt;/td&gt;
&lt;td&gt;Parallel&lt;/td&gt;
&lt;td&gt;G1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The name Parallel comes from using multiple threads in parallel to collect while the program is stopped. It does not mean Parallel runs concurrently with the program&lt;sup id="fnref3"&gt;3&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;To keep pauses short, G1 has a phase that scans live objects across the whole heap without stopping the program (concurrent marking). Concurrent marking ends with a short pause (Remark). It starts at the end of a young GC when old generation occupancy exceeds a threshold&lt;sup id="fnref4"&gt;4&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;After concurrent marking, G1 collects part of the old generation together with the young generation in the following young GCs (mixed GCs). G1 does a full GC only when this collection can't keep up and memory runs short&lt;sup id="fnref5"&gt;5&lt;/sup&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed
&lt;/h2&gt;

&lt;p&gt;Upgrading from Java 8 changes two things: the default GC, and what triggers shrinking the committed heap.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Default GC&lt;/th&gt;
&lt;th&gt;Trigger for shrinking the committed heap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java 8&lt;/td&gt;
&lt;td&gt;Parallel&lt;/td&gt;
&lt;td&gt;Young GC (young generation only)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 11&lt;/td&gt;
&lt;td&gt;G1&lt;/td&gt;
&lt;td&gt;Full GC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 12 to 25&lt;/td&gt;
&lt;td&gt;G1&lt;/td&gt;
&lt;td&gt;Full GC and the end of concurrent marking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  The default GC
&lt;/h3&gt;

&lt;p&gt;When no GC is specified, Java 8 picks Parallel, and Java 9 and later pick G1&lt;sup id="fnref6"&gt;6&lt;/sup&gt;. OpenJDK's stated reason for switching to G1 was that "limiting GC pause times is, in general, more important than maximizing throughput" for many users&lt;sup id="fnref7"&gt;7&lt;/sup&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shrink triggers
&lt;/h3&gt;

&lt;p&gt;Java 8's Parallel resizes the young generation on every young GC, and shrinks it&lt;sup id="fnref8"&gt;8&lt;/sup&gt;. Young GCs keep coming while load continues, so the trigger for shrinking keeps coming too.&lt;/p&gt;

&lt;p&gt;Java 11's G1 shrinks the committed heap only after a full GC&lt;sup id="fnref9"&gt;9&lt;/sup&gt;. G1 does a full GC only when collection can't keep up, so this trigger almost never comes.&lt;/p&gt;

&lt;p&gt;From Java 12, G1 also shrinks the committed heap at the end of concurrent marking&lt;sup id="fnref10"&gt;10&lt;/sup&gt;. But concurrent marking starts only when the old generation is filling up, so under a load where newly allocated objects quickly become unused, this trigger doesn't come either&lt;sup id="fnref11"&gt;11&lt;/sup&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Periodic GC
&lt;/h3&gt;

&lt;p&gt;According to the Java 12 change (JEP 346), G1 avoids full GCs as much as possible and starts concurrent marking only based on heap occupancy and allocation rate, so it often doesn't return heap memory to the OS unless prompted from outside.&lt;/p&gt;

&lt;p&gt;This is a particular disadvantage in container environments where you pay for what you use, so Java 12 added a mechanism in which G1 starts concurrent marking on its own and shrinks the committed heap when no GC has happened for a while&lt;sup id="fnref12"&gt;12&lt;/sup&gt;. This is called periodic GC.&lt;/p&gt;

&lt;p&gt;At a set interval, G1 checks the time since the last GC. If at least one interval has passed, it starts concurrent marking and shrinks at the Remark that ends it. If not, it skips&lt;sup id="fnref13"&gt;13&lt;/sup&gt;. Despite the name, it does not necessarily run a GC every interval.&lt;/p&gt;

&lt;p&gt;For example, with a 10-second interval, if objects keep being allocated and young GCs happen less than a second apart, 10 seconds never pass since the last GC. During that time, periodic GC never runs.&lt;/p&gt;

&lt;p&gt;The interval is set in milliseconds with &lt;code&gt;G1PeriodicGCInterval&lt;/code&gt;. Setting it to 0 disables periodic GC. The default is 0, so periodic GC doesn't run unless you set it&lt;sup id="fnref14"&gt;14&lt;/sup&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  With periodic GC enabled
&lt;/h2&gt;

&lt;p&gt;With periodic GC enabled, it is skipped while load continues, and runs after load stops to shrink the committed heap.&lt;/p&gt;

&lt;p&gt;I simulated two situations after a traffic surge subsides and compared them&lt;sup id="fnref15"&gt;15&lt;/sup&gt;.&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;Peak (5 seconds)&lt;/th&gt;
&lt;th&gt;After the peak&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Load continues&lt;/td&gt;
&lt;td&gt;Traffic surges. Allocates 1000MB of objects per second&lt;/td&gt;
&lt;td&gt;Normal requests keep coming. Keeps allocating 200MB of objects per second&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Load stops&lt;/td&gt;
&lt;td&gt;Same&lt;/td&gt;
&lt;td&gt;Requests stop. Allocates nothing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both ran Java 25 with &lt;code&gt;-XX:G1PeriodicGCInterval=10000&lt;/code&gt;, a 10-second interval.&lt;/p&gt;

&lt;h3&gt;
  
  
  While load continues
&lt;/h3&gt;

&lt;p&gt;When load continues after the peak, the check at 10 seconds after startup writes the following line to the GC log. This line appears when &lt;code&gt;-Xlog:gc+periodic=debug&lt;/code&gt; is set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[10.017s][debug][gc,periodic ] Last GC occurred 505ms before which is below threshold 10000ms. Skipping.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last GC was 505ms earlier, short of the 10000ms interval, so periodic GC was skipped&lt;sup id="fnref13"&gt;13&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;In the 45 seconds after the peak, the check ran 5 times, was skipped all 5 times, and periodic GC never ran. The committed heap didn't change from the end of the peak to the end of the run.&lt;/p&gt;

&lt;h3&gt;
  
  
  After load stops
&lt;/h3&gt;

&lt;p&gt;When nothing was allocated after the peak, periodic GC ran at the check 20 seconds after startup. At the 10-second check, 10 seconds had not yet passed since the GC at the end of the peak. First, a line for the young GC that starts concurrent marking appears.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[20.016s][info ][gc          ] GC(46) Pause Young (Concurrent Start) (G1 Periodic Collection) 195M-&amp;gt;37M(377M) 1.104ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;(G1 Periodic Collection)&lt;/code&gt; marks this as a GC started by periodic GC. &lt;code&gt;195M-&amp;gt;37M&lt;/code&gt; is the used heap before and after, and the &lt;code&gt;377M&lt;/code&gt; in parentheses is the committed heap&lt;sup id="fnref16"&gt;16&lt;/sup&gt;. This young GC does not change the committed heap.&lt;/p&gt;

&lt;p&gt;On the Remark line that follows, the committed heap in parentheses is &lt;code&gt;27M&lt;/code&gt;&lt;sup id="fnref10"&gt;10&lt;/sup&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[20.019s][info ][gc          ] GC(47) Pause Remark 37M-&amp;gt;7M(27M) 0.478ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The committed heap seen in JVM metrics (the sum of committed across the heap memory pools, read via &lt;code&gt;MemoryPoolMXBean&lt;/code&gt;) dropped by the same amount&lt;sup id="fnref17"&gt;17&lt;/sup&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Measured at&lt;/th&gt;
&lt;th&gt;Committed heap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Just before periodic GC&lt;/td&gt;
&lt;td&gt;377.0 to 432.0MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After periodic GC&lt;/td&gt;
&lt;td&gt;27.0 to 30.0MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The container memory compared at the start (&lt;code&gt;memory.current&lt;/code&gt;, sampled every second) dropped too. It went from 393.9 to 421.4MB at the sample just before periodic GC to 53.8 to 59.5MB at the last sample.&lt;/p&gt;

&lt;h2&gt;
  
  
  Diagnosing your own environment
&lt;/h2&gt;

&lt;p&gt;Use the JVM metrics and GC logs you already collect. For GC logs, the lines printed with &lt;code&gt;-Xlog:gc&lt;/code&gt; are enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which GC was selected
&lt;/h3&gt;

&lt;p&gt;In JVM metrics, the heap shows up as several areas, and their names depend on the GC. These areas are called memory pools&lt;sup id="fnref18"&gt;18&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;The pool name is in the &lt;code&gt;id&lt;/code&gt; tag of &lt;code&gt;jvm.memory.committed&lt;/code&gt; in Micrometer, and in the &lt;code&gt;pool&lt;/code&gt; label of &lt;code&gt;jvm_memory_pool_committed_bytes&lt;/code&gt; in the Prometheus Java client&lt;sup id="fnref19"&gt;19&lt;/sup&gt;. The names tell you which GC was selected in production.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;GC&lt;/th&gt;
&lt;th&gt;Heap memory pools&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Parallel&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;PS Eden Space&lt;/code&gt;, &lt;code&gt;PS Survivor Space&lt;/code&gt;, &lt;code&gt;PS Old Gen&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G1&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;G1 Eden Space&lt;/code&gt;, &lt;code&gt;G1 Survivor Space&lt;/code&gt;, &lt;code&gt;G1 Old Gen&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you see the Parallel pools, the behavior described here doesn't apply. If you see neither, see the "Scope" section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is it a leak?
&lt;/h3&gt;

&lt;p&gt;When memory doesn't drop, a leak is the first suspect. With a leak, the amount of live objects keeps growing over time even under constant load&lt;sup id="fnref20"&gt;20&lt;/sup&gt;. With the behavior described here, live objects stay few and only the committed heap is high.&lt;/p&gt;

&lt;p&gt;To tell them apart, compare the committed heap with the used heap after GC. Read the committed heap as the sum of committed across the heap pools. Read the used heap after GC from the after-GC value on each GC line in the GC log. A pool's used value is not suitable for this comparison, because the Eden part goes up and down with every GC.&lt;/p&gt;

&lt;p&gt;On Java 11 with continued load, the first young GC line after the peak was&lt;sup id="fnref21"&gt;21&lt;/sup&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[6.877s][info ][gc            ] GC(27) Pause Young (Normal) (G1 Evacuation Pause) 679M-&amp;gt;89M(989M) 1.773ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On this line, the &lt;code&gt;89M&lt;/code&gt; after the arrow is the used heap after GC, and the &lt;code&gt;989M&lt;/code&gt; in parentheses is the committed heap. Most of the committed heap is free, and collection is keeping up. The freed space just isn't being returned to the OS.&lt;/p&gt;

&lt;p&gt;Live objects can't exceed the used heap after GC, so if the used heap after GC isn't growing, you don't need to suspect a leak.&lt;/p&gt;

&lt;p&gt;However, a young GC collects only the young generation, so garbage piled up in the old generation stays in the used heap after a young GC. This value can creep up even without a leak.&lt;/p&gt;

&lt;p&gt;If it is growing, the behavior described here doesn't explain it on its own. If you have values after a GC that also collects the old generation (a full GC, or a mixed GC after concurrent marking), look at those. If they keep growing too, or if no such GC has happened, investigate it as a leak&lt;sup id="fnref20"&gt;20&lt;/sup&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  GCs that trigger shrinking
&lt;/h3&gt;

&lt;p&gt;Next, look in the GC log after the peak for lines from GCs that trigger shrinking the committed heap. The line names are the same in Java 11 and Java 25&lt;sup id="fnref22"&gt;22&lt;/sup&gt;. Java 11 also prints &lt;code&gt;Pause Remark&lt;/code&gt;, but it doesn't shrink the committed heap there.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;GC lines that trigger shrinking&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java 11&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Pause Full&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 12 to 25&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Pause Full&lt;/code&gt;, &lt;code&gt;Pause Remark&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When concurrent marking starts, a &lt;code&gt;Pause Young (Concurrent Start)&lt;/code&gt; line also appears before &lt;code&gt;Pause Remark&lt;/code&gt;. If none of these appear, no shrink trigger has come. On Java 11, that settles it. On Java 12 and later, the remaining trigger is periodic GC.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is periodic GC running?
&lt;/h3&gt;

&lt;p&gt;On Java 12 and later, if there is no &lt;code&gt;Pause Remark&lt;/code&gt;, periodic GC isn't running either. Periodic GC starts concurrent marking, so when it runs, &lt;code&gt;Pause Remark&lt;/code&gt; appears.&lt;/p&gt;

&lt;p&gt;If it isn't running, either the interval isn't set, or it is set but the check is skipped every time because load continues (see "With periodic GC enabled"). Besides the startup option, the interval can also be set on a running JVM with &lt;code&gt;jcmd&lt;/code&gt;&lt;sup id="fnref14"&gt;14&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;If all of the following hold, memory isn't dropping because G1 isn't getting a trigger to shrink the committed heap, not because of a leak.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The pools are G1's&lt;/li&gt;
&lt;li&gt;The used heap after GC isn't growing&lt;/li&gt;
&lt;li&gt;There is no &lt;code&gt;Pause Full&lt;/code&gt; after the peak, and on Java 12 and later, no &lt;code&gt;Pause Remark&lt;/code&gt; either&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fixes
&lt;/h2&gt;

&lt;p&gt;If it isn't a leak but you still want memory to drop, either keep G1 and add a trigger to shrink when load stops, or go back to a GC that shrinks on every young GC.&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;Enable periodic GC&lt;/th&gt;
&lt;th&gt;Go back to Parallel&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Option&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-XX:G1PeriodicGCInterval=&amp;lt;milliseconds&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;-XX:+UseParallelGC&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shrink trigger&lt;/td&gt;
&lt;td&gt;When no GC happens for longer than the interval&lt;/td&gt;
&lt;td&gt;Every young GC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;On a server whose load never stops&lt;/td&gt;
&lt;td&gt;Doesn't shrink&lt;/td&gt;
&lt;td&gt;Shrinks&lt;sup id="fnref23"&gt;23&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When load stops right after the peak&lt;/td&gt;
&lt;td&gt;Shrinks&lt;/td&gt;
&lt;td&gt;Doesn't shrink&lt;sup id="fnref24"&gt;24&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;On Java 11&lt;/td&gt;
&lt;td&gt;Not available&lt;sup id="fnref25"&gt;25&lt;/sup&gt;
&lt;/td&gt;
&lt;td&gt;Available&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single pause&lt;/td&gt;
&lt;td&gt;Stays short, as with G1&lt;/td&gt;
&lt;td&gt;Gets longer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Which to choose depends on what you found in "Diagnosing your own environment" and on whether there are periods when load stops.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What you found&lt;/th&gt;
&lt;th&gt;Load stops at times&lt;/th&gt;
&lt;th&gt;Load never stops&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java 11, no &lt;code&gt;Pause Full&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Upgrade to Java 12 or later and enable periodic GC, or go back to Parallel&lt;/td&gt;
&lt;td&gt;Go back to Parallel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 12 or later, periodic GC not set&lt;/td&gt;
&lt;td&gt;Enable periodic GC&lt;/td&gt;
&lt;td&gt;Go back to Parallel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Periodic GC set, but no &lt;code&gt;Pause Remark&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Shorten the interval&lt;/td&gt;
&lt;td&gt;Go back to Parallel&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Check for periods when load stops using the timestamp at the start of each GC log line. If there are periods with long gaps between GCs, periodic GC can run there.&lt;/p&gt;

&lt;p&gt;Periodic GC runs when, at an interval check, at least one interval has passed since the last GC. Because the check comes only once per interval, it can take up to twice the interval from when load stops until it runs. Set the interval to less than half the length of the periods when load stops&lt;sup id="fnref13"&gt;13&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Going back to Parallel makes each pause longer. How much longer depends on heap size and the amount of live objects. Before production, run the same load in a test environment with &lt;code&gt;-XX:+UseParallelGC&lt;/code&gt; and compare the pause times at the end of each GC log line with those under G1.&lt;/p&gt;

&lt;p&gt;A committed heap that doesn't drop is a problem when alerts or autoscaling are based on container memory. In that case, before changing GC settings, I want to check whether that metric can be switched to the used heap after GC.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;The tests in this article ran in a 2GB, 2-CPU container with no GC specified. The committed heap's upper limit is set by &lt;code&gt;-Xmx&lt;/code&gt;, and its value at startup by &lt;code&gt;-Xms&lt;/code&gt;. The tests did not set &lt;code&gt;-Xms&lt;/code&gt;. The load was one where newly allocated objects quickly became unused. The conclusions don't apply in the following cases.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Case&lt;/th&gt;
&lt;th&gt;How it differs from the conclusions&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Small containers (1 CPU, or less than 1792MB of memory)&lt;/td&gt;
&lt;td&gt;Java 8 to 26 select Serial&lt;sup id="fnref26"&gt;26&lt;/sup&gt;. The Parallel and G1 discussion doesn't apply&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;-Xms&lt;/code&gt; is set&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;-Xms&lt;/code&gt; is also the lower bound of the committed heap, and G1 won't shrink below it. If it equals &lt;code&gt;-Xmx&lt;/code&gt;, memory won't drop even with periodic GC&lt;sup id="fnref27"&gt;27&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Parallel, with no GC after the peak&lt;/td&gt;
&lt;td&gt;Parallel shrinks only during GC, so memory doesn't drop&lt;sup id="fnref24"&gt;24&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Load where objects survive and the old generation fills up&lt;/td&gt;
&lt;td&gt;Concurrent marking starts, and G1 on Java 12 and later shrinks at its Remark. Memory may drop without periodic GC&lt;sup id="fnref28"&gt;28&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 26&lt;/td&gt;
&lt;td&gt;Both G1 and Parallel shrink after young GCs. Memory may drop without periodic GC&lt;sup id="fnref29"&gt;29&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 27&lt;/td&gt;
&lt;td&gt;G1 is selected even in small containers&lt;sup id="fnref30"&gt;30&lt;/sup&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can tell Serial was selected if the memory pool names are &lt;code&gt;Eden Space&lt;/code&gt;, &lt;code&gt;Survivor Space&lt;/code&gt;, and &lt;code&gt;Tenured Gen&lt;/code&gt;&lt;sup id="fnref31"&gt;31&lt;/sup&gt;, or if the following line appears in the GC log at startup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0.003s][info][gc     ] Using Serial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java 15 and later, 8u372 and later, and 11.0.16 and later can read cgroup v2 container limits. Earlier versions pick the GC based on the host's CPU and memory&lt;sup id="fnref32"&gt;32&lt;/sup&gt;.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Docker on a GitHub Actions runner, with a 2GB, 2-CPU container and &lt;code&gt;-Xmx1500m&lt;/code&gt;. A 5-second peak (simulating a sudden surge in traffic by allocating 1000MB of objects per second) was followed by 45 seconds of normal load, allocating 200MB of objects per second. Ranges are over 3 runs per version. Scripts and results are in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/25661a9/g1-heap-not-returned" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt; and the &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/40435540901" rel="noopener noreferrer"&gt;GitHub Actions run&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Oracle's &lt;a href="https://docs.oracle.com/en/java/javase/25/gctuning/garbage-collector-implementation.html" rel="noopener noreferrer"&gt;HotSpot Virtual Machine Garbage Collection Tuning Guide, Generations&lt;/a&gt; (Java 25).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;The same Oracle guide (Java 25): &lt;a href="https://docs.oracle.com/en/java/javase/25/gctuning/parallel-collector1.html" rel="noopener noreferrer"&gt;The Parallel Collector&lt;/a&gt;, &lt;a href="https://docs.oracle.com/en/java/javase/25/gctuning/garbage-first-g1-garbage-collector1.html" rel="noopener noreferrer"&gt;Garbage-First (G1) Garbage Collector&lt;/a&gt;, and &lt;a href="https://docs.oracle.com/en/java/javase/25/gctuning/available-collectors.html" rel="noopener noreferrer"&gt;Selecting a Collector in Available Collectors&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;In Java 25, &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1Policy.cpp#L745-L764" rel="noopener noreferrer"&gt;the code that decides at the end of a young GC whether to start concurrent marking&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;The same Oracle guide (Java 25), &lt;a href="https://docs.oracle.com/en/java/javase/25/gctuning/garbage-first-g1-garbage-collector1.html" rel="noopener noreferrer"&gt;Garbage-First (G1) Garbage Collector&lt;/a&gt;: the Space-Reclamation Phase and the description of Full GC that follows.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;When Java 8 is started with &lt;code&gt;-XX:+PrintCommandLineFlags&lt;/code&gt;, the flags line the JVM prints includes &lt;code&gt;-XX:+UseParallelGC&lt;/code&gt;. Java 11 and Java 25 print a &lt;code&gt;Using G1&lt;/code&gt; line at the top of the GC log (in Java 11, &lt;code&gt;[0.004s][info ][gc          ] Using G1&lt;/code&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;The Motivation section of &lt;a href="https://openjdk.org/jeps/248" rel="noopener noreferrer"&gt;JEP 248: Make G1 the Default Garbage Collector&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;Java 8's Parallel &lt;a href="https://github.com/openjdk/jdk8u/blob/d6b9b398581ed6d4c9e94584aae048f2c23644f9/hotspot/src/share/vm/gc_implementation/parallelScavenge/psScavenge.cpp#L606-L614" rel="noopener noreferrer"&gt;resizes the young generation on every young GC&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;Java 11's G1 &lt;a href="https://github.com/openjdk/jdk11u/blob/815fd2c9c6e788ccd5491c81f924f1f174c0b6f3/src/hotspot/share/gc/g1/g1CollectedHeap.cpp#L1046-L1050" rel="noopener noreferrer"&gt;resizes the committed heap&lt;/a&gt; as part of a full GC.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;The change that made Remark shrink the heap is &lt;a href="https://bugs.openjdk.org/browse/JDK-6490394" rel="noopener noreferrer"&gt;JDK-6490394&lt;/a&gt; (Java 12). In Java 25, the committed heap is resized &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1ConcurrentMark.cpp#L1464-L1465" rel="noopener noreferrer"&gt;inside Remark&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn11"&gt;
&lt;p&gt;With 200MB of objects allocated per second after the peak, concurrent marking did not start after the peak on any of Java 11, 17, 21, or 25.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn12"&gt;
&lt;p&gt;The Motivation section of &lt;a href="https://openjdk.org/jeps/346" rel="noopener noreferrer"&gt;JEP 346: Promptly Return Unused Committed Memory from G1&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn13"&gt;
&lt;p&gt;Java 25's &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1PeriodicGCTask.cpp#L36-L68" rel="noopener noreferrer"&gt;periodic GC check&lt;/a&gt; and &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1_globals.hpp#L299-L302" rel="noopener noreferrer"&gt;the flag that makes it start concurrent marking by default (&lt;code&gt;G1PeriodicGCInvokesConcurrent&lt;/code&gt;)&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn14"&gt;
&lt;p&gt;&lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1_globals.hpp#L294-L297" rel="noopener noreferrer"&gt;The definition of &lt;code&gt;G1PeriodicGCInterval&lt;/code&gt;&lt;/a&gt;. It is a &lt;code&gt;MANAGEABLE&lt;/code&gt; flag, so it can be changed on a running JVM.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn15"&gt;
&lt;p&gt;2GB, 2-CPU container, &lt;code&gt;-Xmx1500m&lt;/code&gt;, no GC specified. In the load-continues runs, objects were allocated for 45 seconds after the peak. GC logs were captured with &lt;code&gt;-Xlog:gc*&lt;/code&gt; and &lt;code&gt;-Xlog:gc+periodic=debug&lt;/code&gt;. The committed heap table shows the sample just before periodic GC and the first sample after it.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn16"&gt;
&lt;p&gt;How to read GC log lines ("used before GC" -&amp;gt; "used after GC" ("heap size")) in &lt;a href="https://docs.oracle.com/en/java/javase/25/gctuning/garbage-collector-implementation.html" rel="noopener noreferrer"&gt;Generations&lt;/a&gt; in Oracle's GC tuning guide (Java 25).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn17"&gt;
&lt;p&gt;The same test was run 3 times. Values shown as "X to Y" are the range across the 3 runs.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn18"&gt;
&lt;p&gt;The pool names are defined in the source of &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1MemoryPool.cpp#L45-L67" rel="noopener noreferrer"&gt;G1&lt;/a&gt; and &lt;a href="https://github.com/openjdk/jdk8u/blob/d6b9b398581ed6d4c9e94584aae048f2c23644f9/hotspot/src/share/vm/services/memoryService.cpp#L344-L363" rel="noopener noreferrer"&gt;Parallel&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn19"&gt;
&lt;p&gt;Both read values from &lt;code&gt;MemoryPoolMXBean&lt;/code&gt;. &lt;a href="https://github.com/micrometer-metrics/micrometer/blob/v1.13.0/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jvm/JvmMemoryMetrics.java#L78-L90" rel="noopener noreferrer"&gt;JvmMemoryMetrics in Micrometer 1.13.0&lt;/a&gt;, &lt;a href="https://github.com/prometheus/client_java/blob/v1.3.1/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetrics.java#L104-L108" rel="noopener noreferrer"&gt;JvmMemoryMetrics in the Prometheus Java client 1.3.1&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn20"&gt;
&lt;p&gt;Oracle's &lt;a href="https://docs.oracle.com/en/java/javase/25/troubleshoot/troubleshooting-memory-leaks.html" rel="noopener noreferrer"&gt;Troubleshooting Guide, Troubleshoot Memory Leaks&lt;/a&gt; (Java 25).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn21"&gt;
&lt;p&gt;The first young GC line after the peak, under the condition where 200MB of objects per second kept being allocated after the peak.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn22"&gt;
&lt;p&gt;Java 11's &lt;a href="https://github.com/openjdk/jdk11u/blob/815fd2c9c6e788ccd5491c81f924f1f174c0b6f3/src/hotspot/share/gc/g1/g1CollectedHeap.cpp#L1137" rel="noopener noreferrer"&gt;&lt;code&gt;Pause Full&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/openjdk/jdk11u/blob/815fd2c9c6e788ccd5491c81f924f1f174c0b6f3/src/hotspot/share/gc/g1/g1CollectedHeap.cpp#L2857-L2859" rel="noopener noreferrer"&gt;&lt;code&gt;Pause Young (Concurrent Start)&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://github.com/openjdk/jdk11u/blob/815fd2c9c6e788ccd5491c81f924f1f174c0b6f3/src/hotspot/share/gc/g1/g1ConcurrentMarkThread.cpp#L343" rel="noopener noreferrer"&gt;&lt;code&gt;Pause Remark&lt;/code&gt;&lt;/a&gt;. Java 25's &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1CollectedHeap.cpp#L874" rel="noopener noreferrer"&gt;&lt;code&gt;Pause Full&lt;/code&gt;&lt;/a&gt;. The other two for Java 25 appear in the logs in "With periodic GC enabled".&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn23"&gt;
&lt;p&gt;Java 11's Parallel also &lt;a href="https://github.com/openjdk/jdk11u/blob/815fd2c9c6e788ccd5491c81f924f1f174c0b6f3/src/hotspot/share/gc/parallel/psScavenge.cpp#L568-L569" rel="noopener noreferrer"&gt;resizes the young generation on every young GC&lt;/a&gt;. With &lt;code&gt;-XX:+UseParallelGC&lt;/code&gt; on Java 11 and 200MB of objects per second allocated after the peak, the committed heap went from 369.0 to 380.0MB at the end of the peak to 103.5 to 120.5MB at the last sample, and container memory (&lt;code&gt;memory.current&lt;/code&gt;) dropped by 141.4 to 145.5MB.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn24"&gt;
&lt;p&gt;Parallel resizes during young GC on both &lt;a href="https://github.com/openjdk/jdk8u/blob/d6b9b398581ed6d4c9e94584aae048f2c23644f9/hotspot/src/share/vm/gc_implementation/parallelScavenge/psScavenge.cpp#L606-L614" rel="noopener noreferrer"&gt;Java 8&lt;/a&gt; and &lt;a href="https://github.com/openjdk/jdk11u/blob/815fd2c9c6e788ccd5491c81f924f1f174c0b6f3/src/hotspot/share/gc/parallel/psScavenge.cpp#L568-L569" rel="noopener noreferrer"&gt;Java 11&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn25"&gt;
&lt;p&gt;On Java 11, setting it makes the JVM print &lt;code&gt;Unrecognized VM option 'G1PeriodicGCInterval=10000'&lt;/code&gt; and fail to start. Periodic GC is available from Java 12.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn26"&gt;
&lt;p&gt;Only &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/runtime/os.cpp#L1928-L1947" rel="noopener noreferrer"&gt;when there are 2 or more CPUs and memory is at least 2GB minus 256MB&lt;/a&gt; does the JVM select &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/shared/gcConfig.cpp#L97-L111" rel="noopener noreferrer"&gt;G1 on Java 9 and later&lt;/a&gt; or &lt;a href="https://github.com/openjdk/jdk8u/blob/d6b9b398581ed6d4c9e94584aae048f2c23644f9/hotspot/src/share/vm/runtime/arguments.cpp#L1593-L1600" rel="noopener noreferrer"&gt;Parallel on Java 8&lt;/a&gt;. &lt;a href="https://github.com/openjdk/jdk8u/blob/d6b9b398581ed6d4c9e94584aae048f2c23644f9/hotspot/src/share/vm/runtime/os.cpp#L1435-L1454" rel="noopener noreferrer"&gt;Java 8's check&lt;/a&gt; is the same.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn27"&gt;
&lt;p&gt;&lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/runtime/arguments.cpp#L2379-L2393" rel="noopener noreferrer"&gt;&lt;code&gt;-Xms&lt;/code&gt; sets both the committed heap at startup and its lower bound (MinHeapSize)&lt;/a&gt;. &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1HeapSizingPolicy.cpp#L247-L264" rel="noopener noreferrer"&gt;G1 does not shrink below MinHeapSize&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn28"&gt;
&lt;p&gt;G1 &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1Policy.cpp#L745-L764" rel="noopener noreferrer"&gt;checks old generation occupancy at the end of a young GC to start concurrent marking&lt;/a&gt;, and &lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/g1/g1ConcurrentMark.cpp#L1464-L1465" rel="noopener noreferrer"&gt;shrinks at Remark&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn29"&gt;
&lt;p&gt;Java 26's &lt;a href="https://github.com/openjdk/jdk26u/blob/baf63fbe42b8758448fee570a9d9bb914272d259/src/hotspot/share/gc/g1/g1CollectedHeap.cpp#L968-L979" rel="noopener noreferrer"&gt;G1&lt;/a&gt; (&lt;a href="https://bugs.openjdk.org/browse/JDK-8238687" rel="noopener noreferrer"&gt;JDK-8238687&lt;/a&gt;) and &lt;a href="https://github.com/openjdk/jdk26u/blob/baf63fbe42b8758448fee570a9d9bb914272d259/src/hotspot/share/gc/parallel/parallelScavengeHeap.cpp#L889-L919" rel="noopener noreferrer"&gt;Parallel&lt;/a&gt; (&lt;a href="https://bugs.openjdk.org/browse/JDK-8338977" rel="noopener noreferrer"&gt;JDK-8338977&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn30"&gt;
&lt;p&gt;&lt;a href="https://openjdk.org/jeps/523" rel="noopener noreferrer"&gt;JEP 523&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn31"&gt;
&lt;p&gt;&lt;a href="https://github.com/openjdk/jdk25u/blob/1775ea114f649f0314e4e43eb6c802ff16dfab7c/src/hotspot/share/gc/serial/serialHeap.cpp#L112-L121" rel="noopener noreferrer"&gt;Serial's memory pools&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn32"&gt;
&lt;p&gt;The change to read cgroup v2 limits &lt;a href="https://bugs.openjdk.org/browse/JDK-8230305" rel="noopener noreferrer"&gt;landed in Java 15&lt;/a&gt; and was backported to &lt;a href="https://bugs.openjdk.org/browse/JDK-8283559" rel="noopener noreferrer"&gt;11.0.16&lt;/a&gt; and &lt;a href="https://bugs.openjdk.org/browse/JDK-8297880" rel="noopener noreferrer"&gt;8u372&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>java</category>
      <category>devops</category>
      <category>performance</category>
      <category>docker</category>
    </item>
    <item>
      <title>Orders stall only during sales: work done while holding a SELECT FOR UPDATE lock</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Tue, 03 Nov 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/postgres-for-update-caps-throughput-at-1-lock-hold-time-3598</link>
      <guid>https://dev.to/shinagawa-web/postgres-for-update-caps-throughput-at-1-lock-hold-time-3598</guid>
      <description>&lt;p&gt;If you keep stock as one row per item and every order takes that row with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; before updating it, there is a ceiling on how many orders for the same item can complete per second. That is because the row lock is not released until &lt;code&gt;COMMIT&lt;/code&gt;, and other orders that come for the same row wait on the lock in the meantime.&lt;sup id="fnref1"&gt;1&lt;/sup&gt; Call the time from when &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; returns with the lock until &lt;code&gt;COMMIT&lt;/code&gt; the hold time. The ceiling is then 1 ÷ hold time per second, which is 10 per second for a hold time of 0.1s. The hold time includes everything the application server does while holding the lock, such as calls to external APIs.&lt;/p&gt;

&lt;p&gt;The excess orders pile up unfinished only during periods like a sale, when orders for the same item arrive faster than this ceiling, and response time for that item's orders keeps growing. An order waiting for the lock holds its connection while it waits, so other requests on the same connection pool also slow down while they wait for a free connection. Adding DB connections does not push completions per second past the ceiling. Waiting orders use no DB CPU, so this happens even while DB CPU usage stays low.&lt;/p&gt;

&lt;p&gt;In traces collected with Datadog, OpenTelemetry, or similar tools (a trace lays out, for a single request, the time spent on each operation, such as each SQL statement sent to the DB; one operation is called a span), the span of the &lt;code&gt;SELECT&lt;/code&gt; containing &lt;code&gt;FOR UPDATE&lt;/code&gt; grows by the time spent waiting for the lock. The &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;INSERT&lt;/code&gt; spans that follow are short. Once the connection pool is full, most of the response time is spent waiting for a connection.&lt;/p&gt;

&lt;p&gt;I reproduced this on PostgreSQL 18.6 and checked the ceiling and the effect of moving the application server's work outside the lock.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening
&lt;/h2&gt;

&lt;p&gt;The diagram below shows orders for the same item arriving every 0.05s (20 per second) with a hold time of 0.1s (a ceiling of 10 per second). It assumes there are enough connections and that orders take the lock in arrival order (the actual order is not necessarily the arrival order&lt;sup id="fnref3"&gt;3&lt;/sup&gt;). Each character is 0.01s, &lt;code&gt;=&lt;/code&gt; is time holding the lock, &lt;code&gt;.&lt;/code&gt; is time waiting for the lock, and t is seconds since the first order arrived.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t    0.0       0.1       0.2       0.3       0.4
A    ==========
B         .....==========
C              ..........==========
D                   ...............==========
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Completions stay at one per 0.1s, so the gap between that and the 20 per second arriving accumulates. As long as orders keep arriving, the wait grows with each one: 0.05s for B, 0.1s for C, 0.15s for D. If you add connections, the new connections fill up with orders for the same item. Orders that were waiting for a connection move to waiting for the lock, and completions stay at one per 0.1s.&lt;/p&gt;

&lt;h2&gt;
  
  
  Completions and response time at peak
&lt;/h2&gt;

&lt;p&gt;I sent orders for the same item at an average of 20 per second, at random intervals, for 30 seconds. Each order sends the following SQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 0.1s of work on the application server&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over the 30 seconds of arrivals, &lt;code&gt;COMMIT&lt;/code&gt;s per second averaged 9.8, leveling off at about the ceiling of 10 per second for a 0.1s hold time.&lt;/p&gt;

&lt;p&gt;The p99 response time (the value at the 99% position when response times are sorted from fastest to slowest) was 30.5s, longer than the 30 seconds during which orders kept arriving.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identifying it in your own environment
&lt;/h2&gt;

&lt;p&gt;If, while response times are growing, orders for the item arrive at more than 1 ÷ hold time per second and completions flatten out at that level, this is what is happening. Count arrivals and completions by filtering order requests and successful responses on the item ID in the URL or access log.&lt;/p&gt;

&lt;p&gt;To get the hold time, take a few order traces, measure from the end of the &lt;code&gt;SELECT&lt;/code&gt; span containing &lt;code&gt;FOR UPDATE&lt;/code&gt; to the end of the last SQL span (the &lt;code&gt;COMMIT&lt;/code&gt; span, if there is one), and take the median. The spans and gaps in between are the work done while holding the lock.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixes
&lt;/h2&gt;

&lt;p&gt;Move the work found in the previous section, between the &lt;code&gt;SELECT&lt;/code&gt; containing &lt;code&gt;FOR UPDATE&lt;/code&gt; and the last SQL statement, to before the transaction or after &lt;code&gt;COMMIT&lt;/code&gt;. Either way, only SQL is left in the hold time. Only work that does not use values from the locked row (such as the stock count) can be moved out. For work that can fail, such as payment, what you have to undo when it fails depends on which side you move it to.&lt;/p&gt;

&lt;h3&gt;
  
  
  Before the transaction
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- 0.1s of work on the application server&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- if stock is 0, ROLLBACK and reject the order&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The work finishes before stock is checked, so even an order that is rejected because stock ran out has already done the work. When rejecting, undo the result of that work. For payment, release the authorization (the hold placed on the card's credit limit).&lt;/p&gt;

&lt;h3&gt;
  
  
  After COMMIT
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&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;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 0.1s of work on the application server&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The order and the stock decrement are committed before the work runs, so when the work fails (for payment, when the payment does not go through), cancel the order and restore the stock in a separate transaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Completions and response time with the work moved out
&lt;/h3&gt;

&lt;p&gt;As in the peak run, I sent orders for the same item at 20 per second for 30 seconds.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0.1s of work&lt;/th&gt;
&lt;th&gt;COMMITs per second&lt;/th&gt;
&lt;th&gt;p99&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Inside the lock&lt;/td&gt;
&lt;td&gt;9.8&lt;/td&gt;
&lt;td&gt;30.5s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Before the transaction&lt;/td&gt;
&lt;td&gt;20.0&lt;/td&gt;
&lt;td&gt;0.10s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;After COMMIT&lt;/td&gt;
&lt;td&gt;20.0&lt;/td&gt;
&lt;td&gt;0.18s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In both cases, completions per second catch up with the 20 per second arriving, and p99 drops below 0.2s.&lt;/p&gt;

&lt;p&gt;Work you thought you had moved to after &lt;code&gt;COMMIT&lt;/code&gt; can still stay inside the lock. With a setting that wraps the whole request in one transaction, such as Django's &lt;code&gt;ATOMIC_REQUESTS&lt;/code&gt;, &lt;code&gt;COMMIT&lt;/code&gt; does not happen until the view returns its response, so work written after the SQL also runs while holding the lock.&lt;sup id="fnref4"&gt;4&lt;/sup&gt; When using &lt;code&gt;FOR UPDATE&lt;/code&gt;, I want to look at the settings before the code to check where the transaction ends.&lt;/p&gt;

&lt;h2&gt;
  
  
  Appendix: getting a span per SQL statement with OpenTelemetry
&lt;/h2&gt;

&lt;p&gt;If your order traces do not show a span for each SQL statement, add OpenTelemetry instrumentation (a library that records operations such as SQL statements as spans) to the application server. The application server in the test is written in Python, sends SQL to the DB with psycopg 3, and uses psycopg_pool for the connection pool. These are the packages and versions installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;opentelemetry-api==1.45.1
opentelemetry-sdk==1.45.1
opentelemetry-instrumentation==0.66b1
opentelemetry-instrumentation-dbapi==0.66b1
opentelemetry-instrumentation-psycopg==0.66b1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable the instrumentation once at startup, before creating the connection pool, with the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instrumentation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;psycopg&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;PsycopgInstrumentor&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trace&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;TracerProvider&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;export&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;BatchSpanProcessor&lt;/span&gt;

&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TracerProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_span_processor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BatchSpanProcessor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exporter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_tracer_provider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tracer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_tracer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"server"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;PsycopgInstrumentor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;instrument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tracer_provider&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;exporter&lt;/code&gt;, use an exporter that sends spans to your APM (a service for viewing traces, such as Datadog) or to a collector.&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Once &lt;code&gt;PsycopgInstrumentor().instrument(...)&lt;/code&gt; has been called, each SQL statement sent through a cursor's &lt;code&gt;execute&lt;/code&gt; becomes one span. The span name is the first word of the statement, so statements with and without &lt;code&gt;FOR UPDATE&lt;/code&gt; are both named &lt;code&gt;SELECT&lt;/code&gt;. To tell which span contains &lt;code&gt;FOR UPDATE&lt;/code&gt;, look at the &lt;code&gt;db.statement&lt;/code&gt; attribute, which holds the full SQL text. &lt;code&gt;conn.commit()&lt;/code&gt; does not produce a span, so Identifying it in your own environment measures the hold time up to the end of the last SQL span.&lt;/p&gt;

&lt;p&gt;Open a parent span for each order, and take a connection and send the SQL inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;tracer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start_as_current_span&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"POST /orders"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;cur&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="s2"&gt;"SELECT stock FROM items WHERE id = 1 FOR UPDATE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;# work done while holding the lock&lt;/span&gt;
            &lt;span class="n"&gt;cur&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="s2"&gt;"UPDATE items SET stock = stock - 1 WHERE id = 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cur&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="s2"&gt;"INSERT INTO orders (item_id) VALUES (1)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The SQL spans are recorded as children of this parent span, so all the SQL for one order lines up in one trace. The parent span is opened before the connection is taken, so time spent waiting for a connection does not become a span. It shows up as the gap between the start of the parent span and the first SQL span.&lt;/p&gt;

&lt;p&gt;For one order from the peak run, the exporter wrote these four spans (attributes other than &lt;code&gt;db.statement&lt;/code&gt; are omitted).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"trace_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5a660400fdbe203fae688234501042ab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"span_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"8e6cf824a2827572"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"parent_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ee5e5356e7faea3e"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"SELECT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"start_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974910167581150&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"end_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974912090909676&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"attributes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"db.statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"SELECT stock FROM items WHERE id = 1 FOR UPDATE"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"UNSET"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"trace_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5a660400fdbe203fae688234501042ab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"span_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"a70b9b5a83f4056a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"parent_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ee5e5356e7faea3e"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"UPDATE"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"start_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974912191022767&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"end_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974912191166374&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"attributes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"db.statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"UPDATE items SET stock = stock - 1 WHERE id = 1"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"UNSET"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"trace_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5a660400fdbe203fae688234501042ab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"span_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"79f979eafb07d7ff"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"parent_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ee5e5356e7faea3e"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"INSERT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"start_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974912191198963&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"end_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974912191307658&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"attributes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"db.statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"INSERT INTO orders (item_id) VALUES (1)"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"UNSET"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"trace_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5a660400fdbe203fae688234501042ab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"span_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ee5e5356e7faea3e"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"parent_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"POST /orders"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"start_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974892585093046&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"end_ns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792974912191552889&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"attributes"&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;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"UNSET"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spans from the same order share a &lt;code&gt;trace_id&lt;/code&gt;, so group them by it. Among them, find the &lt;code&gt;SELECT&lt;/code&gt; span whose &lt;code&gt;db.statement&lt;/code&gt; contains &lt;code&gt;FOR UPDATE&lt;/code&gt;, and measure from its &lt;code&gt;end_ns&lt;/code&gt; to the &lt;code&gt;end_ns&lt;/code&gt; of the last SQL span (here, &lt;code&gt;INSERT&lt;/code&gt;). For this order that is 1792974912191307658 − 1792974912090909676 = 100,397,982 ns, about 0.10s. This is the hold time measured in Identifying it in your own environment.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/18/explicit-locking.html#LOCKING-ROWS" rel="noopener noreferrer"&gt;Row-Level Locks&lt;/a&gt; section of the PostgreSQL 18 documentation. A row lock taken with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; is not released until the transaction ends, and a transaction that comes to run &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt; on the same row waits until then.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Run on a GitHub Actions ubuntu-24.04 runner with PostgreSQL on its default settings. The results are in &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/40249991824" rel="noopener noreferrer"&gt;the CI run&lt;/a&gt;, and the code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/cbcc18007ea888bfd26b15dd994c7336c243e734/select-for-update-throughput-ceiling" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://github.com/postgres/postgres/blob/REL_18_6/src/backend/executor/nodeLockRows.c#L182-L183" rel="noopener noreferrer"&gt;&lt;code&gt;ExecLockRows()&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/postgres/postgres/blob/REL_18_6/src/backend/access/heap/heapam_handler.c#L383-L397" rel="noopener noreferrer"&gt;&lt;code&gt;heapam_tuple_lock()&lt;/code&gt;&lt;/a&gt; in the PostgreSQL 18.6 source. When the preceding transaction updates the row, a waiting transaction moves to the new row version the update created and tries to take the lock again, competing with transactions that arrived later.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;The &lt;a href="https://docs.djangoproject.com/en/5.2/topics/db/transactions/#tying-transactions-to-http-requests" rel="noopener noreferrer"&gt;Tying transactions to HTTP requests&lt;/a&gt; section of the Django documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;The test used a custom exporter that writes spans to a file as JSON, one span per line.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>performance</category>
      <category>devops</category>
    </item>
    <item>
      <title>Slow 200s in nginx, fast in the application server's logs</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Fri, 30 Oct 2026 13:02:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/nginx-upstreamconnecttime-comes-in-1-second-steps-357a</link>
      <guid>https://dev.to/shinagawa-web/nginx-upstreamconnecttime-comes-in-1-second-steps-357a</guid>
      <description>&lt;p&gt;An nginx latency alert fires, yet the logs and metrics of the application server behind nginx can show the same requests finishing quickly. This happens when the application server stops entirely for a few seconds, for example during a full GC&lt;sup id="fnref1"&gt;1&lt;/sup&gt;, and connections that arrive in the meantime are kept waiting.&lt;/p&gt;



&lt;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant N as nginx
    participant K as Application server's kernel
    participant U as Application server
    Note over U: Stopped by a full GC or similar
    N-&amp;gt;&amp;gt;K: SYN (open a connection)
    Note over K: The accept queue is full, so the SYN is dropped&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The dropped SYN is resent every second and connects after the application server resumes. From nginx, these look like 200s that took about 1, 2, or 3 seconds to connect. The three lines below are from the nginx access log, which records &lt;code&gt;$upstream_connect_time&lt;/code&gt; (the seconds nginx took to finish connecting to the application server) as uct. The columns are time, request ID, status, uct, &lt;code&gt;$upstream_header_time&lt;/code&gt; (uht), &lt;code&gt;$upstream_response_time&lt;/code&gt; (urt), and &lt;code&gt;$request_time&lt;/code&gt; (rt). uht and urt are the seconds from when nginx starts connecting to the application server until it receives the start of the response, and until it has received the whole response. rt is the seconds from when nginx receives the request until it finishes sending the response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1792402370.463 487aefe88a1328206a8e93c7c242399a 200 uct=1.002 uht=1.002 urt=1.002 rt=1.002
1792402370.464 cfdd11ceca67677e3601df796850a712 200 uct=2.067 uht=2.067 urt=2.067 rt=2.068
1792402371.040 dd774240d10448d9dc41bfcaaa7d8dfe 200 uct=3.054 uht=3.055 urt=3.055 rt=3.054
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application server receives the request ID that nginx assigned, and its log records time, request ID, and processing time in milliseconds. These are its lines for the same request IDs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1792402370.463 487aefe88a1328206a8e93c7c242399a 0.092
1792402370.463 cfdd11ceca67677e3601df796850a712 0.104
1792402371.040 dd774240d10448d9dc41bfcaaa7d8dfe 0.084
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nearly all of the time went to connecting, and processing on the application server took about 0.1 ms. The application server starts timing only after it takes the connection off the queue, so the wait to connect does not appear in its logs or metrics.&lt;/p&gt;

&lt;p&gt;The logs above were taken from an application server that receives 200 requests per second, stopped for 3 seconds&lt;sup id="fnref2"&gt;2&lt;/sup&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening
&lt;/h2&gt;

&lt;p&gt;When the application server stops, the accept queue fills up. SYNs that arrive after that are dropped, and connections are made through retransmissions by the nginx-side kernel.&lt;/p&gt;

&lt;h3&gt;
  
  
  The accept queue
&lt;/h3&gt;

&lt;p&gt;The kernel carries out the exchange that sets up a TCP connection (the handshake). The connecting side sends a SYN to request a connection, the receiving side replies with a SYN-ACK to accept it, and the handshake completes when the connecting side sends an ACK. A completed connection waits in the kernel's accept queue until the server process takes it with the accept system call.&lt;/p&gt;

&lt;p&gt;Even while the application server is stopped, the kernel on its side keeps completing handshakes and queuing the connections. Nothing takes connections off the queue during the stop, so the queue fills up.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant N as nginx
    participant NK as nginx-side kernel
    participant UK as Application server's kernel
    participant U as Application server
    Note over U: Stopped
    N-&amp;gt;&amp;gt;NK: Connect to the application server
    NK-&amp;gt;&amp;gt;UK: SYN
    UK--&amp;gt;&amp;gt;NK: SYN-ACK
    NK-&amp;gt;&amp;gt;UK: ACK
    NK--&amp;gt;&amp;gt;N: Connected
    Note over UK: Puts the connection in the accept queue
    Note over U: On resuming, takes connections off the queue in order&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The maximum number of connections the queue can hold is the backlog. The application server passes the backlog to the kernel when it starts listening for connections (when it calls listen). The value comes from the runtime's or framework's default, or from configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  SYNs are dropped when the queue is full
&lt;/h3&gt;

&lt;p&gt;When the number of connections in the queue exceeds the backlog, the application server's kernel drops newly arriving SYNs without replying&lt;sup id="fnref3"&gt;3&lt;/sup&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retransmissions get through
&lt;/h3&gt;

&lt;p&gt;When a SYN gets no reply, the nginx-side kernel waits and sends it again. This is SYN retransmission. If the nginx-side kernel is Linux 6.5 or later, the first few retransmissions go out at 1-second intervals&lt;sup id="fnref4"&gt;4&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Retransmitted SYNs are dropped too while the queue is full. Once the application server resumes and the queue has room, the next retransmission gets a SYN-ACK and the connection is made. Depending on which retransmission gets through, &lt;code&gt;$upstream_connect_time&lt;/code&gt; is about 1, 2, or 3 seconds. The three log lines at the top are these cases.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant N as nginx
    participant NK as nginx-side kernel
    participant UK as Application server's kernel
    participant U as Application server
    Note over U: Stopped
    Note over UK: The accept queue is full
    N-&amp;gt;&amp;gt;NK: Connect to the application server
    NK-&amp;gt;&amp;gt;UK: SYN
    Note over UK: Drops it without replying
    loop Every second while the queue is full
        NK-&amp;gt;&amp;gt;UK: Resend the SYN
        Note over UK: Drops it again
    end
    Note over U: Resumes and takes connections off the queue
    NK-&amp;gt;&amp;gt;UK: Next retransmission
    UK--&amp;gt;&amp;gt;NK: SYN-ACK
    NK-&amp;gt;&amp;gt;UK: ACK
    NK--&amp;gt;&amp;gt;N: Connected ($upstream_connect_time is about 1, 2, or 3 seconds)&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  Verification
&lt;/h2&gt;

&lt;p&gt;I stopped the application server for 3 seconds to confirm this sequence. Requests that arrived during the stop split into those that connected immediately and those that connected after waiting about 1, 2, or 3 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;nginx and the application server ran as containers on the same Docker network, and nginx was sent 200 requests per second for 15 seconds. Five seconds in, the application server process was stopped from outside with SIGSTOP, then resumed with SIGCONT 3 seconds later. This mimics a full GC stopping the whole process.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;nginx is 1.30.6, with a single application server in the &lt;code&gt;upstream&lt;/code&gt; block. &lt;code&gt;proxy_connect_timeout&lt;/code&gt; (how long nginx waits to finish connecting to the application server) is left at its default of 60 seconds&lt;/li&gt;
&lt;li&gt;The application server is an HTTP server that returns 200 immediately for every request, and listens with a backlog of 100&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Time to connect
&lt;/h3&gt;

&lt;p&gt;In the 3 seconds between the stop and the resume, 600 requests reached nginx, and all of them returned 200. The counts by &lt;code&gt;$upstream_connect_time&lt;/code&gt; are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;$upstream_connect_time&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Requests&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 seconds (connected immediately)&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;About 1 second&lt;/td&gt;
&lt;td&gt;204&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;About 2 seconds&lt;/td&gt;
&lt;td&gt;205&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;About 3 seconds&lt;/td&gt;
&lt;td&gt;89&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This holds beyond the three lines at the top: for all 600 requests, the processing time in the application server's log was under 1 ms.&lt;/p&gt;

&lt;p&gt;Which row a request falls into depends on when it arrived. The queue was full about 0.5 seconds after the stop. Connections that arrived before that went into the queue and connected immediately. Connections that arrived later had their SYNs dropped, the nginx-side kernel resent them every second, and they connected on the first retransmission after the application server resumed. The earlier a connection arrived, the more times its SYN was resent and the longer it waited.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spotting it in your own logs
&lt;/h2&gt;

&lt;p&gt;To tell whether the accept queue overflowed, match the nginx access log against the application server's log. The access log shows that SYNs were dropped and the connections were made by retransmission. The application server's log shows that the application server was stopped.&lt;/p&gt;

&lt;h3&gt;
  
  
  Access log
&lt;/h3&gt;

&lt;p&gt;In the access log, look at &lt;code&gt;$upstream_connect_time&lt;/code&gt; on the 200 lines from when the alert fired. If the values cluster around 1, 2, and 3 seconds, SYNs were dropped and the connections were made by retransmission.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$upstream_connect_time&lt;/code&gt; is not part of the default &lt;code&gt;combined&lt;/code&gt; format, so add it with &lt;code&gt;log_format&lt;/code&gt;&lt;sup id="fnref5"&gt;5&lt;/sup&gt;. The verification used this format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;log_format&lt;/span&gt; &lt;span class="n"&gt;lab&lt;/span&gt; &lt;span class="s1"&gt;'$msec $request_id $status uct=$upstream_connect_time uht=$upstream_header_time urt=$upstream_response_time rt=$request_time'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Application server log
&lt;/h3&gt;

&lt;p&gt;In the application server's log, find the lines with the same IDs as the requests whose &lt;code&gt;$upstream_connect_time&lt;/code&gt; came in 1-second steps, and look at how the lines are spaced around that time. If those requests finished quickly and the lines stop for a while around that time, the application server was stopped. The lines after the gap are bunched at the time it resumed.&lt;/p&gt;

&lt;p&gt;To match the two logs, pass the ID that nginx assigns to each request (&lt;code&gt;$request_id&lt;/code&gt;) to the application server. On the nginx side, this takes one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt; $&lt;span class="n"&gt;request_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the application server side, its access log needs to be configured to include the received &lt;code&gt;X-Request-Id&lt;/code&gt; header.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix
&lt;/h2&gt;

&lt;p&gt;If the application server's backlog is set larger than the number of requests that arrive during a stop (requests per second × seconds stopped), the queue does not overflow&lt;sup id="fnref6"&gt;6&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;The default backlogs of JVM servers, which can stop for a full GC, are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tomcat and Spring Boot: 100&lt;sup id="fnref7"&gt;7&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;Jetty: 50&lt;sup id="fnref8"&gt;8&lt;/sup&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even without an overflow, requests that arrive during a stop wait in the queue until the application server resumes. That wait shows up in &lt;code&gt;$upstream_header_time&lt;/code&gt;, so 200s slowed by the length of the stop remain.&lt;/p&gt;

&lt;p&gt;If slow 200s remain after the backlog is raised, I want to shorten the stop itself by revisiting the GC settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  How this varies by environment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  With a shortened connect timeout
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;proxy_connect_timeout&lt;/code&gt; is shorter than the stop, nginx gives up on connections whose retransmissions have not gotten through in time and returns 504.&lt;/p&gt;

&lt;h3&gt;
  
  
  When the stop is not 3 seconds
&lt;/h3&gt;

&lt;p&gt;A stop of a different length should change the values &lt;code&gt;$upstream_connect_time&lt;/code&gt; clusters around. This article tests a 3-second stop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Kernels older than 6.5
&lt;/h3&gt;

&lt;p&gt;If the nginx-side kernel is older than 6.5, &lt;code&gt;$upstream_connect_time&lt;/code&gt; clusters around 1 and 3 seconds, and about 2 seconds does not appear. This is because the retransmission interval doubles each time: after 1 second, then 2 seconds after that, then 4 seconds after that.&lt;/p&gt;

&lt;p&gt;The stock kernel of Ubuntu 24.04 (6.8) is 6.5 or later. Those of Ubuntu 22.04 (5.15), Debian 12 (6.1), and RHEL 9 (5.14) are older. Whether a distribution has backported this change into its own kernel has not been checked.&lt;/p&gt;

&lt;h3&gt;
  
  
  With a proxy in front of the application server
&lt;/h3&gt;

&lt;p&gt;If a proxy that accepts connections first sits between nginx and the application server (Docker's userland-proxy, which handles published ports, or a sidecar proxy), that proxy answers the SYNs from nginx. Even if the application server's queue overflows, no retransmissions happen on the nginx side, so the method in this article should not apply. This article does not verify this case.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Ordinary GC pauses are not this long. The JVM's G1 collector has a default pause-time goal of 200 ms (&lt;a href="https://github.com/openjdk/jdk/blob/jdk-25-ga/src/hotspot/share/gc/g1/g1Arguments.cpp#L213-L216" rel="noopener noreferrer"&gt;&lt;code&gt;G1Arguments::initialize()&lt;/code&gt;&lt;/a&gt; in JDK 25). Pauses of several seconds come from things like a full GC under heap pressure, which this article does not verify.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;The results are &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/39230808164/job/117988019977" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and the code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/e1f8b7e929b7084062f4b4bd96ff1e4ce8a40713/nginx-accept-queue-overflow" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;. It ran on GitHub Actions ubuntu-24.04 (x86_64, kernel 6.17) with Docker 28.0.4.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;In the Linux 6.17 source, &lt;a href="https://github.com/torvalds/linux/blob/v6.17/net/ipv4/tcp_input.c#L7166-L7169" rel="noopener noreferrer"&gt;&lt;code&gt;tcp_conn_request()&lt;/code&gt;&lt;/a&gt; drops the SYN when the queue is full. &lt;a href="https://github.com/torvalds/linux/blob/v6.17/include/net/sock.h#L1062-L1065" rel="noopener noreferrer"&gt;&lt;code&gt;sk_acceptq_is_full()&lt;/code&gt;&lt;/a&gt; decides whether it is full, and treats the queue as full when the number of connections in it is greater than (&lt;code&gt;&amp;gt;&lt;/code&gt;) the backlog.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;The number of retransmissions sent at 1-second intervals is set by &lt;code&gt;net.ipv4.tcp_syn_linear_timeouts&lt;/code&gt; (default 4; see the Linux 6.17 &lt;a href="https://github.com/torvalds/linux/blob/v6.17/Documentation/networking/ip-sysctl.rst#L1075-L1083" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;Adding variables with &lt;code&gt;log_format&lt;/code&gt; is covered in &lt;a href="https://dev.to/shinagawa-web/requesttime-looks-fast-but-users-say-its-slow-breaking-nginx-latency-into-4-parts-1pn9"&gt;nginx request_time is fast, but users say it's slow&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;To go above 4096, also raise &lt;code&gt;net.core.somaxconn&lt;/code&gt; in the application server's kernel. The kernel caps the backlog at &lt;code&gt;somaxconn&lt;/code&gt; (&lt;a href="https://github.com/torvalds/linux/blob/v6.17/net/socket.c#L1906-L1908" rel="noopener noreferrer"&gt;&lt;code&gt;__sys_listen_socket()&lt;/code&gt;&lt;/a&gt; in Linux 6.17), and its default is 4096 (&lt;a href="https://github.com/torvalds/linux/blob/v6.17/net/core/net_namespace.c#L388-L390" rel="noopener noreferrer"&gt;&lt;code&gt;preinit_net_sysctl()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://github.com/torvalds/linux/blob/v6.17/Documentation/networking/ip-sysctl.rst#L333-L335" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;The Tomcat 11 documentation on &lt;a href="https://tomcat.apache.org/tomcat-11.0-doc/config/http.html#Attributes_Standard%20Implementation_acceptCount" rel="noopener noreferrer"&gt;HTTP connector configuration&lt;/a&gt;, and &lt;a href="https://github.com/spring-projects/spring-boot/blob/v4.1.1/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java#L126" rel="noopener noreferrer"&gt;&lt;code&gt;TomcatServerProperties&lt;/code&gt;&lt;/a&gt; in the Spring Boot 4.1.1 source.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;Jetty passes 0, and the JDK replaces it with 50. The default is 0 in &lt;a href="https://github.com/jetty/jetty.project/blob/jetty-12.1.14/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ServerConnector.java#L80" rel="noopener noreferrer"&gt;&lt;code&gt;ServerConnector&lt;/code&gt;&lt;/a&gt; in the Jetty 12.1.14 source, and &lt;a href="https://github.com/openjdk/jdk/blob/jdk-25-ga/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java#L336" rel="noopener noreferrer"&gt;&lt;code&gt;ServerSocketChannelImpl&lt;/code&gt;&lt;/a&gt; in the JDK 25 source turns any value below 1 into 50.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>nginx</category>
      <category>linux</category>
      <category>devops</category>
      <category>performance</category>
    </item>
    <item>
      <title>HPA does not keep the replicas you set with kubectl scale</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Tue, 27 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/hpa-does-not-keep-the-replicas-you-set-with-kubectl-scale-2jpg</link>
      <guid>https://dev.to/shinagawa-web/hpa-does-not-keep-the-replicas-you-set-with-kubectl-scale-2jpg</guid>
      <description>&lt;p&gt;Sometimes you want more Pods even though CPU usage is low: when the workers in each Pod are all tied up waiting on a downstream response, or when you want capacity in place before an event. In those cases, people sometimes raise replicas with &lt;code&gt;kubectl scale&lt;/code&gt;, even on a Deployment that has an HPA (HorizontalPodAutoscaler). HPA is a controller that decides the number of Pods from metrics such as CPU usage and rewrites the Deployment's replicas.&lt;/p&gt;

&lt;p&gt;Right after the scale-up, READY in &lt;code&gt;kubectl get deploy&lt;/code&gt; matches the new count. HPA does not revert it right away either, so the count looks like it will hold.&lt;/p&gt;

&lt;p&gt;A few minutes later, it is back to the original count. No errors or warnings appear. If HPA reverted it, it is not clear why it took a few minutes to do so.&lt;/p&gt;

&lt;p&gt;I reproduced this state and checked whether HPA was really what reverted it, and why the revert happens a few minutes later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduction
&lt;/h2&gt;

&lt;p&gt;I reproduced the state above on a Kubernetes v1.37.0 cluster created with kind.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;I attached an HPA to a Deployment that runs 2 Pods using almost no CPU. The HPA targets 50% CPU utilization, with &lt;code&gt;minReplicas&lt;/code&gt; 2 and &lt;code&gt;maxReplicas&lt;/code&gt; 5. HPA decides the count again every 15 seconds. Below, each of these runs is called a sync.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl get hpa c1-1
NAME   REFERENCE         TARGETS       MINPODS   MAXPODS   REPLICAS   AGE
c1-1   Deployment/c1-1   cpu: 0%/50%   2         5         2          81s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I scaled this Deployment from 2 to 4 with &lt;code&gt;kubectl scale&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;$ kubectl scale deploy/c1-1 --replicas=4
deployment.apps/c1-1 scaled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right after the scale-up, READY reached &lt;code&gt;4/4&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;$ kubectl get deploy c1-1
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
c1-1   4/4     4            4           107s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;About 5 minutes later, it went back to 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl get deploy c1-1
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
c1-1   2/2     2            2           7m17s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Narrowing it down
&lt;/h2&gt;

&lt;p&gt;Deployment replicas can be written back by &lt;code&gt;kubectl apply&lt;/code&gt; run by another person or by CI, by GitOps tools, and by controllers such as HPA.&lt;/p&gt;

&lt;p&gt;Kubernetes objects carry managedFields, a record of which manager (writer) wrote each field of the object.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; Viewing the Deployment after it went back to 2 with &lt;code&gt;kubectl get deploy c1-1 -o yaml --show-managed-fields&lt;/code&gt; showed that the manager of &lt;code&gt;spec.replicas&lt;/code&gt; was &lt;code&gt;kube-controller-manager&lt;/code&gt;.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;apps/v1&lt;/span&gt;
    &lt;span class="na"&gt;fieldsType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;FieldsV1&lt;/span&gt;
    &lt;span class="na"&gt;fieldsV1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;f:spec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;f:replicas&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="na"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;kube-controller-manager&lt;/span&gt;
    &lt;span class="na"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Update&lt;/span&gt;
    &lt;span class="na"&gt;subresource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;scale&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;kube-controller-manager is the in-cluster process that runs HPA and the other controllers. If &lt;code&gt;kubectl apply&lt;/code&gt; or a GitOps tool had written the field, the manager would be some name other than kube-controller-manager.&lt;/p&gt;

&lt;p&gt;Events show which controller wrote it. An Event is a record of something that happened, which Kubernetes keeps per object. &lt;code&gt;kubectl events --for hpa/c1-1&lt;/code&gt; showed a &lt;code&gt;SuccessfulRescale&lt;/code&gt; at the time of the revert.&lt;sup id="fnref3"&gt;3&lt;/sup&gt; &lt;code&gt;New size: 2&lt;/code&gt; means HPA rewrote replicas to 2. HPA was what reverted it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl events --for hpa/c1-1
LAST SEEN               TYPE      REASON                         OBJECT                         MESSAGE
16s                     Normal    SuccessfulRescale              HorizontalPodAutoscaler/c1-1   New size: 2; reason: All metrics below target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What remains of the opening question is why HPA takes about 5 minutes to revert.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What a sync does
&lt;/h3&gt;

&lt;p&gt;On each sync, HPA does the following.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reads the Pods' CPU utilization&lt;/li&gt;
&lt;li&gt;Divides the average utilization by the target to get a ratio, and computes a Pod count from the ratio. Below, this count is called the computed count. If the ratio is between 0.9 and 1.1, the computed count is the current count (the Deployment's &lt;code&gt;spec.replicas&lt;/code&gt;). Outside that range, it is the current number of Pods multiplied by the ratio, rounded up.&lt;sup id="fnref4"&gt;4&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;When scaling down, does not go below the maximum computed count over the past 5 minutes. It then clamps the result to the range from &lt;code&gt;minReplicas&lt;/code&gt; to &lt;code&gt;maxReplicas&lt;/code&gt;.&lt;sup id="fnref5"&gt;5&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;If the count decided in step 3 differs from the Deployment's &lt;code&gt;spec.replicas&lt;/code&gt; on the cluster, rewrites &lt;code&gt;spec.replicas&lt;/code&gt; to that count&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The "5 minutes" in step 3 is the value used when the HPA has no &lt;code&gt;behavior&lt;/code&gt;, and it comes from the default of kube-controller-manager's &lt;code&gt;--horizontal-pod-autoscaler-downscale-stabilization&lt;/code&gt;.&lt;sup id="fnref6"&gt;6&lt;/sup&gt; It can be changed per HPA with &lt;code&gt;behavior.scaleDown.stabilizationWindowSeconds&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  From the scale-up to the revert
&lt;/h3&gt;

&lt;p&gt;The table below shows the syncs from the scale-up to the revert. Seconds are the time elapsed since running &lt;code&gt;kubectl scale&lt;/code&gt;. The computed counts were taken from kube-controller-manager logs in the reproduction environment, with &lt;code&gt;--v=4&lt;/code&gt; (a setting that makes the logs more detailed).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Since the operation&lt;/th&gt;
&lt;th&gt;Metrics for the 2 new Pods&lt;/th&gt;
&lt;th&gt;Computed count&lt;/th&gt;
&lt;th&gt;replicas&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;8 s&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;23 s&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;38 s&lt;/td&gt;
&lt;td&gt;present (0%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(same every 15 s)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;323 s&lt;/td&gt;
&lt;td&gt;present (0%)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At the 8 s and 23 s syncs, the 2 new Pods are running but have no metrics yet. metrics-server is the component that collects Pod CPU and memory usage from each node and passes it to HPA. Values for a Pod that has just started do not appear right away.&lt;/p&gt;

&lt;p&gt;When the ratio computed from only the Pods that have metrics is below 1, HPA recomputes it, treating the Pods without metrics as 100% utilized.&lt;sup id="fnref7"&gt;7&lt;/sup&gt; Here the 2 existing Pods are at 0% and the 2 new ones at 100%, so the average is 50%. The ratio to the 50% target is 1.0, so in step 2 the computed count is the current count, 4.&lt;/p&gt;

&lt;p&gt;From the 38 s sync on, metrics for the 2 new Pods are available and the computed count becomes 0. In step 3, though, HPA does not go below 4, the maximum computed count over the past 5 minutes, so replicas stays at 4.&lt;/p&gt;

&lt;p&gt;At the 323 s sync, the computed count of 4 from 23 s is now more than 5 minutes old, and the maximum computed count over the past 5 minutes becomes 0. HPA clamps this to &lt;code&gt;minReplicas&lt;/code&gt;, 2, and rewrites &lt;code&gt;spec.replicas&lt;/code&gt; to 2.&lt;/p&gt;

&lt;p&gt;To HPA this is an ordinary scale-down, so the only Event is a Normal &lt;code&gt;SuccessfulRescale&lt;/code&gt;, with no errors or warnings.&lt;/p&gt;

&lt;h2&gt;
  
  
  How long the kubectl scale count lasts
&lt;/h2&gt;

&lt;p&gt;HPA takes the replicas a person wrote with &lt;code&gt;kubectl scale&lt;/code&gt; into its calculation as the current count. When scaling down, HPA preserves only the computed counts from the past 5 minutes.&lt;/p&gt;

&lt;p&gt;The count a person wrote lasts only until 5 minutes after the computed count last matched it. In the reproduction, the 4 Pods lasted about 5 minutes because the computed count was 4 at the syncs before the 2 new Pods had metrics, and that 4 stayed as the maximum computed count over the past 5 minutes.&lt;/p&gt;

&lt;p&gt;If the computed count never matches that number, the first sync reverts it. In the same setup, scaling from 2 to 3 went back to 2 at the sync about 10 seconds later. Even with the 1 Pod without metrics treated as 100% utilized, the average across 3 Pods is about 33%. The ratio to the 50% target is below 0.9, and the computed count is 2.&lt;/p&gt;

&lt;p&gt;Whether the revert comes a few minutes later or right away, HPA is what does it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remedies
&lt;/h2&gt;

&lt;p&gt;The two places to change are the HPA's lower bound (&lt;code&gt;minReplicas&lt;/code&gt;) and its scale-down rules (&lt;code&gt;behavior.scaleDown&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Raise &lt;code&gt;minReplicas&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Raise the HPA's &lt;code&gt;minReplicas&lt;/code&gt; to the count you want. replicas goes up immediately. Only the lower bound goes up, so HPA can still add more Pods if load increases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl patch hpa &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;hpa&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --type=merge -p &lt;span class="s1"&gt;'{"spec":{"minReplicas":4}}'&lt;/span&gt;
horizontalpodautoscaler.autoscaling/&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;hpa&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; patched
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the count you want is above &lt;code&gt;maxReplicas&lt;/code&gt;, raise &lt;code&gt;maxReplicas&lt;/code&gt; in the same patch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disable scale-down, then scale up
&lt;/h3&gt;

&lt;p&gt;Set the HPA's &lt;code&gt;behavior.scaleDown.selectPolicy&lt;/code&gt; to &lt;code&gt;Disabled&lt;/code&gt;, then scale up with &lt;code&gt;kubectl scale&lt;/code&gt;. HPA no longer reduces replicas.&lt;sup id="fnref8"&gt;8&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl patch hpa &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;hpa&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --type=merge -p &lt;span class="s1"&gt;'{"spec":{"behavior":{"scaleDown":{"selectPolicy":"Disabled"}}}}'&lt;/span&gt;
horizontalpodautoscaler.autoscaling/&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;hpa&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; patched
$ kubectl scale deploy/&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;deployment&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --replicas=4
deployment.apps/&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;deployment&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; scaled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Which to choose
&lt;/h3&gt;

&lt;p&gt;I choose to raise &lt;code&gt;minReplicas&lt;/code&gt;. The only thing it stops is scaling down below the lower bound. HPA keeps adding Pods as load rises and keeps scaling down as far as the lower bound. Disabling scale-down also stops HPA from removing the Pods it added in response to load.&lt;/p&gt;

&lt;p&gt;When you no longer need the extra Pods, set &lt;code&gt;minReplicas&lt;/code&gt; back to its original value.&lt;/p&gt;

&lt;p&gt;If you raise &lt;code&gt;minReplicas&lt;/code&gt; every time the workers are tied up waiting on a downstream response, the CPU utilization that HPA reads is not the right metric for deciding the number of Pods. I want HPA to decide the number of Pods from a metric that reflects how many workers are tied up, such as the number of requests in progress.&lt;sup id="fnref9"&gt;9&lt;/sup&gt;&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;The results are &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/38530770017" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and the code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/c5c2b71/hpa-reverts-manual-scale" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management" rel="noopener noreferrer"&gt;Field management&lt;/a&gt; in the Kubernetes documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;Events are deleted after 1 hour by default, because the default of kube-apiserver's &lt;a href="https://kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/" rel="noopener noreferrer"&gt;&lt;code&gt;--event-ttl&lt;/code&gt;&lt;/a&gt; is &lt;code&gt;1h0m0s&lt;/code&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;&lt;a href="https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/#algorithm-details" rel="noopener noreferrer"&gt;Algorithm details&lt;/a&gt; in the Kubernetes documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/kubernetes/blob/v1.37.0/pkg/controller/podautoscaler/horizontal.go#L1109-L1127" rel="noopener noreferrer"&gt;&lt;code&gt;normalizeDesiredReplicas&lt;/code&gt;&lt;/a&gt; in kube-controller-manager.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;kube-controller-manager's &lt;a href="https://github.com/kubernetes/kubernetes/blob/v1.37.0/pkg/controller/podautoscaler/config/v1alpha1/defaults.go#L43-L45" rel="noopener noreferrer"&gt;defaults&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;&lt;a href="https://github.com/kubernetes/kubernetes/blob/v1.37.0/pkg/controller/podautoscaler/replica_calculator.go#L121-L128" rel="noopener noreferrer"&gt;replica_calculator.go&lt;/a&gt; in kube-controller-manager.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;&lt;a href="https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/#example-disable-scale-down" rel="noopener noreferrer"&gt;Example: disable scale down&lt;/a&gt; in the Kubernetes documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;&lt;a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-multiple-metrics-and-custom-metrics" rel="noopener noreferrer"&gt;Autoscaling on multiple metrics and custom metrics&lt;/a&gt; in the Kubernetes documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>sre</category>
      <category>debugging</category>
    </item>
    <item>
      <title>301 redirect change doesn't take effect for some users</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Fri, 23 Oct 2026 13:03:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/301-redirect-change-doesnt-take-effect-for-some-users-3gh4</link>
      <guid>https://dev.to/shinagawa-web/301-redirect-change-doesnt-take-effect-for-some-users-3gh4</guid>
      <description>&lt;p&gt;On the documentation site, &lt;code&gt;/docs&lt;/code&gt; redirects to the pages for the latest version. Links on other sites, such as the product site, point to &lt;code&gt;/docs&lt;/code&gt;. For the v4 release, the nginx config was edited to change the target from &lt;code&gt;/docs/v3/&lt;/code&gt; to &lt;code&gt;/docs/v4/&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="na"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&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;301&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v4&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;301 is the status code meaning the requested URL has moved permanently to another URL. The &lt;code&gt;/docs&lt;/code&gt; URL itself will not change, so it was set up as a 301.&lt;/p&gt;

&lt;p&gt;After the change, reports keep coming in: "When I open it from the link on the product site, I get v3." Running curl against &lt;code&gt;/docs&lt;/code&gt; locally returns a 301 to v4.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;$ &lt;span class="n"&gt;curl&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;sI&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//&amp;lt;host&amp;gt;/docs&lt;/span&gt;
&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;301&lt;/span&gt; &lt;span class="nc"&gt;Moved&lt;/span&gt; &lt;span class="nc"&gt;Permanently&lt;/span&gt;
&lt;span class="nc"&gt;Location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//&amp;lt;host&amp;gt;/docs/v4/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I reproduced this with nginx and browsers and measured why some users land on v3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Impact
&lt;/h2&gt;

&lt;p&gt;Some of the users who opened &lt;code&gt;/docs&lt;/code&gt; from the product site link while it pointed to v3 keep reading the v3 pages after the v4 release. No error appears, so only the users who notice send a report. The ones who don't notice go on configuring and working by the v3 instructions.&lt;/p&gt;

&lt;p&gt;This does not go away, however many days pass. It happens again every time a release changes the target.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduction
&lt;/h2&gt;

&lt;p&gt;I reproduced the scenario above with nginx 1.31.6 and three browsers driven by Playwright 1.63.0: Chromium, WebKit (the engine behind Safari), and Firefox.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;A page containing only a link to &lt;code&gt;/docs&lt;/code&gt; is served on a host name separate from the documentation site, and &lt;code&gt;/docs&lt;/code&gt; is opened from that page. &lt;code&gt;/docs&lt;/code&gt; responds with the same &lt;code&gt;return 301&lt;/code&gt; as above.&lt;/p&gt;

&lt;p&gt;While the target is v3, open &lt;code&gt;/docs&lt;/code&gt; from the link page. Then change the nginx config to v4 and reload. After that, open &lt;code&gt;/docs&lt;/code&gt; from the link page three times: right after the switch, 70 seconds later, and after restarting the browser. Each time, record which version's page the browser landed on.&lt;/p&gt;

&lt;p&gt;Chromium and WebKit landed on v3 all three times. Firefox landed on v4 from right after the switch.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;/docs&lt;/code&gt; was opened while the target was v3, all three browsers left a &lt;code&gt;/docs&lt;/code&gt; line in the nginx access log.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;client IP&amp;gt; - - [15/Oct/2026:23:13:51 +0000] "GET /docs HTTP/1.1" 301 169 ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the switch, there was no &lt;code&gt;/docs&lt;/code&gt; line for any of the three times Chromium and WebKit landed on v3. Firefox, which landed on v4, left a &lt;code&gt;/docs&lt;/code&gt; line all three times. Chromium and WebKit went to v3 without asking the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why v3 keeps showing
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;/docs&lt;/code&gt; 301 does not say how long it may be stored and reused. How to treat a 301 without that lifetime is up to the browser. Chromium treats it as never expiring and WebKit as valid for one year, and both reuse the stored 301 without asking the server.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;sup id="fnref4"&gt;4&lt;/sup&gt; So users who stored the 301 to v3 still land on v3 after the switch. Firefox does not reuse the stored 301 as is. It asks the server every time the page is opened.&lt;/p&gt;

&lt;p&gt;These are the headers of the &lt;code&gt;/docs&lt;/code&gt; 301:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;$ &lt;span class="n"&gt;curl&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;sI&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//&amp;lt;host&amp;gt;/docs&lt;/span&gt;
&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;301&lt;/span&gt; &lt;span class="nc"&gt;Moved&lt;/span&gt; &lt;span class="nc"&gt;Permanently&lt;/span&gt;
&lt;span class="nc"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nginx&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.31&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Thu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="nc"&gt;Oct&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;03&lt;/span&gt; &lt;span class="no"&gt;GMT&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;169&lt;/span&gt;
&lt;span class="nc"&gt;Location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//&amp;lt;host&amp;gt;/docs/v4/&lt;/span&gt;
&lt;span class="nc"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;keep&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;alive&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Cache-Control: max-age=&amp;lt;seconds&amp;gt;&lt;/code&gt; is how a response tells the browser it may store the response and use it without asking for that many seconds. This response has no such header. Nor does it have &lt;code&gt;Last-Modified&lt;/code&gt;, the header that gives the time the file was last modified.&lt;/p&gt;

&lt;p&gt;Firefox computes the lifetime of a 301 without a stated lifetime from &lt;code&gt;Last-Modified&lt;/code&gt;.&lt;sup id="fnref5"&gt;5&lt;/sup&gt; This response has no &lt;code&gt;Last-Modified&lt;/code&gt;, so the lifetime is 0, which means the response is already expired when it is stored. That is why Firefox asks the server every time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix
&lt;/h2&gt;

&lt;p&gt;Switch to a 302, or give the 301 a lifetime with &lt;code&gt;max-age&lt;/code&gt;. Either way, users who have already stored the 301 to v3 stay on v3. Those users are covered in the notes at the end.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use a 302
&lt;/h3&gt;

&lt;p&gt;302 is the status code meaning the requested URL has moved temporarily to another URL. In nginx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="na"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&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;302&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v4&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a 302 with neither a stated lifetime nor &lt;code&gt;Last-Modified&lt;/code&gt;, Chromium, WebKit, and Firefox all set the lifetime to 0 and ask the server for &lt;code&gt;/docs&lt;/code&gt; every time it is opened.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;sup id="fnref4"&gt;4&lt;/sup&gt;&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add max-age to the 301
&lt;/h3&gt;

&lt;p&gt;You can also keep the 301 and state a lifetime with &lt;code&gt;max-age&lt;/code&gt; in &lt;code&gt;Cache-Control&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="na"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt; &lt;span class="s2"&gt;"max-age=60"&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;301&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v4&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Headers added with &lt;code&gt;add_header&lt;/code&gt; are also attached to the 301 sent by &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The tradeoff is that users who opened &lt;code&gt;/docs&lt;/code&gt; just before a release land on the previous version for up to &lt;code&gt;max-age&lt;/code&gt; seconds after the release.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which to choose
&lt;/h3&gt;

&lt;p&gt;I choose the 302. At the start, &lt;code&gt;/docs&lt;/code&gt; got a 301 because its URL does not change. A 301, however, says that the move from &lt;code&gt;/docs&lt;/code&gt; to its target is permanent. The target changes with every release, so the move is temporary.&lt;/p&gt;

&lt;p&gt;I don't choose &lt;code&gt;max-age&lt;/code&gt;. It saves one round trip to &lt;code&gt;/docs&lt;/code&gt; during the lifetime, but in exchange, users who opened &lt;code&gt;/docs&lt;/code&gt; just before a release land on the previous version.&lt;/p&gt;

&lt;p&gt;Once a 301 without a stated lifetime has been returned, a later change to a 302 does not reach the users who have stored it. I want to search the nginx config for &lt;code&gt;return 301&lt;/code&gt; and change it to a 302 now for any URL whose target changes with every release, the way &lt;code&gt;/docs&lt;/code&gt; does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Affected browsers
&lt;/h3&gt;

&lt;p&gt;Chromium-based browsers and Safari keep landing on v3. Firefox 139 and later are not affected. Firefox 138 and earlier, and the ESR releases based on them, are. Firefox 138 and earlier treated a 301 without a stated lifetime as never expiring.&lt;sup id="fnref6"&gt;6&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Chromium stores the 301 separately depending on whether the page was opened from a link on another site or typed into the address bar.&lt;sup id="fnref7"&gt;7&lt;/sup&gt; After the 301 to v3 is stored by opening &lt;code&gt;/docs&lt;/code&gt; from the link page and the target is switched to v4, opening &lt;code&gt;/docs&lt;/code&gt; directly, without going through a link on another site, takes Chromium to v4 and WebKit to v3.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;To check this in your own Chrome, open &lt;code&gt;/docs&lt;/code&gt; from a link on another site, the way users do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Users who still land on v3 after changing to 302
&lt;/h3&gt;

&lt;p&gt;If the 301 to v3 is stored first and the config is changed to &lt;code&gt;return 302&lt;/code&gt; at the switch, Chromium and WebKit still land on v3.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; They keep using the stored 301 to v3, so the change to 302 never reaches these users.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Clear-Site-Data&lt;/code&gt; is a header that tells the browser receiving it to delete that host's cache and other stored data. The v4 page, on the same origin as &lt;code&gt;/docs&lt;/code&gt;, returns this header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v4&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="nc"&gt;Clear&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Site&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Data&lt;/span&gt; &lt;span class="s1"&gt;'"cache"'&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;For users who open this page, the stored 301 is deleted, and Chromium and WebKit also land on v4 from &lt;code&gt;/docs&lt;/code&gt;.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; The header only reaches users who open a v4 page. Users being sent to v3 never get to a v4 page from &lt;code&gt;/docs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that the header tells the browser to delete the entire cache for that host, not just the v4 page. The header also works only over HTTPS (or on localhost).&lt;sup id="fnref8"&gt;8&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setups that give the redirect a lifetime
&lt;/h3&gt;

&lt;p&gt;If &lt;code&gt;expires&lt;/code&gt; is set at the server level, the 301 sent by &lt;code&gt;return&lt;/code&gt; in a location also gets a &lt;code&gt;Cache-Control&lt;/code&gt; header.&lt;sup id="fnref9"&gt;9&lt;/sup&gt; For example, with &lt;code&gt;expires 30d&lt;/code&gt; at the server level and only &lt;code&gt;return 301&lt;/code&gt; in the &lt;code&gt;/docs&lt;/code&gt; location, the 301 for &lt;code&gt;/docs&lt;/code&gt; gets &lt;code&gt;Cache-Control: max-age=2592000&lt;/code&gt; (30 days). Users land on the previous version for up to 30 days after the release.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setups not tested
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Sites with a registered Service Worker that answers requests to &lt;code&gt;/docs&lt;/code&gt; with a locally held response&lt;/li&gt;
&lt;li&gt;Setups with a CDN in front of nginx that stores the &lt;code&gt;/docs&lt;/code&gt; 301 and serves it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both, the request does not reach nginx, so the &lt;code&gt;/docs&lt;/code&gt; lines in the access log cannot tell you whether the browser used a stored 301.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Run on GitHub Actions (ubuntu-24.04). Browser versions: Chromium 153.0.8010.12, WebKit 26.6, Firefox 155.0. The logs and curl output in the text have the per-condition paths and host names rewritten to &lt;code&gt;/docs&lt;/code&gt; and &lt;code&gt;&amp;lt;host&amp;gt;&lt;/code&gt;. The config, scripts, and raw logs are in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/4ad843b9d33fc20b1b97344b94d560cb5a809f46/cached-301-stale-redirect" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Verification results: &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/38822188959/job/116721140834" rel="noopener noreferrer"&gt;CI job&lt;/a&gt;&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;Chromium's &lt;a href="https://github.com/chromium/chromium/blob/971a7443b0c9b0a9b2860529b33331b76077ec62/net/http/http_response_headers.cc#L1356-L1370" rel="noopener noreferrer"&gt;http_response_headers.cc&lt;/a&gt; (&lt;code&gt;GetFreshnessLifetimes&lt;/code&gt;). A 301 without a stated lifetime never expires. A 302 gets 0.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;WebKit's &lt;a href="https://github.com/WebKit/WebKit/blob/4d05d732e5a84f32675bef4cc135a2e7a9269a87/Source/WebCore/platform/network/CacheValidation.cpp#L120-L149" rel="noopener noreferrer"&gt;CacheValidation.cpp&lt;/a&gt; (&lt;code&gt;computeFreshnessLifetimeForHTTPFamily&lt;/code&gt;). A 301 without a stated lifetime gets one year. Anything else gets a lifetime computed from &lt;code&gt;Last-Modified&lt;/code&gt;, or 0 if there is none.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;Firefox's &lt;a href="https://github.com/mozilla-firefox/firefox/blob/d065a04bc5610f496762935dee56604a78b91b51/netwerk/protocol/http/nsHttpResponseHead.cpp#L627-L701" rel="noopener noreferrer"&gt;nsHttpResponseHead.cpp&lt;/a&gt; (&lt;code&gt;ComputeFreshnessLifetime&lt;/code&gt;). A 301 without a stated lifetime gets a lifetime computed from &lt;code&gt;Last-Modified&lt;/code&gt;, or 0 if there is none. A 302 without a stated lifetime gets 0.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;Mozilla &lt;a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1957619" rel="noopener noreferrer"&gt;Bug 1957619&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;Chromium's &lt;a href="https://github.com/chromium/chromium/blob/971a7443b0c9b0a9b2860529b33331b76077ec62/net/http/http_cache.cc#L865-L873" rel="noopener noreferrer"&gt;http_cache.cc&lt;/a&gt;. Top-level navigations coming from another site are stored under a separate cache key.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;W3C's &lt;a href="https://w3c.github.io/webappsec-clear-site-data/#clear-response" rel="noopener noreferrer"&gt;Clear Site Data&lt;/a&gt;. &lt;code&gt;"cache"&lt;/code&gt; deletes the cache for the same host as the response URL (4.2.3). If the response URL is not treated as secure (HTTPS and localhost URLs are), nothing is deleted (4.2).&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;nginx's &lt;a href="https://nginx.org/en/docs/http/ngx_http_headers_module.html" rel="noopener noreferrer"&gt;ngx_http_headers_module&lt;/a&gt;. &lt;code&gt;expires&lt;/code&gt; adds &lt;code&gt;Expires&lt;/code&gt; and &lt;code&gt;Cache-Control&lt;/code&gt; to responses with status code 301 as well.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>nginx</category>
      <category>webdev</category>
      <category>devops</category>
      <category>caching</category>
    </item>
    <item>
      <title>K8s upgrade removes cgroup paths and changes how you read the values</title>
      <dc:creator>Kazu</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:01:00 +0000</pubDate>
      <link>https://dev.to/shinagawa-web/k8s-upgrade-drops-a-path-fixed-scripts-read-other-values-1nje</link>
      <guid>https://dev.to/shinagawa-web/k8s-upgrade-drops-a-path-fixed-scripts-read-other-values-1nje</guid>
      <description>&lt;p&gt;After a Kubernetes version upgrade replaces the node image, procedures and scripts that read container CPU and memory values from under &lt;code&gt;/sys/fs/cgroup&lt;/code&gt; stop working.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -- cat /sys/fs/cgroup/cpu/cpu.stat
cat: can&lt;span class="s1"&gt;'t open '&lt;/span&gt;/sys/fs/cgroup/cpu/cpu.stat&lt;span class="s1"&gt;': No such file or directory
command terminated with exit code 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;ls&lt;/code&gt; inside the container shows no &lt;code&gt;cpu/&lt;/code&gt; and no &lt;code&gt;memory/&lt;/code&gt;. &lt;code&gt;cpu.stat&lt;/code&gt; and &lt;code&gt;memory.stat&lt;/code&gt;, which used to live in separate directories, are listed directly. The node's &lt;code&gt;/sys/fs/cgroup&lt;/code&gt; looks the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -- ls /sys/fs/cgroup
cgroup.controllers
cgroup.events
...
cpu.stat
...
memory.current
memory.events
...
memory.stat
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The layout with one directory per controller is cgroup v1, and this single flat layout is cgroup v2. The switch from v1 to v2 happened when the node image changed.&lt;/p&gt;

&lt;p&gt;Removing &lt;code&gt;cpu/&lt;/code&gt; from the script's path opens a file with the same name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl exec &amp;lt;pod&amp;gt; -- cat /sys/fs/cgroup/cpu.stat
usage_usec 768869
user_usec 684571
system_usec 84297
nice_usec 0
core_sched.force_idle_usec 0
nr_periods 38
nr_throttled 37
throttled_usec 1592698
nr_bursts 0
burst_usec 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;throttled_time&lt;/code&gt; line from the old &lt;code&gt;cpu.stat&lt;/code&gt; is missing, and there is a &lt;code&gt;throttled_usec&lt;/code&gt; line in its place. A script that collected &lt;code&gt;throttled_time&lt;/code&gt; finds no matching line and returns an empty result without raising an error.&lt;/p&gt;

&lt;p&gt;Even once the files open, some values read the v1 way come out different. This article covers three of them: CPU throttle time, memory usage, and OOM count.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;It is written for people who read the files under &lt;code&gt;/sys/fs/cgroup&lt;/code&gt; with their own scripts or runbooks. If you see these values through a monitoring agent such as cAdvisor, the agent handles v2 itself, and that case is outside the scope here. An explanation of v2 itself and the migration steps are on the &lt;a href="https://kubernetes.io/blog/2026/10/06/kubernetes-cgroups-v2-shift/" rel="noopener noreferrer"&gt;Kubernetes blog&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three reads that change
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Throttle time
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;throttled_time&lt;/code&gt;, missing from the &lt;code&gt;cpu.stat&lt;/code&gt; above, became &lt;code&gt;throttled_usec&lt;/code&gt;. It is the total time the cgroup was stopped for hitting its CPU limit, and its unit changed from nanoseconds to microseconds.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; If you change only the key name and keep the &lt;code&gt;/1e9&lt;/code&gt; that converts nanoseconds to seconds, you get a value 1/1000 of the real one. To get seconds, divide by &lt;code&gt;/1e6&lt;/code&gt;.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -- awk &lt;span class="s1"&gt;'$1=="throttled_usec"{printf "%.6f\n", $2/1e6}'&lt;/span&gt; /sys/fs/cgroup/cpu.stat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;nr_throttled&lt;/code&gt; and &lt;code&gt;nr_periods&lt;/code&gt;, which give the fraction of periods that hit the limit, keep the same names and meanings as in v1, so the division works unchanged.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory usage
&lt;/h3&gt;

&lt;p&gt;In v1, memory usage was calculated with this formula.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;memory.usage_in_bytes − total_inactive_file (a line in memory.stat)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a process reads or writes a file, the kernel keeps the contents in memory as page cache, and the cgroup's usage includes it. The part of the page cache that hasn't been used recently is &lt;code&gt;inactive_file&lt;/code&gt;, which the kernel reclaims as soon as memory runs short. Usage minus &lt;code&gt;inactive_file&lt;/code&gt; is the part that can't be released right away: the working set. The kubelet exposes this value as &lt;code&gt;container_memory_working_set_bytes&lt;/code&gt;, and the memory column of &lt;code&gt;kubectl top pod&lt;/code&gt; shows the same value.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;In v2, the formula becomes:&lt;sup id="fnref4"&gt;4&lt;/sup&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;memory.current − inactive_file (a line in memory.stat)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;v2's &lt;code&gt;memory.stat&lt;/code&gt; has no lines starting with &lt;code&gt;total_&lt;/code&gt;. The aggregate lines from v1 appear in v2 under their names with &lt;code&gt;total_&lt;/code&gt; removed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -- cat /sys/fs/cgroup/memory.current
108158976
$ kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -- grep ^inactive_file /sys/fs/cgroup/memory.stat
inactive_file 104906752
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference, about 3 MiB, is the working set.&lt;/p&gt;

&lt;p&gt;The corrected value matches &lt;code&gt;container_memory_working_set_bytes&lt;/code&gt; from the kubelet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;$ kubectl get --raw &lt;span class="s2"&gt;"/api/v1/nodes/&amp;lt;node&amp;gt;/proxy/metrics/resource"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; grep ^container_memory_working_set_bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  OOM count
&lt;/h3&gt;

&lt;p&gt;In v1, a container's OOM count came from &lt;code&gt;oom_kill&lt;/code&gt; in &lt;code&gt;memory.oom_control&lt;/code&gt;.&lt;sup id="fnref5"&gt;5&lt;/sup&gt; In v2, the same &lt;code&gt;oom_kill&lt;/code&gt; is in &lt;code&gt;memory.events&lt;/code&gt;, a file that counts the memory events in that cgroup by type. However, &lt;code&gt;oom_kill&lt;/code&gt; in the container's &lt;code&gt;memory.events&lt;/code&gt; reads 0 even after the container has restarted due to OOM. The count is kept in the &lt;code&gt;memory.events&lt;/code&gt; of the Pod's slice, one level above the container.&lt;/p&gt;

&lt;p&gt;The rest of this section covers why it reads 0, how to find the Pod's slice, and how to read the slice's &lt;code&gt;memory.events&lt;/code&gt;, in that order.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why the container's oom_kill reads 0
&lt;/h4&gt;

&lt;p&gt;In a Pod that restarted due to OOM, the container's &lt;code&gt;memory.events&lt;/code&gt; reads 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;$ kubectl &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -- grep oom /sys/fs/cgroup/memory.events
oom 0
oom_kill 0
oom_group_kill 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On v2 nodes, the kubelet sets the container's &lt;code&gt;memory.oom.group&lt;/code&gt; to 1.&lt;sup id="fnref6"&gt;6&lt;/sup&gt; When OOM occurs in this cgroup, the kernel kills every process in the container, the container is recreated along with its cgroup, and the counters start again from 0.&lt;/p&gt;

&lt;p&gt;To check whether your nodes behave this way, see whether the container's &lt;code&gt;/sys/fs/cgroup/memory.oom.group&lt;/code&gt; is 1. If the kubelet's &lt;code&gt;singleProcessOOMKill&lt;/code&gt; is true, it stays 0.&lt;/p&gt;

&lt;h4&gt;
  
  
  Finding the Pod's slice
&lt;/h4&gt;

&lt;p&gt;The Pod's slice is the cgroup that groups all of the Pod's containers, and its &lt;code&gt;memory.events&lt;/code&gt; adds up the counts of the containers under it. The slice survives container restarts, so the count survives too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/sys/fs/cgroup/
└── kubepods.slice/
    └── kubepods-burstable.slice/
        └── kubepods-burstable-pod&amp;lt;uid&amp;gt;.slice/         ← Pod's slice
            └── cri-containerd-&amp;lt;container-id&amp;gt;.scope/   ← container's scope
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To locate the slice, search for the scope by container ID. The container ID is the part of the Pod's &lt;code&gt;containerID&lt;/code&gt; after &lt;code&gt;containerd://&lt;/code&gt;. When you enter the node with &lt;code&gt;kubectl debug node&lt;/code&gt;, the node's files appear under &lt;code&gt;/host&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;$ kubectl get pod &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;pod&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -o &lt;span class="nv"&gt;jsonpath&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'{.status.containerStatuses[*].containerID}'&lt;/span&gt;
containerd://&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;container-id&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
$ kubectl debug node/&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;node&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -it --image=busybox
&lt;span class="c"&gt;# find /host/sys/fs/cgroup -type d -name 'cri-containerd-&amp;lt;container-id&amp;gt;.scope'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parent of the directory it finds is the Pod's slice. &lt;code&gt;cri-containerd-&lt;/code&gt; is the prefix used when the runtime is containerd. If you can log in to the node as root, drop &lt;code&gt;/host&lt;/code&gt; from the path.&lt;/p&gt;

&lt;h4&gt;
  
  
  Reading the slice's memory.events
&lt;/h4&gt;

&lt;p&gt;In the slice's &lt;code&gt;memory.events&lt;/code&gt;, &lt;code&gt;oom_group_kill&lt;/code&gt; is the OOM count, and it matches restartCount. &lt;code&gt;oom_group_kill&lt;/code&gt; is the number of times all processes in the cgroup were killed together because of &lt;code&gt;memory.oom.group&lt;/code&gt;.&lt;sup id="fnref7"&gt;7&lt;/sup&gt; For a Pod with a restartCount of 3, it looks like this.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# grep oom &amp;lt;slice&amp;gt;/memory.events
oom 3
oom_kill 9
oom_group_kill 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;oom_kill&lt;/code&gt; goes up each time the kernel kills a process, so a single OOM raises it by more than one. In this Pod, three OOMs brought it to 9.&lt;/p&gt;

&lt;p&gt;Even if you fix the paths and how the values are read for v2, the next time kernel or kubelet behavior changes, the script will again return wrong values without raising an error. If a monitoring agent such as cAdvisor provides the values the script reads, I want to stop reading &lt;code&gt;/sys/fs/cgroup&lt;/code&gt; directly.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;I checked these on a Kubernetes 1.37.0 cluster started with kind v0.33.0 on a GitHub Actions runner (ubuntu-24.04). The results are &lt;a href="https://github.com/shinagawa-web/show-your-work/actions/runs/38369999475" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and the code is in &lt;a href="https://github.com/shinagawa-web/show-your-work/tree/0edeb00/cgroup-v2-scripts-read-wrong-values" rel="noopener noreferrer"&gt;show-your-work&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;The &lt;code&gt;cpu.stat&lt;/code&gt; entry in the kernel's &lt;a href="https://docs.kernel.org/admin-guide/cgroup-v2.html#cpu-interface-files" rel="noopener noreferrer"&gt;cgroup v2 documentation&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;The &lt;code&gt;memory&lt;/code&gt; entry in the Kubernetes &lt;a href="https://kubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline/" rel="noopener noreferrer"&gt;Resource metrics pipeline&lt;/a&gt; documentation.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;cAdvisor's &lt;a href="https://github.com/google/cadvisor/blob/v0.60.6/lib/container/libcontainer/handler.go#L876-L899" rel="noopener noreferrer"&gt;handler.go&lt;/a&gt; subtracts &lt;code&gt;total_inactive_file&lt;/code&gt; for v1 and &lt;code&gt;inactive_file&lt;/code&gt; for v2. The &lt;a href="https://github.com/kubernetes/website/blob/main/content/en/examples/admin/resource/memory-available-cgroupv2.sh" rel="noopener noreferrer"&gt;memory-available-cgroupv2.sh&lt;/a&gt; example in the Kubernetes documentation writes the v2 formula the same way.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;The &lt;code&gt;memory.oom_control&lt;/code&gt; entry in the kernel's &lt;a href="https://docs.kernel.org/admin-guide/cgroup-v1/memory.html#oom-control-deprecated" rel="noopener noreferrer"&gt;cgroup v1 memory documentation&lt;/a&gt;. &lt;code&gt;oom_kill&lt;/code&gt; is the number of processes in that cgroup killed by OOM.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;In the kubelet's &lt;a href="https://github.com/kubernetes/kubernetes/blob/v1.35.0/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go#L314-L320" rel="noopener noreferrer"&gt;kuberuntime_container_linux.go&lt;/a&gt;, &lt;code&gt;memory.oom.group&lt;/code&gt; is set to 1 on v2 nodes unless &lt;code&gt;singleProcessOOMKill&lt;/code&gt; is true. This was added in &lt;a href="https://github.com/kubernetes/kubernetes/pull/117793" rel="noopener noreferrer"&gt;kubernetes/kubernetes#117793&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;The &lt;code&gt;memory.events&lt;/code&gt; entry in the kernel's &lt;a href="https://docs.kernel.org/admin-guide/cgroup-v2.html#memory-interface-files" rel="noopener noreferrer"&gt;cgroup v2 documentation&lt;/a&gt;.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>kubernetes</category>
      <category>linux</category>
      <category>devops</category>
      <category>monitoring</category>
    </item>
  </channel>
</rss>
