Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High Latency with NodeJS

This problem pertains specifically to Nodejitsu, but similar effects seem to happen on other VPSes. I have a real time game using socket.io, and one thing I've noticed is that occasionally the server will wait an inordinate amount of time before responding. If multiple requests are sent during that timeframe, they behave as if they've all been queued up and processed at once. I suspect it's vaguely correlated to the presence of other users on the hardware shared (as is the case with any VPS).

Anyway, to test this out (and make sure that it wasn't due to my game's code), I built a minimal test case:

express = require('express')
http = require('http')

app = express()
server = http.Server(app)

io = require('socket.io').listen(server)

io.sockets.on('connection', function(sock){
    sock.on('perf', function(data, cb){
        cb([Date.now()]); //respond with the current time
    })
})

app.get('/', function(req, res){
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Methods", "HEAD,GET,PUT,POST,DELETE")
    res.header("Access-Control-Allow-Headers", "X-Requested-With")

    res.end(JSON.stringify([Date.now().toString()])); //http equivalent of perf function
})

server.listen(process.env.PORT || 6655, function(){
    console.log('listening now')
})

I had a simple blank HTML page with socket.io which would periodically send a perf event and time how long it took for the callback to fire. And it still shows the same thing:

graph showing lag spike

Note that the bar length represents the square root of the amount of time, not the linear quantity.

When instead of relying on socket.io, I use XHR to do a similar measurement of the current response time, the result is pretty similar, a lot of low latency responses (though with a higher baseline than websockets, as expected) and some occasional spikes that appear to pile up.

The odd thing is that if you open it up in multiple browser windows and different browsers, there seems to be a correlation between the different browsers (and the fact that it's totally absent or significantly less frequent on some servers) which seems to imply that it's a server side phenomenon. However, there are latency spikes that happen for some browsers but not others, and the two Chrome windows which are of the same session appear to be virtually exact duplicates, which suggests that it's something that happens locally (per computer, or per browser, networking wise).

From Left to Right: Chrome Incognito, Chrome (regular), Firefox, Chrome (regular)

charts on four windows

Anyway, this has been confusing me for months and I'd really like to understand what is causing it and how to fix it.

like image 681
antimatter15 Avatar asked Jun 22 '13 02:06

antimatter15


People also ask

CAN NodeJS handle high traffic?

Since Node. js uses non-blocking IO, the server can handle multiple requests without waiting for each one to complete, which means Node. js can handle a much higher volume of web traffic than other more traditional languages.

How does NodeJS have high IO throughput?

Node. js is asynchronous and single-threaded. This means that all I/O operations don't block any other operations. It also means that you can send emails, read files, query the database, etc.


1 Answers

I assume you checked if you have a cpu or ram issue.

The only thing that can slow down node in a "surprising" way is the garbage collector - try to run your node with the --trace* to see what is going on. (See node --v8-options.)

I personally assue that you don't find out anything from that, because - and thats just my feeling - the issue is somewhere else.

With that perfect delay of a multiply of 500ms I assume you have a packet loss. You can check with ifconfig if that is a general issue and then tcpdump the packets and see if they retransmit.

like image 132
CFrei Avatar answered Oct 20 '22 05:10

CFrei