← Lessons

quiz vs the machine

Silver1120

Concurrency

The Event Loop Scalability

Why one loop over many connections beats one thread per connection.

4 min read · intro · beat Silver to climb

The Event Loop Scalability

An event loop flips the thread per connection model on its head. Instead of one thread per client, a single thread sits in a loop asking the operating system which connections are ready to read or write, then handles only those. Idle connections cost almost nothing because no thread is parked on them.

The loop repeats a simple cycle: wait for ready events, then run a short handler for each ready connection, then loop again. Because handlers must never block, slow work like disk or database calls is started and its result is picked up in a later turn of the loop.

  • Constant memory One thread and a list of connection states, so ten thousand idle connections cost kilobytes not gigabytes.
  • No switching A single thread means no kernel scheduling between connections.
  • Never block Any blocking call would freeze every connection, so all work must be non blocking or offloaded.

The event loop scales because cost grows with active work, not with the number of connections. This is why a single threaded server can hold tens of thousands of mostly idle sockets while staying responsive.

Key idea

An event loop watches many connections from one thread and runs only the ready ones, so cost scales with active work rather than with connection count.

Check yourself

Answer to earn rating on the learn ladder.

1. How does an event loop handle many connections?

2. Why must event loop handlers avoid blocking calls?