Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Firebase Cloud Functions respond slowly, especially on the first call

ErrorThe first request after a quiet period takes several seconds, later ones are fast

Short answer

Most slow first calls are cold starts: the runtime starts from scratch, loads your code and runs global-scope work before it can answer. Keep one instance warm with minInstances, import only what each function needs, and leave concurrency high so existing instances are reused.

  • Applies to firebase-functions 6, 2nd gen functions, Node.js 22, firebase-tools 14
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the function logs and compare the duration of a first call after idle with a call a few seconds later. If only the first is slow, it is a cold start; if all are slow, look at your own code.

    firebase functions:log --only myFunction
  2. 2Match what you see, then jump to its fix

    1. 1Slow only after idle periodsCold starts on a function that scales to zeroFix
    2. 2The slow start is long even when the handler does almost nothingHeavy imports and global initialization run on every cold startFix
    3. 3Latency climbs when many requests arrive togetherConcurrency is set too low, so traffic creates new instancesFix

What does the error look like?

  • The first request after a quiet period takes several seconds, later ones are fast
  • Every function in the project is slow after deploy, even tiny ones
  • Latency rises sharply when traffic spikes

Cause 1 of 3

Cold starts on a function that scales to zero

1Check

Functions are stateless and the execution environment is often initialized from scratch. That start-up time is the cold start.

firebase functions:log --only myFunction

2Fix

Keep a minimum number of instances running for latency-sensitive functions. Idle instances are billed at idle rates, and the CLI shows a cost estimate when you deploy.

const { onRequest } = require('firebase-functions/v2/https');
exports.api = onRequest({ minInstances: 1 }, (req, res) => {
  res.send('ok');
});

3Verify

Deploy and call the function after a quiet period. The first call is no slower than the next.

firebase deploy --only functions:api

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 3

Heavy imports and global initialization run on every cold start

1Check

Loading unused dependencies adds to cold start latency. Global scope runs at load time, so initialize only what the first request needs.

grep -rn "require(\|^import " functions/index.js

2Fix

Import big libraries inside the function that uses them, and create expensive clients lazily the first time they are needed, then reuse them.

let storageClient;
exports.report = onRequest(async (req, res) => {
  if (!storageClient) {
    const { Storage } = require('@google-cloud/storage');
    storageClient = new Storage();
  }
  res.send('ok');
});

3Verify

Compare the cold-start duration in the logs before and after the change.

firebase functions:log --only report

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

Concurrency is set too low, so traffic creates new instances

1Check

Reusing existing instances during spikes reduces cold starts. Limiting concurrency limits how existing instances can be used. For 2nd gen functions the default concurrency is 80, and concurrency needs at least 1 full CPU.

grep -rn "concurrency\|cpu" functions/index.js

2Fix

Remove a low concurrency setting, or give the function a full CPU so it can serve several requests at once.

exports.api = onRequest({ concurrency: 80, cpu: 2 }, handler);

3Verify

Run a short burst of requests and watch whether the extra cold starts disappear.

firebase functions:log --only api

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Keep every function in its own small file and import only what it needs.
  • Set minInstances only on the few functions a user waits for.
  • Compare cold and warm durations in the logs after each deploy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

firebase-admin warns that FIREBASE_CONFIG is missing

Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail

Cloud Functions for Firebase sets FIREBASE_CONFIG for you; a script run with plain node does not have it.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.