Reputation: 292
Does Firebase real-time database offer strong consistency?
We are using Firebase real-time database in some cases as a queue:
item
document to /items
/items
and calls Server B via POST /processItem/:itemID
items/:item
and starts processingitem.processedAt = <timestamp>
Scenario 1 logic seems to work fine, however, we started wondering if Firebase actually offers strong consistency and whether we can be sure that when Server B fetches items/:itemID
from Firebase, the document certainly exists.
item
document to /items
and waits for the .set()
operation to go through.POST /processItem/:itemID
items/:itemID
and starts processingitem.processedAt = <timestamp>
This potentially is even more "risky" as Server A may call Server B potentially even before child_added
event would have fired.
Are both of these scenarios safe?
Upvotes: 1
Views: 1040
Reputation: 56
Bhavin Panara is correct. Both scenarios are "safe" in the way you've described. Once a write is acknowledged, the Realtime Database guarantees that any subsequent reads will see the result of the acknowledged write. This is also true of a Cloud Function being triggered by a write.
As you've noted, listeners are updated asynchronously, so a client listening to a location may not be updated about a write for some time.
Note that it is still possible to have race conditions even with a strongly consistent database. Consider the following scenario:
/items/item1
/items/item1
/items/item1
, but the item has been deleted.In these kinds of scenarios it may be helpful to fall back on transactions.
Upvotes: 2
Reputation: 448
IMHO both of these scenarios can be safe.
Scenario 1 is completely safe. Because cloud function is invoked only when firebase has written the data completely. So, you are definitely safe in scenario 1.
For scenario 2. You can go this way.
item
document to /items
waits for the callback
of completion from firebase.POST /processItem/:itemID
items/:itemID
and starts processingitem.processedAt = <timestamp>
Upvotes: 2