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.