user12396958
user12396958

Reputation:

Is using local db a good way to reduce data read/writes from Cloud Db?

I am using Cloud Firestore in a React Native app and I am trying to reduce the read/writes operations to a minimum. I just thought of using a local DB so that all data fetched from the cloud are saved in the local storage but I would add a snapshot listener to listen for changes whenever the user starts the app.

Is this a good approach for what I am aiming? If not, why? And if yes, do you have any suggestion related to its implementation?

Upvotes: 2

Views: 757

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317487

I feel compelled to point out that the other (currently accepted) answer here is flat out incorrect, or at least misleading for a few reasons.

First, Firestore doesn't use HTTP, and the results of queries are never going to be maintained by your typical browser cache. The claims the answer makes about HTTP caching semantics simply do not apply.

Second, the Firestore SDK uses an internal cache, which is enabled by default on Android and iOS, because its sense of cache is almost always going to benefit the end user. Web applications would do well to enable this cache as well. It requires one line of code. This cache will be queried when the client is offline, an can be queried directly if cached results are desired.

Third, adding an additional layer of cache or persistence is actually very necessary for applications that must be fully usable offline. Firestore was not designed to be use fully offline, so having a local-first option is necessary for some applications. The additional cache can be synchronized with Firebase as a sort of cloud backup.

All told, the question is technically too broad for Stack Overflow, and it requires conversation to understand if it's worthwhile to enable Firstore's cache, or add an additional cache on top of that. But it's not patently false that client caching is a bad idea.

Upvotes: 1

symcbean
symcbean

Reputation: 48357

No, it's not a good approach.

Caching data is generally a good idea, however implementing this at the DBMS tier will involve writing a lot of code to implement a caching mechanism you have yet to define. The reason it's a bad idea is because JavaScript running on a client already has access to a data tier with very well defined caching semantics already implemented in the the runtime environment - http

Upvotes: 0

Related Questions