Will multiple cloud functions for the same trigger cause problems? #AskFirebase

Viewed 874

Should cloud functions be consolidated per trigger, or is it fine to write multiple functions for the same trigger? In particular, if I'm reading documents inside... Are there significant performance or billing implications?

// Example: multiple onWrite functions triggered by the same Firestore doc path
functions.firestore.document('myCollection/{itemId}').onWrite((change, context) => {
   // do one complex thing, potentially reading/writing data
}

functions.firestore.document('myCollection/{itemId}').onWrite((change, context) => {
   // do another complex thing, potentially reading/writing the same or different data
}

or...

// Example: one trigger and a monolithic function handling everything...
functions.firestore.document('myCollection/{itemId}').onWrite(async (change, context) => {
   const otherDataSnapshot = await admin.firestore().ref('myPath').once('value').then();
   this.doOneComplexThing(change, context, otherDataSnapshot);
   this.doAnotherComplexThing(change, context, otherDataSnapshot);
}

const doOneComplexThing = (change, context, otherDataSnapshot) => {
   // do one complex thing
}

const doAnotherComplexThing = (change, context, otherDataSnapshot) => {
   // do that other complex thing
}

AskFirebase

1 Answers

If you have two functions trigger instead of one, you will be billed for two invocation for each onWrite rather than just one. It will always be more expensive. There may be good reasons for splitting it up that make it worthwhile to split it up, so you will have to decide that for your specific case.

Related