Next.js Discord

Discord Forum

High Initial Server Response Time

Answered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
Hi there! Bit of a newbie question - I'm using Next 14.1 and the App Directory, and I'm noticing that my page's initial load times feel very slow. Navigating between tabs is better, but still not optimal.

I'm getting >2s server response times for the initial document load on prod and development environments, ~1s or so when navigating between tabs. How would I start to debug where this slowdown is happening, since it seems to be happening on the server?

The only blocking call that I can think of would be my calls to our GraphQL API, but those should all resolve much quicker than 2s. Even then, I've tried removing my react-query prefetch in the main page server component - no change in load time. I've tried turning the entire page into a client component, still seeing 2s response times.

Would appreciate any advice/suggestions. Thanks
Answered by Northeast Congo Lion
switch to edge runtime. I've seen my cold start times decrease by quite a bit, especially with a cold cache
View full answer

56 Replies

@DirtyCajunRice | AppDir so its more likely your code
Northeast Congo LionOP
Sure, fair enough - any suggestions on how I could go about profiling/debugging where this slowdown is though?
or go the opposite route and remove everything, adding back chunks till its slow
Blue whiting
@Northeast Congo Lion did you find a solution - i am struggling with the same problem. 2 sec server response times. Yet if i host the entire application on railway i get 40ms....
there is no cold-start on railway
keep in mind if your app is getting a steady flow of traffic, the cold starts will be less frequent because your users will be keeping the lambdas warm for you
alternatively you can pre-heat your lambdas
by running a cron job every 5 minutes that pings your lambdas
to keep them warm
Blue whiting
I see thanks @linesofcode but if i just refresh the page i get 2-3 sec response time? the cache never hits either.
not sure about that...
Blue whiting
Thanks for the information regardless - something new i learnt for today
it could be something else
unrelated to cold starts, perhaps you're not memoizing your db connection
and paying a cost for each connection
Blue whiting
The thing is its not my repo - its a prewritten repo from medusajs - and they do not have this problem
no problem
Blue whiting
i basically copied the repo and hosted it myself
do they host it on vercel?
Blue whiting
correct
maybe they are artifically keeping it warm
Blue whiting
Could be - i have no clue really why this would happen - development is also super fast
another thing to consider
is what region your function is deployed to
Blue whiting
So my Postgres / Redis is set to EU and my region is set to EU
and if its making subsequent calls, where those endpoints are located
Blue whiting
Postgres / Redis on railway - region for vercel EU for frontend
EU?
Blue whiting
Correct im from Africa
so thats the lowest ping for me
London to be specific
What about af-south-1 ?
Blue whiting
I tried that as well - also same problem
i thought the same thing
:lolsob:
i feel i must be doing something wrong
to spice it up even more - the admin UI ( of the backend ) is hosted on vercel too - speaking to the same API and the server response time is normal ( 200ms ) for me
Blue whiting
I will keep digging around and if i find anything will post here 🙂
@linesofcode by running a cron job every 5 minutes that pings your lambdas
Blue whiting
appreacite the new information
No problem. Good luck
(Btw you can set that up on GitHub actions or vercel even or railway)
Be sure to send an http OPTIONS request
It’s enough to preheat the lambda without executing the actual logic
@Blue whiting <@361910844143173632> did you find a solution - i am struggling with the same problem. 2 sec server response times. Yet if i host the entire application on railway i get 40ms....
Northeast Congo LionOP
switch to edge runtime. I've seen my cold start times decrease by quite a bit, especially with a cold cache
Answer
it's not always feasible to switch to edge
Northeast Congo LionOP
I realize that, but that is what helped for our case
@Northeast Congo Lion switch to edge runtime. I've seen my cold start times decrease by quite a bit, especially with a cold cache
Blue whiting
thanks @Northeast Congo Lion 🙂 appreciate the info