---
title: "How does DNS work?"
date: "2015-03-29"
category: "dns"
slug: "dns-work"
---

DNS (Domain Name System) translates hostnames like `example.com` or `www.example.com` into IP addresses that computers use to communicate. This translation happens through a series of recursive queries until the destination server's address is resolved.

## How a DNS Lookup Works

### First-Time Query

When a browser visits `http://example.com` for the first time, no cached result exists, so a full lookup must complete:

- **(0 ms)** Browser queries the local DNS resolver (on the same computer) -- no cached result found
- Browser forwards the query to the DNS servers configured in the network settings
- **(2 ms)** DNS server asks the root (`.`) nameservers where `com.` is located
- **(42 ms)** Root nameservers return the authoritative TLD servers for `com.`
- DNS server asks the TLD servers where `example.com.` is located
- **(26 ms)** TLD server responds with the [[nameserver-settings]] delegated to `example.com.`
- DNS server queries those nameservers for the IP address of `example.com.`
- **(15 ms)** Nameservers reply that the A record for `example.com.` is `1.2.3.4`
- Browser sends `GET / HTTP/1.1` to `1.2.3.4:80`
- The web server looks up its configuration, finds the matching site at its [[where-is-site-content-served-from]], and returns a `HTTP/1.1 200 OK` response
- Browser renders the page

In this example, the total DNS overhead is approximately 85 ms. Actual times vary based on physical distance between each DNS resolver -- greater distance means more network latency.

### Cached Query

To avoid repeating this overhead on every request, DNS responses are cached at each step. The cache duration is controlled by the [[how-long-does-dns-propagation-take]] (time-to-live) parameter, part of the [DNS specification](https://tools.ietf.org/html/rfc1034#page-12) since 1987.

- **(0 ms)** Browser queries the local DNS resolver for `example.com`
- **(1 ms)** Local resolver returns `1.2.3.4` from cache
- Browser sends `GET / HTTP/1.1` to `1.2.3.4:80`
- The web server finds the matching site at its [[where-is-site-content-served-from]] and returns a `HTTP/1.1 200 OK` response
- Browser renders the page

Because the DNS result was served from the local cache, all intermediate lookups are skipped, reducing DNS resolution to approximately 1 ms.

## See Also

- [[reducing-dns-propagation-time]]
