如果我使用Promise制作文档,我会达到每个数据库每秒最大写入次数吗? [英] will I hit maximum writes per second per database if I make a document using Promise.all like this?

查看:51
本文介绍了如果我使用Promise制作文档,我会达到每个数据库每秒最大写入次数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序.而且我想向我的所有用户收件箱发送一条消息.代码在我的云函数中是这样的.

I am now developing an app. and I want to send a message to all my users inbox. the code is like this in my cloud functions.

        const query = db.collection(`users`)
                        .where("lastActivity","<=",now)
                        .where("lastActivity",">=",last30Days)

        const usersQuerySnapshot = await query.get()

        const promises = []

        usersQuerySnapshot.docs.forEach( userSnapshot => {

            const user = userSnapshot.data()
            const userID = user.userID

            // set promise to create data in user inbox
            const p1 = db.doc(`users/${userID}/inbox/${notificationID}`).set(notificationData)
            promises.push(p1)
            
        })

        return await Promise.all(promises)

Firebase有一个限制:

there is a limit in Firebase:

每个数据库每秒最大写入10,000(每个数据库最多10 MiB) 第二)

Maximum writes per second per database 10,000 (up to 10 MiB per second)

假设我是否向2万5千个用户发送邮件(向2万5千个用户创建文档),

say if I send a message to 25k users (create a document to 25K users),

await Promise.all(promises)的操作将运行多长时间?我担心操作将花费不到1秒,我不知道它是否会达到该限制或不使用此代码.我不确定这个的开通率

how long the operations of that await Promise.all(promises) will take place ? I am worried that operation will take below 1 second, I don't know if it will hit that limit or not using this code. I am not sure about the operation rate of this

如果我达到了这个极限,如何随着时间的推移而扩展呢?你能给个头绪吗?抱歉,我是新手.

if I hit that limit, how to spread it out over time ? could you please give a clue ? sorry I am a newbie.

推荐答案

如果要限制文档写入的速度,则可能不应盲目地在循环中开始大量的写入.尽管无法保证它们会以多快的速度发生,但是您有可能超过10K/秒/数据库的限制(取决于客户端的网络连接质量以及Firestore通常响应的速度).通过移动或Web客户端,我怀疑您是否会超出限制,但是在与Firestore数据库位于同一区域的后端上,谁知道-您必须对其进行基准测试.

If you want to throttle the rate at which document writes happen, you should probably not blindly kick off very large batches of writes in a loop. While there is no guarantee how fast they will occur, it's possible that you could exceed the 10K/second/database limit (depending on how good the client's network connection is, and how fast Firestore responds in general). Over a mobile or web client, I doubt that you'll exceed the limit, but on a backend that's in the same region as your Firestore database, who knows - you would have to benchmark it.

您的客户代码可以简单地用一些简单的逻辑来衡量其进度.

Your client code could simply throttle itself with some simple logic that measures its progress.

如果您有很多文档要尽快写入,而又不想限制您的客户端代码,请考虑使用

If you have a lot of documents to write as fast as possible, and you don't want to throttle your client code, consider throttling them as individual items of work using a Cloud Tasks queue. The queue can be configured to manage the rate at which the queue of tasks will be executed. This will drastically increase the amount of work you have to do to implement all these writes, but it should always stay in a safe range.

这篇关于如果我使用Promise制作文档,我会达到每个数据库每秒最大写入次数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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