Home > Web Front-end > JS Tutorial > body text

A brief analysis of DNS caching issues under Nodejs

高洛峰
Release: 2016-12-06 15:44:46
Original
1871 people have browsed it

I accidentally saw an article about sending http requests under nodejs without caching dns results. This means that if you write an http collection program based on nodejs, if you do not provide dns caching, each request will foolishly repeat the resolution of the domain name into an ip address. Sounds like it will affect performance a lot, right?

In my project, sending http requests does not use node’s native http library, but relies on a commonly used Request library. I checked the relevant documents and github issues of the library, and also found some posts related to dns. But what most people say is that the dns issue itself is not within the scope of the Request library, but is attributed to the core issue of nodejs. omg, it feels so profound!

Fortunately, the article mentioned above also proposed two solutions:

Application level: dnscache

OS level: Bind, dnsmasq and unbound

No matter which solution it is, it seems to be very simple. , just install and initialize it. But the question is, how do we verify that they are real and effective? Since my local development machine operating system environment is win7 64bit, I cannot test the operating system level solution mentioned above. Then let’s take a look at whether the application level solution is effective~~

First of all, we need to enable win to track dns requests. Here I found a software that can be run directly without installation after downloading. Then, we also need a method to clear the cache, you can see here, simply execute it in the terminal:

ipconfig /flushdns
Copy after login

The tool is ready, we create a test script:

const Request = require('request');
function fetch(url, callback){
Request.head({
url: url,
timeout: 10000,
tunnel: true,
gzip: true,
proxy: false,
followRedirect: false
}, callback);
}
let now = Date.now();
fetch('http://blog.kazaff.me', function(err, response, body){
console.log('lookup time without cache: ', Date.now() - now);
});
Copy after login

Okay Yes, now open DNSQuerySniffer, and then clean up the local DNS cache first. When everything is ready, execute our test script node test.js. You will see a DNS request and its related information in DNSQuerySniffer. Within a certain time interval, if you run our test script repeatedly, you will find that the DNS request will not be triggered again. What does this mean? My win7 environment itself has its own operating system level DNS cache (but the cache time is very short).

Modify our test script as follows:

const dnscache = require('dnscache')({
"enable": true
});
const Request = require('request');
function fetch(url, callback){
Request.head({
url: url,
timeout: 10000,
tunnel: true,
gzip: true,
proxy: false,
followRedirect: false
}, callback);
}
let now = Date.now();
fetch('http://priceline.com', function(err, response, body){
console.log('lookup time without cache: ', Date.now() - now);
setTimeout(function(){
now = Date.now();
fetch('http://priceline.com', function(err, response, body){
console.log('lookup time with cache: ', Date.now() - now);
});
}, 2000);
});
Copy after login

This time we quickly clear the local DNS cache after executing the test script (if you are not fast, you can appropriately extend the trigger interval of setTimeout), you will find that two seconds The subsequent http request does not re-query the DNS. What does this mean? Obviously, our application maintains its own DNS cache, so the second request does not care whether the corresponding DNS cache record exists locally in the operating system.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template