Skip to content

Lesson 3 of 9 · 23m

Microtasks, nextTick, and the Starvation Trap

Distinguish promise microtasks from nextTick and macrotasks, and stop recursive scheduling before it monopolizes the event loop.

A service I debugged years ago had perfect dependency hygiene, no blocking loops, and still melted under moderate traffic. The culprit was elegant-looking asynchronous recursion: every completed task scheduled promise continuations that scheduled more continuations. Network I/O never got a turn. The team had learned "avoid sync code" but not the second rule: unbounded microtasks can starve the loop just as effectively.

Two queues, different semantics

Promise continuations go onto JavaScript's microtask queue. process.nextTick goes onto Node's own next-tick queue, which is drained before promise microtasks after the current operation completes:

setImmediate(() => console.log('immediate'));
Promise.resolve().then(() => console.log('promise'));
process.nextTick(() => console.log('nextTick'));
console.log('sync');

Typical output is sync, nextTick, promise, immediate. The important point is not trivia night. It is that both microtask-style queues run before the loop moves on to more I/O.

Recursive nextTick is a loop in disguise

This is starvation:

function work() {
  if (!queue.empty()) {
    process.nextTick(work);
    return processOne();
  }
}

Because each nextTick schedules another next-tick callback, the runtime never meaningfully leaves the queue-draining cycle. Sockets remain readable, timers expire, and shutdown signals pile up, but JavaScript continues servicing its private queue.

The safer yield primitive

When draining application work cooperatively, prefer yielding through the event loop:

async function drain(queue) {
  while (!queue.empty()) {
    processOne(queue.pop());
    if (queue.length % 100 === 0) await new Promise(setImmediate);
  }
}

That gives timers, sockets, keepalive handling, metrics exporters, and shutdown logic a chance to run. It also creates natural batching boundaries you can measure.

Async/await does not change the physics

await is not a thread switch; it only schedules continuation. CPU-heavy code such as buildHugeReport(body) still runs synchronously on the main thread. Chunk computation, use worker_threads, stream intermediate output, or reduce algorithmic complexity.

Backpressure starts at scheduling

When asynchronous producers outpace consumers, queues grow invisibly. You see memory rise first, then event-loop lag, then timeout cascades. Bound every internal queue and expose its depth as a metric. A queue you cannot measure becomes a leak you explain badly during an incident.

Field notes

  • process.nextTick is not a portable browser primitive and has different starvation behavior from promise continuations.
  • queueMicrotask is closer to promise microtasks than to nextTick; do not substitute one for the other blindly.
  • If shutdown hangs, inspect application-level task loops before blaming the HTTP server close logic.

Was this lesson helpful?

Sign in to rate