Analysis of DNS caching problem under Nodejs _node.js

Source: Internet
Author: User
Tags http request

Accidentally saw an article about Nodejs sending an HTTP request does not cache DNS results. This means that if you write an HTTP capture program based on Nodejs, not providing DNS caching will allow each request to be silly repeatedly resolved domain name for IP address. It's going to sound very influential, isn't it?

In my project, sending an HTTP request is not a node native HTTP library for use, but relies on a common request library. I looked up the library's documentation and GitHub issue and found some DNS-related posts. But most of all, about the DNS problem, is not the scope of the request library, but due to the Nodejs kernel problem. OMG, it feels so deep!

Fortunately, the article mentioned above also presents two solutions:

Application level: DnsCache

Operating system level: Bind, DNSMASQ and Unbound

Whatever the scenario, it all seems simple, just install and initialize it. But the question is, how are we going to verify that they're really effective? Since my local developer operating system environment is Win7 64bit, I cannot test the operating system-level scenarios mentioned above. So let's take a look at the application level scenario.

First of all, we need to let win to track DNS requests, here I found a software, download does not need to install direct operation. Then we also need a way to clear the cache, which can be seen here, simply by executing in a terminal:

Ipconfig/flushdns

The tool is ready and 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);

OK, now open dnsquerysniffer, then clean up the local DNS cache, and execute our test script node Test.js when everything is ready. You will see a DNS request and its associated information in the Dnsquerysniffer. At a certain time interval, run our test scripts over and over again and you will find that the DNS request will not be triggered again, what does that mean? My Win7 environment itself has an operating system-level DNS cache (with only a short cache time).

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);};};

This time, after we execute the test script, we quickly clean up the local DNS cache (if your hand is fast and you can extend the SetTimeout trigger interval appropriately), you will find that two seconds after the HTTP request did not requery DNS, what does this mean? Obviously, our application maintains the DNS cache itself, so the second request simply does not care if the DNS cache record exists locally on the operating system.

The above is a small set to introduce the Nodejs under the DNS cache problem, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.