I know this question has been asked before and the firebase official documentation also suggests that both these have exactly the same effect.
However I have confusion in perceiving both as same because of a few things:
Considering the above points, I feel that transactions are more safe for updating counters particularly in a case where for example multiple clients can update a single counter, so if the above assumptions are true FieldValue.increment() will fail whenever two or more clients try to update the counter within a span of one second. But doing the same with transactions there will be the advantage of automatic retrying upon abortion due to concurrency.
So what I want to know is that are my assumptions correct? If not then in what way exactly the above 3 things are done? Also considering the example of multiple users updating a single counter, is there an advantage of using transactions over FieldValue.increment().
Firestore now has a specific operator for this called FieldValue. increment() . By applying this operator to a field, the value of that field can be incremented (or decremented) as a single operation on the server.
A FieldValue is a union type that may contain a primitive (like a boolean or a double), a container (e.g. an array), some simple structures (such as a Timestamp ) or some Firestore-specific sentinel values (e.g. ServerTimestamp ) and is used to write data to and read data from a Firestore database.
There are two types of atomic operations in Cloud Firestore: Transactions: a transaction is a set of read and write operations on one or more documents. Batched Writes: a batched write is a set of write operations on one or more documents.
Unlike transactions, there is no round trip required between the client and server for a conflicting increment operation. That's because there is no need for the client to recompute the new value. The server is well aware that it is only going to be an increment of an existing field of a single document, and it can retry internally on its own.
In the event that a limit is exceeded, the write operation will fail, just like any other write operation that exceeds a limit. There is nothing special about increments in this respect. Bear in mind that two rapid changes to a document will not necessarily fail outright. The limit of 1 change per second is a sustained limit. The system can handle a few rapid changes over a short period of time, but that can't be sustained indefinitely. Again, this applies to all writes, not just increments.
No, each transaction will drive independently to creation. It doesn't matter if there are any "identical" transactions (in whatever way you define that).
There is no advantage to using transactions over increments if all you're doing is updating a single document. However, you are required to use a transaction if you have updates across multiple documents that must be atomic.
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