firestore cloud functions onCreate/onDelete sometimes immediately triggered twice

Hajime Nakamura
2 min readFeb 23, 2018

--

I encounterd a problem with my firebase cloud function which is sometimes immediately triggered twice when used with firestore in combination.

There seems to be someone with the same problem on stack overflow.

As mentioned in the document Limitations and Guarantees section, there seems to be the possibility of multiple invocations for a single event.

In my case, I’m using a firebase cloud function to send a push notification when a post is posted, but due to this problem, it is frequent that two notifications are sent with one post.

Since it was written that it is necessary to write the function to idempotence in the document, I saved the event ID in the global variable and added a code to ignore the event which triggered already.

I’m watching how this works.

Please get in touch if you know how to solve this problem!

At a developers’ conference (Try! Swift Tokyo 2018) I asked a member of the Firebase team about best practices for sending push notifications using Firebase cloud function.

He said that they hadn’t established a best practice yet, and promised to e-mail me when they found a solution.

He also said that my implementation will not be able to resolve the problem, because a global variable is created for each Firebase cloud function.

In fact, my implementation only resolved the problem some of the time.

I think it was only successful when a function was implemented on the same instance by chance.

I also asked about an implementation using Firestore transactions, He said that transactions sometimes retry tasks in a transaction, and in such cases will not be able to resolve the problem.

Additional notes

When the function is triggered, it seems to work fine by saving the event ID in the firestore and checking it. Here is the code.

--

--