3

I have heard very interesting thing..

If we have 1 cpu and we spawn 2 worker processes, it means that each worker process uses 50% resource of that 1 cpu. If we had spawned 1 worker processes, resources from cpu for this worker process would be 100%. So for 1 cpu only, it's better to have only 1 single worker process.

Then he continues:

worker_connections is how many connections each worker process can accept. I have 1024.

Then he continues again:

So, worker_processes * worker_connections = max connections.

So question: if worker_processes * worker_connections = max connections, then wouldn't it be still great to have 2 worker processes even for only 1 cpu? that way 2 * 1024 = 2048 connections would be better than 1 * 1024. Am I wrong or what happens?

1 Answer 1

5

NGINX uses an event driven model. Most activity from webserving, the CPU is waiting for IO from disk or to the network. Nginx does not block while waiting for something to happen.

If the problem you are trying to solve is How do I get 2048 connections with one CPU? then your best configuration would be.

worker_processes number auto; worker_connections 2048; 

The auto will default to number of CPUs, which is 1.

Connections are lightweight in nginx. They don't use much memory.

If you have 2 worker_processes and 1 CPU, then the kernel will have to keep switching between the 2 processes. This context switching adds some overhead that you don't need.

There is quite a good write up here: https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/

Sign up to request clarification or add additional context in comments.

2 Comments

What if I create 10 worker processes?
@shashwatsrivastava that will create too much context switching and it can degrade the performance

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.