DEV Community

Joud Awad
Joud Awad

Posted on

How Servers Handle Requests: Threads vs Event Loop Explained

One CPU-heavy line, like hashing a password, can freeze every request on a Node server. In Go, it slows down just one. Here's why:

Both serve thousands of connections. They differ in who runs your code.

Node (event loop):

• All your JavaScript runs on one thread.

• The loop picks up a ready request, runs its code, then moves to the next one.

• A synchronous hash holds that thread. Every other request waits until it finishes.

Go (goroutines):

• Each request gets its own goroutine.

• The runtime spreads goroutines across OS threads, one per CPU core by default.

• The scheduler can preempt a long-running goroutine, so the others still get a turn.

Neither one is broken. Node works well when requests mostly wait on the network or the database. Go is more forgiving when requests also compute.

In Node, keep heavy work off the loop: use the async hash (it runs in the libuv thread pool) or a worker thread.

The video follows one login request from the moment it reaches the machine to the response, and shows how Python and Java handle the same wait.

https://youtu.be/buPqtt6Lm8Y

Over to you: What's the slowest line of code you've found blocking a whole service?

Top comments (0)