How to fix functions/deadline-exceeded in Firebase

FirebaseINTERMEDIATEHIGHUpdated Dec 22, 2025· 131 views

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.

Quick fix

Fixes functions/deadline-exceeded

import * as functions from "firebase-functions";

exports.myFunction = functions
  .runWith({ timeoutSeconds: 180 })
  .https.onCall(async (data, context) => {
    // Your function code
  });
  1. Increase function timeout in code
  2. Increase memory allocation
  3. Optimize loops and async operations
  4. Break large operations into smaller batches
firebase — error
import * as functions from "firebase-functions";
exports.myFunction = functions
.runWith({ timeoutSeconds: 180 })
.https.onCall(async (data, context) => {
// Your function code
});
functions/deadline-exceeded

What this error means

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.

Typical symptoms

  • Cloud Function fails with "Error: 4 DEADLINE_EXCEEDED: Deadline exceeded"
  • HTTP callable functions return deadline-exceeded errors inconsistently
  • Firestore operations (read/write) timeout when processing large datasets
  • Background functions (Firestore triggers) fail after some execution time
  • Client receives the error after ~60 seconds of waiting
  • Function may have partially completed despite the error

Common causes

  1. Function execution takes longer than the 60-second HTTP timeout or 540-second background timeout
  2. Processing large batch operations or iterating over many documents (200+) without optimization
  3. Inefficient loops using forEach with async operations instead of Promise.all() or for...of
  4. Large Firestore read/write operations that hit quota limits or network latency
  5. Deployment resetting timeout and memory settings back to defaults
  6. Client-side timeout settings not synchronized with server-side timeout configuration

How to fix "functions/deadline-exceeded"

1Increase function timeout in code

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:

typescript
import * as functions from "firebase-functions";

exports.myFunction = functions
  .runWith({ timeoutSeconds: 180 })
  .https.onCall(async (data, context) => {
    // Your function code
  });

For Firestore triggered functions:

typescript
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)

2Increase memory allocation

Functions with more memory execute faster (more CPU allocated). Increase memory alongside timeout:

typescript
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).

3Optimize loops and async operations

Replace inefficient forEach loops with Promise.all() for concurrent operations:

typescript
// 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:

typescript
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))
    );
  }
}
4Break large operations into smaller batches

For bulk data operations, split into multiple smaller operations instead of one large batch:

typescript
// 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();
  }
}
5Set client-side timeout to match server-side

If using callable functions from the client, ensure the timeout is set appropriately:

typescript
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.

6Monitor function performance

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.

Advanced notes

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.

Frequently asked questions

What does "functions/deadline-exceeded" mean?
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.
How do I fix "functions/deadline-exceeded"?
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.
What causes "functions/deadline-exceeded"?
Function execution takes longer than the 60-second HTTP timeout or 540-second background timeout Processing large batch operations or iterating over many documents (200+) without optimization Inefficient loops using forEach with async operations instead of Promise.all() or for...of
Is the "functions/deadline-exceeded" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading