什么时候应该使用Firebase交易 [英] When should you use Firebase Transactions

查看:190
本文介绍了什么时候应该使用Firebase交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,由于Firebase是一个实时数据库,我认为Firebase交易可以对某个值进行原子更新,因为它的旧值和新值是

我们必须谨慎地使用交易而不是实时特征。

这里是一个例子:

$ ol

  • 据我所知,如果你正在对一个数值进行一些数学操作(加入喜欢或相当的数值),那么使用事务是有意义的

  • 我不明白在以下用例中使用事务是否合理:假设任何数量的用户都可以更新文本字段,并且我们对所有更新都感兴趣,因为它们是实时发生的。 Firebase建议我们在这种情况下使用交易吗?或者是最终的持续运营,只发生在Firebase服务器时钟的每个时间戳粒度的单个持续运营上?

    进一步保证这些事件将按照最终值的持续顺序传递。无论何时在数据库上使用事务,都会牺牲一些可伸缩性,以便获得更强大的数据一致性保证。解决方案Firebase数据库中的交易没有什么不同,除了开发者倾向于在更高度并发的情况下使用Firebase。

    使用任何交易系统:保持系统可扩展性的关键是为了尽量减少竞争更新相同数据的客户端的数量。在Firebase的情况下,您可以通过在JSON树中尽可能少地运行事务来实现这一点。即一个柜台就是一个可以在交易中很好地工作的例子。

    对于较大的数据部分,例如文本编辑示例,使用事务将不能很好地扩展。对于这种使用情况,最好找到一种方法来完全避免冲突。通常这归结为存储每个用户正在做的delta,而不是存储更新的状态。一个很好的例子就是 Firepad示例,它使用操作转换在顶部创建高度并发的协作编辑器的Firebase数据库。


    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 assume, one must use transactions only sparingly and not in 'realtime features'

    here's an example:

    1. I understand that if you are performing some mathematical operation on a value (adding 'likes' or equivalent), it makes sense to use transaction

    2. I dont understand if it makes sense to use transactions in the following use case: Say a text field can be updated by any number of users, and we are interested in all the updates, as they occur in real time. Does firebase recommend we use transaction in this case? Or is the final 'persist operation' that occurs on the value, only limited to a single 'persist operation' per timestamp granularity of the Firebase server clock?

    Further is it guaranteed that the events will be delivered in the order of which the final values were persisted?

    解决方案

    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.

    这篇关于什么时候应该使用Firebase交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