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

10 messages · Page 1 of 1 (latest)

naive raptor
#

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.

fathom orbitBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
carmine stream
naive raptor
#

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?

carmine stream
#

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

naive raptor
#

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.

carmine stream
#

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

#

(* 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)