How to avoid the DEADLINE_EXCEEDED error in Firestore when writing many documents per second

If you stumbled upon the DEADLINE_EXCEEDED error when using Firebase Firestore then it is probably because you have been writing documents over the limit which is one document per second.

This is from the Firebase website.

Limit the collection write rate1,000 operations/second Keep the rate of write operations for an individual collection under 1,000 operations/second.
From Google Cloud documentation

So we need to stay in limit, and how do we do that? Well by using batches. Batches are great because we can update 500 documents at the time. I’ve tested this and updated hundreds of thousands documents without any issues at all. On top of that it is really fast as well. Below is the full code sample of how this can be accomplished. I am using node.js for this.

var updatelist = []

obj = {
    body: "hello world",
    id: 123

// of course in reality this would contains much more objects

var counter = 0;
var commitCounter = 0;
var batches = [];

batches[commitCounter] = db.batch();

updatelist.forEach((a, ind) => {
    if (counter <= 498) {
        var thisRef = db.collection('mycollection').doc(; // document name will be 123
        batches[commitCounter].set(thisRef, { ...a }); // data will be { id: 123, body: "hello world" }
        counter = counter + 1;
    } else {
        counter = 0;
        commitCounter = commitCounter + 1;
        batches[commitCounter] = db.batch();
for (var i = 0; i < batches.length; i++) {
    if (i == 0) {
    batches[i].commit().then(function () {
        console.count('wrote batch');

Update with your own data

var updatelist = []

obj = {
    body: "hello world",
    id: 123


First off you will have a array that you loop through, in this example we have updatelist. Fill this array with your objects that you want to pass to the Firestore. Here we only have one object in our array as you can see. My properties for my object are body and id.

thisRef = db.collection('mycollection').doc(;

With the help of the id property, I will name the Firestore document if doesn’t exist and create it, and if it exists, it will be updated because we have that document id reference. The actual data of the object gets applied at the batches[commitCounter].set(thisRef, { …a }). line.

Then the rest is pretty self explanatory, we create as many batches as possible, and we will execute (commit) them in the for loop one by one when we have the batches ready filled with write queries.

0 0 votes
Article rating
Notify of

Inline Feedbacks
View all comments