简体   繁体   English

Cloud Functions 中的 Firebase Firestore 冷启动缓慢

[英]Slow Firebase Firestore cold starts in Cloud Functions

On a cold start (after deploying or after 3hrs) the function to request a document from Firestore takes an incredible amount of time which is different to when it's used rapidly.在冷启动时(部署后或 3 小时后),从 Firestore 请求文档的功能需要花费大量时间,这与快速使用时不同。

Cold Start:冷启动:

Function execution took 4593 ms, finished with status code: 200

Rapid fire (me sending using the same function over and over):快速射击(我一遍又一遍地使用相同的功能发送):

Function execution took 437 ms, finished with status code: 200

My code for getting the documents is quite simple:我获取文档的代码非常简单:

function getWorkspaceDocument(teamSpaceId) {
    return new Promise((resolve, reject) => {
        var teamRef = db.instance.collection('teams').doc(teamSpaceId);
        teamRef.get().then(doc => {
            if (doc.exists) {
                resolve(doc.data());
                return;
            }
            else {
                reject(new Error("Document cant be found"));
                return;
            }
        }).catch(error => {
            reject(new Error("Document cant be found"));
        });
    });
}

I'm trying to make a Slack bot and the slow returns on Firebase Firestore throw time outs in Slacks API.我正在尝试制作一个 Slack 机器人,Firebase Firestore 的缓慢返回导致 Slacks API 超时。 Is there a way on Firebase to stop cold starts from happening and letting it persist through out? Firebase 有没有办法阻止冷启动的发生并让它持续下去?

If the cloud function needs to start a new instance your cold start time seems normal. 如果云功能需要启动新实例,则冷启动时间似乎很正常。 This is one drawback of a serverless function. 这是无服务器功能的一个缺点。

I think there is a problem with your implementation. 我认为您的实施存在问题。 Could you show more details? 您能显示更多详细信息吗?

Here is a nice little video about this topic: https://youtu.be/v3eG9xpzNXM 这是关于这个主题的漂亮的小视频: https : //youtu.be/v3eG9xpzNXM

Another thing I would suggest checking is the amount of memory allocated to a particular function.我建议检查的另一件事是分配给特定函数的内存量。 Each level selected increases non only the RAM, but the CPU frequency as well (and the costs, be careful and don't forget about the pricing calculator !).选择的每个级别不仅会增加 RAM,还会增加 CPU 频率(以及成本,请小心,不要忘记定价计算器!)。 There is a direct dependency between the package size of your function and the cold-start (source: https://mikhail.io/serverless/coldstarts/gcp/ ).函数的包大小与冷启动之间存在直接依赖关系(来源: https : //mikhail.io/serverless/coldstarts/gcp/ )。

I can see that you are using the Firestore admin package, which is not considered to be lightweight (source: https://github.com/firebase/firebase-admin-node/issues/238 ).我可以看到您正在使用 Firestore 管理包,它不被认为是轻量级的(来源: https : //github.com/firebase/firebase-admin-node/issues/238 )。 Thus, 128MB configuration might not be enough.因此,128MB 的配置可能还不够。

For our project increasing the RAM from 128MB to 512MB decreased the cold boot 10x from 20 seconds to 2.5seconds on average.对于我们的项目,将 RAM 从 128MB 增加到 512MB,将冷启动时间从 20 秒平均减少到 2.5 秒,减少了 10 倍。 Be sure not to overlook this in case you have several dependencies (7 in our case).如果您有多个依赖项(在我们的示例中为 7),请务必不要忽略这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM