DEV Community

Varid Vaya Yusuf
Varid Vaya Yusuf

Posted on

Slow mDNS Name Resolution

Today is my first time working with mDNS, and I set everything up correctly, so now it's test time. I'm on macOS, but I think this issue might apply to other operating systems too. I'm writing this because a lot of people online don't seem to fully understand the problem. Many don’t even realize they’re using mDNS and just expect the .local domain to work magically.

The Issue

Everything works fine, even when I use curl with the mDNS name, but the response time is always consistently 5 seconds.

Here’s what I tested before finding the solution:

  • I used dns-sd -B _http._tcp to discover services, and got results in milliseconds.

  • I did an IP address lookup with dns-sd -G v4 <hostname>.local, and the result was also in milliseconds.

  • But when I curl an API, it takes 5 seconds to get a response.

Solution

It turns out that curl (or whatever tool you’re using) is likely waiting for an IPv6 address when resolving the domain name. You can see this in the packet capture from Wireshark:

Wireshark Dump Result

The problem is that the host only supports IPv4, so curl waits for the IPv6 resolution for about 5 seconds before it falls back to IPv4 and gets a response right away.

There are a few ways to fix this, depending on your setup and needs. You can force curl (or your tool) to use IPv4 by using curl -4, or you can configure the host to support IPv6.

What I Found Online

I couldn’t find any clear explanations online. Most people don’t realize they’re relying on mDNS with the .local hostname. Others try to bypass the issue without understanding the cause, like adding the hostname to /etc/hosts, which defeats the purpose of using mDNS in the first place.

Top comments (0)