Cloud & Virtualization

The Cold Start Problem: Why First Requests Are Slow and How to Fix It

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

You deploy a new version, open the site, and the first page takes several seconds. Reload, and it appears almost instantly. That gap is a cold start: the extra time a system needs before it can serve requests at full speed. Cold starts are most famous in serverless platforms, but they appear almost everywhere, from Java services and PHP opcode caches to databases with empty memory buffers. Understanding the causes lets you choose where to pay that cost: before users arrive, not while they wait.

What happens during a cold start

A request that hits a "cold" component may trigger several kinds of setup work:

  • Provisioning: creating or resuming a VM, microVM or container.
  • Loading code: pulling an image, reading files from disk, loading libraries.
  • Runtime start-up: starting the JVM, .NET runtime, Node.js or Python interpreter, and running initialization code.
  • Compilation: JIT compilation in Java or .NET, or PHP compiling scripts before OPcache stores them.
  • Connections: opening database connections, TLS handshakes, DNS lookups.
  • Empty caches: the database buffer pool, the OS page cache and application caches all start empty, so early requests go to disk.

A "warm" component has already done all this and keeps the results in memory.

Where you will meet cold starts

LayerCold start causeTypical fix
Serverless functionsNew instance created after idle time or scale-outKeep warm, smaller packages, provisioned concurrency where offered
ContainersImage pull and app boot on a new nodeSmaller images, pre-pulled images, readiness probes
JVM / .NET appsClass loading and JIT compilationWarm-up requests, class data sharing, ahead-of-time options
PHPOPcache empty after restartPreload, warm-up script, avoid needless restarts
DatabasesBuffer pool and page cache emptyPrewarming, enough RAM, avoid frequent restarts
Caches and CDNsEmpty cache after deploy or purgeCache warming, staggered purges

How to measure it

Measure the first request separately from steady-state requests. A simple test with curl shows the difference:

for i in 1 2 3 4 5; do
  curl -o /dev/null -s -w "%{time_starttransfer}\n" https://example.com/
done

The first time_starttransfer value is often much higher than the rest after a restart. For services, check start-up logs with journalctl -u yourapp --since "10 min ago" and record the time between "starting" and "ready". For containers, compare the time from container creation to the first passing readiness check.

Practical ways to reduce cold starts

1. Keep things running

The simplest fix on a VPS is that nothing goes cold unless you restart it. A long-running process on your own server does not spin down after idle time, unlike many serverless platforms. If a cold start costs you, a small always-on server can be the cheaper answer. Compare plans on the pricing page.

2. Warm up before taking traffic

Make the application ready before the load balancer sends it users:

  • Add a readiness endpoint that returns success only after initialization is complete.
  • In Kubernetes, use a readinessProbe (and a startupProbe for slow starters) so pods receive traffic only when ready.
  • After a deploy, run a warm-up script that requests the most important pages:
while read -r path; do
  curl -s -o /dev/null "http://127.0.0.1:8080$path"
done < warmup-paths.txt

3. Make start-up cheaper

  • Use smaller container images and fewer dependencies.
  • Defer non-critical initialization until after the service is ready.
  • Reuse database connections with a pool instead of opening one per request.
  • In PHP, enable OPcache and consider opcache.preload for frameworks that support it.
  • In Java, look at class data sharing or ahead-of-time compilation options available in your runtime.

4. Warm the data layer

On PostgreSQL, the pg_prewarm extension can load chosen tables or indexes into memory:

CREATE EXTENSION IF NOT EXISTS pg_prewarm;
SELECT pg_prewarm('orders');

MySQL and MariaDB InnoDB can save and reload the buffer pool contents across restarts with innodb_buffer_pool_dump_at_shutdown and innodb_buffer_pool_load_at_startup, which are enabled by default in recent versions.

5. Deploy without going cold

Rolling, blue-green or canary deployments let new instances warm up while old ones keep serving. Switch traffic gradually instead of restarting everything at once. Our guide on zero-downtime deployment covers the patterns.

6. Stagger cache purges

Purging an entire CDN or application cache at once sends every request to the origin together, which can look like a cold start for the whole site. Purge selectively, or warm key pages right after a purge.

Cold start checklist

  1. Measure first-request latency after a restart.
  2. Identify which layer is slow: runtime, connections or data.
  3. Add readiness checks so cold instances get no traffic.
  4. Script a warm-up of key paths after deploys.
  5. Keep caches and buffer pools warm where possible.
  6. Prefer rolling deploys over full restarts.

FAQ

Do cold starts happen on a normal VPS?

Less often than on serverless platforms, because your processes keep running. They still appear after reboots, service restarts and deployments, when runtimes, opcode caches and database buffers start empty.

Is a keep-alive ping a good fix?

It can keep a serverless function or idle service warm, but it only keeps one instance warm and adds a small constant cost. It does not help when traffic scales out to new instances. Treat it as a stopgap and fix start-up time where you can.

How much RAM helps with cold starts?

Enough RAM lets the database buffer pool and OS page cache hold your working set, so once warm, it stays warm. Too little RAM causes constant eviction, which feels like a permanent partial cold start.

Related guides