Concurrency FundamentalsThreads vs ProcessesMedium⏱️ ~2 min

Choosing Between Threads and Processes

The Key Question
Do you need walls between your workers, or do they need to share a kitchen? Isolation vs speed - that is the tradeoff.
Processes vs Threads: Quick Comparison
PROCESSESSeparate apartmentsCrash isolationSecurity separationSlow: 1-10ms to createIPC overheadTHREADSRoommates sharingFast: 10-100us to createInstant communicationNo crash isolationNeeds synchronization

Choose Processes When Safety Matters

Running code you do not trust? Put it in a separate process. Browser tabs run untrusted JavaScript. Each tab gets its own process so a malicious site cannot steal data from other tabs.

Need to contain crashes? Use processes. If a web server worker crashes handling a bad request, only that request fails. Other workers keep serving.

Choose Threads When Speed Matters

Need to share big data? Use threads. A database has gigabytes of cached data. All query handlers need access. Copying that between processes would be absurdly slow.

Doing parallel math? Use threads equal to your CPU cores. 8 cores means 8 threads. More threads just waste time switching.

Real Systems Use Both

Chrome: Process per tab (isolation) + threads within each tab (efficiency). Nginx: Worker processes (isolation) + async IO within each. Databases: Shared buffer pool via threads + separate processes for crash isolation.

No Right Answer: The best choice depends on your specific needs. Many systems combine both to get the benefits of each.
💡 Key Takeaways
Processes for safety: untrusted code, crash containment, privilege separation
Threads for speed: shared data, instant communication, parallel computation
CPU-bound work: threads = number of cores; more threads adds overhead
IO-bound work: more threads than cores is fine (threads wait on IO)
Real systems often combine both for best of both worlds
📌 Examples
1Chrome: process per tab (security) + threads within (rendering, JS, networking)
2Database: shared buffer pool (threads) + process isolation for crash safety
3Web server: worker processes (crash isolation) + thread pool per worker (efficiency)
← Back to Threads vs Processes Overview
Choosing Between Threads and Processes | Threads vs Processes - System Overflow