When should you use Firebase Transactions

前端 未结 1 990
余生分开走
余生分开走 2020-12-14 02:12

I understand that Firebase transactions enable the atomic update of some value, given its old value and new value

But given that Firebase is a realtime database, I a

相关标签:
1条回答
  • 2020-12-14 03:10

    Whenever you use transactions on a database, you sacrifice some of your scalability in order to have a stronger data consistency guarantee. Transactions on the Firebase Database are no different, except maybe that developers tend to use Firebase in more highly concurrent situations.

    With any transactional system: the key to keeping the system scalable is to minimize the number of clients contending to update the same data. In the case of Firebase, you'd accomplish that by running your transaction as low as possible in your JSON tree. I.e. a counter is a example of something that could work well under a transaction.

    For larger pieces of data, such as your text editing example, using a transaction will not scale well. For such use-cases it is better to find a way to avoid the conflict altogether. Quite often this boils down to storing the delta that each user is making, instead of storing the updated state. A great example of this is in the Firepad example, which uses operational transform to create a highly concurrent collaborative editor on top of the Firebase Database.

    0 讨论(0)
提交回复
热议问题