Learning on Web Dev Open is free for all.

System Design & Performance > Where the milliseconds goCold starts, regions, and the round trip you forgot
Phase 06Where the milliseconds go301 of 434

Cold starts, regions, and the round trip you forgot

Your function is in Washington and your database is in Frankfurt, and each query now costs ninety milliseconds of geography.

Concept14 minAI adversary

A serverless function that has not run recently has no warm instance, so the platform must allocate one, load the runtime and your bundle, and run module-level code before your handler starts. That is tens to hundreds of milliseconds, paid by an unlucky user, and it is worse the larger your bundle and the heavier your imports. Keeping module scope cheap, importing lazily inside handlers and trimming dependencies all measurably help; so does anything that keeps instances warm, including traffic.

Region choice is the larger and more often ignored cost. Compute in one continent and a database in another means every query pays a hundred milliseconds or so, and a handler making five sequential queries has half a second of pure geography that no index will remove. Put compute next to the data it uses, then push the cacheable output outward, rather than the reverse.

This is why "deploy it to the edge" is not automatically faster. The edge is close to the user, which is excellent for static assets, redirects, personalisation on cached content and auth checks. For a route that needs three round trips to a single-region database, running at the edge moves the compute further from the data and makes the page slower while the dashboard says it is closer to the user.

You should now be able to

  • Explain what happens during a cold start and what shortens it
  • Place compute relative to data on purpose
  • Recognise when the edge is the wrong place to run something
Ask the community

Loading…