Bun implements its own dns module and the node:dns module.
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.
This cache is automatically used by:
bun install
fetch()
node:http (client)
Bun.connect
node:net
node: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.
A database driver is a good example: prefetch the database host’s DNS entry when your application starts, and by the time the rest of the application has loaded, the lookup may already be complete.
dns.prefetch
This API is experimental and may change in the future.
dns.prefetch resolves a hostname before you need it.
Here’s an example:
dns.getCacheStats()
This API is experimental and may change in the future.
dns.getCacheStats() returns the current cache stats as an object with the following properties:
Example:
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:
Why 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.