Replies: 2 comments 1 reply
-
Hi @sayou, High CPU usage by next-server in production can stem from various causes. Here are some common areas to investigate and suggestions: Possible causes Infinite loops or expensive loops in React server components or API routes Misconfigured ISR (Incremental Static Regeneration) Memory leaks or unclosed database connections Next.js or Node.js version issues Heavy or blocking middleware Debug tips Profile CPU usage with clinic.js or Node’s built-in --inspect and profiling tools. Add instrumentation/logging in server components, API routes, and middleware to narrow down hotspots. Test with a minimal reproduction (simple app with no custom logic) to isolate if issue is app-specific or Next.js core. Check for large or frequent ISR revalidations. Suggestions Monitor and optimize server components and API routes for expensive work. Review your ISR and caching strategies. Consider horizontal scaling or increasing instance size if legitimate load. Investigate PM2 settings (max memory, restarts). If possible, switch to Vercel or a managed platform to isolate infrastructure issues. If you can share a minimal repro or profiling logs, the community can help dig deeper. |
Beta Was this translation helpful? Give feedback.
-
Hello @SametDulger Thank you for your time and valuable input on this issue. Apologies for the delay in my response. Following the recommendation, I profiled the application under load using clinic.js to investigate the CPU spikes. I ran the following command:
The clinic doctor report confirms a significant performance issue, showing a sharp increase in Event Loop Delay and Event Loop Utilization under load. ![]() Additionally, our production PM2 logs show a high frequency of ResponseAborted errors during these periods, which suggests that the server is unable to process requests before the client connection is terminated.
To provide more context, I'm sharing the relevant parts of our configuration below. Middleware: Our middleware handles i18n routing, protected routes, and security headers. My primary suspicion is that a bottleneck might exist here, as it runs on most incoming requests.
Next.js Configuration:
Deployment & PM2 Configuration: Here is our CI/CD pipeline and PM2 configuration for additional context on our production environment.
Partial package.json Dependencies: We are using Next.js ^15.4.3 and React 19.1.0.
Analysis & Next Steps Given the clinic.js report and the ResponseAborted errors, the event loop is clearly being blocked under load. The complexity of the middleware seems like the most probable cause. Do you have any suggestions for how to best debug or profile the middleware itself to identify the exact bottleneck? For instance, is there a way to add more granular logging or performance marks to see which part (authentication, i18n, header application) is consuming the most time? I am ready to run any further diagnostics or provide additional information that could help us resolve this. Thank you again for your assistance. Best regards, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi,
I’m experiencing very high CPU usage in production when running my Next.js app with next start. The issue began recently.
Environment
Symptoms
After deployment, CPU usage spikes significantly:
CI/CD Context
Deployment is fully automated using GitHub Actions. Here’s a summary of what happens after a commit:
Question
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions