DEV Community

Cover image for Part 5 — Concurrency & Node.js Internals: What's Actually Happening Behind `async/await`?
Alok Kumar
Alok Kumar

Posted on

Part 5 — Concurrency & Node.js Internals: What's Actually Happening Behind `async/await`?

This is the fifth article in my Computer Fundamentals: No BS series.

We've been working with Node.js for years. We've written thousands of lines using promises, callbacks, async/await, and asynchronous APIs.

But there's something we should understand better:

If JavaScript is single-threaded, how can Node.js handle thousands of requests at the same time?

And if everything runs on one main thread, what happens when one request performs a CPU-heavy operation?

In this part we will explore concurrency, the event loop, libuv, and what actually happens behind asynchronous JavaScript.

Here's what stood out to me.

1. Concurrency is not the same as parallelism

These two terms are often used interchangeably, but they describe different things.

Concurrency means multiple tasks can make progress during overlapping periods of time.

Parallelism means multiple tasks are actually executing at the same time.

Think of one chef managing multiple dishes. While one dish is cooking, they can prepare another. That's concurrency.

Now imagine multiple chefs working on different dishes simultaneously. That's parallelism.

This distinction matters because Node.js primarily executes JavaScript on a single main thread, but it can manage many concurrent operations.

Concurrency doesn't necessarily require multiple CPU cores.

2. Node.js is more than just V8

One thing we should understand is - what Node.js actually consists of.

It's not simply JavaScript running on a server.

There are three important pieces:

  • V8: Executes JavaScript. It handles parsing, compilation, execution, memory management, and garbage collection.
  • libuv: Provides the event loop, asynchronous I/O abstractions, and a thread pool.
  • Operating System: Handles lower-level operations such as networking, sockets, and process scheduling.

A simplified picture:

             Node.js
                |
        ┌───────┴────────┐
        |                |
       V8              libuv
        |                |
   JavaScript        Event Loop
   Execution             |
                    ┌────┴─────┐
                    |          |
                  OS I/O   Thread Pool
Enter fullscreen mode Exit fullscreen mode

The important distinction:

V8 executes JavaScript. libuv provides much of the asynchronous infrastructure that allows Node.js to handle operations without blocking JavaScript execution.

3. The Event Loop: The heart of Node.js

We've used the event loop in interviews and discussions many times, but understanding why it exists is much more useful than memorising its phases.

JavaScript executes synchronous code using a call stack.

Consider:

console.log("Start");

setTimeout(() => {
  console.log("Timer completed");
}, 1000);

console.log("End");
Enter fullscreen mode Exit fullscreen mode

Output:

Start
End
Timer completed
Enter fullscreen mode Exit fullscreen mode

What's happening?

  1. JavaScript executes console.log("Start").
  2. setTimeout registers a timer.
  3. JavaScript continues executing without waiting for the timer.
  4. console.log("End") executes.
  5. Once the timer becomes eligible, its callback is scheduled.
  6. The callback executes when the JavaScript thread is available.

The interesting part is that setTimeout doesn't execute JavaScript in the background.

It schedules a callback to run later.

The event loop coordinates when these callbacks get an opportunity to execute.

Microtasks and event loop phases

Promises introduce another important concept.

console.log("Start");

setTimeout(() => {
  console.log("Timeout");
}, 0);

Promise.resolve().then(() => {
  console.log("Promise");
});

console.log("End");
Enter fullscreen mode Exit fullscreen mode

Output:

Start
End
Promise
Timeout
Enter fullscreen mode Exit fullscreen mode

Promise callbacks are scheduled as microtasks.

Node.js also has process.nextTick(), which has its own queue and is processed before the regular promise microtask queue in the usual execution model.

For example:

console.log("Start");

process.nextTick(() => {
  console.log("Next Tick");
});

Promise.resolve().then(() => {
  console.log("Promise");
});

setTimeout(() => {
  console.log("Timeout");
}, 0);

console.log("End");
Enter fullscreen mode Exit fullscreen mode

Output:

Start
End
Next Tick
Promise
Timeout
Enter fullscreen mode Exit fullscreen mode

