'NestJS for firebase functions: are the start times production-suitable?
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?
Solution 1:[1]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |