# Domain Name System: How Names Turn Into Addresses in System Design

> DNS is the internet's phone book. Learn how a lookup works, which record types matter, and how DNS sends users to different servers.

**In short:** DNS is the internet's phone book: it turns a website name into the IP address a computer needs to reach it.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/domain-name-system

## What you will learn

Every time you open a website, your computer needs a number to reach it. Computers find each other by IP address, a numeric address such as 192.0.2.1. People prefer names such as example.com. The Domain Name System, or DNS, links the two, so it is often called the internet's phone book.

In this lesson you will learn how a DNS lookup works step by step, which record types you will meet most, why a change can take hours to reach every user, and how system designers use DNS to send users to different servers. A worked example uses netflix.com. Load balancers, CDNs, and service discovery have their own lessons, so this one only touches them.

- **System design:** Planning how the parts of a software system fit together so that it is fast, reliable, and able to grow.
- **Domain name:** A human-friendly name for a site, such as example.com.
- **IP address:** A number that tells computers where to send data.
- **DNS lookup:** A question that asks which IP address belongs to a domain name.
- **Resolver:** A server that finds the answer to a DNS question on behalf of a device.
- **Cache:** A short-term store of answers, kept so the same question does not need asking again.
- **Record:** One entry in DNS data, such as the rule that a name points to a certain address.
- **TTL (time to live):** The number of seconds a resolver may keep an answer before it must ask again.

> **Tip:** When you read any DNS answer, ask two questions: which server gave it, and how long may it be kept?

## How does a DNS lookup work?

A lookup is not one jump to one server. Your device checks its own memory first. If it finds nothing, it hands the question to a resolver, which does the searching for it. The steps below follow the usual path when nothing is cached.

1. **Check local memory:** The browser and the operating system each keep recent answers. The operating system also reads a hosts file, a plain text file where a person can write names and addresses by hand. A match here ends the lookup.
2. **Ask a recursive resolver:** If nothing matches, the device sends the question to a recursive resolver. This server does the search for you. Internet providers run many of them, and public services such as 1.1.1.1 and 8.8.8.8 offer others.
3. **Ask a root server:** If the resolver holds no cached answer, it asks a root server. The root server does not know the address of a normal site, but it knows which servers handle each top-level domain, such as .com.
4. **Ask a TLD server:** A TLD (top-level domain) server handles one ending, such as .com. The resolver asks the .com server who handles example.com, and that server names the domain's own name servers.
5. **Ask the authoritative server:** The authoritative name server is the final source for the domain. It holds the records the owner published, and it gives the IP address for the name asked about.
6. **Store and return the answer:** The resolver keeps the answer for the time the record allows, then passes the IP address back to your device. Your device then connects to that address.

The whole chain can take only a moment, but it has many hops. That is why the caches at each step matter so much.

### What are the four kinds of DNS server?

Every server in a lookup has one job. Knowing the four kinds makes the steps above easier to follow.

- **Recursive resolver:** The middleman between your device and the rest of DNS. It answers from its cache when it can, and otherwise it walks the chain for you and keeps what it learns.
- **Root server:** The first stop for a resolver that has no answer. It points the resolver to the right TLD servers. The 13 root server types have many copies around the world, which together count as over 600 machines.
- **TLD server:** Keeps data for every domain that shares one ending, such as .com or .org. It points the resolver to the name servers of each domain under that ending.
- **Authoritative name server:** Holds the real records for one domain. Its answer is the one the resolver trusts most, because the domain owner controls it.

### Why does caching make lookups fast?

Most lookups never reach the root or TLD servers. Resolvers remember the answers they receive, so a later question about the same name can be answered from memory. One source notes that resolvers often keep the TLD server details for about two days, so they rarely ask the root again.

This speed has a price. A stored answer keeps being used even after the owner changes it. The next section explains how TTL controls that delay.

> **Tip:** Caching is the reason DNS feels instant for popular sites, and also the reason a DNS change does not reach everyone at once.

## What are the common record types?

A record is one entry in a domain's DNS data. Each record has a type that says what kind of answer it gives. Five types matter most for a beginner in system design.