Node.js also has different event loop phases, including timers, poll, check, and close callbacks.

I found it useful to understand these phases conceptually rather than trying to memorise every possible callback ordering.

One detail worth remembering: recursively scheduling process.nextTick() can starve the event loop and delay I/O processing.

4. If JavaScript is single-threaded, where does the work happen?

Not every asynchronous operation works the same way.

Some operations are handled by the operating system, while others use libuv's thread pool.

For example:

const fs = require("node:fs");

fs.readFile("users.json", (err, data) => {
  if (err) throw err;

  console.log("File read completed");
});
Enter fullscreen mode Exit fullscreen mode

A simplified flow:

  1. JavaScript calls fs.readFile().
  2. Node delegates the filesystem operation.
  3. The operation is handled asynchronously, commonly using libuv's thread pool.
  4. Once completed, the callback is scheduled.
  5. The callback eventually executes on the main JavaScript thread.

The main thread doesn't sit idle waiting for the file to be read.

The libuv thread pool has four worker threads by default.

Some operations that commonly use it include filesystem operations, certain DNS lookups, cryptographic operations, and compression.

But network socket operations generally use operating system mechanisms rather than consuming one thread-pool worker per connection.

That's an important reason Node.js can manage many concurrent network connections efficiently.

5. What does await actually do?

We use async/await so often that it's easy to forget what's happening underneath.

Consider:

async function getUser() {
  console.log("Start");

  const response = await fetch("/api/users/1");

  const user = await response.json();

  console.log(user);
}

getUser();

console.log("Outside function");
Enter fullscreen mode Exit fullscreen mode

When execution reaches await, the async function pauses and returns control to its caller through its promise.

When the awaited promise settles, the function resumes through promise-based scheduling.

This allows other JavaScript code to execute while the asynchronous operation is in progress.

The important distinction:

await pauses the async function, not the entire JavaScript runtime.

But await doesn't automatically make an operation non-blocking.

If you await a function that performs a large synchronous calculation, the main thread is still blocked while that calculation runs.

Sequential vs concurrent execution

Consider:

const user = await getUser();
const orders = await getOrders();
const payments = await getPayments();
Enter fullscreen mode Exit fullscreen mode

These operations execute sequentially.

If they don't depend on each other, we can initiate them together:

const [user, orders, payments] = await Promise.all([
  getUser(),
  getOrders(),
  getPayments()
]);
Enter fullscreen mode Exit fullscreen mode

Now the operations can make progress concurrently.

Promise.all() doesn't create threads or guarantee parallel execution. It coordinates multiple promises.

That distinction is important.

6. Why CPU-heavy work can block the entire server

This is where the theory becomes directly relevant to production applications.

Imagine two endpoints:

  • /users fetches data from a database.
  • /heavy performs a large synchronous calculation.

The database request spends much of its time waiting for I/O.

The heavy endpoint spends its time using the CPU on the main JavaScript thread.

While that calculation is running, Node.js cannot execute other JavaScript callbacks on that thread.

This means even unrelated API requests can become unresponsive.

This is called event loop blocking.

I found this distinction useful:

I/O-bound work CPU-bound work
Database queries Large calculations
HTTP requests Image processing
Reading files Video encoding
Network communication Complex transformations

Node.js is particularly effective for I/O-heavy workloads.

For CPU-heavy work, we may need Worker Threads, child processes, or separate background workers.

7. Worker Threads and Child Processes

Node.js provides ways to move work away from the main JavaScript thread.

Worker Threads allow JavaScript to execute in separate threads. They're useful for CPU-intensive JavaScript operations.

Child Processes create separate operating system processes with their own memory spaces and runtime environments.

A simplified comparison:

Worker Threads Child Processes
Separate threads Separate OS processes
Can share memory Separate memory spaces by default
Useful for CPU-heavy JavaScript Useful for isolation and separate programs
Communicate through messages/shared memory Communicate through IPC

Something worth remembering: Worker Threads are different from libuv's internal thread pool. One is something we explicitly use to execute JavaScript in another thread; the other supports certain runtime operations behind the scenes.

8. Race Conditions: Single-threaded doesn't mean race-condition-free

