How to make idempotent aggregation in Cloud Functions?

后端 未结 1 1988
小鲜肉
小鲜肉 2020-12-30 09:34

I\'m working on a Firebase Cloud Function that updates some aggregate information on some documents in my DB. It\'s a very simple function and is simply adding 1 to a total

相关标签:
1条回答
  • 2020-12-30 10:13

    The Cloud Functions documentation gives some guidance on how to make retryable background functions idempotent. The bullet point you're most likely to be interested in here is:

    Impose a transactional check outside the function, independent of the code. For example, persist state somewhere recording that a given event ID has already been processed.

    The event parameter passed to your function has an eventId property on it that is unique, but will be the same when an even it retried. You should use this value to determine if an action taken by an event has already occurred, so you know to skip the action the second time, if necessary.

    As for how exactly to check if an event ID has already been processed by your function, there's a lot of ways to do it, and that's up to you.

    You can always opt out of making your function idempotent if you think it's simply not worthwhile, or it's OK to possibly have incorrect counts in some (probably rare) cases.

    0 讨论(0)
提交回复
热议问题