How to deal with firebase trigger function execution order

Viewed 2593

I have a firebase http function which appends data to a firebase database list. A second function is configured to process the list when it changes and then update some summary data. In my case, these updates come in bursts. I'm using node.js firebase functions.

Looking at the firebase logs, I see this sequence when starting with an empty list:

  1. add to list from http - list has 1 element
  2. add to list from http - list has 2 elements
  3. add to list from http - list has 3 elements
  4. add to list from http - list has 4 elements
  5. summarize list with 1 element
  6. summarize list with 3 elements
  7. summarize list with 4 elements
  8. summarize list with 2 elements

My problem is the summary only includes 2 elements instead of 4.

It would appear the summarize trigger functions are invoked in parallel instead of sequential so when several trigger in close proximity, the last one to finish may be be one of the first ones triggered instead of the last.

What approaches can be used to insure the summary computation has 'all the data' and a prior summary computation running slower doesn't overwrite a later one? Can firebase function triggers be serialized to execute in the order they are initiated?

Ideally I'd like to avoid computing the summary N times when a burst comes in so some solution where the summary could be 'scheduled' for some short time in the future and then canceled and rescheduled if a new event comes in would be nice.

2 Answers
Related