Can't determine what function was invoked so many times Firebase - firebase

I've been looking at the cloud functions in my Firebase console, and I've notice for one day in the past month that I somehow reached 23k invocations. I initially thought to check because my billing said I was charged $0.02 for them.
I've searched high and low, inspected my billing, reports, logs. I can't seem to see what was called 23k times that day. Does anyone know where I can find this information.
Thanks!
Screen Shot

Unfortunately there is no report that drill down a cost by each function for Cloud Function Billing. In order to see what functions are being invoked I would recommend that you keep some logs of usage of the functions, you can check how to setup logs in this Official Documentation.
It's important to note that this many invocations of your functions should not generate any billing to you, since as you can see on the Pricing Documentation for Cloud Functions, there is a free tier of 2 million invocations per month to all of your functions. So, for all your functions, you actually had at least 2 million invocations + 23.000 invocations that generated that $0.02 billing.

Related

Cloud Firestore Read Operation 10 Times Higher in Billing

One of my App uses Firebase Firestore and has almost never exceed the daily free quota of 50k read operations. However last month I was charged for a few millions Firestore read operations.
Wanting to find out what was the problem, I checked my console (Usage and Billing) and confirmed that my everyday usage was under the 50k daily free quota during the whole billed month. Then I checked my usage today, in Firebase Console (Usage and Billing) it showed I have 45k read operations, but in my Google Cloud Platform (Quota), it showed 0.45m read operations, which was 10 times higher.
Anyone faced this problem before? What could've gone wrong?
Thank you in advanced.
I had the same problem (it was also 10 times the amount!), and I am pretty sure for me it must be because reads to do backups are not counted on the firebase usage page, but are billed. For a large database backups can be quite a problem.
It turns out there is a message here:
https://firebase.google.com/docs/firestore/manage-data/export-import?authuser=1
It says:
"Caution: Exporting data from Cloud Firestore will incur one read operation per document exported. However, these reads will not appear in the usage section of the console. Make sure you understand this before setting up recurring exports to avoid an unexpected bill."
Hope this saves someone a bit of time!

Firebase cloud function scheduler pricing for multiple users

I want to write a scheduler to be triggered once every month to do a particular job that affects all users in a firestore database. What I want to know is, if I have for example one million users compared to having just one user in the database. what is the cost of running that scheduler for one user compared to one million users?
The Firebase documentation states that it cost $0.10 per job per month, but this does not seem to take into account the number of users involved.
So does the pricing of the scheduler change depending on the number users, can someone please clarify this, thanks in advance.
You are using three services, each with their own pricing:
Cloud Task Scheduled, which charges per job.
Cloud Functions, which charges per invocation, and then for the memory/cpu usage for as long as the function is active.
Cloud Firestore, which is charged per read/write operation per document, and for the bandwidth used to read data.
The total you pay is the combination of all these services. I recommend putting all your info in the Google Cloud Pricing Calculator to get an estimate of the cost, and (more importantly) in the factors that determine that cost.
As explained in the official documentation here, of Cloud Scheduler - that it seems to be the one you will be using:
Cloud Scheduler pricing is based exclusively on the job. A Cloud Scheduler job defines a single activity scheduled to run at a frequency provided in the definition.
The number of users is not considered as well directly in the Cloud Functions pricing, as its per invocations of the function. To summarize, it doesn't matter the number of users, but actually the number of jobs and invocations, that you will need to control, so you can calculate the real cost you will have.

Is it possible to set some kind of firestore read/write quotas to avoid overspending when using Blaze plan?

I have upgraded to the firebase Blaze plan since I want to use cloud functions with nodejs 10 and the Pubsub schedule functions but I am a bit scared of overspending by mistake once I reach the free quota.
I am already aware that I can set up some budget alerts to monitor my monthly spending but I am more looking for a safety net solution in case my usage of Firestore suddenly goes through the roof because of an error in the code (infinite loop for example, like it happened to me last week) or even some kind of DDoS attack.
The documentation mentions a daily spending limit which is exactly what I was looking for but it seems to be deprecated since end of 2019. Is there a way to set some kind of quotas for Firestore (read/write per day or minutes) in a similar way to how we can set quotas for the number of invocations of cloud functions?
I find it hard to believe that Gcloud removed the daily spending limit without offering any alternative as it could, in case of human errors, bugs, or even attacks, leave independent developers with gigantic bills they can't afford. I am seriously considering downgrading back to the free plan.
Is there any alternative solution to cap Firestore spending or usage?
It is not possible to set a quota limit on the Firebase operations. This is a known issue but you can mitigate it by setting alerts on the GCP project. There is more information from a Googler in https://stackoverflow.com/a/52325021/6003934

