Understanding Cloud Functions: Retries and idempotence (Firecasts)
Why Stack Overflow recommends it
Stack Overflow users link it when explaining that Cloud Functions guarantees at-least-once delivery, so duplicate invocations happen and functions need to be written idempotently.
Recommended by 3 people across 3 different months, 2018 to 2022.
What it is
A 7-minute Firebase Firecasts episode in which Doug Stevenson explains why Cloud Functions can trigger a function more than once for a single event. He covers enabling retries for asynchronous functions and why idempotence matters.
What people said
Recommends Doug Stevenson · 2019-03 · Stack Overflow · on “Cloud functions idempotency issue”
Cloud Functions gives at-least-once guarantees for the execution of background functions. This means that it's possible for a function to execute multiple times per event. While it is not common, it is just the way that scalable systems deal with potential errors when multiple machines must coordinate and communicate asynchronously. If repeating a function invocation for an event is problematic for your function, you need to code your function in a way that makes it idempotent. You can watch this video [this video] to understand a little bit more, and read the documentation [LINK] . The overall problem that can't be avoided is call the Two Generals problem [LINK] in computer science. You can read
Recommends Doug Stevenson · 2019-05 · Stack Overflow · on “Mismatch in count even with FieldValue.increment”
Without seeing your code, the only thing I can imagine is that your function isn't idempotent. It's possible that functions may be invoked more than once per triggering event. This would be an explanation why your count exceeds the expectation. Read more about Cloud Functions idempotency in the documentation [LINK] and also this video [this video] .
Recommends Renaud Tarnec · 2021-05 · Stack Overflow · on “Are cloud functions that trigger from authentication events guaranteed to run?”
Are cloud functions that trigger from authentication events guaranteed to run? Yes, as explained in the Cloud Functions doc [LINK] , for asynchronous background functions (i.e. non-HTTPS Cloud Functions, like Cloud Functions triggered by Authentication events), Cloud Functions "guarantees at-least-once execution of an event-driven function for each event emitted by an event source". Note that " at-least-once execution " means that Cloud Functions does not guarantee that it will be executed only once: in some rare cases the platform may run it more that once, see this video [this video] for more details.
Recommends DIGI Byte · 2021-05 · Stack Overflow · on “Any way to disable GCP pub/sub retries?”
You will need to consider idempotency and flag any recent retries to prevent them from firing again. This could be a timestamp stored in a database and filter based on time and any metadata you contain. Another important thing is to return a successful result. Doug covers this concept in a video, while it doesn't reference pubsub, it is still just as valid: [this video]
Cites Bob Snyder · 2018-02 · Stack Overflow · on “firestore cloud functions onCreate/onDelete sometimes immediately triggered twice”
See the Cloud Firestore Triggers Limitations and Guarantees [LINK] : Delivery of function invocations is not currently guaranteed. As the Cloud Firestore and Cloud Functions integration improves, we plan to guarantee "at least once" delivery. However, this may not always be the case during beta. This may also result in multiple invocations for a single event , so for the highest quality functions ensure that the functions are written to be idempotent. There is a Firecast video [this video] with tips for implementing idempotence. Also two Google Blog posts: the first [LINK] , the second [LINK] .
Cites samthecodingman · 2020-04 · Stack Overflow · on “Firebase Cloud Funtion call trigger mutiple time”
By design, Cloud Functions may be called multiple times. It is up to you to ensure that your functions can handle being retried [this video] . However, your error likely stems from the value of the update_at key. If this key's value is an object, the === and !== operands aren't going to produce the correct result. You will need to check the object for an "isEqual" or "equals" method, or use a third-party solution such as _.isEqual [LINK] . Below, I have refactored your code for performance and ease-of-use. I could not identify the rest object you were using for the API call, so I have substituted it for node-fetch . const fetch = require("node-fetch"); // see [LINK] // for object equality tests /
Cites Renaud Tarnec · 2020-12 · Stack Overflow · on “Firebase Cloud Function Increment Counter”
Is it reasonable to store the context.eventId in a subcollection document field and only execute the function if the new context.eventId is different? Yes, for your use case, using the Cloud Function eventId is the best solution to make you Cloud Function idempotent. I guess you have already watched this Firebase video [this video] . In the Firebase doc [LINK] from which you have taken the code in your question, you will find at the bottom, the similar code for a Cloud Function. I've adapted this code as follows, in order to check if a doc with ID = eventId exists in a dedicated ratingUpdateIds subcollection: exports.aggregateRatings = functions.firestore .document('restaurants/{restId}/ratings/{
Cites Roopa M · 2022-06 · Stack Overflow · on “How to run a Google Cloud Function with a timeout of 60 min from a Google Cloud Task?”
Idempotent means that **you should be able to call your Function multiple times with the same input data and the result of the initial call does not change.** for more details refer this [this video] and your understanding is right but 3)As you enabled retry configuration for `Cloud task`, it will retry to call the cloud function as long as it meets retry configuration instead of terminating the task.
Cites Madhav Kumar · 2022-07 · Stack Overflow · on “Getting cal cancelled error in firebase cloud functions”
3rd Edit: Finally I've found the solution to this problem. A same error occurred in 2 cloud functions and I solved both of them by following the same process. I'm not deleting the previous wrong answers, which didn't work for me, so that you also could know what won't work. This error occurs because the Firestore has hit a limit. I was trying to do lots of writes and updates in Firestore in parallel. Example: Here all the functions are called all together. This is very fast as all the tasks are done in parallel. And recommended by Firebase. But this might hit a limit in firestore and give error. exports.botsCompletingLectures = functions.region('asia-south1') .database.ref('triggerCloudFunct