<?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: Ludwig Maingi</title>
    <description>The latest articles on DEV Community by Ludwig Maingi (@ludwig_maingi).</description>
    <link>https://dev.to/ludwig_maingi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4146342%2Fa3e2f314-4f5e-4fea-add8-62d4570d9201.webp</url>
      <title>DEV Community: Ludwig Maingi</title>
      <link>https://dev.to/ludwig_maingi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ludwig_maingi"/>
    <language>en</language>
    <item>
      <title>The Phantom Heartbeat: How an App With Zero Users Kept Neon Awake for 720 Hours</title>
      <dc:creator>Ludwig Maingi</dc:creator>
      <pubDate>Wed, 25 Nov 2026 12:37:11 +0000</pubDate>
      <link>https://dev.to/ludwig_maingi/the-phantom-heartbeat-how-an-app-with-zero-users-kept-neon-awake-for-720-hours-koj</link>
      <guid>https://dev.to/ludwig_maingi/the-phantom-heartbeat-how-an-app-with-zero-users-kept-neon-awake-for-720-hours-koj</guid>
      <description>&lt;p&gt;My app had no users. My database was awake 720 hours that month.&lt;/p&gt;

&lt;p&gt;It was a staging environment for a multi-tenant commerce platform. No campaigns, no shoppers, no traffic. Then, on a quiet afternoon, every request started failing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: compute allocation limit reached (100 hours).
Database suspended until next billing reset in 3 days.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neon's free tier gives you a monthly compute allowance, and serverless Postgres is supposed to auto-suspend after 5 minutes of inactivity. Mine had been awake 24 hours a day for 30 days, with nobody using it.&lt;/p&gt;

&lt;p&gt;Nothing was broken. No 500s, no memory leaks, no frozen event loop. Tests passed, endpoints returned 200. The app wasn't broken, it was too diligent. It kept tapping the database on the shoulder to ask "are you awake?", and every tap reset the 5-minute suspend timer.&lt;/p&gt;

&lt;p&gt;Here are the five culprits.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The health check that ran a query
&lt;/h2&gt;

&lt;p&gt;Render was probing &lt;code&gt;/api/v1/health&lt;/code&gt; every 15 to 30 seconds. My handler did this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;apiRouter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// wakes Postgres 2,880+ times a day&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;To check whether the Node server was alive, I woke the database. Even with the whole world asleep, the load balancer kept Postgres running.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The unconditional background sweeper
&lt;/h2&gt;

&lt;p&gt;A BullMQ repeatable job ran every 5 minutes to expire stale orders. Orders only time out after 30 minutes, yet the worker queried Postgres every 300 seconds around the clock, even with zero orders in the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The landing page probe
&lt;/h2&gt;

&lt;p&gt;A well-intentioned pre-flight check in the landing page called &lt;code&gt;/health&lt;/code&gt; on mount. Every refresh, and every search bot crawl, sent traffic through to the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The dashboard that re-fetched everything on navigation
&lt;/h2&gt;

&lt;p&gt;The app shell called &lt;code&gt;fetchFull()&lt;/code&gt; on every mount. Each click between Products, Customers, Expenses and Settings fired 10 parallel analytical queries, just to render two badge numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Forgotten tabs polling forever
&lt;/h2&gt;

&lt;p&gt;Order confirmation and payment status views used &lt;code&gt;setInterval&lt;/code&gt; to poll every 3.5 seconds. A test tab left open overnight meant 12 hours of continuous queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: engineer for idle sleep
&lt;/h2&gt;

&lt;p&gt;The rule is simple: &lt;strong&gt;nothing that can happen while nobody is using the app may touch Postgres.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Health checks answer from memory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;apiRouter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/health"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no database call&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gate background work behind a 1-byte Redis flag
&lt;/h3&gt;

&lt;p&gt;State coordination has to live outside Postgres, in a cheap store that can stay awake. Checking a table to see if work exists is itself the thing that wakes the database.&lt;/p&gt;

&lt;p&gt;Whenever a write path creates work that will need sweeping later (checkout, signup, bulk import), it sets a flag with a 1-hour TTL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app:has_active_work"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"EX"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3600&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;Before the worker touches SQL, it checks Redis:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;hasWork&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app:has_active_work"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;hasWork&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;skipped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"IDLE_PRESERVED"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Postgres stays asleep&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sweepAbandonedOrders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your app has no traffic between 10 PM and 8 AM, the flag expires. For those 10 hours the worker pings Redis, finds nothing, and skips. Postgres sleeps at $0 compute.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fail open, never lose work
&lt;/h3&gt;

&lt;p&gt;If Redis is unreachable, the worker must run the sweep anyway, so tasks are never silently dropped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;hasActiveWork&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;hasActiveWork&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"app:has_active_work"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;hasActiveWork&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fail open&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Keep Redis inside the free tier
&lt;/h3&gt;

&lt;p&gt;Free Redis instances (Upstash, 25 MB) fill up if job history piles up. Cap it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;removeOnComplete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;removeOnFail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fix the frontend too
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Landing page: no API call on mount.&lt;/li&gt;
&lt;li&gt;Dashboard: fetch once, cache, and refetch only on an explicit action, not on every navigation.&lt;/li&gt;
&lt;li&gt;Polling views: stop when the tab is hidden and cap the number of attempts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The bonus: recovery in under 90 seconds
&lt;/h2&gt;

&lt;p&gt;With the database suspended and three days until reset, development could have stalled. Instead, all 51 schema files were versioned, idempotent, and tracked in a &lt;code&gt;schema_migrations&lt;/code&gt; table. Recovery:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a fresh Neon project in the same region (clean free allowance).&lt;/li&gt;
&lt;li&gt;Swap &lt;code&gt;DATABASE_URL&lt;/code&gt; locally and on Render.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;npm run migrate&lt;/code&gt;. All 51 migrations applied inside atomic transactions.&lt;/li&gt;
&lt;li&gt;Seed the admin account.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When the original project unlocked, switching back was one connection string and one &lt;code&gt;npm run migrate&lt;/code&gt;: it skipped the applied files and ran only what was new.&lt;/p&gt;

&lt;h2&gt;
  
  
  The result
&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;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Compute&lt;/td&gt;
&lt;td&gt;~720 hrs/mo, awake 24/7&lt;/td&gt;
&lt;td&gt;~40 hrs/mo&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Idle behavior&lt;/td&gt;
&lt;td&gt;Never suspends&lt;/td&gt;
&lt;td&gt;Suspends after 5 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shoppers during the burn&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Get the production version
&lt;/h2&gt;

&lt;p&gt;I packaged the exact implementation into a standalone kit: a compiling BullMQ v6 gated worker, a timing-safe Express tick endpoint (&lt;code&gt;crypto.timingSafeEqual&lt;/code&gt;) for GitHub Actions or any external cron, retention settings that fit free 25 MB Redis instances, and a failure-mode matrix with a before/after verification checklist.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://whop.com/saasplate/scale-to-zero-jobs" rel="noopener noreferrer"&gt;Scale-to-Zero Jobs on Whop&lt;/a&gt;&lt;/strong&gt;. &lt;strong&gt;20% off launch pricing for the first 24 hours.&lt;/strong&gt; No code needed, the discount is already on the page.&lt;/p&gt;

&lt;p&gt;If you're building a full multi-tenant backend that feeds these queues, there's also the raw-SQL &lt;a href="https://whop.com/saasplate/saas-starter" rel="noopener noreferrer"&gt;SaaS Starter API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What's the quietest thing that has ever kept your database awake?&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>serverless</category>
      <category>node</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
