Imagine a simple express web app like below.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
takeTimePlain()
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
function takeTimePlain () {
/* Long Job Operation Simulation */
let date = Date.now();
let end = Date.now() + 5000;
while (date < end ) {
console.log ("Iterating through while loop")
date = Date.now()
}
return
}
Then I'm calling http://localhost:3000
3 times concurrently. If I inspect the latency or some call it server waiting time,
- If latency for the first request is
x
- then for the second request it takes
2x
time - and for the third request it takes
3x
time to get completed
If we assume 10 client's connect to endpoint at the same time, then the 10th client should wait 10x
time to get a response. Imaging a few thousand requests per second.
Why Nodejs being praise for it's scalability, event driven and single threaded nature even it can't handle common scenario like above without help of out side solutions like running multiple instances of the same service?
Is there any non-blocking alternative to takeTimePlain()
?
I'm aware about setTimeout(cb, 5000)
, However my intention here isn't about waiting 5 seconds. This takeTimePlain()
can be anything like sorting through and array, ordering, find, etc. Its a common coding solution to replicate a process which takes cpu time.
Edit: This question isn't about skepticism or suggestions. This is about clarifying facts after showing actual benchmarks. Feel free to edit the title if it doesn't fit the content.