0
votes

Could anyone share some knowledge on how exactly firebase mobile client knows what data to download from the server in case network connection is lost and re-established?

Let's say that:

  • There was an object in the database with keys key1 and key3.
  • Mobile client downloaded them and went offline.
  • In the meantime, key2 and key4 were added
  • The client restores the connection and receives somehow key2 and key4.

How exactly does it know what has changed?

  • Does it download the entire collection again?
  • Does it somehow exchange the keys of all objects to compare what it missing?
  • Does the server memorize which clients have what?
  • Anything else?
1
Can you clarify downloaded them? i.e. if you observe the node one time (observeSingleEvent) to 'download' them then the client wouldn't know about changes even if it was online. Likewise, if you are watching a node for deletions, and two children were added, then again, the app wouldn't be notified even if it was online. - Jay

1 Answers

1
votes

The server keeps track of the active listeners for each client, but it does not track what each client knows.

Instead when you attach a listener to a location that is in your local cache, the client calculates the composite hash keys of all branches of the cache data. It sends those hashes to the server, which performs the same hashing on the actual data. If any of the hashes is different, then data was modified and so it it sent back from the server to the client.


Note that this hashing/exchange only happens for data that you have cached and attach a listener to. Any cached data that you are not listening to does not get this treatment. Since you typically add listeners to the different parts of your data over the lifetime of your app, this cost of the hashing and synchronizing spreads out over the lifetime of the app. The only times where I have seen a less benevolent behavior is where developers had called keepSynced(true) on the root of their database, which essentially forces a "giant sync" when your app starts. In that scenario the hashing of the local cache and its synchronization may take non-trivial time, memory and bandwidth.