Reputation: 3081
I have been reading the documentation for transactions and bacthed operations in Firestore but I think I am not understanding this correctly. An atomic transaction is an indivisible and irreducible series of database operations such that either all occur (for example, updating multiple documents that depend on each other with the state of the system), or nothing occurs, and I was developing supposing that batched writes will also provide isolation.
In other words, what I am trying to ask is, how to handle the situation of simultaneous users writing the same document? That is, how to make the data update correctly in order for it, correctly handling concurrency.
The truth is that I don't know if I'm getting it right.
For example, if a user wants to enter a room where there can only be two people (room.size < 2). Applying a transaction (get room + update size) would guarantee that size always takes the expected values and is updated simultaneously? I thought that yeah, but now I am not sure, because of what I said in the first paragraph.
Or is the CID from ACID provided by default on all Firestore operations?
Upvotes: 4
Views: 2477
Reputation: 317542
You will want to use a transaction to ensure consistency between concurrent writes to a document. The transaction will ensure that writes are serialized, and that each subsequent write sees the contents of the document just before it's written.
A batch write will ensure that writes are serialized, but if you need to compute the new value of a field in the document based on the prior contents, the batch write will not help you - you should use a transaction for that. (The exception to this is FieldValue.increment - those are processed correctly by batch writes in a sort of "mini-transaction" that ensures an atomic increment.)
Upvotes: 3