Sub topic 1
How DNS works: resolvers, authoritative servers and TTL
Prerequisites
This chapter assumes you know what an IP address and a port are, and that you can run commands in a terminal. You don't need an AWS account yet. Later chapters use the AWS CLI and the Route 53 console.
What DNS does
The Domain Name System (DNS) is a distributed database that maps names such as www.example.com to data such as IP addresses. Browsers and other programs ask DNS where to connect. No single server holds all the answers. Each piece of the namespace is owned by a different server, and those servers refer to each other.
The players in a lookup
Four kinds of server take part in a typical lookup.
- Stub resolver: the small client built into your operating system. It sends one question to a configured recursive resolver and waits for the final answer.
- Recursive resolver: a server, usually run by your ISP, your company, or a public service. It does the work of finding the answer and caches what it learns.
- Root and TLD name servers: the root servers know who runs each top-level domain (TLD) such as
.com. The.comservers know which name servers runexample.com. - Authoritative name server: the server that holds the actual records for a zone. Its answer is the source of truth. Route 53 acts as an authoritative server for the zones you host in it.
A zone is the set of records a single party manages for a part of the namespace. The word "authoritative" means the server answers from its own data, not from a cache.
Following one query
Suppose a laptop looks up www.example.com and the resolver's cache is empty.
The client asks once. The resolver asks up to three more questions, each of which returns either a referral (a pointer to the next servers) or the final answer. The referral from the .com servers is the link between a registered domain and its DNS host. Later chapters cover how you set it.
TTL and caching
Every record carries a time to live (TTL), in seconds. The authoritative server sets it. A resolver may keep the record in its cache until the TTL runs out, and it answers repeat questions from the cache during that time. Caching makes DNS fast and keeps load off authoritative servers.
The cost is that changes are not instant. If a record has a TTL of 300 and you change its value, some resolvers keep returning the old value for up to 5 minutes. Resolvers that cached the record at different moments expire it at different moments, so users see the change at different times.
| TTL | Effect |
|---|---|
| Short (30 to 60 seconds) | Changes spread quickly, but there are more queries and slightly slower lookups |
| Medium (300 seconds) | A common default that balances both |
| Long (86400 seconds) | Few queries, but changes take up to a day to spread |
A common practice is to lower the TTL a day or so before a planned change. Once the change is done and stable, raise it again.
Resolvers also cache negative answers, such as "this name does not exist", for a time set by the zone. A lookup for a name you have not created yet can therefore keep failing after you create it.
Seeing it yourself
The dig tool shows the answer and the remaining TTL.
dig www.example.com AIn the answer section, the number after the name is the TTL. Run the command twice a few seconds apart. If your resolver cached the record, the number counts down.
dig +trace www.example.com AThe +trace option makes dig follow the chain itself, from the root to the authoritative servers. It prints each referral as in the diagram above.
Common mistakes
- Assuming a change is live everywhere as soon as you save it. Check the TTL that was cached, not the new one.
- Testing only through one resolver. A different resolver may hold a different cached copy.
- Confusing the registrar (where you buy a name) with the DNS host (where the records live). They can be different companies.
Exercise
A record has a TTL of 3600. You change its IP address at 10:00. A resolver cached the old value at 09:30. When will that resolver at the latest return the new value, and why?
Show answer
At 10:30. The resolver cached the record at 09:30 and may keep it for 3600 seconds (one hour), so the cache entry expires at 10:30. After that it asks the authoritative server again and gets the new address.
Exercise
In the lookup of www.example.com, which server gives the final answer, and which servers only give referrals?
Show answer
The authoritative server for example.com gives the final answer. The root servers and the .com servers only return referrals to the next set of servers.