对同一订阅使用多个小型Firebase函数是否会受到惩罚?

对同一订阅使用多个小型Firebase函数是否会受到惩罚?,firebase,google-cloud-functions,Firebase,Google Cloud Functions,我有一个React web应用程序,它使用Firestore作为其数据库,每个Firestore集合具有多个(10+)Firebase功能,以产生副作用,例如发布到社交媒体等: exports.onUserSignUp = functions.auth.user().onCreate(user => { // Insert into users collection // Insert into profiles collection // Notify via Discord

我有一个React web应用程序,它使用Firestore作为其数据库,每个Firestore集合具有多个(10+)Firebase功能,以产生副作用,例如发布到社交媒体等:

exports.onUserSignUp = functions.auth.user().onCreate(user => {
  // Insert into users collection
  // Insert into profiles collection
  // Notify via Discord webhook
  // insert into history collection to keep track of actions performed by users
})

exports.onCityCreate = functions.firestore.document('cities/{cityId}').onCreate(doc => {
  // Notify via Discord webhook
  // Send tweet
  // Update cache in a collection
  // Insert into history collection to keep track of actions performed by users
})
等等

在Firebase函数教程中,他们通常建议您声明Firebase函数以产生以下副作用:

exports.onCityCreate = functions.firestore.document('/cities/{documentId}')
  .onCreate((snap, context) => {
    ...
  })
如果你有一个小的副作用,比如通过TwitterRESTAPI发送一条tweet,这是有意义的。但是随着时间的推移,我已经添加了越来越多的副作用,比如通过webhook发送到Discord,将新记录插入其他数据库,缓存数据等等

当我在AWS学习lambdas时,我被教导将我的函数变小,并限制为一个作业,而不是一个大函数。这意味着,如果一个副作用失效,它将不会降低其他副作用。它使代码更容易理解和阅读。它使调试更容易

因此,可以将上述功能拆分为每个作业:

exports.sendTweetOnCityCreate =  functions.firestore.document('cities/{cityId}').onCreate(doc => {
  // send tweet via REST API
})

exports.sendDiscordOnCityCreate =  functions.firestore.document('cities/{cityId}').onCreate(doc => {
  // send Discord message via webhook
})

exports.updateCitiesCache =  functions.firestore.document('cities/{cityId}').onCreate(doc => {
  // update a record in a cache collection with some kind of tally etc.
})

exports.recordCityInHistory =  functions.firestore.document('cities/{cityId}').onCreate(doc => {
  // insert into history collection to keep track of actions performed by users
})

如果为每个副作用声明一个小的、独立的函数,而不是像我目前拥有的“做所有事情”的一个大函数,是否会受到惩罚(性能-较慢或财务-来自谷歌的更多成本)?性能不会有明显的变化。无论有多少函数,事件从Firestore传递到函数所需的时间基本相同


使用一体式方法时,最糟糕的情况是您的函数没有大规模扩展,因为每个函数最多只能处理1000个并发服务器实例。但这是相当多的例子。如果将功能拆分,每个功能将能够扩展到1000个服务器实例。不过,这只在非常大的范围内有用。

您能为您的想法添加一些参考吗?