Reputation: 2661
In the documentation it says:
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.
But does this include documents creation/deletion?
Imagine this situation where a user can re-create/delete two documents "A" or "B" when pressing the buttons "Action A" or "Action B".
The creation/deletion of both documents happen in parallel.
Both documents are added/removed to/from the same collection.
(SEE THE IMAGE I HAVE ADDED WHILE READING)
When a document is created/deleted from this collection, a trigger function start executing the following code:
if(docB has been deleted) {
if(docA exists) {
// Create a document... other operations
console.log("HERE")
}
}
/* else if(docA has been created) {
...
} */
The problem comes with this situation:
Initial state: (second 0)
- docA is not created
- docB is created
Both actions running in parallel:
-started Action A (second 0)
-started Action B (second 0)
-finished Action B -> docB has been deleted (second 1.1)
-finished Action A -> docA has been created (second 1.11)
As you can see this is a concurrency problem caused because of pressing two buttons too fast in the frontend (or network latency, ...).
Can this problem be solved by wrapping the algorithm in a "database transaction"? I mean, will the transaction be re-executed (or something) if the document on whose existence the algorithm depends has been created/deleted before the transaction finishes?
Pd: I have also tried checking all possible states in the onTrigger function... but the problem is that two instances of the same function can be running on parallel...
Any ideas? Thanks.
Upvotes: 1
Views: 327
Reputation: 600100
does [Firestore's transaction mechanism] include documents creation/deletion?
Document creation and deletions are modifications of the document, so are included in the guarantee that you quote.
Upvotes: 2