Google cloud task or cloud Scheduler - firebase

I'm trying to achieve a similar function as blablacar has, app users can post leisure plans. So every leisure plan has a start date and an end date (even for more than 30 days), when a leisure plan ends I want to send a push notification. If a user publishes a leisure plan, I can create a task in the cloud, the problem is that if the finish date is greater than 30 days, this approach no longer works for me. I thought about using cloud scheduler, what happens is that I do not know if it would be very expensive to make queries to see which dates are finished about 3 times a day.
What approach should I take?

As you can see here "Cloud Scheduler pricing is based exclusively on the job... The actual running of a job is called an execution. A job is not billed for individual executions". That means that it does not matter the number of times you execute a job, you always be charged for a job (in case you only have 1).
In addition, Cloud Scheduler allows you to have up 3 free jobs per month, per billing account. So, if you need to use 4 jobs for this task, you only will be charged for 1 (the other 3 are free).
Finally, the other important thing to take into account is from where you are going to retrieve that information (Cloud SQL, Datastore, etc). This is to estimate a cost for all the resources you are going to use.
I think that a good option to estimate this is to use the GCP Calculator.

Related

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.

Can't determine what function was invoked so many times 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.

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.

how to trigger a firebase cloud function after specific amount of delay? [duplicate]

I have a unique problem here. I actually need to schedule a task to send emails to eligible users at specific times. I have checked and found out that scheduling cron jobs can solve the problem. However, in my case, I need to schedule these tasks programmatically based on when specific users meet certain conditions.
Here is my scenario
I am running a referral campaign in my application where users who refer up to 5 persons get a discounted subscription
After the user has referred 5 persons, the cost of subscription is slashed as long as he pays within 24 hours. Otherwise the cost returns to the original value
I'm using firebase cloud functions to send eligible users reminder email reminders at certain times to make payment before the 24 hours elapses
Now the problem
I want to send reminder emails at specific times say as soon as he refers 5 persons, when his time is remaining say 3 hours etc.
If I just schedule a function regardless of the user, I may have to set cron jobs at regular intervals say 1 minute to always check if the users in my db are eligible and then send emails to them. This pose serious issue such that cloud functions are ran whether or not there are eligible users.
Another problem is that with the above implementation, I cannot send emails to users at specific/exact times unless I schedule tasks for like every 1 second which will of course sky rocket price as cloud functions will be called around a whooping 86400 times (86400 seconds) a day
My proposed solution
I just need a way to schedule cron tasks dynamically so that I can schedule a job for particular users. This will solve many problems like sending the emails at particular times, prevent cloud functions from running when not necessary, ability to set different email send times for different users etc
I plan using http triggers with a request parameter of the user ID for scheduling tasks so that firebase can use this ID to assign tasks to only specific users, i.e each eligible user will have his own http trigger
Please is there a way to achieve this or any other good solution for my case?
Instead of trying to dynamically creating scheduled function, consider having a single function that runs on a fixed schedule, and then implementing your timing logic inside of that function.
So instead of saying "I need to schedule a function that runs 3 hours from now and send a message to these 5 people", think of it as a task that you can write into a database: "at x:yz send an email to these 5 people", and then have a periodic Cloud Function that checks what tasks it needs to execute.
Also see Delay Google Cloud Function, How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?
Alternatively you can use Cloud Scheduler to create a task for the specific action you want to perform, and then have it post to Cloud Functions via PubSub.
As an even newer alternative: Use a separate scheduler service that has an API to create schedules, like Cloud Tasks. Doug wrote a great article about that in How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).

How to create cron jobs in firebase programmatically

I have a unique problem here. I actually need to schedule a task to send emails to eligible users at specific times. I have checked and found out that scheduling cron jobs can solve the problem. However, in my case, I need to schedule these tasks programmatically based on when specific users meet certain conditions.
Here is my scenario
I am running a referral campaign in my application where users who refer up to 5 persons get a discounted subscription
After the user has referred 5 persons, the cost of subscription is slashed as long as he pays within 24 hours. Otherwise the cost returns to the original value
I'm using firebase cloud functions to send eligible users reminder email reminders at certain times to make payment before the 24 hours elapses
Now the problem
I want to send reminder emails at specific times say as soon as he refers 5 persons, when his time is remaining say 3 hours etc.
If I just schedule a function regardless of the user, I may have to set cron jobs at regular intervals say 1 minute to always check if the users in my db are eligible and then send emails to them. This pose serious issue such that cloud functions are ran whether or not there are eligible users.
Another problem is that with the above implementation, I cannot send emails to users at specific/exact times unless I schedule tasks for like every 1 second which will of course sky rocket price as cloud functions will be called around a whooping 86400 times (86400 seconds) a day
My proposed solution
I just need a way to schedule cron tasks dynamically so that I can schedule a job for particular users. This will solve many problems like sending the emails at particular times, prevent cloud functions from running when not necessary, ability to set different email send times for different users etc
I plan using http triggers with a request parameter of the user ID for scheduling tasks so that firebase can use this ID to assign tasks to only specific users, i.e each eligible user will have his own http trigger
Please is there a way to achieve this or any other good solution for my case?
Instead of trying to dynamically creating scheduled function, consider having a single function that runs on a fixed schedule, and then implementing your timing logic inside of that function.
So instead of saying "I need to schedule a function that runs 3 hours from now and send a message to these 5 people", think of it as a task that you can write into a database: "at x:yz send an email to these 5 people", and then have a periodic Cloud Function that checks what tasks it needs to execute.
Also see Delay Google Cloud Function, How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?
Alternatively you can use Cloud Scheduler to create a task for the specific action you want to perform, and then have it post to Cloud Functions via PubSub.
As an even newer alternative: Use a separate scheduler service that has an API to create schedules, like Cloud Tasks. Doug wrote a great article about that in How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).

Resources