1

I make new documents multiple times without checking the success of the write:

for element in data {
   val document = firestore.collection("test")
   document.set(element)
}

Sometimes data contains hundreds of elements and I have discovered that in some rare cases not every document is uploaded to the cloud (e.g. 5% are not successfully uploaded, but only the last elements).

Is there any limit for multiple writes? Should I wrap it into the batch?

Nominalista
  • 4,632
  • 11
  • 43
  • 102

1 Answers1

5

Yes, there are limits on Firestore performance for a large number of writes.

  • There is a one update per second limit to a single document. In this case, it would definitely be better to write the document all at once rather than one value at a time (plus, it would cost a lot less). The documentation notes that this limit is somewhat soft but recommends strongly against exceeding it.

  • If you are writing in a narrow document range (or with a new collection), you may also run into performance problems. Make sure you are either using automatic ids or are scattering them around the range.

Finally, there are some flat out limits to writes, including a 500 per second write rate to a collection in which documents contain sequential values on an indexed field.

Based on another recent question batching is at best a wash against parallel writes, and does add complexity, but it is faster than doing sequential writes. That answer also suggests not using the client SDKs for bulk writes (e.g. use the Admin SDK).

robsiemb
  • 6,157
  • 7
  • 32
  • 46
  • Thanks for your answer, it explains a lot. I am still wondering, what to do if you want to create more than 500 documents based on user data located on a device? Separate them into batches with 500 elements and wait one second before committing next batch? – Nominalista Nov 23 '19 at 23:08
  • I haven't actually tried to do this, but yes, some form of throttle is how I would approach it-- note the [guidance on how to ramp up traffic](https://cloud.google.com/firestore/docs/best-practices#ramping_up_traffic) as well. Likewise, I'd want to make sure the keys were auto-generated (or at least not sequential) if at all possible, and avoid sequential indexed fields. – robsiemb Nov 23 '19 at 23:12