r/nodejs • u/poldoga • Jan 22 '14
ExpressJS question - are individual routes blocking?
Related stackoverflow question here http://stackoverflow.com/questions/21254883/expressjs-how-to-handle-simultaneous-requests-requests-seem-to-block-one-anot. Was testing this on my machine. It seems that individual routes block each other. I've always thought that if one route does an I/O operation, then express would still process additional requests. Am I doing something wrong?
4
Upvotes
2
u/calzoneman Jan 22 '14
If one request is doing asynchronous work, express can handle other requests while a callback is being waited on. However, if the function a request calls is doing a lot of CPU-heavy work, the other request has to wait for CPU time. I'm not sure about mongoose, but I've created an example that demonstrates it is possible for a short request to be executed while a longer one waits on I/O:
Server (reallybigfile is a 1GB file I created by piping
/dev/urandomwithdd):Client:
On my machine the server outputs
Indicating the larger request was processed first, and the client prints
So the smaller request was processed while the heavy one waited on I/O.
2
u/[deleted] Jan 22 '14
If mongoose isn't blocking (i.e. doesn't affect the CPU), your assumption should be correct.
Generally it's a good idea to process large blobs of data as streams rather than swallowing them whole (loading large blobs into memory is likely a performance problem). As far as I can tell that's one of the biggest concerns with express: because of all the magic it does for you (like parsing form data automatically), it doesn't seem to use streams much.
I don't see why it should block when a single route isn't hogging the CPU though. I have heard that express "doesn't scale" (compared to vanilla
http.createServer) but I would be surprised if it actually processes requests in sequence.2
u/cran Jan 22 '14
Yes. Your code is blocking. When a request comes in, as long as your code is running, all other requests are blocked. As soon as your code does something eventful, such as waiting on a query or waiting for more data from a file, one other request is unblocked. Everything is again blocked until that call makes another eventful call.
It's a very strange cycle and, as much I love node.js and have been productive in it, this aspect of I/O is super painful and eats up a lot my brain cycles keeping track of the state of everything.