- **A record:** Maps a name to an IPv4 address, such as 192.0.2.1. A name can hold several A records, which lets one name point to more than one server.
- **AAAA record:** Maps a name to an IPv6 address, the longer 128-bit format.
- **CNAME record:** Makes one name an alias for another name. The resolver must then look up the target name too. A CNAME cannot sit at the bare domain, and it usually must be the only record at its name.
- **NS record:** Names the authoritative name servers for a zone. It is how the parent layer hands control of a domain to the owner's servers.
- **MX record:** Lists the mail servers for a domain.

## What are TTL and slow changes?

Each record carries a TTL, which stands for time to live. It is a number of seconds that tells resolvers how long they may keep the answer. A TTL of 300 means five minutes. While the answer is fresh, a resolver does not ask again, even if the owner has changed the record since.

`ttl_cache.py`

```python
# A tiny cache with a TTL in seconds.
records = {"example.com": ("192.0.2.1", 300)}
cache = {}

def resolve(name, now):
    entry = cache.get(name)
    if entry and now < entry[1]:
        return entry[0] + " (from cache)"
    ip, ttl = records[name]
    cache[name] = (ip, now + ttl)
    return ip + " (from authoritative server)"

print(resolve("example.com", 0))
print(resolve("example.com", 100))
records["example.com"] = ("198.51.100.7", 300)
print(resolve("example.com", 200))
print(resolve("example.com", 301))

```

Output:

```text
192.0.2.1 (from authoritative server)
192.0.2.1 (from cache)
192.0.2.1 (from cache)
198.51.100.7 (from authoritative server)

```

Read the output. The owner changes the record at second 200, but the resolver still holds the old address until its copy expires at second 300. Only then does it ask again and see the new address. The lesson for designers is simple: a short TTL lets changes spread faster, but it means more lookups reach the authoritative servers.

The same idea explains a planned server move. Then the change can be made and spreads quickly.

**Quick check:** Your team moves a website to a new server with a new IP address and updates the A record. Hours later, some users still reach the old server. Which DNS idea explains this, and what could you have done before the move?

A. Load balancing: the new server has not yet been given any traffic.
B. Caching with a long TTL: resolvers keep the old answer until it expires, so the TTL should have been lowered before the change.
C. Certificate errors: browsers refuse the new address until a new certificate is issued.
D. Root server failure: the root servers still hold the old IP address for the domain.

**Answer:** B. Resolvers keep an answer for the record's TTL, so the old address survives in caches after the change. Lowering the TTL well before the move shortens how long old answers can last.

## DNS as a traffic director

In system design, DNS is often the first step of a request. A client must learn an address before it can reach a load balancer or a web server. Designers can use that moment to choose which group of servers a user reaches. The answer can depend on the user's location, the health of each server, or a planned rollout.

- **Weighted routing:** Splits answers by share, such as 70, 20, and 10 percent across three targets. Teams use it for gradual moves and for canary releases, where a small group of users tries a new version first.
- **Latency-based routing:** A round trip is the time a message takes to go to a server and back.
- **Geolocation routing:** Maps the user's location, often by country, to a fixed region. It suits legal rules, such as keeping one country's data in one place, because it follows location rather than speed.
- **Failover routing:** A health check is an automatic test that asks whether a server still responds. When the test fails, traffic moves to a backup.

Real systems often combine these rules. For example, one design first picks a continent by location, then picks the fastest data center inside it, then splits traffic between identical servers there.

### Tradeoffs to weigh

DNS routing is useful but has limits. Four tradeoffs matter most when you plan with it.

- **Caching delay:** Because answers are stored for their TTL, a routing change can take minutes or hours to reach every user. Keep TTLs short on records you expect to change.
- **Resolver location:** A user far from their resolver may therefore get a poor answer.
- **Single point of failure:** If the DNS service itself goes down, users may not reach any server, even healthy ones. Health-checked endpoints help with the servers, but the DNS service needs its own backup plan, such as a second provider.
- **Missing default:** A geolocation rule with no default record can leave users in unlisted countries with no answer at all.

> **Tip:** Add a default record to every geolocation rule, and test your health checks before you rely on them.

## Worked example: netflix.com

Here is what a visit to netflix.com looks like in general. This lesson does not know how Netflix configures its own DNS, so the steps describe how DNS works, and the commands let you observe the result yourself.

