DNS
Use Bun's DNS module to resolve DNS records
Bun implements its own dns module and the node:dns module.
import * as dns from "node:dns";
const addrs = await dns.promises.resolve4("bun.com", { ttl: true });
console.log(addrs);
// => [{ address: "172.67.161.226", ttl: 0 }, ...]import { dns } from "bun";
dns.prefetch("bun.com", 443);Choosing a resolver backend#
Bun.dns.lookup() accepts a backend option that selects the resolver implementation:
"c-ares": Bun's own asynchronous resolver, which is named after the c-ares library that it is modeled on. It reads/etc/resolv.confdirectly, so it does not consult NSS modules such assystemd-resolved. If a name server refuses or fails a query, Bun asks the next one. Bun sends names under.localand.localhostto"system". It does the same for other names if/etc/resolv.confhas nonameserverline and you have not calledsetServers(). This backend is the default forBun.dns.lookup()on Linux."system": the platform's own resolver (the non-blocking system API on macOS,getaddrinfoon a thread pool everywhere else). This backend is the default on macOS, Windows, and Android."getaddrinfo"(alias"libc"): the POSIXgetaddrinfo(3)function on a thread pool. On Windows it is the same as"system".
import { dns } from "bun";
const [{ address }] = await dns.lookup("example.com", { backend: "system" });node:dns.lookup() always uses the "system" backend to match Node.js, whose dns.lookup() is documented as calling getaddrinfo(3). node:dns.lookupService() calls getnameinfo(3) and returns an international name in its ASCII form. The node:dns.resolve*() functions use Bun's own resolver where Node.js uses c-ares.
Resolving records#
Bun.dns.resolve() asks the name servers for one type of record: A (the default), AAAA, ANY, CAA, CNAME, MX, NAPTR, NS, PTR, SOA, SRV, TLSA or TXT. Each type except A and AAAA also has its own function, such as resolveMx().
import { dns } from "bun";
await dns.resolve("example.com"); // => [{ address: "192.0.2.1", ttl: 300 }]
await dns.resolveMx("example.com"); // => [{ exchange: "mail.example.com", priority: 10 }]
await dns.reverse("192.0.2.1"); // => ["example.com"]These functions share their name servers with node:dns. Their results differ:
- Addresses always come with their
ttl. - Text is decoded as UTF-8.
node:dnsreturns one character for each byte of aTXTorCAArecord. - Only a query for
ANYaddstypeto a record. - The
codeof an error starts withDNS_, as inDNS_ENOTFOUND.
DNS caching in Bun#
Bun caches DNS lookups, which makes repeated connections to the same hosts faster.
The cache holds up to 256 entries for a maximum of 30 seconds each. If a connection to a host fails, Bun removes that host's entry from the cache. Simultaneous connections to the same host share one DNS lookup.
Bun uses this cache automatically in:
bun installfetch()node:http(client)Bun.connectnode:netnode:tls
When should I prefetch a DNS entry?#
Web browsers expose <link rel="dns-prefetch"> to resolve a hostname before it's needed. In Bun, dns.prefetch does the same thing: use it when you know you'll connect to a host soon and want to avoid the initial DNS lookup.
import { dns } from "bun";
dns.prefetch("my.database-host.com", 5432);A database driver is a good example: prefetch the database host's DNS entry when your application starts. By the time the rest of the application has loaded, the lookup may already be complete.
dns.prefetch#
dns.prefetch resolves a hostname before you need it.
dns.prefetch(hostname: string, port?: number): void;Here's an example:
import { dns } from "bun";
dns.prefetch("bun.com", 443);
//
// ... sometime later ...
await fetch("https://bun.com");dns.getCacheStats()#
dns.getCacheStats() returns the current cache stats as an object with the following properties:
{
cacheHitsCompleted: number; // Cache hits completed
cacheHitsInflight: number; // Cache hits in flight
cacheMisses: number; // Cache misses
size: number; // Number of items in the DNS cache
errors: number; // Number of times a connection failed
totalCount: number; // Number of times a connection was requested at all (including cache hits and misses)
}Example:
import { dns } from "bun";
const stats = dns.getCacheStats();
console.log(stats);
// => { cacheHitsCompleted: 0, cacheHitsInflight: 0, cacheMisses: 0, size: 0, errors: 0, totalCount: 0 }Configuring DNS cache TTL#
Bun caches DNS entries for 30 seconds by default. To change the TTL, set the $BUN_CONFIG_DNS_TIME_TO_LIVE_SECONDS environment variable. For example, to set it to 5 seconds:
BUN_CONFIG_DNS_TIME_TO_LIVE_SECONDS=5 bun run my-script.tsWhy is 30 seconds the default?#
The system API underneath (getaddrinfo) does not expose the TTL of a DNS entry, so Bun has to pick a number. We chose 30 seconds because it's long enough to see the benefits of caching and short enough to be unlikely to cause issues if a DNS entry changes. Amazon Web Services recommends 5 seconds for the Java Virtual Machine, though the JVM's default is to cache indefinitely.
Connecting to a name with several addresses#
When a name resolves to more than one address, Bun tries the addresses one at a time (Happy Eyeballs, RFC 8305). Bun keeps the resolver's order and alternates between IPv6 and IPv4. This applies to fetch (except over HTTP/3), WebSocket, Bun.connect, Bun.SQL, RedisClient, S3Client and bun install. node:net follows its autoSelectFamily option instead.
Bun tries the next address as soon as an attempt fails, or after 250 ms without a connection. Earlier attempts keep going, up to six at a time, and Bun uses the first one that connects. While the name stays in the DNS cache, later connections start with the address that connected last.
To change the delay, set $BUN_CONFIG_CONNECT_ATTEMPT_DELAY_MS. Set it to 0 to connect to the first four addresses at once and keep whichever answers first:
BUN_CONFIG_CONNECT_ATTEMPT_DELAY_MS=0 bun run my-script.ts