Receiving an alert when a cloud function receives too many requests

585 views
Skip to first unread message

will

unread,
Feb 8, 2019, 11:50:19 PM2/8/19
to Firebase Google Group
I was playing around with Firebase Cloud Functions and I ended up creating a function stuck in an infinite loop which was executed over 30k times in two days. Luckily for me, billing wasn't enabled.

Is there a way to prevent this from happening? For example:
a. Receiving an alert if the same function is running too many times in a short period of time;
b. Preventing a function with an onUpdate (or onWrite) trigger to run multiple times when the action is coming from an admin (nodeJS) rather than a regular user.

The latter was my issue here. I had an onUpdate trigger which would update the document with the number of times that document had been edited. However, obviously, every time I was updating the document, then my function would run again.

I'm worried one day I (or someone from my team) will make a mistake like that and we'll end up with a huge bill to pay.

Kato Richardson

unread,
Feb 9, 2019, 1:13:06 PM2/9/19
to Firebase Google Group
Hi Will,

Functions actually prevent recursive calls from background triggers like Firestore (ref). You could also mark the db records with some sort of processed boolean and ignore them on future calls.

However, if you're running into a lot of recursion, you might want to look at more granular triggers (e.g. onCreate or onDelete rather than onWrite) or a different strategy or data structure to reduce duplication.

☼, Kato 

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/4f0cac4c-85cc-41d6-8d92-1e7993ce775d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

will

unread,
Feb 10, 2019, 6:10:53 PM2/10/19
to Firebase Google Group
Hi Kato,

Thank you for your reply. My issue was already with a more granular trigger (onUpdate). My function was being called 15k times per day. I know we can set billing alerts to prevent high bills but can we also set usage alerts to prevent a function from being invoked so many times when it isn't supposed to do so?

Kato Richardson

unread,
Feb 11, 2019, 6:44:47 PM2/11/19
to Firebase Google Group
Thanks, Will. Without fully understanding your use case, two simple solutions for reducing calls to onUpdate():

1) Set a processed boolean on the records and ignore incoming requests if change.after.data().processed === true

2) Write the updates to a queue, e.g. /potential_write_to_foo/{autoId} and have Functions write out the change to the final path, e.g. /foo/{recordId} as part of the Functions invocations. This would prevent cyclic calls since updates to /foo would not re-trigger the update function.

I hope that helps!

☼, Kato


For more options, visit https://groups.google.com/d/optout.

will

unread,
Feb 12, 2019, 2:46:12 PM2/12/19
to Firebase Google Group
Thanks again, Kato.

Sorry, I didn't explain my last question very well. I would like to know if there's a way to set up an alert for when a function is being called too many times in a short period of time. For example,

I have an "updateFunction" which is usually called 5 times per minute. If it's suddenly called 100 times per minute, then I'd receive a notification.

Your answer is great as it shows how to prevent those mistakes but they might happen either way. So, I'd like to have a way to quickly detect them when they happen instead of just finding out when the bill comes. ;)

Hiranya Jayathilaka

unread,
Feb 12, 2019, 7:07:19 PM2/12/19
to fireba...@googlegroups.com
Hi Will,

There's no out-of-the-box solution as far as I know. However, we have done a demo where we use Cloud Functions to track updates to Firestore documents, and if the documents get updated too frequently take some action. Actually in that demo we tracked updates made by individual users, and when too many updates were detected we banned those users from the system. You may need to implement a similar tracking mechanism.

Here's the demo from last year's Firebase Summit: https://youtu.be/Hlxby1Mmic8?t=762

Thanks,
Hiranya


For more options, visit https://groups.google.com/d/optout.


--

Hiranya Jayathilaka | Software Engineer | h...@google.com | 650-203-0128

will

unread,
Feb 13, 2019, 12:57:20 AM2/13/19
to Firebase Google Group
Awesome, Hiranya! Thanks for sharing! :)

Kato Richardson

unread,
Feb 13, 2019, 2:42:53 AM2/13/19
to Firebase Google Group
Great question. I've never tried something like this before, but you should be able to set up StackDriver alerting, given that functions logs are output into StackDriver and already available for StackDriver monitoring.

☼, Kato

On Tue, Feb 12, 2019 at 11:46 AM will <wce...@gmail.com> wrote:

For more options, visit https://groups.google.com/d/optout.

will

unread,
Feb 14, 2019, 10:25:55 AM2/14/19
to Firebase Google Group
Perfect, thanks a lot! :)
Reply all
Reply to author
Forward
0 new messages