1. **Type the name:** You type netflix.com into the browser. The browser first checks its own cache for a recent answer.
2. **Reach your resolver:** If the browser has no answer, the question goes to your recursive resolver, which may answer from its cache or walk the chain of servers.
3. **Get an address for a nearby server:** If the domain uses latency or geolocation routing, the resolver's location, or a client subnet hint, can change which address is returned. Two people in different countries may get different IP addresses for the same name.
4. **Connect and measure:** The browser connects to the returned address. The Network tab in the browser's developer tools shows timing for each request, and the DNS lookup is one of the timed parts.

`lookup.sh`

```bash
nslookup netflix.com

```

Run the command from two places, such as your home network and a phone on mobile data, or ask someone in another country to run it. Compare the address lines in the answers. If they differ, the name was answered differently for different locations. Expect answers to change over time, because caches expire and routing rules change.

## Check your understanding

Try this scenario. It uses the routing ideas from this lesson.

**Quick check:** A bank must keep each country's customer data inside that country's data center. Customers use the service from many countries. Which DNS routing policy fits best?

A. Weighted routing, which sends a fixed share of users to each region.
B. Latency-based routing, which sends each user to the region with the fastest response.
C. Geolocation routing, which sends each user to the region assigned to their country.
D. Simple routing, with one A record that gives the same address to every user.

**Answer:** C. Geolocation follows where the user is, so it can match a legal boundary. Latency routing looks only at speed, so it could send a user's data to a region outside their country.

## Key takeaways

DNS turns a name into an IP address through a chain of resolver, root, TLD, and authoritative servers, with caches at almost every step. TTL controls how long those caches may keep an answer, which makes DNS fast and also slow to show changes. In system design, DNS is an early tool for sending users to the right region or server, using weighted, latency, geolocation, and failover policies. Use short TTLs on records you expect to change, and plan for the DNS service itself to fail.

## Sources

- [Cloudflare Learning: DNS server types](https://www.cloudflare.com/learning/dns/dns-server-types/)
- [freeCodeCamp: How DNS Works, A Guide to Understanding the Internet's Address Book](https://www.freecodecamp.org/news/how-dns-works-the-internets-address-book/)
- [PacketFlo: How DNS Resolution Works, Root, TLD, and Authoritative Servers](https://packetflo.io/learn/dns-trace)
- [arXiv: Domain Name System Security and Privacy, A Contemporary Survey](https://arxiv.org/pdf/2006.15277)
- [DNSFilter: Authoritative vs. Recursive DNS Explained](https://www.dnsfilter.com/blog/authoritative-vs-recursive-dns)
- [DevWithTools: DNS Explained, How Domain Name Resolution Actually Works](https://devwithtools.org/blog/dns-explained)
- [ShowMyIP: DNS Record Types Explained](https://www.showmyip.com/dns-record-types-explained/)
- [DomainDetails: DNS Record Types Explained](https://domaindetails.com/kb/technical-guides/dns-record-types-explained)
- [Who.is: DNS Record Types Explained](https://who.is/learn/dns-record-types)
- [DNScale: DNS Record Types Explained](https://dnscale.eu/learning/dns-record-types)
- [ShowDNS: DNS Records Explained, All Record Types Guide](https://showdns.net/learn/dns-records-explained)
- [PhoenixNAP: DNS Record Types Explained](https://phoenixnap.com/kb/dns-record-types)
- [Substack: Your Load Balancer Isn't the First Decision, DNS Routing Explained in 6 Policies](https://oolooroo.substack.com/p/your-load-balancer-isnt-the-first)
- [StackHarbor: Route 53 routing policies decoded](https://stackharbor.com/en/knowledge-base/awsplan-route53-routing-policies/)
- [DEV Community: How to Route Users to the Nearest Healthy Region Automatically](https://dev.to/noahcontrolplane/how-to-route-users-to-the-nearest-healthy-region-automatically-and-how-fast-each-method-fails-over-3cn3)
- [Medium (Google Cloud): Deep-dive into Cloud DNS Routing Policies](https://medium.com/google-cloud/deep-dive-into-cloud-dns-routing-policies-7c3dc6ef0b38)
- [Andrew Mercer docs: Geolocation / Proximity DNS Routing](https://www.andrewmercer.net/docs/dns/geolocation-routing/)
- [Jayendra Patil: AWS Route 53 Routing Policies](https://jayendrapatil.com/aws-route-53-routing-policy/)
