Search code examples
androidfirebasekotlinfirebase-realtime-database

Firebase RTDB: How do multiple queries handled under the hood?


My case is the following. I make two calls like ref.get() at the same time. The first call asks for a bit larger piece of data (5MB), than the second one (100KB). How Firebase SDK for Android will manage these calls: sequentially, or in parallel manner? Thanks.


Solution

  • How Firebase SDK for Android will manage these calls: sequentially, or in a parallel manner?

    If you perform two Firebase Realtime Database calls at the same time, the calls are performed in parallel. In your case, because there is a huge difference in the amount of data you download, the second request will be completed before the first one. Remember that Firebase is able to pipeline the requests.

    If you want the operations to be sequentially, then you have to wait for the first operation to complete, and right after that perform the second operation.

    Edit:

    I think that Doug refers to the fact that two queries cannot start at exact same time, it's one after another, in the order that the queries were requested. However, as soon as the queries start, both will get the data in parallel. This means that once the two queries start, both will work in parallel to return the data. The second query doesn't wait for the first one to complete. The Firebase Realtime Database is designed to handle multiple requests in parallel. The actual execution time of each request may vary based on factors such as network latency, or the complexity of the request.

    So when you call ref.get() twice, the Firebase SDK initiates separate requests for each reference. However, one is registered first and the second one is registered right after the first one. These requests will run in parallel until their work is done. This also means that none of the queries will wait for each other to finish. Furthermore, the SDK doesn't guarantee which request will be completed first, even if one fetches more data.

    So once the two queries start, both will get the data from the Firebase Realtime Database concurrently (in parallel).