firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firestore: Support for large offline collections

Open adammeyer opened this issue 6 years ago • 25 comments

  • Operating System version: Mac
  • Browser version: Chromium 73
  • Firebase SDK version: 7.8.0
  • Firebase Product: Firestore

[REQUIRED] Describe the problem

When using offline persistence, the browser locks up if many records were modified since last load.

Steps to reproduce:

  • Launch web app
  • Enable offline persistence
  • Add 3,500 (or more) documents to Firestore
  • Close app
  • Delete those 3,500 documents from admin console (or another app, browser or computer)
  • Relaunch web app (in original browser) and retrieve docs collection().get()
  • Browser becomes unresponsive

Sometimes the browser become responsive again and all data will be correct, but that can take > 30 seconds sometimes.

To fix I simply delete the Firestore IndexedDB and reload the app, all is good until another large amount of docs are modified or deleted. This isn't an issue with the amount of docs in a collection, it's an issue with how the offline persistence "syncs" a large amount of changes.

Please advise if anyone has a workaround or solution.

adammeyer avatar Feb 03 '20 15:02 adammeyer

@adammeyer Thanks for filing this issue! We are aware that there is room for improvement when it comes to our IndexedDB performance and are constantly looking at ways to continue to improve this situation.

Unfortunately, there are no short term fixes: Most of the perceived slowness is coming from the fact that loading data from IndexedDB is very slow and that our main query processing logic currently loads all documents in a collection to guarantee cache-coherent results.

For now, I would suggest that your modify your usage of Firestore to better fit our query implementation:

  • You should only load as much data as needed.
  • You should use a larger number of small collections, rather than putting a lot of data into a single collection.
  • You should re-use queries. Queries that are executed frequently perform much better as of v7.5.0.
  • You can reduce the cache size via cachesizebytes

I hope that these suggestions solve some of your pain points.

schmidt-sebastian avatar Feb 03 '20 17:02 schmidt-sebastian

Follow up: My colleague pointed out that you may hit an additional code path that could slow you down. Every time a document is deleted remotely but still available locally, the SDK performs something that we call "limbo resolution". This is an explicit lookup that the SDK performs to bring cached data in sync with the server. In your case, after we processed 3500 items from the cached collection, this likely means that the SDK issues 3500 individual read requests to update its cache. We will discuss internally if there is a short term fix for at least this part of the problem.

schmidt-sebastian avatar Feb 03 '20 18:02 schmidt-sebastian

Thank you @schmidt-sebastian!

One thing I have noticed is that after docs have been deleted or modified and the app reloaded, the onSnapshot listener fires an "added" event, then a "removed"/"modified" event for every doc. This may be causing unnecessary queries/lookups.

My idea for a hack was to check if there is more than 500 modifications or deletions and if so to delete the local indexeddb and reload the app. That doesn't work since 3,000 deleted docs show as 3,000 'added' and lock the browser.

adammeyer avatar Feb 03 '20 18:02 adammeyer

The "added" event is fired because the first snapshot is served from cache. Once we have computed the first snapshot and received the response from the server, we know that our state is inconsistent with the backend. To resolve this, we issue individual listen requests. As this finish one by one, we will update the snapshot that we raised originally. So over time you will see a "remove" event for every document until our local cache is once again synced with the backend ([SnaphshotMetadata.fromCache](https://firebase.google.com/docs/reference/js/firebase.firestore.SnapshotMetadata.html#fromcache) will be false).

schmidt-sebastian avatar Feb 03 '20 19:02 schmidt-sebastian

Got it, thanks.

I don't mind if the "get" after large modification or deletions take a long time. But is there any way to get a status or count so I can build some type of progress bar? And for the process to not lock up the browser? Could I set a limit and do things in batches? Thank you.

adammeyer avatar Feb 03 '20 19:02 adammeyer

Indeed @adammeyer a status or count would be nice, for either a progress bar or the "wipe indexedDB + reload" hack you mentioned.

cmditch avatar Apr 28 '20 06:04 cmditch

@schmidt-sebastian here are some interesting discoveries we've made, in case your dev team is unaware of this behavior.

We've noticed that the "browser hang" increases linearly with both the length of the document path and the number of documents in a given collection.

The latter is unsurprising based on the info you've provided above, but the doc path behavior is pretty interesting.

e.g. Updates made to /this/is/three/collections/deep/{id} freezes our application three times as long as /shallowDocs/{id}.

Hope you find this useful. Cheers.

cmditch avatar Apr 28 '20 06:04 cmditch

@cmditch That's very interesting. I will investigate.

schmidt-sebastian avatar Apr 28 '20 17:04 schmidt-sebastian

@cmditch I don't see any statistically relevant difference in a quick test: https://gist.github.com/schmidt-sebastian/d2bc9e77becaf8b75359407d744fd0c7

Some questions for you:

  • Do you have persistence enabled?
  • What browser are you testing on?
  • Are there documents at /this/is/three/collections/deep/{id}? Does this reproduce if you invoke clearPersistence()?

schmidt-sebastian avatar Apr 28 '20 18:04 schmidt-sebastian

Hey @adammeyer. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar May 12 '20 01:05 google-oss-bot

Happy to help here, what other information can I provide?

adammeyer avatar May 17 '20 12:05 adammeyer

@adammeyer Do you mind answering the questions in https://github.com/firebase/firebase-js-sdk/issues/2591#issuecomment-620794082? Thank you!

schmidt-sebastian avatar May 18 '20 18:05 schmidt-sebastian

Can I advise the Firestore team to prioritize this please ? this is really killing us and has been sort of a major problem for a long time now.

Most of our queries are for collection +id and these at least should come from the cache instantly since you can map to the exact document.

InputNeuron avatar Oct 24 '20 18:10 InputNeuron

Would it be possible to get an ETA for this. Or at least let us know if it's on the short term agenda?

This is becoming a serious blocker for our app and I'm considering writing our own persistence layer with indexes to solve this problem.

Our indexes aren't super complicated so we should be able to build this out well and get better performance.

... but would love to be lazy and have you guys fix it! We're also willing to be guinea pigs if you have something in the works.

Our app (getpolarized.io) is trying to really leverage offline snapshots so the user can work offline or online (including on mobile) and the cache performance for larger datasets just falls over.

I'm burton at getpolarized if you'd like to reach out directly.

InputNeuron avatar Nov 11 '20 01:11 InputNeuron

While we are considering prioritizing "offline first" usage for 2021, no decision has been made and there is no ETA.

schmidt-sebastian avatar Nov 11 '20 20:11 schmidt-sebastian

@schmidt-sebastian Thanks! This helps a lot and helps us prioritize.

One thing I'd like to point out is that I think offline-first could be THE killer feature for firestore and might really help with growth.

When I first saw that you guys had offline snapshots I was super excited and was even ok with 'offline-tolerant' but finding out that no indexes were used and that performance was bad was really a big hit on our development plans.

It's sort of hidden that the performance is bad and really only acceptable for smaller apps.

The snapshot API for Firestore is really amazing to work with and offline cache WITH indexes would really really be amazing.

I'll be holding my breath in the mean time so no pressure :)

InputNeuron avatar Nov 12 '20 00:11 InputNeuron

@schmidt-sebastian any update on this? This is by far the biggest bug/issue we have in firestore.

InputNeuron avatar Jan 13 '21 16:01 InputNeuron

This is an enormous undertaking. We don't have an ETA.

schmidt-sebastian avatar Jan 13 '21 17:01 schmidt-sebastian

I think I have a basic/simple API for this and will try to publish the code.

My idea is to create a wrapper for the snapshot APIs but use a key factory and save to the store directly.

It's a limited API vs what you guys expose but might work well for some people.

I think in your situation the paths are sort of the main problem. And the indexes. We're just going to build our own simple indexes.

InputNeuron avatar Jan 13 '21 22:01 InputNeuron

Nearly a year later. Can we get an update on this?

This is critical for us and basically (by far) the biggest negative to using Firestore for us.

Performance is critical.

I'll either have to completely rewrite our data to function from our own cache or I have to migrate to something like:

https://replicache.dev/

We've made some design tradeoff due to this which aren't super fun including storing blobs in GCS but they don't participate in transactions like in Firestore.

This puts just an absolutely massive burden on us and it might just be easier to migrate away from Firestore rather than implement things from the ground up.

InputNeuron avatar Nov 20 '21 23:11 InputNeuron