How to reduce and boost your Firebase cold start times

Firebase cold start times are a common problem for developers. In this tutorial, we'll show you how to reduce and boost your Firebase cold start times.

Firebase is a great platform for building serverless applications. It provides a lot of great features out of the box, such as authentication, database, storage, and hosting.

However, one of the downsides of using Firebase is that it can be slow to start up: this is because Firebase is a serverless platform, which means that it doesn't have any servers running all the time.

Instead, it starts up a server when a request comes in, and then shuts it down when the request is complete. This is great for saving money, but it can be slow to start up, especially when loading the Firestore Admin SDK in your API function. This has been a very popular issue on the Firebase community forums: the only solution was switching to a REST API, which meant not being able to rely on the official SDK.

In this post, I want to share two tips to reduce and boost your Firebase cold start times, thanks to recent updates to the Firebase Functions and the Firestore Admin SDK.

1. Switch to the Firestore REST API

If you update your Firestore Admin Node.js SDK to version 9.14.0 or higher, you can now use the Firestore Admin SDK using REST calls instead of the gRPC protocol. This will immediately boost your cold start times since the Firestore Admin SDK no longer needs to access files from the file system.

NB: this will improve your cold start times regardless of which platform you use, such as AWS, Vercel or Netlify. In fact, this issue existed in all serverless platforms, not just Firebase Functions.

How to enable the Firestore REST API

To enable the Firestore REST API, you need to update your firebase-admin package to version 9.14.0 or higher, and then update your firebase-admin/firestore settings when creating the Firestore instance.

For example, you can use the function below and replace your plain getFirestore instances:

import { getFirestore, Firestore } from 'firebase-admin/firestore';
let firestore: Firestore;
/**
* @name getRestFirestore
* @description Get the Firestore instance with REST API enabled. This is
* faster than the default Firestore instance which uses GRPC.
*/
function getRestFirestore() {
// prevent multiple instances of Firestore
if (firestore) {
return firestore;
}
firestore = getFirestore();
// enable REST API for faster Firestore operations
firestore.settings({
preferRest: true,
});
return firestore;
}
export default getRestFirestore;

Now, use getRestFirestore instead of getFirestore in your functions whenever you're loading the Firestore SDK in your API functions.

2. Use the new Firebase Functions v2

If you are using Firebase Functions, you can further reduce your cold start times by switching to the new Firebase Functions v2.

The Firebase Functions v2 now uses Google Cloud Run: a more modern and faster serverless product with better control, configuration and monitoring.

The most important change is that Function instances can now execute more than one request at a time: new functions with 1 dedicated CPU or higher will default to 80 concurrent requests. This means that your functions will be able to handle more requests at the same time, which will reduce the number of cold starts.