I have the following code in a cloud function which is returning an error with the message
Error: 3 INVALID_ARGUMENT: maximum 500 writes allowed per request
console.log(`${projectId} doClassifySources: Got ${_.size(output)} items`)
const lastClassification = new Date().toJSON()
const batch = firestore.batch()
batch.update(projectRef, {lastClassification})
_.forEach(output, item => {
batch.set(projectRef.collection('output').doc(encodeURIComponent(item.url)), {
classifiedAt: admin.firestore.FieldValue.serverTimestamp(),
...item
}, {
merge: true
})
})
return batch.commit().then(() => lastClassification)
Yet the firebase logs show this right before the error is thrown:
12:28:19.963 PM classifySources ZklZYB5hq96J43CroKgP doClassifySources: Got 310 items
That looks to me like the batch should contain 310 items, well below the 500 limit. Am I missing something in how that 500-item limit is calculated? Does the merge: true
influence that in any way? Is it to do with the spread of item
in the object being written (i.e. does that increase the number of writes needed)?
Each transaction or batch of writes can write to a maximum of 500 documents.
10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.
The Firebase Limits does not state any limits on collections, but there is a limit on the depth of sub-collections. The limit is that you can only go 100 subcollections deep, which is very large and you should never reach that point unless you have the most detailed and specific app in the world.
This means that Cloud Firestore will try to retrieve an up-to-date (server-retrieved) snapshot, but fall back to returning cached data if the server can't be reached. This is how we can reduce the number of reads in Cloud Firestore, by forcing the SDK to use only the offline cache and get the data only upon request.
With some additional testing, it appears that using admin.firestore.FieldValue.serverTimestamp()
counts as an extra 'write'. With the code as above, I'm limited to 249 items (i.e. when output.length >= 250
, the code will fail with that error message. If I remove the reference to serverTimeStamp()
, I can get up to 499 items per write.
I can't find this documented anywhere, and perhaps it is a bug in the firebase library, so I will post an issue there and see what happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With