Consider a simple bank account:

let balance = 100;

async function withdraw(amount) {
  if (balance >= amount) {
    await someAsyncOperation();

    balance -= amount;
  }
}

withdraw(80);
withdraw(80);
Enter fullscreen mode Exit fullscreen mode

Both calls can check the balance before either updates it.

The sequence could look like this:

Initial balance = 100

Request A checks balance >= 80
Result: true

Request B checks balance >= 80
Result: true

Request A subtracts 80
Balance = 20

Request B subtracts 80
Balance = -60
Enter fullscreen mode Exit fullscreen mode

Both requests passed validation, but the final result is incorrect.

The problem is that reading, validating, and updating the balance isn't one atomic operation.

This is a race condition.

Even though JavaScript executes on one main thread, asynchronous operations can interleave.

And once we have multiple Node.js instances, processes, or servers, the problem becomes even more important.

How do we prevent it?

Some common approaches include:

  • Atomic database updates: Combine validation and modification into one database operation.
  • Transactions: Group related database operations into a consistent unit.
  • Optimistic concurrency: Use version numbers to detect conflicting updates.
  • Locks: Ensure only one operation accesses a protected resource at a time.
  • Distributed locks: Coordinate access across multiple application instances when necessary.

For example, a conditional PostgreSQL update:

UPDATE accounts
SET balance = balance - 80
WHERE id = 1
AND balance >= 80;
Enter fullscreen mode Exit fullscreen mode

The validation and update happen together in one statement.

We can check the affected row count to determine whether the withdrawal succeeded.

9. A small experiment that made it click

I built a tiny Node.js server with three endpoints:

app.get("/normal", (req, res) => {
  res.json({ message: "Normal request" });
});

app.get("/cpu", (req, res) => {
  const start = Date.now();

  while (Date.now() - start < 10000) {
    // Simulate CPU-heavy work
  }

  res.json({ message: "CPU task completed" });
});

app.get("/io", async (req, res) => {
  const response = await fetch(
    "https://jsonplaceholder.typicode.com/users"
  );

  const data = await response.json();

  res.json(data);
});
Enter fullscreen mode Exit fullscreen mode

I tested the endpoints from different terminals.

What I observed:

  • The I/O endpoint could wait for an external response without blocking the JavaScript thread.
  • The normal endpoint responded quickly when the server was free.
  • When the CPU-heavy endpoint ran, even the normal endpoint became unresponsive.

That was a practical demonstration of event loop blocking.

Node.js also provides monitorEventLoopDelay() through node:perf_hooks, which can help measure event loop delays.

This is a useful metric to understand when investigating production performance issues.

The mental model takeaway

Here's how to think about a Node.js request:

          Incoming Request
                 |
                 v
           Node.js Server
                 |
                 v
          JavaScript Thread
                 |
          ┌──────┴──────┐
          |             |
      Sync Work     Async Operation
          |             |
                  ┌─────┴─────┐
          |       |           |
                 OS I/O   Thread Pool
          |       |           |
                  └─────┬─────┘
          |             |
                  Operation Done
          |             |
                 Callback / Promise
          |             |
                        v
          -  -  -> Event Loop
                        |
                        v
                JavaScript Resumes
Enter fullscreen mode Exit fullscreen mode

To sum-up:

  1. Concurrency and parallelism are different.
  2. JavaScript execution on the main thread is single-threaded by default.
  3. Node.js uses the operating system and libuv to handle asynchronous operations.
  4. await doesn't block the entire runtime.
  5. CPU-heavy synchronous work can block the event loop.
  6. Race conditions can happen even in single-threaded JavaScript.
  7. Multiple application instances require coordination beyond local in-memory locks.

Node.js is not fast simply because it's asynchronous. Its architecture allows it to spend less time waiting and more time making progress on other work.

Understanding that changes how we approach API performance, background jobs, and concurrent operations.


What's next?

In Part 6, We'll move beyond a single application and explore Distributed Systems Fundamentals.

Load balancers, caching, message queues, replication, consistency, fault tolerance, and the CAP theorem.

Because understanding how one server works is only part of understanding how production systems work.

Top comments (0)