Optimizing Firestore Caching in Firebase Cloud Functions with @libs-jd/cloud-firestore-cache

Learn how to reduce Firestore reads and boost performance in single-instance Firebase Cloud Functions using the lightweight @libs-jd/cloud-firestore-cache library.

deepika beniwal

3 months ago

optimizing-firestore-caching-in-firebase-cloud-functions-with-libs-jd-cloud-firestore-cache

Introduction

Working with Firestore in Firebase Cloud Functions? You’ve probably run into performance bottlenecks from repeated reads. While Firestore is fast, it’s not free—especially when you’re hitting it multiple times for the same data.

If your Cloud Function is configured with maxInstances: 1, you can now cache Firestore data in-memory with a tiny yet powerful library: @libs-jd/cloud-firestore-cache. Let's break it down.

🔍 What is @libs-jd/cloud-firestore-cache?

This library offers a scoped, in-memory caching layer around Firestore operations, perfect for Cloud Functions running on a single instance. Think of it as a mini caching proxy that lives inside your function instance.

📦 Installation

npm install @libs-jd/cloud-firestore-cache

🚀 Basic Usage Example

const { initializeApp } = require("firebase-admin/app");

const { getFirestore, FieldValue } = require("firebase-admin/firestore");

const { FirestoreCache } = require("@libs-jd/cloud-firestore-cache");

initializeApp();

const firestoreInstance = getFirestore();

const db = FirestoreCache(firestoreInstance, FieldValue);

// Fetch user data (cached after first read)

db.get("users/user123").then((result) => {

console.log("Cached or fetched result:", result);

});

✅ On the first call, it fetches from Firestore and caches the result
✅ On subsequent calls, it returns the value from memory

🧠 How It Works

  • Internally, it stores responses in memory.

  • It doesn’t persist across cold starts—so it's ephemeral.

  • Great for reducing duplicate reads within warm invocations.

⚙️ Configuration Requirement

Set your function like this:

export const yourFunction = functions

.runWith({ maxInstances: 1 })

.https.onRequest((req, res) => {

// your handler

});

Why? Because multi-instance functions don’t share memory—this cache won’t work across instances.

🛠 Use Case Scenarios

This library shines in scenarios where:

  • You expect repeated reads for the same documents

  • You want better performance without setting up external caches (like Redis)

  • You are using singleton Cloud Functions (e.g., HTTP endpoints, webhooks)

✅ Benefits

  • 🔥 Fewer Firestore reads → Lower cost

  • ⚡ Faster responses for frequently-accessed documents

  • 🧼 Cleaner code without managing your own cache layer

📌 Final Thoughts

If you’re using Firebase Cloud Functions with maxInstances: 1, @libs-jd/cloud-firestore-cache is a lightweight, developer-friendly solution for reducing Firestore reads and improving speed. Just don’t forget its scope: it works only in-memory and only on a single instance.

Use it to supercharge your endpoints, webhook handlers, or repeat-read functions—and say goodbye to redundant Firestore hits.