Processes, Threads & Concurrency
How servers juggle millions of simultaneous requests. Understand the physical boundaries of memory, the hidden latency cost of CPU context switching, and why modern architectures chose Event Loops and Goroutines over traditional thread-per-request models.
Server Concurrency Model Stress Test
๐ข 1. What is a Process? (The Apartment Building)
Imagine an apartment building. Each process is like a completely private, locked apartment. It has its own kitchen, bedroom, front door key, and power meter. People living in Apartment A cannot reach into Apartment B to grab a glass of water without ringing the doorbell (inter-process communication).
In computer hardware, a Process is an executing instance of a program. The Operating System creates a protective sandbox called a Virtual Address Space. Process A's memory address 0x00400000 points to completely different physical RAM chips than Process B's 0x00400000.
๐ฅ 2. What is a Thread? (Roommates in the Same Apartment)
If a process is an apartment, Threads are roommates living together inside that apartment. They share the kitchen fridge (the Heap) and the living room TV (the Code). However, each roommate has their own private backpack (their own Thread Stack & CPU Registers) to keep track of their personal tasks.
| Dimension | Process | Thread |
|---|---|---|
| Memory Boundary | Completely isolated virtual address space. | Shares Heap, Code, & Data with sibling threads. |
| Creation Overhead | Heavy (~10โ50ms, kernel page table cloning). | Lightweight (~10โ50ยตs, just allocates a stack). |
| Communication (IPC) | Slow: Sockets, Unix Pipes, Shared Memory maps. | Ultra-fast: Direct pointer reads in shared Heap. |
| Failure Blast Radius | Isolated: 1 crashed process doesn't kill others. | Fatal: If 1 thread corrupts memory, entire process dies. |
โฑ๏ธ 3. Concurrency vs Parallelism (The Master Chef)
Engineers frequently confuse these two terms. Rob Pike (creator of Go) famously said: "Concurrency is about structure; Parallelism is about execution."
One chef juggling 3 dishes. They chop onions for 5 seconds, flip the steak for 5 seconds, then stir the soup. Only 1 dish is actively touched at any microsecond, but all 3 make progress over time.
Four chefs cooking 4 dishes simultaneously. Each chef has their own stove (CPU core). Physical work happens at the exact same instant in time.
๐ 4. Context Switching โ The Hidden Latency Tax
Imagine you are reading 10 dense textbooks at the same time. Every 2 minutes, someone yells "SWITCH!". You must stop, write down the exact page and sentence on a bookmark, put the book back on the shelf, grab the next book, open to its bookmark, and spend 15 seconds remembering what the chapter was about.
If you switch books 100 times an hour, you spend more time managing bookmarks than actually reading! This is exactly what happens when an OS runs too many threads.
The CPU stops Thread A and dumps registers (RAX, RIP, stack pointer) into kernel memory.
OS scheduler algorithm (Linux CFS) picks the highest-priority runnable thread from the run-queue.
Switching processes flushes the TLB cache. CPU L1/L2 data caches get evicted (Cold Cache Penalty).
Thread B's registers are reloaded into hardware and the instruction pointer jumps to Thread B's code.
๐๏ธ 5. Three Server Architectures (The Interview Core)
Every web framework in the world (Spring, Node.js, Go, Nginx, Django, Elixir) picks one of these three architectures. In a system design interview, you must justify why you chose one over the others.
Every incoming HTTP socket connection is assigned a dedicated OS thread from a pool. The thread executes your code synchronously โ when it calls db.query(), the thread sits 100% blocked in sleep state until the database responds.
ONE single OS thread handles all connections using OS kernel notification mechanisms (epoll on Linux, kqueue on macOS). When an I/O query starts, the thread registers a callback and immediately services the next user. When data arrives, the event loop triggers the callback.
Combines the clean synchronous coding style of Model 1 with the ultra-low memory efficiency of Model 2. The Go runtime creates lightweight Goroutines that start with only 2KB stack size (grows dynamically). The Go M:N scheduler multiplexes 1,000,000 goroutines onto a small pool of 8 real OS threads.
resp := fetch()) without callback hell. Scales to millions of connections.โ ๏ธ 6. Concurrency Bugs: Race Conditions & Deadlocks
Because threads share the same Heap memory, concurrent access without synchronization causes silent, catastrophic data corruption.
Thread A acquires a hardware lock (mutex.Lock()) before reading. Thread B is forced to wait until Thread A finishes writing and unlocks. Only 1 thread touches the shared memory at a time.
Thread A holds Lock 1 and waits for Lock 2.
Thread B holds Lock 2 and waits for Lock 1.
Both threads freeze forever waiting on each other.
๐ฏ 7. Test Your Understanding
Your Java Spring Boot server handles 500 concurrent users with 5% CPU usage. When traffic spikes to 5,000 users, CPU usage stays at only 20%, but response latency jumps to 15 seconds and the server crashes with OutOfMemoryError. What is the root cause?
A single-instance Node.js chat server comfortably holds 50,000 open WebSockets. However, whenever a user requests a PDF invoice export (which computes a complex vector graphic in JavaScript), all 50,000 chat users experience a complete 3-second freeze. Why?
Full Deep-Dive Locked
This production architecture deep-dive, interactive canvas labs, and 5-language code templates require an active Pro Pass ($1/mo or $49 lifetime).