Concurrency vs parallelism
Three jobs, A, B and C, each a light in the program's hall. Run them one after another, take turns on one core, or spread them over three. Then swap waiting work for number crunching and watch which trick stops helping.
What you'll take away
- Tell concurrency from parallelism, and which kind of work each one speeds up
- Use
Promise.allto wait on three API calls together: about 300 ms, not 900 - Move heavy computing in Node to a worker thread, so timers and requests stay on time
- Spot a race condition, and avoid it by not sharing data, or with a lock or an atomic
The 1-minute version
Tap a point and the picture below plays that step.
Waiting work: 0.2 s of work, 1.6 s waiting, 0.2 s more. Crunching: 2 s of work straight. The model's numbers. Watch who each core runs, who goes out to the network and who waits on the bench
Node calls 3 outside APIs, 300 ms each: one await after another, or Promise.all. The difference?
The picture: the program's hall. Each desk is a core, lit in the colour of the job it runs, dashed while it is kept for a job that is out waiting, dark when free. Jobs A, B and C are lights: on the bench while they wait for a core, across the street at the network while they wait on it, on the mat when done. Model seconds (0.2 s of work, 1.6 s waiting, 0.2 s more; or 2 s of crunching), not a measurement.
Compare them all: Concurrency vs parallelism
The same three jobs in every tile, each working briefly and then waiting a long time on the network. See when each way finishes and how busy the cores are.
Use it when A few jobs, or jobs that must finish in order. The simplest code to read
Skip it when Work that waits on the network or the disk: the core sits idle while other jobs wait their turn
A model: waiting work 0.2 + 1.6 waiting + 0.2 s, crunching 2 s, switching every half second. Not measured. Press any tile to see it large above, with when it fits.
Which helps what
| Waiting work (I/O-bound) | Crunching (CPU-bound) | |
|---|---|---|
| One after another | 6 s, the core mostly idle | 6 s |
| Concurrency, one core | 2.4 s: the waits overlap | 6 s: nothing to overlap |
| Parallelism, three cores | 2 s, the cores mostly idle | 2 s |
Dealing with vs doing
Concurrency is about structure: many jobs in progress, switching when one has to wait. Parallelism is about execution: several running at the same instant on several cores. A web server mostly waits (on databases, other services), which is why one event loop handles thousands of requests.
Sharing is where it hurts
Once two things run at once and touch the same data, the order can change the result (a race). Prefer not sharing (pass messages, give each worker its own data); when you must share, use locks or atomic operations, and keep them small.
In real code
// Concurrency on one thread: three calls to a slow server (300 ms each) awaited one after another, then together with
// Promise.all. The waits overlap, so together takes about as long as one. Real server and client on 127.0.0.1.
// Run: node waits.ts (Node 23.6+; on Node 22: node --experimental-strip-types waits.ts)
import assert from "node:assert/strict";
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
const server = createServer((_, res) => setTimeout(() => res.end("ok"), 300)); // the "API" waits 300 ms
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`;
const call = () => fetch(url).then((r) => r.text());
let t = performance.now();
for (let i = 0; i < 3; i++) await call();
const oneByOne = performance.now() - t;
t = performance.now();
await Promise.all([call(), call(), call()]);
const together = performance.now() - t;
server.close();
assert.ok(oneByOne >= 900);
assert.ok(together < oneByOne / 2, `together ${together} vs ${oneByOne}`);
const r = (ms: number) => `~${Math.round(ms / 100) * 100} ms`;
console.log(`one after another: ${r(oneByOne)} · Promise.all: ${r(together)} (one thread, the waits overlap)`);
node waits.ts (Node 23.6+ · on Node 22 add --experimental-strip-types)It printed: one after another: ~900 ms · Promise.all: ~300 ms (one thread, the waits overlap)Three calls to a slow server (300 ms each): one after another about 900 ms, together about 300 ms, on one thread. In Go, three goroutines do the same. Both really run (the times are rounded to 100 ms).
Common questions
What's the difference between concurrency and parallelism?
Concurrency is dealing with many jobs in the same stretch of time, taking turns, even on one core. Parallelism is doing several at the same instant, which needs several cores. Work that mostly waits (I/O-bound) wants concurrency; crunching (CPU-bound) wants parallelism.
Node has one thread. How does it handle thousands of requests?
Because a web server mostly waits, on databases and other services. While one waits, the event loop picks up the next request, so the waits overlap on one thread. But one handler that crunches hard makes every other request wait for it.
await one by one vs Promise.all: what's the difference?
Three API calls at 300 ms each: awaited one after another, about 900 ms. With Promise.all they're sent together and waited on together, about 300 ms. It's all still one thread: that's concurrency, not more cores.
What do I do with heavy computing in Node?
Move it to a worker thread or another process. On the main thread it holds the event loop until it's done: in this lesson's code, 300 ms of computing makes a 10 ms timer fire about 300 ms late, and in a worker it stays on time.
Do more cores speed up work that mostly waits?
Barely. In the picture, three cores finish in 2 s against 2.4 s for concurrency on one core, and the cores sit idle most of the time. It's also harder to get right, with shared data and locks.
What is a race condition?
A result that depends on who goes first, like two threads reading the same value and each writing over the other. Fix it with a lock or an atomic operation, or better, don't share the data at all and pass messages instead.
Are goroutines concurrency or parallelism?
They're a way to write concurrent work, and Go's runtime runs them across several threads and cores, so you get both. Goroutines are very light: tens of thousands are fine.
Next lesson
Event loopLevel: Intermediate
Taking turns on one core is what Node's event loop does; next, open it up: the call stack, microtasks, tasks and Node's phases.