Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

How one uptime monitor used 80% of our database allowance

Serverless databases bill for being awake Neon bills for compute time, not for queries. When nothing connects for about five minutes, the database suspends and costs nothing. Every new connection resets that timer. So

Serverless databases bill for being awake

Neon bills for compute time, not for queries. When nothing connects for about five minutes, the database suspends and costs nothing. Every new connection resets that timer.

So the question that matters is never "how many queries". It is "does anything stop the database from going back to sleep". One query every four minutes, forever, costs as much as a database that is busy around the clock.

The first fixes were real, and missed

The allowance ran out once in August. We fixed what we found: background tabs polling the server, a sitemap that queried during the build, and a health-check route that ran through more code than it needed to. Every fix was real, and we checked each one shipped. A month later the bill was growing just as fast.

Checking that a fix exists is not the same as checking the problem is gone. For a database that should sleep, the only real check is whether it sleeps. Ours hadn't suspended once since August 17.

What was actually keeping it awake

Sentry had created an uptime monitor for us automatically. It requested the homepage once a minute, from rotating regions around the world.

Every request to the site runs through a small piece of middleware that checks the visitor's IP against a block list, and the block list lived in Postgres behind a 60-second in-memory cache. The cache lasted exactly as long as the gap between checks, and each region hit a different server instance anyway, so it never hit. That was about 1,440 queries a day against a table with no rows in it. Over one week the database was awake for 165.7 of 167.5 hours.

The health-check route we had carefully fixed in August received 2 requests in that same day.

The fix

The block list moved off the database onto Vercel Edge Config, built for reads on every request. Postgres stays the source of truth, and the admin tools republish the list whenever it changes. The monitor now checks the health route, which caches a successful probe for hours.

Ten minutes after the deploy, the database suspended for the first time in three weeks.

What we check now

  • Measure the outcome, not the fix. For a sleeping database that means its suspend events, not a list of merged pull requests.
  • List every external caller: uptime monitors, cron services, CI. The one that cost us was a service nobody remembered configuring.
  • A cache that expires at or below the idle window is not a cache. It has to outlast the timer it is meant to protect.
  • Nothing on the per-request path reads the database unless the page itself needs it.
  • When rendering moves from the browser to the server, ask again who can trigger the query, and how often.

Originally published on the XBio blog. XBio is a gamified link-in-bio platform — xbio.gg.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.