Firebase Blaze plan and Cloud Functions

Unfortunately Firebase's Flame plan disappeared and I now have to deal with Blaze plan, which I'm not fan of because of it's "unlimited" budget (but my wallet isn't). Yesterday I was coding and by mistake made a loop that called 3 Cloud Functions, I spotted the loop fast enough to stop the carnage.
In only 15 seconds of execution, I've been able to trigger more than 10K requests, and without errors, that a pretty good performance of Firebase, but it's scary as well! How can I cap this to throttle requests coming from users? I'm pretty confident in my code, but as we all know if someone gets my Firebase config file, I'm done.
I'm a solo developer and it's my first app done on my spare time, I don't want to to become a mess because of triggering reads / writes / Cloud Functions. No one is perfect, everyone makes mistakes, maybe I didn't spot a small bug in my code that'll reveal it's true potential only once in production. I'm not asking Firebase to handle my mistakes, but I'd like to be able to stop the CF and Firebase if I want to.
My only desire is to sleep confident, I don't want to wake up one morning with a 10.000$ bill. I've read that's it's possible to totally disable billing account on a project with CloudFunctions, but what will happen to my Firebase Storage bucket for example (for the storage superior to the Spark plan)? And it doesn't seem easy to do.
Firebase is a great product and I love how easy / fun it is to use, but now that budget locked plans as Flame are gone I feel really trapped now that my application is almost ready to go into production, and I don't think I'm the only one out there.
Ah you've stumbled onto the 'rate-limiting' conundrum. Not to worry, I've spent many nights worrying about this myself.
In order to get a bit more control over your application, you're going to have to link your project to google cloud platform. Then navigate to IAM & Admin>Quotas (once you've selected your firebase project).
Now you might be overwhelmed initially, and confused as to why there are so many Cloud functions API limiters. These quotas should allow you to rate limit your cloud functions API (similar to what twitter does) in a number of ways, including but not limited to:
Read Requests per day
Read requests per 100 seconds
Function invocations per 100 seconds
Function invocations per day
etc
The API that will be listed are the ones you've enabled, so you can set limits for cloud storage as well.
By default, the max number of invocations per second is set to the maximum of 5000 invocations per day on the spark plan. but according to the docs, the absolute maximum is 100,000,000/100 seconds!
Also worth having a look at is the pricing docs, which have a nice example of a typical monthly use case for a successful app. To grossly oversimplify, a single invocation costs $0.0000004, hence 10,000,000 invocations will cost you a whopping $3.20. However don't let that very low cost fool you, if you write some terrible code and it has exponential complexity (like reading every document in a firestore collection every invocation), you could get slapped with an exponential cost. So make sure you set those quotas :)
Remember, server admin is as much a part of the application as the code itself. If your app goes to production, be prepared to spend some time each day going through the Google cloud dashboard and checking limits, analyzing trends, etc. This way you can kind of step up the amount of invocations you can allow per day and sleep well knowing that if you shoot yourself in the foot, you won't bleed too much.
Best of luck with it
Despicable B.

Cloud Function Affect Read/Write Quotas

I am looking for clarification on how the actions triggered by cloud functions affect the cost of hosting an app with Firebase.
My situation:
I have a cloud function that is triggered when a post is made...that cloud function writes that post to all of the appropriate uid's on the /feed node mentioned below.
I am using Firestore to host a feed/follow system. It is setup with each user having their own feed at the following path
/feed/{uid}/posts
My question is, if a user has 1,000,000 followers...does that translate to 1,000,000 writes in the eyes of Firestore?
Does that mean that single post will cost $1.80 to distribute? Based on the cost of $0.18/100k writes.
I am just trying to see how this will scale with thousands of users posting dozens of posts...those distribution costs seem very expensive at scale for this situation.
EDIT
It looks like it does cost for every read/write in a cloud function base on the following video:
https://www.youtube.com/watch?time_continue=49&v=6NegFl9p_sE&feature=emb_logo
It depends on how you are writing to Firestore, but bottom line is that yes, you get billed for each read/write operation performed to Firestore as mentioned over at their documentation.
Additionally, you get billed per Cloud Function invocation as mentioned here, so you would need to keep this in mind if you want to keep your expenses low.
Hope you find this useful.

Resources