How DNS works: what happens when you type a domain
6 min readNetworking · Guides · Infrastructure
Computers talk to each other using numbers. You type names. The thing that bridges the two is the Domain Name System, and it is working every time you open a page, send an email or call an API. When it works nobody thinks about it. When it breaks, it breaks everything at once, and it is surprisingly easy to misdiagnose.
This post follows one lookup from the moment you press Enter. If you have read my posts on localhost and on HTTPS, this is the step that comes before both: before a browser can open a secure connection to a site, it has to know where the site actually is.
The one-sentence version
DNS is a distributed, hierarchical database that maps names to records, most often an IP address. Nobody owns the whole thing. It is split into zones, each run by whoever is responsible for that part of the namespace, and the pieces point to each other. That design is why it scales to the entire internet and also why a lookup can involve several different servers.
The players in a lookup
- Stub resolver - the small client inside your operating system or browser that asks the question.
- Recursive resolver - a server, usually run by your ISP or a public provider, that does the hard work of finding the answer for you and caches it.
- Root servers - the top of the hierarchy. They do not know the answer, but they know who handles each top-level domain.
- TLD servers - the servers for
.com,.org,.netand country domains. They know which servers are responsible for each domain under them. - Authoritative servers - the final source of truth for a domain. They hold the actual records you configured.
What happens when you type a domain
Say you type www.example.com. First your browser checks its own cache, then your operating system's cache and the local hosts file. If any of those already has the answer, the lookup ends there. That is why the same site loads instantly the second time.
If nothing is cached, your stub resolver sends a query to the recursive resolver you are configured to use. That resolver checks its own cache. If it is empty, it starts at the top and works down.
- Ask a root server - the resolver asks where
www.example.comis. The root does not know, but replies with the addresses of the.comTLD servers. - Ask the TLD server - the
.comserver does not know the final IP either, but it knows which name servers are authoritative forexample.comand returns them. - Ask the authoritative server - this one holds the record and answers with the IP address.
- Answer and cache - the recursive resolver returns the IP to your computer and stores it for later, for as long as the record says it may.
Only after that does your browser open a TCP connection to the IP address and begin the TLS handshake. The whole DNS stage normally takes milliseconds, because most answers come from a nearby cache rather than a trip to the root.
Record types you will actually meet
A domain's zone is a collection of records, and each type does a different job. You do not need to memorise them all, but you should recognise these when you open a DNS panel.
- A - maps a name to an IPv4 address. AAAA does the same for IPv6.
- CNAME - an alias that points one name at another name, such as
wwwpointing toexample.com. A CNAME cannot sit at the zone root next to other records. - MX - says which mail servers accept email for the domain, with priorities.
- TXT - free text, used heavily for domain verification, SPF, DKIM and DMARC email settings.
- NS - names the authoritative name servers for the zone.
- CAA - restricts which certificate authorities may issue certificates for the domain.
Caching, TTL and the propagation myth
Every record carries a TTL, a time to live in seconds. It tells resolvers how long they may keep the answer before asking again. A TTL of 3600 means up to an hour. This is the real reason DNS changes seem to spread slowly. Nothing is being pushed across the internet. Different resolvers simply still hold the old answer until their copy expires.
That gives you a practical technique. If you plan to move a site to a new server, lower the TTL on the relevant record a day or two beforehand, wait for the old long TTL to run out, then change the IP. Caches around the world will pick up the new value within minutes instead of hours. Raise the TTL again after the move so lookups stay cheap.
Debugging with dig
The most useful DNS tool is dig, which is built into most Linux and macOS systems. On Windows you can use nslookup, or install it through WSL. The trick is to ask specific questions and to ask more than one resolver.
# the simple answer
dig example.com A +short
# ask a specific public resolver instead of your own
dig @1.1.1.1 example.com A
# who is authoritative for this domain?
dig example.com NS +short
# ask the authoritative server directly, bypassing caches
dig @ns1.example-dns.net example.com A
# follow the whole chain from the root down
dig example.com +traceThe pattern I use: ask the authoritative server first. If it gives the right answer and your resolver gives the old one, you are waiting on a cache and the fix is time. If the authoritative server gives the wrong answer, the mistake is in your zone, and no amount of waiting will help.
Where it bites
- It is always DNS - the joke exists because DNS failures look like everything else: timeouts, certificate errors, an app that suddenly cannot reach its database.
- Forgotten dots and typos - a missing trailing dot or a record pointing at the wrong hostname is easy to miss in a web panel.
- Stale local caches - your browser, your OS and your router can all hold old answers. Flushing one is not enough if the others still hold it.
- Registrar vs host confusion - the place you bought the domain and the place that runs its DNS can be different companies. Edit records where the NS records actually point.
- Plain-text queries - ordinary DNS is not encrypted, so your resolver and network can see which names you look up. DNS over HTTPS and DNS over TLS hide that from the local network, though not from the resolver you choose.
- Hijacking and spoofing - an attacker who controls your registrar account or your records controls where your domain goes. Use a strong, unique password and two-factor authentication on the registrar.
A short checklist when a site will not load
Work from the bottom up. Can you reach the IP directly? If so, networking is fine and DNS is the suspect. Does dig return an answer from a public resolver? Does the authoritative server agree? Is the TTL still counting down on an old value? Did you edit the record at the provider whose name servers are actually in use? Four or five questions like these solve most cases without any guessing.
DNS is not magic and it is not slow. It is a chain of questions, each answered by someone who knows a little more than the last, and most of what looks like mystery is just a cache.
Reference: RFC 1034 and RFC 1035

Written by
Shihab Hossain — Full-Stack Developer
I build thoughtful digital products from Bangladesh — fast web experiences with Next.js, Android apps with Flutter, and the infrastructure behind them.
Work with me