Next.js Discord

Discord Forum

NextJS 13 Bottlenecks using just 2/4 of my server resources and it freezes

Answered
Idra posted this in #help-forum
Open in Discord
Hello everyone!

I Made a website using NextJS13 (Pages Router) with around 50 pages, i put all the code in my vps that has 4 Cores, 8gb of ram and a 500mb Bandwidth.

I Ran a test load with loader.io:
5k Users in 1 Minute - No problems, it handles pretty good (Usage: 8% CPU, 0,800gb RAM, 2mb Bandwidth)
10k Users in 1 Minute - It lags, the website is very slow, without any errors in console (Usage: 25% CPU, 1,6gb RAM, 14mb Bandwidth)

I Mean, the problem is that my website lags in production even if it's using 2/4 of my VPS Server. Isn't this weird?
Theoretically the server would handle around 35k Users in 1 Minute without problems with the usage.. The main problem
is that i'm using Prisma with MySQL with my 12 API Routes, i'm using nginx with a reverse proxy to localhost:3000.

I Don't know if i need to upgrade my machine knowing that my website will lag using just 2/4 of the performance, maybe this is a limit
from nginx? I Searched online and i found a setting called worker_connections, i tried to change this from 1000 to 15000 but this
problem keeps happening, most of my pages are client rendered, could this mean someting?

Can someone help me? Thanks!
Answered by Asian black bear
I would look into something like pm2 in cluster mode with the load balancer and multiple processes. Or possibly renting EC2 machines with a single vcpu and using an external load balancer like ELB. That would require putting the mysql somewhere else though, which would make it even more expensive.
View full answer

36 Replies

from CPU usage is 25%, not saturated, it's I/O bound so the bottleneck could be MySQL. does it scale enough to handle 10k requests?
Is there any way i can verify this?
Okay, i recorded video: https://youtu.be/DxvsB5JZu3Q
I Found the "max_connections" setting on mysql and it was 150, i changed it to 15000 but nothing changes
you need to check usage of cpu, memory of MySQL instances to see if it is the bottle neck.
just check the usage by ur self.
@tafutada777 just check the usage by ur self.
dude i don't think you're helping me in any way😅
i don't know how to do it, how can i check the usage?
is it managed db? AWS, GCP or else?
@tafutada777 is it managed db? AWS, GCP or else?
it's a vps server from a service, i didn't bought it from google or amazon
up
up
Asian black bear
The vital performance stat you have not mentioned is disk IO, like is the disk queue getting full?
Not everything is bounded by CPU or memory
Does your VPS have an SSD volume or is it a regular hard disk?
When you say reverse proxy to localhost:3000, do you mean using next build and next start or is it connecting to next dev?
Asian black bear
If the VPS is on linux you might try the command iostat -x and focus on the columns aqu-sz and %util during a load test
For reverse proxy i mean that the traffic on my domain gets redirected to port 3000 using nginx
Asian black bear
@Idra I saw that you posted an iostat output, but it looked like the the VPS was unloaded so it doesn't really tell us too much.
@Asian black bear <@431049985258160128> I saw that you posted an iostat output, but it looked like the the VPS was unloaded so it doesn't really tell us too much.
Okay! i ran the command while load:
(These numbers were still while the load test was running)
up
European sprat
i think you need to do some more troubleshooting here or create some kind of reproducible example
not sure what you expect other people to be able to tell you here
Asian black bear
@Idra Are you using the newest version of Next?
Also, are you using the /pages router or the /app router?
Asian black bear
That it stops working well with the cpu at 25% specifically makes me suspicious that next start is single threaded, and you are actually running into 100% of a single core, which is indeed a terrible value on a 4 cpu VM. The docs do not mention anything about threading, and JS/node are inherently single threaded, so it actually makes sense. Also self hosting competes with Vercel so it double makes sense.
Asian black bear
I would look into something like pm2 in cluster mode with the load balancer and multiple processes. Or possibly renting EC2 machines with a single vcpu and using an external load balancer like ELB. That would require putting the mysql somewhere else though, which would make it even more expensive.
Answer
Asian black bear
Before any of that though, I would rent a single thread VM and make absolutely sure all of that extra infrastrcture was required.