Skip to the solution
HardLow-Level Design6 min read

How does the Node.js 'dns' module differ from browser-based DNS resolution?

875 views

The problem

Question presented to candidate: "dns.lookup('localhost') and dns.resolve4('localhost') sound like they should do the same thing. Do they actually use the same underlying mechanism, and would you expect identical results?"

What a strong answer should cover:

  • A browser has no direct DNS-resolution API exposed to JavaScript at all β€” DNS resolution happens entirely inside the browser/OS network stack, invisibly, as part of making a request; there is no equivalent to Node's dns module callable from browser JS.
  • Node's dns module exposes two genuinely different code paths, not just two names for the same operation: dns.lookup() uses the operating system's own resolver (via getaddrinfo, dispatched through libuv's thread pool, covered in its own dedicated question) β€” this respects the OS-level hosts file and its configured resolution order.
  • πŸ“Œ Verified, not assumed: dns.resolve()/dns.resolve4()/etc. use a different implementation entirely (the c-ares library), which queries a DNS server directly over the network, bypassing the OS's hosts-file-aware resolution β€” confirmed directly: dns.lookup("localhost") returned ::1 (an OS-resolved loopback address), while dns.resolve4 against a real public domain returned genuine, live public IP addresses fetched directly from DNS servers.
  • This distinction has real practical consequences: dns.lookup() will correctly resolve an entry that only exists in a local hosts file (or via other OS-level resolution mechanisms like mDNS); dns.resolve()'s family of functions will not see that entry at all, since they never consult the hosts file β€” they go straight to a DNS server.
  • dns.lookup(), being dispatched through the thread pool, is subject to the thread pool's fixed size and its associated contention (covered fully in the dedicated thread-pool question) β€” a high volume of concurrent dns.lookup() calls can compete with other thread-pool-bound work (file I/O, some crypto).
  • A precise answer names when each is the right choice: dns.lookup() for "resolve a hostname exactly the way any other program on this OS would" (including hosts-file entries); dns.resolve()'s family for "query DNS records directly and get back exactly what a DNS server returns" (useful for inspecting specific record types like MX/TXT, which lookup() does not expose at all).

Clarifying questions expected:

  • "Does the resolution need to respect the local hosts file, or query DNS servers directly?" β€” the deciding factor between the two.
  • "Is a specific DNS record type (MX, TXT, CNAME) actually needed, beyond just an IP address?" β€” only dns.resolve()'s family exposes those.

Code / implementation expected: Yes β€” actually running dns.lookup and dns.resolve4 and observing genuinely different results/behavior is the concrete, convincing demonstration that these are different mechanisms, not just different function names.

  • dns
  • networking
  • modules

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Read the code

JavaScriptdns.lookup (OS resolver, respects hosts) vs dns.resolve4 (direct DNS query) β€” genuinely different results, verified
const dns = require("dns");

dns.lookup("localhost", (err, address, family) => {
  console.log("dns.lookup:", address, "IPv" + family);
  // dns.lookup: ::1 IPv6  -- resolved via the OS, respecting hosts-file-equivalent config
});

dns.resolve4("google.com", (err, addresses) => {
  console.log("dns.resolve4:", addresses.slice(0, 2));
  // dns.resolve4: [ '142.250.122.138', '142.250.122.100' ]
  // -- queried a real DNS server directly, bypassing the hosts file entirely
});

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 125 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextHard

How do you handle graceful shutdown in a Node.js application?

Continue