Snapwake

DocsRunning your app

Sleep and wake

When an app goes to sleep

A web app goes to sleep 10 seconds after the last byte moved on any of its connections, in either direction. During the beta this idle time is the same for every app. Learning each app's traffic pattern and choosing the time per app is planned.

An app stays awake while:

  • a request is in progress and the app hasn't started answering it yet, for up to 15 minutes (a slow database query or an LLM call doesn't put the app to sleep halfway);
  • bytes keep flowing on any connection, such as a long download or a stream;
  • it has min VMs set to stay awake (see Scaling);
  • its new version was just deployed and the snapshot for later wakes isn't ready yet.

Requests that only Snapwake makes, such as health checks, don't count as traffic.

Queue workers follow different rules; see Background workers.

What this means for your code

  • Don't keep working after you've answered. Work you start after sending the response (fire-and-forget promises, background threads, setTimeout for later) stops when the app goes to sleep 10 seconds later, and it doesn't resume: the next wake starts from the deploy snapshot again. Do the work before answering, or put it on a queue for a worker.
  • Timers don't run while the app sleeps. setInterval, schedule, an in-process cron: none of them fire. Use an external scheduler that calls your app.
  • Streams need a heartbeat. A server-sent events stream or a long poll that is silent for more than 10 seconds lets the app sleep and cuts the stream. Send a comment line (: ping\n\n for SSE) every few seconds.
  • Shutdown handlers don't run. Sleep freezes the app and stops its VM; there is no SIGTERM for web apps.
  • WebSockets don't work through your public URL yet. The edge in front of your app doesn't pass the Upgrade handshake. Holding WebSocket connections on the platform while the app sleeps is planned.

How an app wakes

A request for a sleeping app waits while Snapwake restores the VM from its snapshot, then goes to the app as usual. The client sees a slower first response, never an error page.

Measured on a bare-metal test server (wake plus the first response):

App Wake
Python, http.server 11.7 ms
Node.js, http 14 ms
Express API 16.5 ms
MCP server 18.4 ms
Next.js 18.7 ms
FastAPI 24.8 ms
Medusa store backend 25.7 ms, 108 ms when the first request also reconnects to Postgres

On our slower development machine, stock nginx, Postgres and Redis images wake in 50 to 100 ms, and Spring Boot in 0.56 s instead of a 134 s cold start. Apps with volumes wake from their own saved state, which takes about half a second there.

What makes a wake fast or slow:

  • Memory touched during warm-up. The deploy requests / (/health for Medusa) before taking the snapshot and records which memory pages that used. Those pages are loaded at once on wake; anything else is loaded the first time the app touches it. A first request to a route that loads a lot of new code is slower than one to /.
  • Recent traffic. For apps that had requests in the last 10 minutes, the server keeps a VM process prepared in advance, which saves a few milliseconds.
  • The server's load. A server wakes only as many apps at once as it has CPU cores, and only if it has memory for them. Under a burst, wakes wait their turn; requests come before background wakes. If a wake can't get a slot within 30 seconds, or can't get memory within 5 seconds (after putting idle apps to sleep to make room), the request gets 503 with Retry-After: 2, and further requests to that app get the same answer for 2 seconds.
  • Reconnecting. Connections to databases and other services are closed while the app sleeps, so the first request after a wake usually opens new ones. For Postgres, the connection pool removes most of that cost.

What Snapwake fixes on every wake

A restored VM would otherwise think no time has passed and would repeat the randomness of the moment the snapshot was taken. Before the request that woke the app reaches it, Snapwake:

  1. sets the clock to the current time;
  2. reseeds the kernel's random generator with fresh entropy, so /dev/urandom, getrandom(), Python's os.urandom and similar calls never repeat across wakes;
  3. gives the VM a new network address and aborts every TCP connection that belonged to the snapshot (your code sees a closed or aborted connection and reconnects), except connections on localhost;
  4. writes the current secrets to /run/flare/secrets.env;
  5. runs wake hooks registered with the runtime helpers, waiting up to 1 second for them.

Before sleep, Snapwake also closes the app's outside connections, so your database frees its side right away instead of keeping dead connections.

Randomness inside your runtime

The kernel is reseeded on every wake, but some runtimes seed their own generators once, inside the process. Those generators are part of the snapshot and would produce the same numbers on every wake. Snapwake fixes the common ones; check the table for yours.

Runtime Fixed automatically What you need to do
Node.js 18.18+ OpenSSL's generator inside the process, crypto.randomUUID (its pre-filled cache is turned off), Math.random nothing
Python the global random module, OpenSSL inside the process reseed or create after wake any random.Random() instance or numpy generator your app made at start (an on_restore hook, or create them per request)
Go nothing use crypto/rand for anything that must differ (tokens, IDs); the global generator of math/rand is seeded once at start and repeats on every wake
Java nothing java.util.Random instances and a SecureRandom seeded at start repeat; create them per use or reseed after wake. Untested
Ruby nothing Random::DEFAULT and OpenSSL's generator are seeded at start. Untested
Bun, Deno nothing untested; read from the kernel (crypto.getRandomValues in a fresh call) and test on a few wakes

For Node.js and Python this works without code changes, in framework builds and in any image that contains node (18.18 or newer) or python3. Set FLARE_SDK=0 in the image's environment to turn it off.

A quick check for any runtime: deploy an endpoint that returns a random value and the time, let the app sleep, and call it a few times with a pause longer than 10 seconds between calls. Each answer must be different and show the current time.

Things that are reset on every wake (apps without volumes)

Every wake of an app without volumes starts from the same deploy snapshot. Memory and files changed since then are gone after sleep: sessions kept in memory, in-process caches filled by traffic, files written to the app's own filesystem or /tmp. See How Snapwake works.