Downside of using transactions in google firestore

我们两清 提交于 2020-07-10 09:18:51

问题


I'm developing a Flutter App and I'm using the Firebase services. I'd like to stick only to using transactions as I prefer consistency over simplicity.

await Firestore.instance.collection('user').document(id).updateData({'name': 'new name'});

await Firestore.instance.runTransaction((transaction) async {
      transaction.update(Firestore.instance.collection('user').document(id), {'name': 'new name'});
    });

Are there any (major) downsides to transactions? For example, are they more expensive (Firebase billing, not computationally)? After all there might be changes to the data on the Firestore database which will result in up to 5 retries.

For reference: https://firebase.google.com/docs/firestore/manage-data/transactions

"You can also make atomic changes to data using transactions. While this is a bit heavy-handed for incrementing a vote total, it is the right approach for more complex changes."

https://codelabs.developers.google.com/codelabs/flutter-firebase/#10


回答1:


With the specific code samples you're showing, there is little advantage to using a transaction. If your document update makes a static change to a document, without regard to its existing data, a transaction doesn't make sense. The transaction you're proposing is actually just a slower version of the update, since it has to round-trip with the server twice in order to make the change. A plain update just uses a single round trip.

For example, if you want to append data to a string, two clients might overwrite each other's changes, depending on when they each read the document. Using a transaction, you can be sure that each append is going to take effect, no matter when the append was executed, since the transaction will be retried with updated data in the face of concurrency.

Typically, you should strive to get your work done without transactions if possible. For example, prefer to use FieldValue.increment() outside of a transaction instead of manually incrementing within a transaction.

Transactions are intended to be used when you have changes to make to a document (or, typically, multiple documents) that must take the current values of its fields into account before making the final write. This prevents two clients from clobbering each others' changes when they should actually work in tandem.

Please read more about transactions in the documentation to better understand how they work. It is not quite like SQL transactions.




回答2:


Are there any (major) downsides to transactions?

I don't know any downsides.

For example, are they more expensive (Firebase billing, not computationally)?

No, a transaction costs like any other write operaton. For example, if you create a transaction to increase a counter, you'll be charged with only one write operation.

I'm not sure I understand your last question completely but if a transaction fails, Cloud Firestore retries the transaction for sure.



来源:https://stackoverflow.com/questions/58660770/downside-of-using-transactions-in-google-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!