Firestore clients: To cache, or not to cache? (or both?)

Doug Stevenson
Firebase Developers
7 min readMay 3, 2020

--

The Firestore client SDKs have this handy feature called offline persistence (currently for Android, iOS, and web only). It helps your app built with Firestore to be usable without an internet connection, as far as database access is concerned. From the documentation:

This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data. When the device comes back online, Cloud Firestore synchronizes any local changes made by your app to the Cloud Firestore backend.

While that sums it up pretty nicely, there are some nuances about the way this works that you should be aware of. If you want to make the best use of this cache, read on!

Server goes first, then cache (by default)

When you perform a query, by default, the client SDK will consult the server for results. If your internet connection is slow, the SDK will fall back to results from cache, if any. You might see something in the device log like this:

Could not reach Cloud Firestore backend. Backend didn’t respond within 10 seconds.

--

--