Firebase Cloud Functions exceed their timeout limit when operations take longer than allowed (60 seconds for HTTP functions, 540 for background). Resolve by increasing timeout, optimizing code, or breaking large operations into smaller batches.
The "deadline-exceeded" error occurs when a Cloud Function takes longer to execute than the configured maximum time allowed. By default, Firebase Cloud Functions have a 60-second timeout for HTTP functions and 540 seconds for background functions. This error typically indicates either your function is genuinely slow and needs more time, or it contains inefficient code that should be optimized. Note that for operations that change system state, this error may be returned even if the operation completed successfully—it only means the response took too long to be returned.
The most reliable fix is to set the timeout directly in your function definition. This ensures the setting persists through deployments.
For HTTP callable functions:
import * as functions from "firebase-functions";
exports.myFunction = functions
.runWith({ timeoutSeconds: 180 })
.https.onCall(async (data, context) => {
// Your function code
});For Firestore triggered functions:
exports.myTrigger = functions
.runWith({ timeoutSeconds: 540, memory: "512MB" })
.firestore.document("users/{userId}")
.onWrite(async (change, context) => {
// Your function code
});Valid timeout ranges:
- HTTP functions: 1–3,600 seconds (max 1 hour)
- Background functions: 1–540 seconds (max 9 minutes)
Functions with more memory execute faster (more CPU allocated). Increase memory alongside timeout:
exports.myFunction = functions
.runWith({
timeoutSeconds: 300,
memory: "1GB"
})
.https.onCall(async (data, context) => {
// Your function code
});Memory options: 128MB, 256MB, 512MB, 1GB, 2GB, 4GB, 8GB, 16GB (higher tiers available on Blaze plan).
Replace inefficient forEach loops with Promise.all() for concurrent operations:
// BAD: Sequential execution
async function badApproach(items: Item[]) {
for (const item of items) {
await db.collection("items").add(item); // One at a time
}
}
// GOOD: Concurrent execution
async function goodApproach(items: Item[]) {
await Promise.all(
items.map(item => db.collection("items").add(item))
);
}For larger batches, process in chunks to avoid overwhelming the service:
async function processInChunks(items: Item[], chunkSize: number = 100) {
for (let i = 0; i < items.length; i += chunkSize) {
const chunk = items.slice(i, i + chunkSize);
await Promise.all(
chunk.map(item => db.collection("items").add(item))
);
}
}For bulk data operations, split into multiple smaller operations instead of one large batch:
// For batched writes in Firestore
async function bulkWrite(items: Item[]) {
const batch = db.batch();
let count = 0;
const batchSize = 500; // Firestore batch limit
for (const item of items) {
const docRef = db.collection("items").doc();
batch.set(docRef, item);
count++;
if (count >= batchSize) {
await batch.commit();
batch = db.batch();
count = 0;
}
}
if (count > 0) {
await batch.commit();
}
}If using callable functions from the client, ensure the timeout is set appropriately:
import { getFunction, httpsCallable } from "firebase/functions";
const myFunction = httpsCallable(functions, "myFunction", {
timeout: 300000, // 300 seconds in milliseconds
});
try {
const result = await myFunction({ data: "value" });
} catch (error) {
console.error("Function error:", error);
}Make sure the client-side timeout is longer than or equal to the server-side timeout.
Use Firebase Console to identify which functions are slow:
1. Go to Firebase Console → Functions
2. Click on your function name
3. Review the Metrics tab for:
- Execution time (P50, P95, P99 percentiles)
- Error rate and error details
- Memory usage
4. Check Cloud Logging for detailed error messages:
- Firebase Console → Logging
- Filter by function name
- Look for long execution times before deadline exceeds
This helps identify whether you genuinely need more timeout or if code optimization is needed.
Firestore has special constraints: basic operations like adds should complete in under 10 seconds normally; if you consistently see deadline-exceeded on simple operations, the issue may be quota limits or network latency from your deployment region. Consider deploying functions in the same region as your Firestore database. For aggregation queries, Firestore has a hard 60-second deadline that cannot be increased—if aggregations timeout, use counters or restructure your data model. Background functions (Firestore triggers) can use up to 540 seconds, but note that if you enable retries on event-driven functions, the entire process including retries must complete within that deadline.
Callable Functions: INTERNAL - Unhandled exception
How to fix "Callable Functions: INTERNAL - Unhandled exception" in Firebase
auth/invalid-hash-algorithm: Hash algorithm doesn't match supported options
How to fix "auth/invalid-hash-algorithm: Hash algorithm doesn't match supported options" in Firebase
Hosting: CORS configuration not set up properly
How to fix CORS configuration in Firebase Hosting
auth/reserved-claims: Custom claims use reserved OIDC claim names
How to fix "reserved claims" error when setting custom claims in Firebase
Callable Functions: UNAUTHENTICATED - Invalid credentials
How to fix "UNAUTHENTICATED - Invalid credentials" in Firebase Callable Functions