Eliminate Functions Cold Start

We are trying to deploy a Nest.js API using Vercel functions, but we are experiencing a small cold start hovering around 4 seconds. I have read a few articles about it and I have noticed that once one article even suggested us to setup a cronjob to keep hitting the API so that it never spins down, but that suggestion is not there anymore.

What do I need to know, and what do I need to do in order to either keep the service up fulltime or drastically reduce the cold start in a Nest.js API?

The configuration I’m using:

{
  "version": 2,
  "builds": [
    {
      "src": "src/main.ts",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/main.ts",
      "methods": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]
    }
  ]
}

Hi, @giuseppe-oriumnetwor!

Cross-posting this from another thread, which may be helpful:

I asked v0 on specific recommendations, and it recommended the following:

  1. Consider implementing module caching in your main.ts
  2. Use lazy loading for non-critical modules
  3. Minimize your dependency tree

The old approach of using cron jobs to prevent spin-down isn’t recommended anymore, as Vercel has made significant improvements to function performance, including Rust-powered functions and bytecode caching.

Hope that helps!

2 Likes

How does the “hot” start feature work on paid plans? I have read some documentations but didn’t find anything explaining that. Is it something I have to manually enable or is it automatic?

Hi @giuseppe-oriumnetwor, it works automatically.

Vercel’s infrastructure now keeps a minimum of one function instance warm for production environments on paid plans.

1 Like

I was testing it in a preview environment, so this is probably the reason why I’m having cold starts, right? I’ll deploy it in production and see what happens, and if it works, I’ll mark this conversation as resolved.
Thanks again!

1 Like

Ok, I’m still facing some cold start. It hovers around 4 or 5 seconds, not too high, but more than I wanted it was. Is this the normal warm start or is there anything else I should do in order to reduce it to near zero?
I have already deployed the Api in a production environment, deployed at orium-nest-api-orium-network.vercel.app. You can se a hello world when you navigate to this link.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.