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

FR: (PERF) Ability to read directly from Firestore IndexDB or a new 'collection read' snapshot mode which doesn't use indexes.

Open InputNeuron opened this issue 4 years ago • 11 comments

[REQUIRED] Describe your environment

  • Operating System version: n/a but MacOS 12.1
  • Browser version: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
  • Firebase SDK version: 8.10.0
  • Firebase Product: firestore

[REQUIRED] Describe the problem

Firestore's cached collection performance has probably been the BIGGEST issue holding us back to date and is really crushing our product/market fit.

We assumed that it didn't have pathological performance issues when we built our product and that mistake is really crushing us.

Not trying to be overly dramatic here but this is really hurting our app and I'd like to propose a potential solution.

Right now some of our power users are taking 60-70s to load the webapp. This is on my brand new 2021 MacBook PRO M1 with 64GB of RAM so you can imagine how slow this would be on a 2 year old phone :-/

I did a PoC where I wrote some code to directly scan the local IndexDB firestore index and accessing the code took about 2,000ms vs about 70,000ms for our webapp which uses Firestore snapshots.

I think I have some potential solutions to this:

SCAN mode...

We could build a simple mode in Firestore that does NOT use indexes for satisfying queries from the cache.

ALL (maybe most) of our queries follow the model of filtering by uid=12345 so there really isn't any other type of query.

Basically, one collection has one query type so we can satisfy queries by just looking at the key + collection name which should be much faster.

CHEAT mode...

Instead of the above, you could update the Firestore code to broadcast via postMessage when documents are updated.

Then, one could maintain an in-memory snapshot and we could compute our own docChanges.

Basically, you'd just post docChange events.

I think the reason it has to be done in the Firestore code is due to batches/transactions. It's super rare that a transaction/batch would fail locally but I think it should probably be fixed anyway.

Steps to reproduce:

Just use Firestore with lots of data and lots of snapshots and behold, super slow!

Relevant Code:

I could build a project to duplicate this but I think you guys know already about this problem.

InputNeuron avatar Jan 06 '22 21:01 InputNeuron

Another thought. I think I could implement my 'cheat mode' by wrapping the Firestore client in a 'fast firestore' client and implementing my own snapshot API.

My idea is that I would read directly from IndexDB with a 'read' lock which I can an release before I try to initialize the real firestore.

I would then scan IndexDB and maintain my own snapshot data.

The client could maintain it's own snapshot cache in memory and update it when the client mutates data locally.

I'd need some sort of understanding how the IndexDB structure works but I think I can just access the 'remoteDocuments' object store.

The main flaw with this idea is how do I get updates from the server. I can't think of a way to do this without creating my own internal snapshots which would defeat the whole point because it would be slow too.

InputNeuron avatar Jan 06 '22 21:01 InputNeuron

Another option on this would be to allow us to listen for a snapshot on ALL collections. Basically ANY data and then allow us to figure out what to do with it.

InputNeuron avatar Jan 07 '22 02:01 InputNeuron

Hi @burtonator, thanks for the input. Our engineers will have a look once they have the time. In the meantime, you mentioned that Firestore slows down significantly when there is lots of data. May I ask how much is "lots of data" when you experienced a gradual slow down? This is just so that our engineering team may have a broader view of how we can tackle the issue. Thanks!

argzdev avatar Jan 07 '22 09:01 argzdev

@argzdev What's the best way to get the attention of the Firebase team on this issue?

Honestly, I think it's by FAR the biggest problem that Firestore has right now but we've been waiting for you guys to address it for two years now.

We're basically going live and this bug could make or break us and we're in the process of launching now.

I'm going to get you stats on the data size here but it's not massive. Maybe 1MB or so...

InputNeuron avatar Jan 07 '22 21:01 InputNeuron

Looking into this more. We have 77MB stored in IndexedDB but it's not our fault I think.

Firestore is bloating our data and remapping it and adding a lot of redundant fields.

https://gist.github.com/burtonator/8b9ae32058c03b064eb8f94383733253

We have some objects that should be like 100-200 bytes max and they're ending up around 10k.

Also, since you're not specifying a simple schema that's the main cause here.

InputNeuron avatar Jan 07 '22 22:01 InputNeuron

Thanks for providing more details, @burtonator. I understand that this is a huge problem in your app right now. However, if you could provide us an MCVE of your issue, it'll help us speed up the investigation and allow our engineering team to prioritize it based on its severity.

argzdev avatar Jan 10 '22 13:01 argzdev

Hi @burtonator. I work on the Firestore SDK team and have a few thoughts. tl;dr, however, is that there is no quick and easy fix.

  1. Could you provide a code snippet for the unacceptably-slow query that you are executing, the one that is taking 70,000ms? Please just pick one such query as an example to focus on if there are several problematic queries.
  2. When this problematic query is executed, can you estimate how many documents match the query?
  3. Also regarding the same query, how many documents would be cached locally in the collection?

It is not uncommon for Firestore users who find that our query performance does not meet their requirements to implement their own specialized database to store the information that is optimized for their specific queries. Doing this is probably the best way forward for you as well. That is, use memory persistence (which is the default; don't call enableIndexedDbPersistence()), register a snapshot listener, and use the events to maintain your own offline IndexedDb cache.

Unfortunately, we are unable to devote the time to assist with any attempts of accessing Firestore's IndexedDb tables directly. Those tables should be considered an implementation detail of Firestore and there is no guarantee about their contents and layout.

dconeybe avatar Jan 10 '22 18:01 dconeybe

That is, use memory persistence (which is the default; don't call enableIndexedDbPersistence()), register a snapshot listener, and use the events to maintain your own offline IndexedDb cache.

I had thought of this... the problem is what are we going to do with the server-side snapshots being re-sent each time? I assume the indexDB version in Firestore doesn't resend the data each time but only sends a delta vs what's stored locally.

I don't think there's a way I can implement this on my side.

InputNeuron avatar Jan 13 '22 21:01 InputNeuron

@burtonator You have indeed identified a drawback of using memory persistence and maintaining your own cache. Internally, the Firestore client tries to avoid doing this sort of "complete requery" when resuming a query, but even our fancy logic doesn't well handle deletions on the server side and can fall back to re-running the query from scratch to bring the local cache to a consistent state. We are working on some improvements to this pathological behavior but they are complex and will take time to roll out and I have no ETA to share. I've added this ticket to the list of tickets where customers are trying to maintain their own adjacent cache that optimizes their specific types of queries.

If you can provide some answers to the questions in my last post there is a chance we can suggest a workaround or some improvements with today's functionality of Firestore.

dconeybe avatar Jan 14 '22 02:01 dconeybe

Hey @burtonator. 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 Jan 21 '22 02:01 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@burtonator if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Jan 28 '22 02:01 google-oss-bot