Search code examples
javascriptfirebasefirebase-realtime-database

Guaranteed order of updates in Firebase real-time database for multiple paths


I am struggling to find the answer to my question, I was reading through the docs, but couldn't find any reference related to this.

I have a server that sends updates to my Firebase Realtime Database. For example I am sending 3 separate write requests that updates 3 different paths

set(ref(db, 'location/A'))
set(ref(db, 'location/B'))
set(ref(db, 'location/C'))

if I then set listeners in the same order in my front-end application, is it always guaranteed that I will receive responses in the same order as they were created in the server e.g A,B,C or there is always a chance of a race condition?


Solution

  • The Firebase SDK sends the write operations to the server in the order in which it receives then, and the Realtime Database executes the operations in the order in which it receives them. Since changes are also broadcast to any listeners when they happen, there are no ways for their order to be inverted.


    That said, there is no guarantee that each write operation will be seen by each client.

    Specifically: if one or more clients write to the same path repeatedly, it is possible (and relatively common) for some listeners to not see some of the state changes, but only get the "final" state of the path.

    So in the example you specified, a client listening to the top-level location node, might only see one update for all three writes - the one with the final state. This becomes especially important when later write operations are modifying/overwriting the data of earlier operations.