Analysis on DNS caching in Nodejs and nodejsdns Cache

Source: Internet
Author: User

Analysis on DNS caching in Nodejs and nodejsdns Cache

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 and do not provide dns cache, it will make every request silly to repeatedly resolve the domain name as an IP address. It sounds very performance-impact, isn't it?

In my project, sending http requests is not the native http library of node, but dependent on a common Request library. I checked the related documents of the library and github issue and found some dns-related posts. Most of them, however, the dns issue is not the scope of the Request library, but is due to the nodejs kernel issue. Omg, I feel so profound!

Fortunately, the article mentioned above also provides two solutions:

Application level: dnscache

Operating system level: Bind, dnsmasq and unbound

Whatever the solution, it seems very simple, just install and initialize it. But the question is, how can we verify that they are true and valid? Because my local development machine operating system environment is win7 64bit, I cannot test the aforementioned operating system-level solution. Let's take a look at whether the application-level solution is effective ~~

First, we need to allow win to track dns requests. Here I have found a software, which can be directly run without installation. Then, we also need a method to clear the cache. Here, we can simply execute it in the terminal:

ipconfig /flushdns

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

Okay. Now open DNSQuerySniffer, and then clean up the local DNS Cache. After everything is ready, execute our test Script node test. js. You will see a DNS request and related information in DNSQuerySniffer. After running our test script repeatedly within a certain period of time, you will find that the DNS request is not triggered again. What does this mean? My win7 environment has its own operating system-level DNS cache (but the cache time is short ).

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

After executing the test script this time, we can quickly clear the local DNS cache (if your hands are not fast, you can extend the trigger interval of setTimeout as appropriate). You will find that, two seconds later, the http request did not re-query the DNS. What does this mean? Obviously, our application maintains its own DNS cache, so the second request will not be concerned about whether the local operating system has a corresponding DNS Cache record.

The above is a DNS Cache issue under Nodejs introduced by the small Editor. I hope it will be helpful to you. If you have any questions, please leave a message and the small editor will reply to you in time. Thank you very much for your support for the help House website!

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.