# Fixing DNS in Node.js

DevFeed: [Fixing DNS in Node.js](<https://devfeed.tech/articles/fixing-dns-in-node-js-19048.md>)

Original publisher: [Read original article](<https://httptoolkit.com/blog/configuring-nodejs-dns/>)

Author: HTTP Toolkit; Tim Perry

Published: 2021-02-17T16:00:00Z

Content type: article

Language: en

Sources: [HTTP Toolkit](<https://devfeed.tech/sources/http-toolkit.md>)

Topics: [Domain Name System (DNS)](<https://devfeed.tech/topics/dns-resolution-process.md>), [Node.js](<https://devfeed.tech/topics/node-js.md>), [Cache](<https://devfeed.tech/topics/cache.md>)

Tags: [apis](<https://devfeed.tech/tags/apis.md>), [async](<https://devfeed.tech/tags/async.md>), [asynchronous](<https://devfeed.tech/tags/asynchronous.md>), [caching](<https://devfeed.tech/tags/caching.md>), [dns](<https://devfeed.tech/tags/dns.md>), [file](<https://devfeed.tech/tags/file.md>), [http](<https://devfeed.tech/tags/http.md>), [infrastructure](<https://devfeed.tech/tags/infrastructure.md>), [internet](<https://devfeed.tech/tags/internet.md>), [io](<https://devfeed.tech/tags/io.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [libuv](<https://devfeed.tech/tags/libuv.md>), [monitoring](<https://devfeed.tech/tags/monitoring.md>), [node-js](<https://devfeed.tech/tags/node-js.md>), [os](<https://devfeed.tech/tags/os.md>), [parallel](<https://devfeed.tech/tags/parallel.md>), [performance](<https://devfeed.tech/tags/performance.md>), [threads](<https://devfeed.tech/tags/threads.md>), [ttl](<https://devfeed.tech/tags/ttl.md>), [unrelated](<https://devfeed.tech/tags/unrelated.md>), [webhooks](<https://devfeed.tech/tags/webhooks.md>)

## AI overview

Node.js DNS lookups can look asynchronous while running through synchronous calls in libuv's default four-thread pool. More than four parallel lookups can tie up that pool, delaying other DNS work as well as file I/O and some crypto APIs. Node does not cache DNS itself; each lookup is delegated to the operating system, so frequent lookups--especially across many hostnames or short TTLs--can consume substantial network time. DNS server outages or slowdowns can eventually make outgoing requests fail or become slow. The excerpt identifies these risks but does not include the article's proposed fixes.

## Source excerpt

DNS is one of those invisible technologies that you use every day, but which works so well that you can conveniently ignore it. That is until it breaks completely, or slows your application down to a crawl, or you want to resolve something unusual, and then you're in a world of trouble. Unfortunately, DNS in Node.js isn't implemented quite as you might have hoped, and includes some gotchas that can create some of these problems for you, when you least expect it. If you're a node developer, it's worth understanding how it works in detail, so you can understand what your code is really doing, and boost your application performance & reliability. Why is Node.js DNS problematic? DNS is how you turn domain names (example.com) into IP addresses (93.184.216.34). That sounds easy, but there's a whole world of complexity in here. Critically, this is something that most modern applications do a lot, every time you make an HTTP request or any other network request to a machine, on the internet and often within your internal infrastructure too. In node specifically this can go wrong in quite a few ways, because: DNS requests in node appear asynchronous, but they're actually internally implemented as synchronous calls within node's internal libuv threadpool (which by default has only 4 threads). That means if you do >4 DNS lookups in parallel then you're going to block the libuv threadpool, even though they look like async IO. This will block every other DNS lookup, and also unrelated file IO and various crypto APIs, creating some extremely confusing performance problems. Node itself doesn't do any DNS caching at all. All of that is delegated to the OS, out of your control, and every DNS lookup must go to the OS every time. DNS lookups aren't free. If you're frequently making requests to a wide variety of different hostnames or hostnames with a short TTL (e.g. if you support webhooks, if you're building a monitoring tool, or if you're polling URLs elsewhere) then you may quickly