Accelerate Firebase Cold Starts with Firestore REST Calls

[ad_1]
If you’re running serverless cloud services, whether it’s on AWS or Google Cloud, you know the pain of the cold starts. Firebase Jobs is notorious for having cold start problems and we have previously written about Firebase cold start solutions. Many of these solutions require you to rewrite your code, such as dynamic import modules or the new Google Cloud Functions v2, which allows a Node.js instance to handle multiple requests.
Out of the blue, Firebase stealthily announced a new feature coming with a cold start, preferRest
.
Cloud Firestore API now supports
preferRest
setting to force the use of REST transport until the task requires gRPC.
What does this mean? Hidden Firebase Github ticket description:
There is a long issue with cold start times when using the official Nodejs Firestore library in serverless environments: It seems that the root problem is very difficult to fix (hence the unresolved P1/S1 problem for years), and rightly so. a temporary fix is long overdue.
One solution is to use the Firestore REST API directly. This greatly improves cold start times, but at the cost of a significantly degraded developer experience. There has been at least one attempt by a Firestore user to create an unofficial wrapper library ( This library works, but is maintained by a single developer, and does not replace the official library for obvious reasons.
Basically, by not having the overhead of gRPC and using direct REST Firestore calls, you can speed up Firebase cold start times.
How to use Firestore REST calls with PreferenceRest
We said before the release “secret”, and we mean it. There is a new way to implement Firestore in your projects that is not mentioned in the official Firebase documentation.
You can usually run Firestore in Node.js (see the documentation for some programming language examples) by doing the following:
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
initializeApp({
credential: applicationDefault()
});
const db = getFirestore();
use the getFirestore()
function to create a database instance.
If you want to set up preferRest
you need to run Firestore a little differently with initializeFirestore
:
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { initializeFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
const app = initializeApp({
credential: applicationDefault()
});
const db = const db = initializeFirestore(app, { preferRest: true });
Go through the startup app
and something { preferRest: true }
. That’s easy!
This is a new feature, so be sure to fully test before releasing to production. As Firebase fan favorite Puf says, “I haven’t tried it myself yet.”
About Ayrshare
Ayrshare is a social media API that allows you to publish posts, get analytics, manage comments, and send direct messages to social media directly from your platform. Read more in our social media API documentation.