Firebase Cloud Functions respond slowly, especially on the first call
The first request after a quiet period takes several seconds, later ones are fastShort 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.
Fix it fast
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 myFunction2Match what you see, then jump to its fix
- 1
Slow only after idle periodsCold starts on a function that scales to zeroFix - 2
The slow start is long even when the handler does almost nothingHeavy imports and global initialization run on every cold startFix - 3
Latency climbs when many requests arrive togetherConcurrency is set too low, so traffic creates new instancesFix
- 1
What does the error look like?
The first request after a quiet period takes several seconds, later ones are fastEvery function in the project is slow after deploy, even tiny onesLatency 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 myFunction2Fix
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:apiConfirmed by firebase.google.com
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.js2Fix
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 reportConfirmed by firebase.google.com
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.js2Fix
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 apiConfirmed by firebase.google.com
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:
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
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 fixBack end
firebase deploy --only functions fails with a generic deploy error
HTTP 429
The generic message hides a specific one.
3 causes, each with a fixBack end
firebase deploy cannot load the .env file in the functions folder
FirebaseError: Failed to load environment variables from .env
The CLI reads .env files from the functions folder at deploy time and rejects ones it cannot use.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
Firebase Functions fails to load because csv-parse/sync is missing
Cannot find module 'csv-parse'
Firebase installs only what the functions package.json lists under dependencies, and it ignores your local node_modules at deploy.
3 causes, each with a fixBack end
firebase deploy rejects the Node.js version in a Cloud Functions package.json
`runtime` field is required but was not found in firebase.json or package.json.
The Firebase CLI builds the runtime name straight from engines.node in package.json, so the value must be a plain version such as 22, and a version that Google has decommissioned is refused.
5 causes, each with a fix