Linux Internals
Threads vs Processes: What It Means for Server Performance
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيConfiguration files for web servers, application runtimes and databases are full of settings like worker_processes, pm.max_children, workers or max_connections. Behind each of them is the same design choice: should work run in separate processes, in threads that share one process, or in a small number of event loops? The answer affects memory use, crash isolation and how well your server uses its CPU cores. This article explains how Linux treats threads and processes, the trade-offs between them, and how to size workers sensibly on a VPS.
Processes: separate address spaces
A process is a running program with its own virtual memory, open files, environment and security identity. When one process writes to memory, no other process sees the change unless they deliberately share memory. Processes are created with fork() (or clone() underneath), and Linux uses copy-on-write so the child does not duplicate the parent's memory until either side modifies a page.
Strengths of the process model:
- Isolation: a crash or memory corruption in one worker does not take the others down.
- Simplicity: no shared mutable state means fewer locking bugs.
- Easy recycling: a worker that leaks memory can be replaced after N requests.
The cost is memory. Each process has its own heap, and interpreted runtimes such as PHP or Python load their code and libraries per process, so ten workers can mean ten copies of much of that memory.
Threads: shared memory inside one process
A thread is an independent flow of execution inside a process. All threads in a process share the same memory, file descriptors and code, but each has its own stack and registers. In Linux both are created by clone() with different flags, and the scheduler treats every thread as a schedulable task.
Strengths of threads:
- Lower memory per unit of concurrency, because code and data are shared.
- Cheap communication, since threads read the same data structures directly.
- Faster creation than full processes.
The risks are the mirror image: a bug in one thread can corrupt data used by all of them, and shared state needs locks, which brings contention and deadlocks. A segmentation fault in any thread kills the whole process.
Event loops: a third option
Many high-performance servers avoid one thread per connection altogether. A single thread waits for many sockets at once using the kernel's readiness APIs and handles whichever is ready. Nginx, Node.js, Redis and HAProxy are built around this idea, which is covered in detail in epoll and handling many connections. Event loops scale to huge connection counts with little memory, but any slow, blocking operation inside the loop stalls every connection it serves.
How common software uses them
| Software | Model |
|---|---|
| Nginx | A few worker processes, each running an event loop |
| PHP-FPM | Pool of worker processes, one request per process at a time |
| Apache (event MPM) | Processes containing multiple threads |
| PostgreSQL | One backend process per client connection |
| MySQL / MariaDB | One thread per connection (thread pool optional) |
| Node.js | One main event loop thread plus a helper thread pool |
| Java / Go services | Many threads (or goroutines) inside one process |
Context switches and why "more workers" is not free
A CPU core runs one thread at a time. When there are more runnable threads than cores, the kernel switches between them. Each switch saves and restores state, and it also tends to evict useful data from the CPU caches. A few thousand switches per second is normal; very high rates combined with rising latency suggest too many active workers competing for too few cores.
You can measure this:
vmstat 1 5 # "cs" column = context switches per second, "r" = runnable tasks
pidstat -w 1 5 # per-process voluntary and involuntary switches (sysstat package)If r stays well above your vCPU count, work is queuing for CPU time. Adding workers then increases memory use and switching without adding throughput.
Sizing workers on a VPS
Start from the number of vCPUs, which you can check with nproc, and from the type of work:
- CPU-bound work (image processing, compression, heavy computation): roughly one worker per vCPU. More rarely helps.
- I/O-bound work (requests waiting on a database or external API): more workers than cores is fine, because most of them are waiting rather than computing.
- Memory is the hard limit. Measure the real memory of one worker and divide the RAM you can spare by it.
For PHP-FPM, for example:
ps -o rss= -C php-fpm8.3 | awk '{s+=$1; n++} END {print s/n/1024 " MB average"}'Adjust the binary name to your PHP version. If each worker uses about 60 MB and you can give PHP 1.5 GB, pm.max_children around 25 is a sensible starting point, then watch latency and memory under real traffic.
To see the threads of any running process:
ps -T -p <PID>
top -H -p <PID>A quick decision checklist
- Is the work mostly CPU or mostly waiting? This sets the worker-to-core ratio.
- How much memory does one worker really use? This sets the ceiling.
- Do you need crash isolation between requests? Processes give it for free.
- Does the runtime use a garbage collector? Large shared heaps behave differently, as explained in garbage collection and app performance.
- Change one setting, load test, and compare latency percentiles, not only averages.
FAQ
Are threads always faster than processes?
No. Threads are cheaper to create and share memory more efficiently, but heavy lock contention can make a threaded program slower than independent processes. For many web workloads the difference is small compared to database and network time.
Why does Linux show threads as separate entries in htop?
Because the kernel schedules each thread as its own task with its own ID. htop can show or hide them; press H to toggle user threads. They still share the memory of their parent process, so do not add up their memory columns.
How many workers should I run on a 2 vCPU VPS?
There is no universal number. For CPU-heavy tasks start with two. For typical web apps that spend most of each request waiting on a database, more workers can help, limited by available RAM. Measure under realistic load and adjust.