How do Transactions Work? | Get to know Cloud Firestore #8
Why Stack Overflow recommends it
Stack Overflow users link it when explaining Firestore concurrency, such as why transactions retry on concurrent edits, how server-side locking behaves, and why batched writes don't give isolation.
Recommended by 4 people across 4 different months, 2019 to 2024.
What it is
An episode of Google's 'Get to know Cloud Firestore' series, presented on the Firebase channel, that explains what database transactions are and how they prevent race conditions. It compares client-side and server-side transactions in Cloud Firestore and contrasts them with batched writes.
What people said
Recommends Renaud Tarnec · 2020-06 · Stack Overflow · on “Firebase concurrent read/write”
If I understand correctly your question, what you are describing is the correct behaviour of a Firestore Transaction [LINK] executed with one of the Firestore Client SDKs: You are calling twice a function that aims at decrementing a counter; At the end, the counter is decremented twice. The fact that your two calls are executed "at the same time" should not change the result: The counter should be decremented twice. This is exactly what the Transaction ensures: In the case of a concurrent edit, Cloud Firestore runs the entire transaction again, ensuring that the initial doc ( db.collection('X').doc('SF') in your case) has not changed during the Transaction. If it is the case, it will retry t
Recommends Peter Palmer · 2021-06 · Stack Overflow · on “Firestore - Deleting lot of documents without concurrency issues”
Maybe you are looking for transactions check out the documentation out here: [LINK] Or watch the YouTube video that explains the concurrency problem and the differences between batched writes and transactions : [this video]
Recommends Divyani Yadav · 2021-11 · Stack Overflow · on “How to prevent Firestore write race conditions for a reservation button”
As this is a race condition, You have already acknowledged the use of Transactions for the update which is the most desirable as this can ensure the update is successful before allowing the App to change button status. I.e. by using a transaction and only updating the UI Button state on success, which is explained here [this video] The recommendation is to keep the state of the button mapped to what is in the document, therefore you are likely to exceed rate limits [LINK] by updating the same field continuously based on the flipping of the button. Another way to handle this tracking of the state of enrollment is to add a new document that indicates the state of the enrollment for the user to a co
Recommends Brian · 2024-03 · Stack Overflow · on “Firestore unique index or unique constraint?”
Contrary to the accepted answer, I would recommend against using batched writes for updating separate User and Index collections - this Firestore video [this video] conveys that batched writes do not provide a guarantee of isolation - i.e., documents that a batched write touches can be modified by other requests while the batched write is being executed. Thus we could potentially run into a race condition where two users attempt to choose the same username that doesn't exist yet, and both of their batched writes succeed. (Note also that in the accepted answer, getAfter retrieves the state the database would be in after the batched writes have completed, whereas we want to check whether the userna
Cites kcilc · 2019-05 · Stack Overflow · on “Server Side Locks in Cloud Firestore”
I'm curious about the behavior of the locks that are performed when doing server side transactions on Cloud Firestore as mentioned in this video: [this video] My transaction will be reading multiple documents and placing locks on them. My question is do these locks restrict all access to the documents - including concurrent reads from client code that isn't part of a transaction? Or do they only restrict writes? If they do restrict reads is there any way around this - it could lead to severe slowdown in the app I'm working on. Also in the case that a transaction tries to lock documents that are already locked - what is the retry pattern - how often does it retry, and is there an exponential backo
Cites Marcelo Glasberg · 2019-06 · Stack Overflow · on “Is a Firestore document locked when read by the server (cloud-function) in a Transaction?”
I know Firestore uses locks for Transactions in the SERVER (cloud-functions), as explained by this video: [this video] The video explains in detail how Transactions work in the CLIENT (and it's not using locks there), but doesn't do such a good job for SERVER Transactions. Which of these is true (about SERVER Transactions)?: 1) A document is locked by get s in a server Transaction, so that other server Transactions cannot get it before the first Transaction is finished. 2) A document is locked by get s in a server Transaction, but only for writes, so that other server Transactions can get it but not modify it before the first transaction is finished. 3) A document is NOT locked by get s in a serv
Cites lenz · 2020-07 · Stack Overflow · on “Can I read from a query matching multiple documents Firestore Transaction in Flutter”
I'm trying to read from a query and NOT from an individual document within a transaction. My write is dependant on the result of the current data in the collection. But I seem to understand that, at least for the mobile and web clients, you are not allowed to read from queries, only from individual document reads. If that's the case, how would I achieve that since I don't know what documents I want to read ahead of time? Specifically, I'm referring to this video from the Firebase team [this video] , at 9:38
Cites dsl400 · 2022-02 · Stack Overflow · on “what and how about firestore document field updates in batch”
Documentation says that writing to firestore documents is limited to 1 second per document. Updating different fields of the document fast enough could cause data loss if device A is updating field A and device B is updating field B in the same time ? Will a batch that updates multiple documents on one or many fields fields fail if one of the documents is busy ? Does a batch that update document fields fail if one of the documents is busy ? If the batch update is used offline, what happens if the one of the documents included in the batch is busy when the user gets back online ? in this video [this video] it is stated that hybrid state of the data is impossible when using batches but if a batch w
Cites Sathi Aiswarya · 2022-04 · Stack Overflow · on “How to keep data consistent when sync documents with cloud firestore triggers?”
Using the Cloud Firestore client libraries, you can group multiple operations into a single transaction. Transactions are useful when you want to update a field's value based on its current value, or the value of some other field. A transaction consists of any number of get() operations followed by any number of write operations such as set(), update(), or delete(). In the case of a concurrent edit, Cloud Firestore runs the entire transaction again. For example, if a transaction reads documents and another client modifies any of those documents, Cloud Firestore retries the transaction. This feature ensures that the transaction runs on up-to-date and consistent data. Transactions are a way to
Cites muhammed koç · 2023-05 · Stack Overflow · on “Firestore Transactions and data contention about questions”
I try Android apps Kotlin , I have two data updating devices, My one device Firestore increase number field by 40, my other device increases Firestore number field by 30, My first device: authFireStore.runTransaction { transaction -> var docRef=refCacheTransaction.collection("test").document("doc") var dataDoc=transaction.get(docRef) updateMap.put("test",(dataDoc.get("test") as Long + 40 )) //increment field 30 number transaction.update(docRef,updateCacheDBDataCollection) return@runTransaction null } My second device: authFireStore.runTransaction { transaction -> var docRef=refCacheTransaction.collection("test").document("doc") var dataDoc=transaction.get(docRef) updateMap.put("test",(dataDo