Processes, threads and concurrency
The difference between a process and a thread, and the classic bugs that appear when work runs in parallel.
On the roadmap:Beginner · step 6: Scaling one server
Lesson 11 of 23 · 8 min read · quiz · exercise
Process versus thread
- A process is a running program with its own memory space. Processes are isolated: one crashing does not corrupt another. Communicating between them needs explicit channels (pipes, sockets, shared memory).
- A thread is a unit of execution inside a process. Threads share the process's memory, so they communicate cheaply, but they can also corrupt each other's data.
- A context switch (the CPU changing what it runs) is cheaper between threads than between processes.
| Process | Thread | |
|---|---|---|
| Memory | separate | shared |
| Creation cost | higher | lower |
| Isolation | strong | weak |
| Crash impact | contained | can take down the process |
Figure SDA-CORE-11-1 · From a program on disk to threads · © forcsguy.com
Concurrency versus parallelism
- Concurrency: dealing with many things at once (interleaving tasks).
- Parallelism: actually running at the same time on several CPU cores.
A single core can be concurrent (switching between tasks) but not parallel.
Models you will meet
- Thread per request: simple, but memory and switching costs limit how many you can run.
- Event loop (Node.js, Redis, Nginx): one thread handles many connections using non-blocking I/O. Efficient for I/O-bound work, but one slow task blocks everyone.
- Coroutines / async-await and lightweight threads (goroutines, virtual threads): cheap concurrency that reads like sequential code.
- Worker pool: a fixed number of workers pull jobs from a queue, bounding resource use.
Classic concurrency bugs
- Race condition: result depends on timing. Cure with locks, atomic operations or by not sharing state.
- Deadlock: threads wait on each other's locks forever. Cure with a consistent lock order and timeouts.
- Starvation: a thread never gets its turn.
- Visibility problems: one thread's write is not seen by another without proper synchronisation (memory barriers).
Figure SDA-CORE-11-2 · Classic concurrency bugs · © forcsguy.com
Practical rules
- Prefer immutable data and message passing over shared mutable state.
- Keep critical sections small.
- Use thread-safe data structures from the standard library.
- Bound everything: thread pools, queues and connection pools, so overload degrades gracefully.
Real-world reading
Related reading
- Memory Optimization in Redis at WattpadWattpad engineers · WattpadCase Studies
- Distributed Locks using Redis at GoSquaredGoSquared engineers · GoSquaredCase Studies
- The Chubby Lock Service (paper)Mike Burrows · GooglePapers
- Reduce Memcached Memory Usage by 50% at TrivagoTrivago engineers · TrivagoCase Studies
Show 6 moreShow fewer
- Chubby: Lock Service for Loosely Coupled Distributed Systems at GoogleGoogle engineers · GoogleCase Studies
- Eventually Consistent - RevisitedWerner Vogels · AmazonBlogs
- Black-Box Auditing: Verifying End-to-End Replication Integrity between MySQL and Redshift at YelpYelp engineers · YelpCase Studies
- Debugging Java Memory Leaks at AllegroAllegro engineers · AllegroCase Studies
- Trivago Engineering BlogTrivago engineers · TrivagoBlogs
- Walmart BlogWalmart engineers · WalmartBlogs
Check your understanding
Question 1 of 3
0 correct so far
What is the key difference between a process and a thread?
Practise it: One server is not enough
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
Your URL shortener went live on a single server that runs the app and the database. A newsletter just linked to it and traffic jumped to 900 requests per second.
Goal: Serve the traffic with under 1% errors and a 95th percentile under 300 ms.
One app server handles about 400 requests per second. Adding servers only helps if something spreads requests across them.
Try it yourself
Two threads each run 'counter = counter + 1' 1,000 times on a shared variable. The final value is sometimes below 2,000. Explain why and give two fixes.
Click what a strong design here would need:
More in Building blocks
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.