Concurrency vs Parallelism
Concurrency and parallelism are fundamental concepts in system design and operating systems.
They are often confused, but they are not the same thing.
1. Concurrency
- Concurrency = dealing with multiple tasks at once.
- Tasks may start, run, and complete in overlapping time periods.
- Execution may not literally happen at the same time.
Example:
- A single-core CPU switching between tasks quickly (time-slicing).
- Handling multiple client requests with async I/O.
Analogy:
- A single chef cooking multiple dishes by switching between them.
2. Parallelism
- Parallelism = executing multiple tasks at the exact same time.
- Requires multiple cores/CPUs.
- Focuses on performance and speed.
Example:
- A quad-core CPU running four threads simultaneously.
- MapReduce splitting large jobs across many servers.
Analogy:
- Multiple chefs cooking different dishes at the same time.
3. Concurrency vs Parallelism Table
Aspect | Concurrency | Parallelism |
---|---|---|
Definition | Managing multiple tasks | Executing tasks simultaneously |
Hardware Needed | Not required (can be single-core) | Requires multi-core/multi-CPU |
Goal | Better utilization & responsiveness | Faster execution (performance) |
Example | Async programming, goroutines | Multi-threading, distributed jobs |
4. Real-World Examples
Concurrency:
- Node.js event loop handling thousands of requests.
- Chat server managing messages with async I/O.
Parallelism:
- Video encoding on multiple cores.
- Training ML models on GPU clusters.
5. Interview Tips
- Common question: “Is Node.js concurrent or parallel?”
- Answer: Node.js is concurrent (async I/O), but not parallel by default.
- Be ready to mention that concurrency ≠ parallelism, but they can co-exist.
- Example: A multi-core server handling concurrent async requests → concurrency + parallelism.