Few bugs feel as mysterious as an HTTP request that hangs for almost exactly five seconds before succeeding in 40 milliseconds. Whenever you see a clean 5-second (or 10-second) pause before a connection starts, suspect DNS before you suspect your application code.

On glibc-based Linux systems, the default per-nameserver timeout is 5 seconds. If your first configured resolver is unreachable, or if an IPv6 AAAA query stalls on a broken middlebox, every fresh lookup pays that 5-second penalty.

Step 1: Measure where the time goes

Use curl with a custom format string to separate DNS lookup time from TCP connect and TLS handshake time:

curl -o /dev/null -s -w "dns: %{time_namelookup}s  connect: %{time_connect}s  total: %{time_total}s\n" https://example.com

If time_namelookup is ~5.0s or ~10.0s, you have confirmed DNS is the culprit. Next, see what glibc and systemd think your resolvers are:

ls -l /etc/resolv.conf
cat /etc/resolv.conf
resolvectl status

Step 2: Test the stub resolver vs. upstream

On Ubuntu, /etc/resolv.conf usually points to 127.0.0.53, which is systemd-resolved. Query it directly with dig and compare against a known public resolver:

dig @127.0.0.53 example.com +stats
dig @1.1.1.1 example.com +stats

If @1.1.1.1 answers in 8 ms while @127.0.0.53 times out or takes 5000 ms, check which upstream DNS servers systemd-resolved is actually using via resolvectl dns. Often DHCP handed out an unreachable internal nameserver as the first entry, and every lookup waits 5 seconds before failing over to the second one.

Step 3: Check IPv6 AAAA and ndots behavior

By default, getaddrinfo() sends both A (IPv4) and AAAA (IPv6) queries in parallel. Some older routers or container networks drop or mishandle AAAA packets, causing getaddrinfo() to wait the full 5 seconds for the AAAA response even though the A record arrived in 5 ms.

You can test this quickly with curl -4 vs. curl -6, or by watching port 53 with tcpdump while triggering a lookup in another terminal:

sudo tcpdump -i any -n port 53

Look for unanswered AAAA? queries or repeated retransmissions five seconds apart. Another frequent surprise inside container environments is options ndots:5 in /etc/resolv.conf combined with a long search list. A hostname with fewer than five dots (like api.example.com) gets queried with every search suffix appended first before finally trying the root domain—turning one lookup into six or eight round trips. Setting options ndots:1 or using a trailing dot (api.example.com.) in internal configs eliminates the extra queries.

Step 4: Remember getent vs. dig

dig talks directly to DNS over UDP/TCP port 53 and ignores /etc/hosts, mDNS, and /etc/nsswitch.conf. Applications, however, go through glibc's NSS stack. Always compare dig with getent ahosts:

time getent ahosts example.com

If dig is instant but getent ahosts takes 5 seconds, inspect /etc/nsswitch.conf. A common culprit is mdns4_minimal stalling on .local hostnames or misconfigured LDAP/NIS entries. Also check whether /etc/hosts is missing an entry for the machine's own hostname—some tools resolve hostname on startup and pause if it falls through to DNS.

Clean fixes

Once you spot the root cause, fix it cleanly in /etc/systemd/resolved.conf.d/ rather than editing /etc/resolv.conf directly (which gets overwritten on reboot):

sudo mkdir -p /etc/systemd/resolved.conf.d
sudo tee /etc/systemd/resolved.conf.d/dns.conf > /dev/null <<'EOF'
[Resolve]
DNS=1.1.1.1 9.9.9.9
FallbackDNS=8.8.8.8
EOF

sudo systemctl restart systemd-resolved
resolvectl flush-caches

Run resolvectl query example.com twice afterward: the first call should complete in tens of milliseconds, and the second should hit the local cache in under a millisecond. If you also run Docker containers on the host, remember that Docker copies the host's /etc/resolv.conf (excluding 127.0.0.53) when creating bridge networks, so verify DNS inside a container with docker run --rm alpine nslookup example.com as well.