← Lessons

quiz vs the machine

Silver1050

Concurrency

The Single Threaded Event Loop Revisited

How a runtime with one thread can juggle thousands of pending operations without blocking.

4 min read · intro · beat Silver to climb

One thread doing many things

An event loop runtime executes your code on a single thread. That sounds limiting, yet servers built this way handle tens of thousands of connections. The trick is that the thread never waits idly. Instead of blocking on a slow operation, it hands the operation off and moves on to other ready work.

The loop itself

At its heart the loop is a simple cycle that repeats forever.

  • Take the next ready callback from a queue.
  • Run it to completion, with no interruption.
  • Park any slow operations it starts, registering a callback for later.
  • Repeat once the current callback returns.

Because each callback runs to completion, you never see another callback halfway modify your data mid function. This is the run to completion guarantee.

Why blocking is forbidden

If one callback runs a long loop or a synchronous file read, the whole loop freezes. Every other connection waits. The golden rule is to keep callbacks short and push slow work to the runtime, which watches for completion in the background.

Key idea

A single threaded event loop stays responsive by running short callbacks to completion and offloading slow operations, never blocking the one thread that drives everything.

Check yourself

Answer to earn rating on the learn ladder.

1. Why can a single threaded event loop handle many connections at once?

2. What does run to completion guarantee?

3. What happens if a callback runs a long synchronous loop?