What is the difference between "Transactions" and "Batched-Writes" if only perform a mutation operation in Firestore?

Solution 1:

A batch write either succeeds in all the operations, or fails them all, with no chance for recovery if there is a failure. You can't apply any conditional logic here.

A transaction, like a batch, either succeeds fully or fails fully with all the documents that change in the transaction. The difference is that you can apply logic to the contents of the documents you want to interact with atomically before you decide what you want to do with them. That's what the transaction handler is for. Your transaction handler callback can get each document and examine it to decide what you want to do before you do anything at all. The reads and writes are all atomic, and the transaction is retried if there is a collision with other clients also transacting on the same documents.

If you don't need to examine the document prior to changing it, then there is no need for a transaction. Just use a batch write. But you can certainly use a transaction if you want. If you use a transaction, you are incurring the cost of an extra round trip with the database to satisfy the transaction, which takes extra time and bandwidth. That is the main difference.