云功能中的批量写入或事务处理? [英] Batched Writes or Transaction in Cloud Functions?

查看:72
本文介绍了云功能中的批量写入或事务处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下 Cloud Function ,并想确定我应该使用批量写入还是事务:

I have the following Cloud Function and want to find out whether I should use batched writes or a transaction:

const firestore = admin.firestore()
// The following two queries potentially return hundreds of documents.
const queryA = firestore.collectionGroup('a').where('b', '==', 'c'),
  queryB = firestore.collection('b').where('b', '==', 'c')

const snapshotA = await queryA.get(), snapshotB = await queryB.get()
const batch = firestore.batch()
for (const documentSnapshot of snapshotA.docs.concat(snapshotB.docs)) {
  batch.update(documentSnapshot.ref, { 'b': 'd' })
}
return batch.commit()

我确实要求此操作必须永不失败,但是,我看不到任何这种操作都会失败的情况.

I do require this operation to never fail, however, I do not see any case this would ever fail.

在这种情况下,是否有任何理由使用交易代替?
相反,是否有任何理由不使用此处的交易?

Is there any reason to use a transaction instead in this case?
Conversely, is there any reason not to use a transaction here?

推荐答案

您只是到Firestore(而不是阅读),因此乍一看,由于使用了事务,因此无需使用当您要根据字段的当前值或其他字段的值更新时,交易很有用"(请参阅​​

You are only writing to Firestore (and not reading), so at first sight, there is no need to use a transaction since "transactions are useful when you want to update a field's value based on its current value, or the value of some other field" (see doc), and you should use a Batched Write.

但是,您应该注意每笔交易或批量写入最多可以写入500个文档"(请参阅​​同一文档).由于您提到查询可能返回数百个文档",因此您在这里可能会遇到问题,必须分几批编写.

However, you should note that "each transaction or batch of writes can write to a maximum of 500 documents" (see same doc). Since you mention that your queries "potentially return hundreds of documents" you may encounter a problem here and have to write in several batches.

其他要点:您说:我确实要求此操作必须永不失败,但是我看不到任何可能会失败的情况".您不能确定它会 永远不会 失败:如文档中所述,Cloud Function可能由于内部错误而过早退出"(有关更多详细信息,请参见下面的要点) ).例如,Cloud Function平台和Firestore 1之间可能存在连接问题,并且批量写入失败(与代码无关).

Other point: You say "I do require this operation to never fail, however, I do not see any case this would ever fail". You cannot be sure that it will never fail: as explained in the documentation, a Cloud Function "might exit prematurely due to an internal error" (see point below for more details). For example there might be problem of connectivity between the Cloud Function platform and the Firestore one and your batched write fails (independently of your code).

要满足此要求(即我确实要求此操作永不失败"),您应该利用重试后台云功能的可能性以及批处理的事实的写入操作是原子完成的.有关后台重试Cloud Functions的信息,请参阅此文档,其中说明:

To fulfill this requirement (i.e. "I do require this operation to never fail") you should take advantage of the possibility to retry a background Cloud Function and of the fact that a batch of writes completes atomically. For background Cloud Functions retrying, see this doc which explains:

  1. 为什么会发生这种情况(在极少数情况下,由于内部错误,功能可能会过早退出,默认情况下,该功能可能会自动重试,也可能不会自动重试",并且
  2. 如何处理这种情况(用两个词来说,启用重试并使背景Cloud Functions成为幂等).

这篇关于云功能中的批量写入或事务处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