The Asynchronous Model
The event loop, promises, and everything that freezes a Node server.
The Asynchronous Model
An endpoint that used to respond in 30 milliseconds starts taking 4 seconds, but only when another endpoint is hit. The two share no code, no database, no lock. Execution traces show normal processing time on the application side; what explodes is the time between the packet's arrival and the start of processing. No conventional profiling tool shows the culprit, because the culprit isn't on the observed path.
This scenario is the signature of a blocked event loop, and it is the most common and most baffling failure mode in Node. A single expensive synchronous call — a file read with readFileSync, a JSON.parse on three megabytes, a catastrophic regular expression, a bcrypt hash with a high cost factor — is enough to put every other request in the process on hold. This lesson gives you the exact mental model of what happens, then the tools to measure and fix it.
Stack, message queue, loop
Node runs your JavaScript on a single call stack. When the stack empties, the event loop looks at what is ready and pushes the next unit of work onto it. This loop is not a single queue: it cycles through successive phases, over and over, as long as there is work left.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion