1

I'm working with firestore and have encountered several situations where a document will contain stale data which is a duplicate of the source of truth located elsewhere in the database.

For instance I have a chats collection with documents that contain:

users: [{
    name: 'foo',
    companyName: 'bar'
}]

The source of truth for both name and companyName are located in documents contained in the userProfile and companyProfile collections respectively.

On App startup I start database listeners that fill redux state and then continue to update in the background. I do this for the chats documents that the user is a part of so that there is no loading presented to the user after initial startup. But of course I can't listen to all data in the database, such as other users userProfile documents. So data like this can become stale if it is a duplicate of the source of truth.

Up to this point I had assumed that I can write a cloud function to update all this duplicate data upon any change but I'm thinking that's a bad idea if even feasible.

I'd like to not slow the down the user experience if I can, so I need advice here. Currently I see three options:

  1. Always store the document Id of the source of truth instead of duplicating data, and fetch the up to date data. The user would experience an obtrusive load before seeing this data, or potentially ghost elements with animations.

  2. Keep the data duplication pattern and serve the user potentially stale data, but also show them an unobtrusive load indicator while fetching the source of truth. If there are updates then update the duplicated data in the database.

  3. Write a potentially expensive/complex cloud function that watches the source of truth and updates the duplicates all over the database when changes are detected. A users name/companyName is duplicated in several places, so this may prove difficult to target everything, if possible.

Am I missing anything here? Are there better ways to approach this?

I realize this a trade-off between up to date data and user experience, but I'm hoping there is an eloquent or generally accepted design pattern to combat this.

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.