Next.js Discord

Discord Forum

App route handler, initial third party API call is really slow.

Unanswered
Chartreux posted this in #help-forum
Open in Discord
ChartreuxOP
Hi Guys,

I'm facing a performance issue that needs quick resolution 😦

Our architecture is relatively simple: we have a self-hosted Next.js 13.4.7 app, along with a few legacy PHP APIs that read from the same database. I'm making requests to the PHP APIs from Next.js route handlers because we want to keep the traffic internal.

Now, here's where things get strange:

When I make calls to the PHP APIs via Postman or Thunder, the response times are solid, averaging around 80 - 100ms. However, when I make the same calls from a next route handler, the first request always takes significantly longer—roughly 10 times the Postman response time at around 1000ms. Any subsequent requests from the same route handler within the same context are ultra-fast, clocking in at a maximum of 20ms. Even more peculiar is that changing the order of execution for the API calls doesn't make any difference; the first request is always slow.

Initially, I thought this might be due to a cold start of the handler. However, after conducting some research, I'm fairly certain that our self-hosted version uses the built-in server and isn't subject to cold starts. To further investigate, I tried calling other external services (like the Rick and Morty API at https://rickandmortyapi.com/) and encountered no issues at all. The response time was almost the same between Next.js and Postman. Therefore, I'm leaning towards the conclusion that the problem might lie within the APIs. However, I need to be certain before transferring this issue to the Backend team.

Any assistance, tips, or suggestions would be greatly appreciated, as I'm quickly running out of ideas.

9 Replies

@Chartreux Hi Guys, I'm facing a performance issue that needs quick resolution 😦 Our architecture is relatively simple: we have a self-hosted Next.js 13.4.7 app, along with a few legacy PHP APIs that read **from the same database**. I'm making requests to the PHP APIs from Next.js route handlers because we want to keep the traffic internal. Now, here's where things get strange: When I make calls to the PHP APIs via Postman or Thunder, the response times are solid, averaging around 80 - 100ms. However, when I make the same calls from a next route handler, the first request always takes significantly longer—roughly 10 times the Postman response time at around 1000ms. Any subsequent requests from the same route handler within the same context are ultra-fast, clocking in at a maximum of 20ms. Even more peculiar is that changing the order of execution for the API calls doesn't make any difference; the first request is always slow. Initially, I thought this might be due to a cold start of the handler. However, after conducting some research, I'm fairly certain that our self-hosted version uses the built-in server and isn't subject to cold starts. To further investigate, I tried calling other external services (like the Rick and Morty API at https://rickandmortyapi.com/) and encountered no issues at all. The response time was almost the same between Next.js and Postman. Therefore, I'm leaning towards the conclusion that the problem might lie within the APIs. However, I need to be certain before transferring this issue to the Backend team. Any assistance, tips, or suggestions would be greatly appreciated, as I'm quickly running out of ideas.
since you have verified that the issue is only there for your PHP API, not for any other third party APIs, i think it's a good enough time to let the backend team know
ChartreuxOP
I want to be 100% sure that we are not dealing with cold start. What will be a solid proof that our handlers does not have cold start?
cold start is only applicable to serverless architecture, so Vercel, AWS Lambda, Netlify. If you self-host, you can be 100% sure there are no cold starts
ChartreuxOP
Thanks for the response. I guess I will have to move the ticket to the guys. The only concern that I have is the postman / thunder requests but I guess this is another context.
i think in postman/thunder you also have the option to copy the request as fetch(), try it
copy the fetch, do it in next.js route handlers and see if the perf is the same
at first, i thought this is due to some caching behaviour nextjs does https://nextjs.org/docs/app/building-your-application/data-fetching/caching#fetch, but this deduplicating/caching should (1) not harm perf that much and (2) be applied to all requests regardless of origin (so third party APIs are deduplicated too)
(* assuming you are using prod mode. in dev mode it's not unheard of that a route handler is slower during first run as the dev server has to compile it)
ChartreuxOP
Yes I can confirm exporting postman request as javascript fetch and trying in the handler the behaviour is the same. I can conclude the topic is completed.