firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Batch operation limits are not enforced in emulator

Open pflammertsma opened this issue 5 years ago • 1 comments

Description

There's a limit of 500 operations when submitting a batch operation on Firestore. However, the emulator doesn't enforce this limit and will allow batch operations containing many more than 500 writes, for instance.

Especially because it's unclear when something counts against this limit (e.g. creating a Timestamp), this inevitably will lead to surprises in production as local tests don't fail on the emulator.

[REQUIRED] Environment info

firebase-tools: 8.16.1

Platform: Windows 10

[REQUIRED] Test case

exports.test = functions.https.onRequest((req, res) => {
    var batch = db.batch();
    for (var i = 0; i < 500; i++) {
        var ref = db.collection("collection")
            .doc("doc" + i);
        batch.set(ref, {
                timestamp: admin.firestore.FieldValue.serverTimestamp()
            });
    }
    batch.commit()
        .then(function () {
            console.log("success");
            res.status(200).send('ok');
        })
        .catch(err => {
            console.error("failure", err)
            res.status(500).send('failure');
        });
});

[REQUIRED] Steps to reproduce

  1. Start a Firestore and Firebase Functions emulator.
  2. Invoke the HTTPS request.

[REQUIRED] Expected behavior

  1. The function should fail with response code 500, because the batch limit is exceeded, noting from the documentation:

    A batched write can contain up to 500 operations. Each operation in the batch counts separately towards your Cloud Firestore usage. Within a write operation, field transforms like serverTimestamp, arrayUnion, and increment each count as an additional operation.

[REQUIRED] Actual behavior

  1. The function succeeds on the emulator, despite exceeding the 500 operation limit. However, the function will fail in production.

pflammertsma avatar Nov 24 '20 20:11 pflammertsma

Since march 29, 2023, Firestone no longer limits the number of writes that can be passed to a Commit operation :

https://cloud.google.com/firestore/docs/release-notes#March_29_2023

PsyOhm23 avatar Feb 15 '24 00:02 PsyOhm23