sockets - How can a single-threaded NGINX handle so many connections? -


ngnix uses epoll notification know if there data on socket read.

let assume: there 2 requests server. nginx notificated 2 requests , starts to:

  • receive first request

  • parse ist headers

  • check boudary (body size)

  • send first request upstream server

  • etc.

nginx singe-threaded , can 1 operation @ same time.

but happens second request?

  1. does nginx receive second request during parsing first one?

  2. or begins handle second request after getting first done?

  3. or else don't understand.

if 1. correct don't understand how possible within single thread.

if 2. correct how can nginx fast? because nginx handles incoming requests sequentially. @ given time 1 request handling possible.

please me understand. thanks

nginx not single threaded application. not start thread each connection starts several worker threads during start. nginx architecture described in http://www.aosabook.org/en/nginx.html.

actually single threaded non-blocking application efficient design single processor hardware. when have 1 cpu , application non-blocking application can utilize cpu power. non-blocking application means application not call function might wait event. io operation asynchronous. means application not call simple read() socket because call might wait till data available. non-blocking application uses mechanism how notify application data available , can call read() without risk call wait something. ideal non-blocking application needs 1 thread 1 cpu in system. nginx uses non-blocking calls processing in multiple threads has no meaning because there no cpu execute additional threads.

the real data receiving network card buffer done in kernel when network card issue interrupt. nginx gets request in buffer , process it. has no meaning start processing request till current request processing done or till current request processing requires action might block (for example disk read).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -