Hosting & Domaining Forum

Hosting & Domaining development => Programming Discussion => Topic started by: shahdeepak on Mar 24, 2023, 12:20 AM

Title: Nginx. Why processes, not threads?
Post by: shahdeepak on Mar 24, 2023, 12:20 AM
When developing a web server using the Windows API, I utilized threads to ensure efficient performance on systems with multiple cores. However, Nginx takes a different approach by using processes instead of threads. This prompts the question: why?

There are several possible reasons for this decision, but unfortunately, my research did not yield a definitive answer:

1. It could be to bypass the limit of file descriptors per process.
2. Another possibility is that it enhances fault tolerance, where if one process fails, the others can continue running.
3. There may also be security considerations involved, although I'm unsure about the specifics.

Although other hypotheses are welcome, I am particularly interested in uncovering the actual motivations behind Nginx's choice of worker processes over worker threads. Any insights or additional information on this topic would be greatly appreciated.

Correction:
It's important to note that threads are not necessarily equivalent to processes, and having one thread per connection is not always the case. Different web servers have their own strategies: Nginx uses worker processes, mywebserver uses worker threads, and Apache can employ either a thread or a process for each connection.

Specifically, I am curious about the rationale behind Nginx's decision to prioritize worker processes rather than worker threads. It would be valuable to gain a deeper understanding of the thought process behind this design choice.
Title: Re: Nginx. Why processes and not threads?
Post by: vizzmedia on Mar 24, 2023, 02:05 AM
One possible explanation could be that in the case of using threads, there is a shared address space which presents two significant concerns. Firstly, it creates the potential threat of one thread influencing another. Secondly, on a 32-bit machine, the available 4GB of address space is shared among all threads. In contrast, when using processes, each process has its own dedicated 4GB of address space. Therefore, the approach of utilizing processes seems to have more advantages in terms of resource allocation and isolation.

Additionally, by using processes instead of threads, it becomes easier to achieve fault isolation. If one process fails, it does not affect the others, ensuring a higher level of fault tolerance in the system.

Overall, the decision to use processes over threads in this context appears to provide greater security, scalability, and resource efficiency.
Title: Re: Nginx. Why processes and not threads?
Post by: justinthomsony on Mar 24, 2023, 03:50 AM
Moreover, the aspect of security in this context can be straightforwardly addressed.

Consider a scenario where there are 100500 users on a shared hosting platform, each having their own folder and corresponding site configuration. By launching each nginx-a process with the rights assigned to its respective user, the burden of managing access permissions for different users' directories lies with the operating system (which it handles adeptly).

If, on the other hand, threads were employed under a unified www-data user, there would be nothing preventing a high-ranking user from creating a symbolic or direct link in their configuration file to another user's folder. Consequently, this would enable them to potentially breach the security of other users' files via the web server.

employing processes with individual user rights serves as a security measure to prevent unauthorized access between different users' directories, ensuring robust isolation and protecting the integrity of the shared hosting environment.
Title: Re: Nginx. Why processes and not threads?
Post by: reeze on Mar 24, 2023, 05:24 AM
Initially, the intention was to utilize threads, but ultimately they were not implemented due to several reasons:

1) The programming process would have required more time and effort.
2) There was limited thread support in FreeBSD and Linux, which would have posed challenges.
3) Processes emerged as a more suitable choice since they naturally offer resource isolation, thereby mitigating competition for these resources.
Title: Re: Nginx. Why processes, not threads?
Post by: swatrih on Jun 17, 2024, 03:27 AM
Historically, Nginx was designed and developed with a strong focus on optimizing performance and scalability for serving static content and handling high concurrency. The choice to use worker processes was influenced by the prevalent operating system architecture and resource management mechanisms at the time of its inception in the early 2000s.

From a performance perspective, using worker processes in Nginx provides several advantages over worker threads:

1. Process isolation: Each worker process operates in its own memory space, which enhances fault tolerance and prevents issues in one process from affecting others. If a worker process crashes or encounters a memory leak, it can be terminated and respawned without affecting the overall system stability.

2. Efficient resource utilization: Worker processes in Nginx are designed to be lightweight and have a relatively small memory footprint. This allows for efficient utilization of system resources, as processes can be scaled up or down dynamically based on the workload.

3. Simplicity and portability: The process model is a more straightforward and well-understood concept across various operating systems and architectures. This simplifies the development and maintenance of Nginx, enhancing its portability and compatibility across different platforms.

From a security standpoint, the use of worker processes in Nginx can provide additional layers of protection:

1. Privilege separation: Nginx employs a master process that runs with elevated privileges, while worker processes run with reduced privileges. This minimizes the potential impact of vulnerabilities or compromises within individual worker processes.

2. Sandboxing: Each worker process operates within its own isolated environment, reducing the risk of interactions or interference between processes. This can help contain and mitigate the spread of security breaches or malicious code.

It's worth noting that the decision to use processes or threads is not a universally applicable choice. Different web servers and applications have varying requirements and architectural considerations. For example, Apache HTTP Server offers the option to use either a process-based or a thread-based model, depending on the specific use case and performance requirements.

Nginx's choice of worker processes reflects a design philosophy that prioritizes simplicity, performance, scalability, and fault tolerance – traits that have contributed to its widespread adoption and success in serving high-traffic websites and applications.
Title: Re: Nginx. Why processes, not threads?
Post by: FortaroDP on Aug 24, 2025, 12:03 PM
The master process spawns worker processes that handle requests independently - this avoids the shared-state headaches of threading and provides better isolation. If one worker crashes or gets compromised, others keep humming along without taking the whole server down.

Plus, it plays nice with multi-core systems; the kernel handles load balancing. Threads can be a hot mess with race conditions and debugging nightmares - processes keep things clean and predictable.