Cloudstore data mapping function with cloud functions

We currently have data that is written to Google Firebase as follows.

Someone / People determines that they are “available” along with additional criteria. After sending, they are added to the table / data warehouse.

We need to regularly query this data store for matches. For example, in this data warehouse / data table there may be 1000 people / records. We need to take two records, use the data in another place and delete them from this table / storage.

We need to do this very often, as it is a dating / search service. Therefore, every second we need to run a few searches.

How do we provide parallel data? As at any moment, the same record is not performed by the function performed by the search.

+5
source share
3 answers

Transactions should work for you. Transactions are built specifically for this purpose to ensure proper processing with simultaneous connections or requests.

Official link: https://firebase.google.com/docs/reference/js/firebase.database.Reference#transaction

+2
source

Transactions guarantee that two parallel functions will not simultaneously update your data and allow them to be in an inconsistent / intermittent state. You must use them for your use.

However, using a transaction when interacting with an entity will not prevent it from appearing in the search results of another function. According to your last sentence, I understand what you are trying to achieve. I would suggest adding a “available” boolean field, for example, to your data to reflect its availability and make search functions look for data with this field set to True. The first function that retrieves it can set it to False (in a transaction) so that subsequent search functions do not display it.

0
source

Your question is pretty clear. To ensure atomicity, you need Transactions . Instead of using set() to update / insert fields, you should use transactions() , which changes the existing value to a new value atomically different from set() , which simply overwrites the data. If, while writing your field, any other client modifies the data, your or other transactions will continue to retry until it succeeds with the new current value. (It stops retrying only if you abort the transaction.)

Also, as an important note from firebase, modifying data with set() will cancel any pending transactions at that location, so you should be especially careful when mixing set() and transaction() to update the same data.

0
source

Source: https://habr.com/ru/post/1275585/


All Articles