Launch offer: the first 1,000 users get Settl free for a year* Claim your spot
settlbuilding in public

The sleeping database kit: stop a poll from keeping Postgres awake

The short version

Serverless Postgres like Neon bills compute for every minute the database is awake, and it only goes back to sleep after five quiet minutes. My app checked for updates every minute. Each check made thirteen trips to the database, even when nothing had changed, so the database never got its five quiet minutes. That one poll cost 55 to 80 compute hours a month.

The fix was to ask Redis first. Every write bumps a small version number in Redis. The poll reads that number, and only touches Postgres when it moved. Nothing changed means no database trip, and the database sleeps. Two more traps sit on the same bill: Vercel's free Hobby plan is for non-commercial use only, and a Vercel budget only stops spending if you turn on Pause, which takes the whole app offline.

Number What it means
5 minutes quiet time before Neon suspends compute (the default)
60 seconds how often my app polled, so it never reached 5 quiet minutes
13 database trips per poll, even when nothing changed
55 to 80 compute hours a month that one poll cost
0 database trips per poll now, when nothing changed

Sources: Neon, scale to zero · Vercel, Hobby plan · Vercel, spend management

Copy these prompts

Paste them into Claude Code (or any coding agent) in your app's repo, in this order.

1. Find what keeps the database awake

Find everything in this codebase that runs on a timer or on a repeat: polling in the client (setInterval,
refetchInterval, SWR or React Query refresh), cron jobs, scheduled functions, health checks and uptime pings.
For each one, report: file and line, how often it runs, and how many database queries one run makes
(count every query, including ones inside helpers). Then mark every one that runs more often than every
5 minutes, because those keep a scale-to-zero database awake all day. Multiply runs per hour by queries
per run and sort by that. Don't change any code yet.

2. Ask Redis first

Take the top item from that list. Add a version key in Redis for the data it checks. Every write that
changes that data increments the key. The poll reads the key first and only queries Postgres when the
version differs from the one the client already has; otherwise it returns "no change". If Redis is
unreachable, fall back to the current database query so nothing breaks. Show me the diff before you apply it.

3. Prove the database sleeps

Count database queries for one hour with the app open and nobody changing anything, before and after this
change. Report queries per hour for each, and the longest quiet gap between queries. The gap after the
change should be longer than 5 minutes. List anything else that still wakes the database inside that hour.

The Redis check, in code

A small version of what my app does, with Upstash Redis. Adapt the names to yours.

import { Redis } from '@upstash/redis'
const redis = Redis.fromEnv()

// Call this after every write that changes what the poll watches.
export async function bump(scope: string) {
  await redis.incr(`v:${scope}`)
}

// The poll. Postgres is only touched when the version moved.
export async function poll(scope: string, seen: number) {
  let version: number
  try {
    version = Number(await redis.get(`v:${scope}`)) || 0
  } catch {
    return { changed: true, data: await loadFromPostgres(scope) } // Redis down: old path
  }
  if (version === seen) return { changed: false, version }
  return { changed: true, version, data: await loadFromPostgres(scope) }
}

The client keeps the last version it saw and sends it with every poll. A poll that finds no change costs one Redis read and zero database time.

The three traps on the bill

1. The poll. Anything that runs more often than the database's sleep timer keeps it awake for good. One user with the app open is enough. Check timers, cron jobs and uptime pings, not just your own polling.

2. Free. Vercel's Hobby plan is for personal, non-commercial use. The day your app takes a payment, you need Pro. Plan for that cost before launch, not after the first sale.

3. The cap. A Vercel budget sends alerts, but it only stops spending if you turn on Pause, and Pause takes your whole app offline. So:

Steal this for your app

Run this on your codebase

Start with prompt 1 above. Most apps find one timer that runs every minute or faster, and fixing that one is usually most of the bill.

Get the next one in your inbox