Nextjs TTFB on reinit

Hi, we deploy our nextjs project with SSG. I have spent 2 days investigating slow start on deployed project.

Currently it starts with 3 seconds. Then it works with 500 ms, which is fine. But later function had the same init with 3 seconds again.

May 24, 02:02:34 PM: b8d94f55 INFO   info  - Loaded env from /var/task/.env
May 24, 02:02:34 PM: b8d94f55 INFO   [GET] / (SSR)
May 24, 02:02:38 PM: b8d94f55 Duration: 3210.38 ms	Memory Usage: 143 MB	Init Duration: 212.93 ms	
May 24, 02:07:04 PM: 1f252944 INFO   [GET] / (SSR)
May 24, 02:07:05 PM: 1f252944 Duration: 508.80 ms	Memory Usage: 145 MB	
May 24, 02:45:55 PM: c766cbab INFO   info  - Loaded env from /var/task/.env
May 24, 02:45:55 PM: c766cbab INFO   [GET] / (SSR)
May 24, 02:45:59 PM: c766cbab Duration: 3214.84 ms	Memory Usage: 143 MB	Init Duration: 209.58 ms	
May 24, 04:05:51 PM: ba961469 INFO   info  - Loaded env from /var/task/.env
May 24, 04:05:51 PM: ba961469 INFO   [GET] / (SSR)
May 24, 04:05:55 PM: ba961469 Duration: 3146.45 ms	Memory Usage: 142 MB	Init Duration: 212.01 ms	
May 24, 04:05:55 PM: a89812a0 INFO   [GET] / (SSR)
May 24, 04:05:55 PM: a89812a0 Duration: 422.96 ms	Memory Usage: 144 MB

I guess function is shutdowning itself. And then reinit on a request.
Can I somehow save init cache or prevent it from shutdowning?

Unfortunately, that’s not something AWS Lambda supports at the moment (we use AWS Lambda for Netlify Functions). The containers are short-lived and are terminated in a short time once the code has been executed.

What can I do to reduce cold start? Can we make AWS Lambda warm on init?

Sadly, not. As I said, Lambda is not meant to provide the feature you’re looking for.

You can try using an experimental feature: Edge Functions to see if it changes anything. To enable that, you’d have to add an environment variable: NEXT_USE_NETLIFY_EDGE = true.

Nothing changed after NEXT_USE_NETLIFY_EDGE .

After half an hour after deploy it still loads slowly.

May 30, 12:07:30 PM: 6aa84792 INFO   info  - Loaded env from /var/task/.env
May 30, 12:07:30 PM: 6aa84792 INFO   [GET] / (SSR)
May 30, 12:07:33 PM: 6aa84792 Duration: 2914.83 ms	Memory Usage: 143 MB	Init Duration: 204.94 ms	
May 30, 12:08:02 PM: 1422ce36 INFO   [GET] / (SSR)
May 30, 12:08:03 PM: 1422ce36 Duration: 411.57 ms	Memory Usage: 148 MB	
May 30, 12:08:04 PM: 433dda0a INFO   [GET] / (SSR)
May 30, 12:08:04 PM: 433dda0a Duration: 523.16 ms	Memory Usage: 160 MB	
May 30, 12:08:06 PM: d7b43785 INFO   [GET] / (SSR)
May 30, 12:08:06 PM: d7b43785 Duration: 406.74 ms	Memory Usage: 162 MB	
May 30, 12:09:26 PM: e99460ca INFO   [GET] / (SSR)
May 30, 12:09:27 PM: e99460ca Duration: 467.55 ms	Memory Usage: 162 MB	
May 30, 12:46:11 PM: a8aa2de0 INFO   info  - Loaded env from /var/task/.env
May 30, 12:46:11 PM: a8aa2de0 INFO   [GET] / (SSR)
May 30, 12:46:15 PM: a8aa2de0 Duration: 3158.90 ms	Memory Usage: 142 MB	Init Duration: 198.06 ms	

Unfortunately, in that case that’s just expected behaviour. AWS Lambda needs time to cold start and Next.js on Netlify makes use of Lambda to work.

@hrishikesh Hi! I have two projects deployed with netlify (free tier) and I’ve noticed that after short time when I access the website the start is slower (I guess is the same problem as above?) Should this be solved with nextjs runtinve v5? Because otherwise I’ll have to start looking around because I have all my web vitals failed because TTFB is more/less 2s and that’s not ok at all.

Thanks!

As mentioned above, this is something controlled by AWS Lambda. Next Runtime doesn’t affect this (at least not significantly), though I’d strongly recommend to upgrade to v5 anyways.

1 Like

@hrishikesh Hi! I’ve noticed that after setting the env variable NEXT_FORCE_EDGE_IMAGES=true the cold starts seems to be faster. Could potentially be a reason for this? Or it’s just me?