31 March 2024

Whats faster 0.0.0.0 or 127.0.0.1?

When I was experimenting with hosts and Pi-Hole, I read that "0.0.0.0" address in the hosts works faster then "127.0.0.1". And now I decided to check and prove it.

About tests

  • To automate tests I prepared code on Python "DNS_Loopback_Speed".
  • To get more accurate results I performed 10000 tests for each address and took the average.
  • I have two devices running Windows 10 and Ubuntu 22.04, so I also compare speed of resolving between Windows and Linux.
  • I've seen option of using 127.0.0.0 in hosts, therefore I add it too.
  • Just to compare delays between local resolution (hosts) and external DNS servers I add 2 tests (my local Pi-Hole DNS and Google DNS). My devices connected to network via Wi-Fi, so results may not be reliable.

Brief information regarding these IP addresses:


Scheme

This is how I understand the order and logic of DNS resolution operation.



Results

  • My tests show that DNS resolution in cache (hosts) on Linux much faster than on Windows.
  • I can confirm (based on results on Ubuntu as more indicative) that "0.0.0.0" (and "127.0.0.0") is a bit faster than "127.0.0.1".



What I learned

  • Function "time()" from python module "time" is not enough accuracy, therefore I had to use "perf_counter_ns()".
  • Tool "nslookup" doesn't read the "hosts" file (link onelink two, link three), but send DNS request straight to DNS server, therefore I can't use it in my experiment.
  • Python module "dnspython" also doesn't read the "hosts" file, therefore I used "socket.gethostbyname()".
  • On Windows "socket.gethostbyname()" returns error "socket.gaierror: [Errno 11004] getaddrinfo failed", but on Ubuntu the funtion returns correct address "0.0.0.0".
Hope this helps someone.

No comments:

Post a Comment