Efficiently search/exist() Firestore without exhausting free quota

I’m working on a side project and want to let my users check if their friends have accounts.

Currently I’ve implemented it like this:

  1. Read phone contacts for emails
  2. Loop through the emails
    1. Make .get() query on the user database1 for users with that email
    2. If data comes back, the friend is on the platform and an invite button is displayed
  3. Free quota2 exceeded within an hour

The thing is that any .get is considered a read operation, even if no data comes back. Their doc.exists can only be tun after a .get so a document read is needed to check for existence.

I’m sure I’m overlooking something obvious, what I want to do is in essence to an .exist() like query that does not ‘cost’ a read.


1: I’m not actually storing emails in firestore but their hashes, and am querying those. Same effect, but it allows me to query a secondary user database that doesn’t expose true emails and other data.

2: Not trying to be cheap per se, but if this app turns commercial this would make the billing a nightmare.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

According to your comment, you say that you keep the contacts in memory and for each contact (email address), you search in your existing Firestore database for matches.

Free quota exceeded within an hour

It means that you are searching the Firestore database for a huge number of contacts.

The thing is that any .get is considered a read operation, even if no data comes back.

That’s correct. According to the official documentation regarding Firestore pricing, it clearly states that:

Minimum charge for queries

There is a minimum charge of one document read for each query that you perform, even if the query returns no results.

So if you have for example 1000 contacts and you query the database for each one of them, even if your queries return no results, you’re still charged with 1000 read operations.

I’m sure I’m overlooking something obvious, what I want to do is in essence to an .exist() like query that does not ‘cost’ a read.

That’s not how Firestore works. This means that every query incurs a cost of at least one document read, no matter the results.

1: I’m not actually storing emails in firestore but their hashes, and am querying those. Same effect, but it allows me to query a secondary user database that doesn’t expose true emails and other data.

As you already noticed, doesn’t matter if you store the actual email address or the corresponding hash, the result is the same.

2: Not trying to be cheap per se, but if this app turns commercial this would make the billing a nightmare.

Try for this feature, Firebase realtime database and believe me, both work very well together in the same project.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply