用于 firebase 功能的 NestJS:开始时间适合生产吗?
有很多关于如何使用 Nestjs 设置 Firebase 功能的文章,我已经在本地计算机上开发了大约一个月。终于,到了释放的时候了。我使用了 Nestjs 和 mikro orm 的组合来处理我的服务器和数据库层,有 12 个实体和大约 20 个路由。当地一切都像黄油一样顺利。
然而,在 Firebase 功能环境(默认)中,你有 256MB RAM,天知道有多少 vCPU,突然我的冷启动 + 30 秒的 Nest 旋转,这意味着它不再是生产就绪环境。相比之下,Express 在大约 500 毫秒+冷启动时间内启动。
以下是一些日志屏幕截图,显示每个步骤所花费的时间:
我是否错误地构建了我的应用程序,或者 Nest 在小型环境中是否那么慢?我无法想象延迟加载会有所帮助,并且我会自动发现 mikro orm 的实体。有什么我可以尝试加快这个过程的吗?或者有人对 firebase 函数中的 Nestjs 有任何经验吗?
There's plenty of articles on how to set up firebase functions with nestjs, and I've been developing this for about a month now on my local machine. Finally, it was time to release. I used a combination of nestjs and mikro orm to handle my server and database layer, with 12 entities and around 20 routes. Everything ran smooth as butter locally.
However, in a firebase functions environment (defaults) where you have 256mb of RAM and god knows how much vCPU, suddenly my cold start + 30ish seconds of nest spinning up, it meant that it's no longer a production ready environment. In contrast, express spun up in around 500ms + cold start time.
Here's a few log screenshots to show the time each step took:
Could I have architected my application incorrectly or is Nest just that slow in tiny environments? I can't imagine lazy loading would help, and im auto discovering entities for mikro orm. Is there anything I can try to speed up the process or does anyone have any experience with nestjs in firebase functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NestJS 有一个关于在冷启动环境中优化服务的指南。这可能是一个很好的起点。
对于任何需要在冷启动时完成大量设置工作的情况(例如您正在经历的 NestJS 旋转 30 秒),
minInstances
选项可能值得使用。它可以让您保持最少数量的“热”实例,从而降低最终用户遇到冷启动的可能性。您还可以尝试使用
调整该函数可用的内存量内存
选项(更多的内存也给你更多的CPU)。NestJS has a guide on optimizing serving in environments that have cold starts. That might be a good place to start.
For any case where a lot of setup work has to be done on cold start (like the 30 seconds of NestJS spinning up that you're experiencing), the
minInstances
option may be worth using. It lets you keep a minimum number of instances "hot" so that there is a lower chance of an end user experiencing a cold start.You can also try adjusting the amount of memory available to the function with the
memory
option (more memory gives you more CPU too).