Skip to content

DNS: how a request finds your server

How DNS records turn a name into an address, how the TTL decides how fast a change reaches users, and how geo routing sends each user to a nearby server.

  • Lesson 8 of 125
  • 12 min read
  • Depth
  • Free to read, no account
On this page
  1. Why interviewers ask this
  2. The idea in plain words
  3. Pause and predict
  4. Worked example
  5. Trade-offs
  6. Common misconceptions
  7. How to say it in the interview
  8. Check your understanding
  9. Answers
  10. Sources

What you need first

This lesson uses ideas from these earlier lessons. Read them first if any idea is new to you.

01Why interviewers ask this

Interviewers ask: "How do you move the site to a new server with no downtime?" Or: "How do far-away users reach a near server?" Both start with DNS.

A weak answer says "DNS gives the address". A strong answer says how long the old address stays in use, and how to make that time short.

02The idea in plain words

Think about a phone book. A helper looks up a number for you and remembers it. If the helper does not know a number, the helper asks the office that owns the book.

Two kinds of DNS server turn a host name into an IP address (lesson 7). A resolver looks names up for you and keeps each answer for a while. An authoritative server holds the official list for your domain.

Records

A record is one line in that list: a name, a type, a value and a TTL. (IN means the internet. Ignore it.)

text
results.example.in.   300   IN   A   203.0.113.10
TypeThe value is
A, AAAAAn IP address (AAAA is for the newer, longer kind of address)
CNAMEAnother name: a second name for the same place
MXThe mail servers
TXTPlain text, for example to prove you own the domain
NSThe authoritative servers

A name that has a CNAME cannot have any other record. The plain domain example.in always has NS records, so it cannot be a CNAME.

TTL

TTL (time to live) is the number of seconds that a resolver may keep an answer and reuse it. The record above has a TTL of 300 seconds.

  • A long TTL means fewer lookups. The cost: a change reaches users slowly, because old answers stay in use.
  • A short TTL means a change spreads fast. The cost: more lookups.

Nobody can take back an answer already given. Some programs keep answers longer, so wait a little longer (our own advice).

Geo routing

With geo routing, the authoritative server picks its answer by where the asker is. Askers in India get a Mumbai server. Askers in Germany get a Frankfurt server. Round trips (lesson 7) get shorter.

The limit: the server sees the address of the resolver, not of the user. A user of a far-away resolver may be placed wrongly. EDNS Client Subnet helps: the resolver shares part of the user's address.

Stuck on something? The AI tutor has read this lesson. Ask it with a free account

03Pause and predict

Pause. Guess first.

A record has a TTL of 3,600 seconds. You change its address at 10:00. By what time do all resolvers that follow the TTL use the new address?

Write your guess before you read on. The answer is in the worked example.

04Worked example

The site results.example.in moves to a new server. Assumptions (made up to teach with): TTL 3,600 seconds.

Step 1: change at once. A resolver that stored the answer at 09:59:59 keeps it until 10:59:59, so the last users move at about 11:00. This answers the predict box. For up to 60 minutes, some users reach the old one.

Step 2: a better plan.

  1. At least 3,600 seconds before the move, lower the TTL to 60 seconds.
  2. Change the address. Keep the old server running.
  3. After a few minutes, switch the old server off and raise the TTL.

Old answers now last at most 60 seconds.

Step 3: the price. Assumption: 5,000 resolvers ask for this name, once per TTL. At TTL 3,600 that is 5,000 / 3,600 = 1.4 questions per second. At TTL 60 it is 5,000 / 60 = 83 (60 times more).

Step 4: geo routing. Assumption: from India, the round trip is 30 ms to Mumbai and 150 ms to Frankfurt. A first request needs 2 round trips and 20 ms of work (lesson 7), not counting the DNS lookup.

  • Sent to Mumbai: 2 x 30 + 20 = 80 ms.
  • Sent to Frankfurt by mistake: 2 x 150 + 20 = 320 ms (4 times slower).

05Trade-offs

  • A short TTL solves slow change. It costs more lookups. I would use it only around a planned move.
  • Geo routing solves long round trips. It costs extra records and some wrong answers. I would change it if many users are placed wrongly.
  • DNS solves finding a server by name. It costs slow change: some programs keep old answers longer than the TTL. I would not rely on DNS alone for a switch that must finish in seconds.

06Common misconceptions

"I change the record and everyone sees the new server at once." Stored answers live until the TTL ends: up to 60 minutes here.

"I lower the TTL just before the move." Resolvers hold the old answer for the old TTL. Lower it one old TTL earlier.

07How to say it in the interview

Say it like this

Before a move, I would lower the TTL to 60 seconds, one old TTL ahead of time, because DNS cannot take back an answer. Then I change the record, so old answers last a minute at most, and I raise the TTL again.

08Check your understanding

  1. What are a TTL and a CNAME? What does geo routing do?
  2. A TTL is 1,800 seconds. You change the address at 09:00. (a) When do all resolvers that follow the TTL use it? (b) 20,000 resolvers ask once per TTL. How many questions per second at TTL 1,800, and at TTL 30?
  3. A team moves on Saturday at 02:00. The TTL is 86,400 seconds. When must they lower it?

09Answers

Show the answers
  1. A TTL is how many seconds a resolver may keep an answer. A CNAME makes a name an alias of another name. Geo routing gives each asker the answer for their place.
  2. (a) 09:00 + 1,800 seconds = 09:30. (b) 20,000 / 1,800 = 11 per second (rounded from 11.1). 20,000 / 30 = 667 per second (rounded from 666.7).
  3. One old TTL before: 86,400 seconds is 24 hours, so by Friday 02:00, to 60 seconds.

10Sources

Try it now, no sign-up

Ask the AI tutor about this lesson

The tutor has read this lesson. Ask in your own words, any time. Free account, 20 questions a day.

Reading never needs an account. The account is for the tutor, the quiz, your notes and your progress.

All lessons in Module 2: Web and API basics