Note that there are some explanatory texts on larger screens.

plurals
  1. POHigh Latency with NodeJS
    primarykey
    data
    text
    <p>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).</p> <p>Anyway, to test this out (and make sure that it wasn't due to my game's code), I built a minimal test case:</p> <pre><code>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') }) </code></pre> <p>I had a simple blank HTML page with socket.io which would periodically send a <code>perf</code> event and time how long it took for the callback to fire. And it still shows the same thing: </p> <p><img src="https://i.stack.imgur.com/hPc9u.png" alt="graph showing lag spike"></p> <p>Note that the bar length represents the square root of the amount of time, not the linear quantity.</p> <p>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.</p> <p>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). </p> <p>From Left to Right: Chrome Incognito, Chrome (regular), Firefox, Chrome (regular) </p> <p><img src="https://i.stack.imgur.com/xa20T.png" alt="charts on four windows"></p> <p>Anyway, this has been confusing me for months and I'd really like to understand what is causing it and how to fix it. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI'm curious if you could open a local connection directly on the server (perhaps with something like phantomjs) and perform the same measurements, if you would see similar spikes or not. I'm also curious what version of browsers you are using and whether any are falling back to flash, long polling or iframes. Looks like you're running express without sessions, so it doesn't appear to be session related GC or something like that, and you're positive that the server isn't restarting or anything (that would likely show spikes for all browsers at the same time, so probably not, but just to ask).
      singulars
    2. COI'm guessing you've already been monitoring the server stats too during these? Curious if there are any correlated spikes or drops in memory or cpu at the same time. If you had access to the datacenter, you could plug into a local switch there and eliminate most of the network interference, but that's probably not an option... it would be nice if they offered a socket.io monitoring service from inside the data center.
      singulars
    3. COActually, you could write a local socket.io node client and run it locally on the same server, and measure that too. Sorry to spam you so much, performance problems can be like a needle in a haystack, so just trying to throw out everything I can think of that might help narrow down the problem to some specific area.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload