How reliable is Firestore as an offline persistence mechanism?

martinomburajr picture martinomburajr · Dec 11, 2017 · Viewed 7.5k times · Source

I am currently using Firebase Firestore as a primary backend that retrieves data from a variety of sources. I also use Android's Room for my mobile backend. When the phone receives data it is stored in the Room database in the event the user will not go online again for days even weeks.

After looking through the device files, I see firestore saves the data in files under the /data/data/<your-app>/databases directory.

enter image description here

The file looks something like this enter image description here

I have read the offline persistence docs on the firestore and there is no indication on how durable the offline persistence is It mentions that the data is cached but not for how long. My question is, what is the durability of Firestore's offline persistence. Would one recommend using it instead of having a fully-fledged local DB to store data that may not be synced over long periods of time (days,weeks)?

It seems to already handle syncing data well once a connection is re-established. Im just worried that after some point that file may be deleted by the system and the user loses everything.

Answer

Sam Stern picture Sam Stern · Dec 12, 2017

On Android (as of this writing) Firestore uses SQLite as a persistence mechanism. So for intermittent periods of offline activity you should have no problems with performance or durability.

However if you are going to be offline for days or weeks (as you said) there are some things you should be aware of:

Performance

Because Cloud Firestore is meant to be used mostly online, pending writes that have not yet been synced to the server are held in a queue. If you do many pending writes without going online to resolve them, that queue will grow and it will slow down your overall read/write performance. Most of Cloud Firestore's performance guarantees come from indexing and replication on the backend, but most of those optimizations don't exist when you're operating offline-only.

Conflicts

Firestore's basic conflict resolution model is "last write wins". So if you have many offline clients writing to the same document, only the last one to come online will actually "win" and persist their change.

Features

Most of Firestore's features work offline with one major exception: transactions. Transactions can only execute when you are online. So if your app uses transactions it will not work properly offline without some special handling.