4 ms·
Nginx and Erlang single threaded? Uhhhhh, that's just not true. Nginx and Erlang most certainly use multiple threads by default. They both use thread pools. I'
by slackingoff2017 9y ago
Nginx and Erlang single threaded? Uhhhhh, that's just not true.
Nginx and Erlang most certainly use multiple threads by default. They both use thread pools. I'm not sure about Erlang but Nginx has a single master thread that does async select on the main socket. Once it receives a connection it hands off to worker threads. This is a traditional multithreading setup with control/worker threads.
Node doesn't support threads because JavaScript doesn't. Performance would certainly be better for many workloads if it followed a traditional multithreading approach like Nginx. Async != Single threaded. They're not related concepts. You get the best performance using both non-blocking calls and multithreading , an approach used by Nginx and Netty. They both use nonblocking IO on the main thread and thread pools.
Node requires you to manually yield by calling Response.end() because it doesn't support multithreading. This basically duplicates OS level pre-emption manually and is my single biggest criticism of node. The worker/master thread model allows the runtime to manage worker resources or just defer to the OS in a flexible way.
- kbutler 9y agoYou are confusing threads with processes. Processes are isolated and do not share memory. Threads within a process share memory. For example, see https://msdn.microsoft.com/en-us/library/windows/desktop/ms684841(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms6... Nginx is single-threaded, multiple process: "Each worker process is single-threaded and runs independently" https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/ https://www.nginx.com/blog/inside-nginx-how-we-designed-for-... Erlang code is single threaded. Each Erlang "process" is an isolated, share-nothing single sequence of instructions, and you don't use semaphores, locks, or critical sections in writing Erlang code. The BEAM VM is implemented as a multithreaded process, but this is not exposed to the Erlang code, and Erlang code can be transparently distributed across multiple instances of the BEAM VM (processes) even on separate hosts. You're correct on node.js, and "basically trying to re-create preemptive threading provided by the OS."
- slackingoff2017 9y agoNginx recently implemented thread pools for blocking operations. The distinction between threads and processes depends on the OS. The "nothing shared" model used in Linux forking actually shares the underlying memory until the processes diverge (copy on write). The difference between this and threads is that threads expose the shared memory by default. IMO the difference between threads and processes is somewhat academic. I don't know much about BEAM
- robotresearcher 9y ago> IMO the difference between threads and processes is somewhat academic. Shared memory vs. isolated memory is a huge difference. You use completely different sync/protection/IPC approaches with each, so programs look very different. And the copy-on-write optimization of logically isolated memory doesn't effect that. The beauty of it is that it speeds up forking and saves memory without changing your IPC-based programming model.
- lmm 9y ago> IMO the difference between threads and processes is somewhat academic. At a linux-implementation level the difference is almost irrelevant. At a code level the difference between your variables being shared by default versus "thread"-local by default is huge.
- castell 9y ago> Erlang code is single threaded. Each Erlang "process" is an isolated, share-nothing single sequence of instructions, and you don't use semaphores, locks, or critical sections in writing Erlang code. The BEAM VM is implemented as a multithreaded process, but this is not exposed to the Erlang code The same is true for PHP. No wonder Facebook developed HHVM for PHP and also uses Erlang with WhatsApp and other parts.
- lou1306 9y agoWell, Whatsapp servers have always been Erlang-based (well before the acquisition by Facebook) but your point stands.
- zzzcpan 9y ago"Nginx has a single master thread that does async select on the main socket. Once it receives a connection it hands off to worker threads" Nginx has a master process and worker processes. Master process doesn't do anything, but manage worker processes in case they die or need to be restarted on configuration change. A worker process runs a single event loop that accepts connections. It doesn't hand off them anywhere, but just schedules connection handlers to be run in the same event loop. Worker processes don't share memory (almost, there are some sysv shared memory segments for a few things, but this is irrelevant). Thread pools in nginx were added only relatively recently to do blocking file I/O on linux, which is kind of slow otherwise in some scenarios.
- hakcermani 9y agoNode is single threaded only in Javascript land. Internally it does use threads !
- tracker1 9y agoMentioned in another response, but Node.js does indeed use threads/threadpool... It's used internally for all async IO, and by a number of binary modules as well.