Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX query weird delay between DNS lookup and initial connection on Chrome but not FF, what is it?

I have an AJAX query on my client that passes two parameters to a server:

var url = window.location.origin + "/instanceStats"            
$.getJSON(url, { 'unit' : unit, "stat" : stat }, function(data) {
    instanceData[key] = data;
    var count = showInstanceStats(targetElement, unit, stat, limiter);
});

The server itself is a very simple Python Flask application. On that particular URL, it grabs the "unit" and "stat" parameters from the query to determine the name of a CSV file and line within that file, grabs the line, and sends the data back to the client formatted as JSON (roughly 1KB).

Here is the funny thing: When I measure the time it takes for the data to come back, I observe that some queries are fast (between 20 and 40 ms), and some queries are slow (between 320 and 350 ms). Varying the "stat" parameter (i.e. selecting a different line in the CSV) doesn't seem to have any impact. The fast and slow queries usually switch back and forth (i.e. all even queries are fast, all odd ones are slow). The Python server itself reports roughly the same time for each query.

AJAX itself doesn't seem to have any impact either, as I can take the url that is constructed in the JS and paste it into the browser myself and get the same behavior. Here are some measurements from two subsequent queries:

Fast: http://i.imgur.com/VQ7qopd.png

Slow: http://i.imgur.com/YuG0ROM.png

This seems to be Chrome-specific, as I've tried it on Firefox and the same experiment yields roughly the same query time everytime (between 30 and 50 ms). This is unfortunate, as I want to deploy on both Chrome and Firefox.

What's causing this behavior, and how can I fix it?

like image 667
architect101 Avatar asked Feb 27 '15 10:02

architect101


2 Answers

I've run into this also. It only seems to happen when using localhost. If you use 127.0.0.1 (or even the computer name), it will not have the extra delay.

like image 73
Eric Avatar answered Oct 24 '22 00:10

Eric


I'm having it too, and it's exactly the same: my Node.js application serves Ajax requests and no matter which /url I request it's either 30ms or 300ms and it switches back and forth: odd requests are long, even requests are short.

The thing I see in Chrome Web Inspector (aka Chrome DevTools) is that there is a long gap between "DNS lookup" and "Initial Connection".

They say it's OCSP related here: http://www.webpagetest.org/forums/showthread.php?tid=12357

OCSP is some kind of certificate validation protocol: https://en.wikipedia.org/wiki/Online_Certificate_Status_Protocol

Moving from localhost to 127.0.0.1 seems to fix it: response times are 30ms now.

like image 4
catamphetamine Avatar answered Oct 24 '22 02:10

catamphetamine