Home > Software design >  Number of reads for multiple Firebase trigger functions doing similar things
Number of reads for multiple Firebase trigger functions doing similar things

Time:06-11

I have an onUpdate firestore trigger function that does multiple things:

functions.firestore.document('document').onUpdate((change, context) => {
  const updatedObject = change.after.data()
  if (updatedObject.first) {
    doFirst()
  }
  if (updatedObject.second) {
    doSecond()
  }
})

I am thinking of splitting this trigger into 2 smaller triggers to keep my functions more concise.

functions.firestore.document('document').onUpdate((change, context) => {
  const updatedObject = change.after.data()
  if (!updatedObject.first) {
    return
  }
  doFirst()
})

functions.firestore.document('document').onUpdate((change, context) => {
  const updatedObject = change.after.data()
  if (!updatedObject.second) {
    return
  }
  doSecond()
})

The firestore pricing docs mentions the following:

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)

Would this increase the number of reads from 1 to 2?
The docs does not clearly state the behavior when there are multiple functions listening to the same event.

A more general question I have is would increasing the number of functions listening to the same event increase the number of reads and hence increase my bill?
Is there a best practice for this issue?

CodePudding user response:

firebaser here

The document data passed to Cloud Functions as part of the trigger (so change.before and change.after) comes out of the existing flow, and is not a charged read. Only additional reads that you perform inside your Cloud Functions code would be charged.

  • Related