10,000 Connections, 1 Thread: Async & the Event Loop (Deep Dive)

Опубликовано: 10 Июль 2026
на канале: VibeEngines
No
0

Your browser and Node.js handle ten thousand connections at once — with a single thread. No locks, no races, no deadlocks. How? The secret is almost zen: this one thread succeeds by refusing to ever wait. This is a deep dive into async and the event loop, part of our Concurrency series.

The trouble with threads is that they BLOCK: ask for a file or a network reply and the thread freezes, doing nothing, until the answer comes — so ten thousand connections means ten thousand threads napping, each eating memory. Async flips it: one thread that never blocks. When a task must wait, the waiting is handed to the operating system, which taps the thread on the shoulder only when the data is ready. The machine that runs this is the event loop. Your code runs on a call stack, one function at a time; hit something slow — a fetch, a timer, a file read — and it's offloaded, so the stack keeps flowing. When that slow thing finishes, its callback joins a queue, and the loop's whole job is: when the stack is empty, pull the next callback and run it. Forever.

How do you say "call me back"? Callbacks came first (and nested into "callback hell"), then Promises tidied it up, then async-await made it read like plain top-to-bottom code — "await" just means pause THIS task, let others run, resume later. A subtle twist most people miss: there are actually two queues. Promises ride the fast "microtask" lane; timers sit in the slower "macrotask" lane — so a resolved Promise always cuts ahead of a setTimeout of zero (the source of a hundred interview questions). Watch it serve three users: user one needs a slow database read, so the thread fires it off and immediately takes user two, answers it, takes user three — then the database reply lands and it finishes user one. One thread, three users, zero waiting. And with no shared memory between tasks, there are no races, no locks, no deadlocks — that whole world of pain, gone. The catch: it's still ONE core, so a task that does heavy CPU work hogs the loop and freezes everyone. Async is for waiting (I/O), never for computing.

Chapters:
0:00 10,000 connections, 1 thread
0:20 Why threads block (and waste)
0:41 The flip: hand off the wait
0:59 The event loop machine
1:27 Callbacks → Promises → async/await
1:50 The two-queue gotcha
2:08 Worked example: 3 requests, zero waiting
2:37 No shared state = no races
3:00 The catch: it is still one core
3:21 Never nap while waiting
3:29 What comes next

Watch the loop spin, free, at https://vibeengines.com

Vibe Engines makes intuitive, visual explainers for computer science and AI. A DEEP DIVE in the "Concurrency" series — after Threads vs Processes, before Concurrency vs Parallelism.

#async #eventloop #javascript #nodejs #concurrency #promises #asyncawait #programming #computerscience #webdevelopment