是否按与生成顺序相同的顺序将本地写入提交到Firebase [英] Are local writes committed to firebase in the same order as they were generated

查看:65
本文介绍了是否按与生成顺序相同的顺序将本地写入提交到Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用不稳定的Internet连接上的nodejs firebase sdk将本地更新推送到IoT板上的数据集.该连接可能会丢失大量数据包.

We are pushing local updates to a dataset on an IoT board using a nodejs firebase sdk on flaky internet connection. The connection is mar with lot of packet losses.

现在假设将UpdateA推送,然后将UpdateB推送到firebase(使用set()操作).由于数据包丢失,UpdateA无法将其连接到Firebase服务器,但UpdateB可以使它连接.

Now supppose UpdateA is pushed followed by UpdateB to firebase (using the set() operation). UpdateA fails to make it to Firebase servers due to packet losses but UpdateB makes it.

在这种情况下会发生什么?firebase将提交UpdateB并中继到所有连接的客户端,然后在获取UpdateA时对其进行相同的操作吗?还是Firebase服务器会确保所有写入均按照与本地服务器上发生的顺序相同的顺序提交,并按相同的顺序进行中继?

What will happen in this case? Will firebase commit UpdateB and relay to all connected clients and then when it gets UpdateA, it will do the same for it? Or will Firebase server ensure all writes are committed in the same order as they happened on the local server and relay them in the same order as well?

注意:UpdateA和UpdateB发生在同一服务器上

Note: UpdateA and UpdateB have happened on the same server

更多细节:

我正在IoT设备上的nodejs服务中使用Firebase js sdk.该设备的互联网连接不稳定,可以关闭电源而不会发出警告.将部署数千台设备,并且它们之间必须同步某些数据集(用户数字钱包).由于这种情况,我正在使用mongodb实现本地脱机持久性缓存.钱包的所有更新都首先保存在数据库中,然后将其推送到Firebase.现在,我按更新顺序将所有更新顺序地推送到钱包到Firebase.由于将触发set操作的回调,因此我会记下最后一次成功的全写操作的更新时间戳.现在,如果设备关闭并在一段时间后打开,我可以继续将本地更新从更新的时间推送到Firebase.

I am using Firebase js sdk in a nodejs service on an IoT device. The device has flaky internet connection and can be powered off without warnings. Thousands of these devices will be deployed and they have to sync certain dataset (user digital wallets) between them. Because of this conditions, I am implementing a local offline persistent cache using mongodb. All the updates to the wallet are first persisted in the db and then they are pushed to firebase. Now I push all updates to wallets serially to firebase in the order they are updated. And as callbacks for set operation will fire, I will note down the updated timestamp of the last success full write operation. Now if the device shuts off and is turned on after sometime, I can continue pushing the local updates to firebase from the updated time.

但是,只有在Firebase保证我执行写操作的顺序与它们将被提交到Firebase服务器的顺序相同时(即,也将触发set操作的回调的顺序),此逻辑才起作用.

However, this logic works only if firebase guarantees that order in which I do write operations is the same order they will be committed to firebase server i.e. in the same order the callbacks of the set operation will be fired as well.

推荐答案

此处的Firebase工程师

请参见事件保证:

来自单个客户端的写入将始终被写入服务器,并按顺序广播给其他用户.

Writes from a single client will always be written to the server and broadcast out to other users in-order.

如果您使用的是Firebase SDK,并且与Firebase服务器的连接丢失,则SDK将保留仍需要发送的本地写入操作队列.恢复与服务器的连接后,Firebase客户端将以正确的顺序发送挂起的写入操作.

If you're using a Firebase SDK and the connection with the Firebase server is lost, the SDK will keep a local queue of write operation it still needs to send. When the connection to the server is restored, the Firebase client sends the pending write operation in the correct order.

因此,在您的情况下,UpdateA将在该队列中,并在恢复连接后发送到服务器.

So in your case, UpdateA will be in that queue and sent to the server when the connection is restored.

但是,如果您在写入数据之间存在依赖关系,请考虑使用事务.这样,您的 set()操作可以根据给定属性Y的该值X,将其设置为Z"来指定.

But if you have dependencies between data in your writes, consider using a transaction. That way your set() operation can be specified in terms of "given this value X of property Y, set it to value Z".

这篇关于是否按与生成顺序相同的顺序将本地写入提交到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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