15

I looked up the man page and couldn't find how long dnsmasq cache a dns. what's the rule for caching and expiration?

I am having a problem that request send to an external service sometimes takes over 5 or 10 seconds for namelookup. I could save the external service ip to /etc/hosts but I am afraid of ip change. Installing Dnsmasq improves name lookup but slow namelookup still happens with less frequency.

One solution is to setup a simple ping in cron job but I need to find out the expiration time first.

thanks.

UPDATE

added dig output

; <<>> DiG 9.8.1-P1 <<>> api.mch.weixin.qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45012
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;api.mch.weixin.qq.com.             IN      A

;; ANSWER SECTION:
api.mch.weixin.qq.com.      0       IN      A       140.207.69.102

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Jul 17 19:24:36 2015
;; MSG SIZE  rcvd: 55

UPDATE 2

the previous dig output contains TTL as 0 which is misleading. I have posted another dig output.

; <<>> DiG 9.8.1-P1 <<>> api.mch.weixin.qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60900
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;api.mch.weixin.qq.com.             IN      A

;; ANSWER SECTION:
api.mch.weixin.qq.com.      181     IN      CNAME   forward.qq.com.
forward.qq.com.             181     IN      A       101.226.90.149
forward.qq.com.             181     IN      A       101.226.129.200

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Jul 21 12:01:54 2015
;; MSG SIZE  rcvd: 99
perlwle
  • 301

3 Answers3

12

DNSmasq uses the DNS TTL value for determining the caching period. You can use the dig command to query the current TTL of your DNS server for that domain.

Tero Kilkanen
  • 38,887
5

The option you will need to set for dnsmasq is --min-cache-ttl, the value is in seconds.

According to the dig output, the TTL is 0, if you set this to, say --min-cache-ttl=600, then the TTL value which will be returned by dig will be 600. The TTL for api.mch.weixin.qq.com from where I am is 600, so setting it to 600 seconds yourself should be reasonably safe in avoiding missing any IP changes. (I'm guessing you are located in China.)

This will cause all DNS records to be considered valid for 10 minutes.

See the dnsmasq mailing list message detailing the introduction of this feature.

Phizes
  • 151
  • 4
0

Q: Is the timeout based on last reference from client ('hit') or last real lookup to dns server ? If clients keep hitting cache ('hit') often and this updates the last time referenced then a real lookup to dns server will never happen. Assuming clients making getaddinfo() calls constantly, can we force a real lookup to server every 30 seconds or so with --min-cache-ttl set to 30 ? thx