Skip to content
All writing

NOTES & IDEAS

How One JavaScript Thread Handles Concurrent Requests

Sequential requests, overlapping I/O, and parallel CPU execution are different things. A 20 ms example makes the distinction easier to see.

Node.jsConcurrencySystem Design

Can a single-threaded application handle concurrent tasks? Yes. Does that mean the tasks execute in parallel? Not necessarily.

In my reply, I described ten requests taking 20 milliseconds each and adding up to around 200 milliseconds. That arithmetic describes sequential blocking work. It does not explain the main reason a single JavaScript thread can serve many clients.

The missing distinction is between doing work and waiting for work.

Begin with the blocking case

Imagine ten tasks that each require 20 milliseconds of uninterrupted JavaScript computation on the same thread.

In a simplified model, executing them one after another requires about 200 milliseconds of CPU time, before considering overhead. Other JavaScript callbacks cannot run on that thread while one long synchronous callback holds it.

The Node.js event-loop guide explains why blocking callbacks limit the work the event loop can do for other clients.

The 20 millisecond number is an assumption for this example. It is not a measurement of my application.

Now replace computation with waiting

Imagine that each request asks another service for data. Starting a request might require a small amount of local work, followed by a wait for a response.

While one request is waiting, the application can start another. Several operations can be in progress at once, even though their JavaScript callbacks run one at a time on the event-loop thread.

That overlap is concurrency.

A Node.js application uses an event loop and underlying facilities such as operating-system I/O and a worker pool. “Single-threaded JavaScript” does not mean that the entire runtime or everything it coordinates operates on one thread. The distinction is explained in the Node.js guide.

A small example to reason about

import { setTimeout as delay } from "node:timers/promises";

async function serial() {
  for (let i = 0; i < 10; i++) {
    await delay(20);
  }
}

async function overlapping() {
  await Promise.all(
    Array.from({ length: 10 }, () => delay(20))
  );
}

These functions model waiting with timers. They do not benchmark network traffic, and they do not perform CPU work in parallel.

The first function starts the next wait after the previous wait finishes. The second starts multiple waits before awaiting their completion. Their structure illustrates why summing every wait is not always a useful estimate of concurrent request latency.

In a real service, shared dependencies and the amount of local computation would still matter.

Where parallelism enters

Parallelism means work executes simultaneously. Node.js worker threads can execute JavaScript in parallel and are useful for CPU-intensive operations. The documentation distinguishes that use from ordinary asynchronous I/O, which usually does not benefit from adding JavaScript workers.

Adding workers also introduces decisions about transferring data and reusing workers. The documentation recommends a pool for repeated work rather than creating a worker for every small task.

A clearer answer

I would now separate the interview answer into three cases:

  • Blocking JavaScript work on one thread is processed serially.
  • Asynchronous I/O allows several operations to remain in progress while JavaScript yields.
  • Workers or processes can provide parallel CPU execution when the system has the resources.

Before proposing a solution for a slow service, identify which case dominates. A waiting problem and a computation problem can look similar from the outside, but they need different explanations.