Data Matching Function in Cloud Firestore with Cloud Functions

时间:2018-02-26 17:48:01

标签: firebase google-cloud-firestore google-cloud-functions

We currently have data that is written to Google Firebase in the following fashion.

Someone/People determines they are 'available' along with additional criteria. Once submitted they are added to a table/store of data.

We need to regularly query this data store for matches. For example, there might be 1000 people/records in this data store/table. We would need to take two records, use the data elsewhere and remove it from that table/store.

We need to do this very often as its a matchmaking/search service. So every second we need to run multiple searches.

How do we ensure concurrent data? As in at any time the same record is not held by the function running searches.

2 个答案:

答案 0 :(得分:2)

交易应该适合你。事务是为此目的而构建的,以确保使用并发连接或请求进行正确处理。

官方参考: https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction

答案 1 :(得分:0)

你的问题很清楚。为确保原子性,您需要Transactions。您不应使用set()更新/插入字段,而应使用transactions(),它会将现有值更改为原始值,而不像set()那样只会覆盖数据。如果在编写字段时,任何其他客户端正在修改数据,那么您或其他事务将继续重试,直到它以新的当前值成功为止。(仅当您中止事务时它才会停止重试。)

此外,作为firebase的一个重要说明,使用set()修改数据将取消该位置的所有待处理交易,因此如果将set()transaction()混合更新,则应格外小心相同的数据。