Understanding Firebase Cloud Firestore Billing - google-cloud-firestore

I'm trying to understand Firestore's billing but don't understand exactly what a read/write/delete request is.
If I delete 100,000 items in a single request does that count as 1 delete request or 100,000 delete request?
Also, if a single query returns 250 rows (documents?) does that count as a single read or 250 read requests?

Read and write operations are billed per document affected. So, your examples would incur the cost of 100,000 deletes and 250 reads.

Related

Firestore reading quota

I have 1 collection on Firestore database and there are 2000 test documents (records) in this collection. Firestore gives free 50.000 daily reading quota. When I run my Javascript code to query documents, my reading quota decreases more than I expected. If I count all documents by using one query, is that mean "2000 reading" operation or only "1 reading" operation?
Currently firestore doesn't have any native support for aggregate queries over documents like sum of some fields or even count of documents.
So yes, when you count total number of documents in the collection then you are actually first fetching atleast the references for those docs.
So, having 2000 documents in a collections and using a query to count number of docs in that collection you are actually doing 2000 reads.
You accomplish what you want, you can take a look at the following also https://stackoverflow.com/a/49407570
Firebase Spark free give you
1 GiB total - size of data you can store
10GiB/month - Network egress Egress in the world of
networking implies traffic that exits an entity or a network
boundary, while Ingress is traffic that enters the boundary of a
network In short network bandwidth on database
20K/day Writes
50K/day Reads
20K/day Del
If You reading 2000 documents depends on calling a single call read cause one read if you reading multipal at one consider 1 reads the answer is depends how you reads you calling
Firebase Console also consume some reads writes thats why quota decreases more than you expected

Firebase read calculations for document queries?

Quick question about how firestore reads are calculated. Say I have a collection with 100 items in it, and I do
citiesRef.order(by: "name").limit(to: 3)
This would technically have to look at all 100 items, order them by name, and then return 3. Would this count for 3 reads or would it count for 100 reads, since we're looking at 100 items?
Thanks.
If the above query returns 3 documents then it would count as 3 reads.
You are charged for each document read, write, and delete that you perform with Cloud Firestore.
Charges for writes and deletes are straightforward. For writes, each set or update operation counts a single write.
Charges for reads have some nuances that you should keep in mind. The following sections explain these nuances in detail
https://firebase.google.com/docs/firestore/pricing#operations

Firebase Realtime Database Pricing with Query

I have a question about the firebase database pricing. I have about 400,000 rows in the leaderboard of my database, but in my app I just want to load the last 500 rows, so my question will I get charged for the 500 rows loaded when I run the query or will I get charged for all 400,000 rows.
Realtime database charges 5$ per gb stored and 1$ per gb downloaded. I did the calculation with Firestore and found that Realtime Database would be way cheaper if i get charged for the 500 rows and not the 400,000 rows.
I searched all documentation and have not found anything about queries: https://firebase.google.com/pricing
https://firebase.google.com/docs/database/usage/billing
Can someone tell me if I get charged for just the 500 rows in my collection or for all the data in the collection and if there is a way to only get charged for the 500 rows maybe with security rules?
Here is my query code:
let queryRef = ref.child("Leaderboard").queryOrdered(byChild: "totalStars").queryLimited(toLast: 500)
How the database looks like. (It will have about 500,000 childs same as these and be loaded 200,000 per day, But I just want to be priced on the top 500 that I load and not the whole 500,000 each time a user loads the leaderboard is it possible?)
You will only be charged for the number of Firestore Documents corresponding to the result of your query (not to the number of docs in the collection).
So in your case a maximum of 500 reads, since you would limit the Query to 500 documents.
On the other hand, note that the Realtime Database queries are not shallow (while the Firestore ones are) and therefore if you query for a JSON node you'll get the entire tree under this node.
Renaud's answer is the correct one but let me add some additional information and restate that:
With the Firebase Real Time Database for downloads you are charged for
what is downloaded and not how many nodes you are querying.
So the key is to reduce the amount of data you're downloading. Your nodes are already pretty shallow however, there's a huge savings to be made because in your current structure, the node key (the users uid) is duplicated within the node as a child node, and that's not needed.
You can always get the node key with snapshot.key and remove that child node. So it would look like
uid
fullName: "Logan Paul"
stars: 40
Also, I think your calculations are off a bit. It looks like each node would be about 100 bytes of data, and Firebase strings are UTF-8 Encoded so if you download 500 nodes per user per day and you have 200,000 users, that about 38Gb per day (as binary).
Roughly 400 bytes * 500 nodes * 200,000 users * 0.000000000931322574615479 bytes per Gb = 38Gb
so about $38 a day if I did my math correctly.

Firestore Pricing for deletion

If I perform a query to delete a document that does not exist in my firestore database, will I be charged for the delete operation, read operation or will not be charged at all?
Please refer to the documentation.
If you make a query, you will be charged a read for each document returned by the request, regardless of what you do with it.
If you attempt to delete the document from a query, you will be charged a delete if the delete actually succeeds in deleting that document. Document delete operations that don't actually delete a document are not billed. You will not be charged for an additional read, as described by the docs:
when a document is deleted, you are not charged for a read
It sounds like you will be charged based off of reading this. Even though they don't say "delete", this makes it seem like you will be charged: "There is a minimum charge of one document read for each query that you perform, even if the query returns no results." Now if you receive an error doing the operation you should not be charged. I would recommend testing this with very small amounts of data at first just to make sure though, the fee is only $0.02 per 100,000 deletions.

How many documents can be written in batch mode from firebase cloud function trigger to Firestore?

We have large amount of data where we need to update documents based on a status. We will writing in batch of 500 chunks and wondering how many max records we can commit with a single trigger invocation?
Our trigger is a pubsub trigger in firebase cloud functions.
We see there is a limit of 540secs per invocation so would like to know how many max documents we can write in batches?
Update : Adding usecase
I have an event collection(Events) where users can subscribe for each event happening in a country.
Users have an api to see how many events they have subscribed to. They have query flags like is the event Live/Finished/Upcoming.
As I can't save list of user array who subscribed for an event in the event document(assuming subscribers can go beyond the document limit when stored), I maintained a separate sub-collection under users collection.
Ex : users/user-id/subscribedevents
Event document's status (Live/Finished/Upcoming), i'm updating from a cron job which will be running every minute. This is because I can't apply filters with two different fields (startDate & endDate).
When ever an event's status changes, i need to update in subscribedevents subcollection (which is under user's collection).
As I will be updating all the subscribedevents subcollection entries, I want to do it in batches.
Hope the usecase gives some clarity on where it is applied. As firestore is designed for scale, wondering how others are handling this scenario as its very common.
Each transaction or batch of writes can write to a maximum of 500 documents.
And 20 maximum batched writes.
References
Firestore Write Documents
Transaction and batches in firestore