DNS Basics — DevOps

DNS translates domain names into IPs. The main records (lines of DNS data): A (IPv4), AAAA (IPv6), CNAME (an alias for another name, not allowed at the apex…

You've just pointed notes-api's DNS record at the new server, web-2. Half an hour later, some visitors still reach web-1. From your laptop the IP is already the new one. From a friend's phone, it's still the old one.

DNS (Domain Name System) is the internet's contact list. It translates a name like notes.example.com into an IP address like 203.0.113.10, because computers can only connect through IPs. Picture the contacts on your phone. You just type "Sari", and the phone finds the number. If Sari changes her number, her friends' phones still hold the old one until they update it. DNS works exactly like that, old-number problem included. In DNS, the contact's name is the domain name, and the phone number is the IP. The old contact on a friend's phone is an old answer still kept by a resolver (a server that finds IPs for you, covered below).

The records in the example.com zone

All of a domain's records (lines of DNS data) are gathered in one zone, held by an authoritative nameserver: the server with the official answers. example.com is a domain reserved for examples, so in this lesson pretend its zone belongs to the notes-api team. Its authoritative nameservers are hera.ns.cloudflare.com and elliott.ns.cloudflare.com (check with dig +short NS example.com). The notes records below are only an example: the real zone has no such name, so a real dig gets the answer NXDOMAIN (no such name). These are the records for notes-api, the same as in the Lab:

notes.example.com.      3600  IN  A      203.0.113.10
notes.example.com.      3600  IN  AAAA   2001:db8::10
www.notes.example.com.  3600  IN  CNAME  notes.example.com.
notes.example.com.      3600  IN  MX     1 smtp.google.com.
notes.example.com.      3600  IN  TXT    "v=spf1 include:_spf.google.com ~all"

Each line: name, TTL (how many seconds an answer may be kept, covered below), class (IN = internet), type, value. The dot at the end of a name marks it as a full name.

Asking with dig

dig asks DNS for a record and prints the answer. @1.1.1.1 means ask Cloudflare's resolver. +noall +answer means print only the answer part:

$ dig @1.1.1.1 www.notes.example.com +noall +answer
www.notes.example.com.  3600    IN      CNAME   notes.example.com.
notes.example.com.      3600    IN      A       203.0.113.10

The journey of one question

A resolver (such as Cloudflare's 1.1.1.1 or Google's 8.8.8.8) is a server that finds answers for you and keeps them in its cache. When an answer isn't cached yet, the resolver asks level by level. First it asks a root server, the top level of DNS. Then a TLD server, which handles one name ending such as .com. Last, the authoritative. dig +trace replays that journey one step at a time, and dig itself asks each server:

Step Who is asked Their answer
1 the resolver 1.1.1.1 the list of 13 root servers, a.root-servers.net to m
2 a root server, such as l.root-servers.net "I don't know, ask the .com servers" (a.gtld-servers.net to m)
3 a .com TLD server, such as e.gtld-servers.net "example.com is run by hera.ns.cloudflare.com and elliott.ns.cloudflare.com"
4 the authoritative hera.ns.cloudflare.com notes.example.com A 203.0.113.10, TTL 3600

The last two steps of the output of dig @1.1.1.1 +trace +nodnssec notes.example.com (+nodnssec hides the DNSSEC signatures to keep it short):

example.com.            172800  IN      NS      hera.ns.cloudflare.com.
example.com.            172800  IN      NS      elliott.ns.cloudflare.com.
;; Received 365 bytes from 192.12.94.30#53(e.gtld-servers.net) in 24 ms

notes.example.com.      3600    IN      A       203.0.113.10
;; Received 62 bytes from 173.245.58.162#53(hera.ns.cloudflare.com) in 8 ms

The ;; Received lines name the server that answered: e.gtld-servers.net (a .com server), then hera.ns.cloudflare.com. The root and TLD servers don't know notes.example.com's IP. They only point to the next server, until the authoritative answers. Before asking a resolver, the browser and the OS (operating system) check their own caches. That part is covered in the DNS: The Internet's Address Book lesson (Web Fundamentals track).

TTL: why a change takes time

TTL (Time To Live) is how many seconds an answer may stay in a cache. Until the TTL has passed, the resolver answers from its cache without asking again. dig shows the TTL left on that answer, which keeps going down. With a TTL of 3600:

Time What happens Answer from the resolver 1.1.1.1
09:50 the resolver asks the authoritative and keeps the answer for 3600 seconds 203.0.113.10, TTL 3600
10:00 you change A to 203.0.113.20 203.0.113.10, 3000 left
10:30 the authoritative already answers with the new IP 203.0.113.10, 1200 left
10:50 the cached answer expires, the resolver asks again 203.0.113.20, TTL 3600

The resolver 1.1.1.1 starts using the new IP only at 10:50. What people call "DNS propagation" is really just waiting for the cached answers in thousands of resolvers around the world to expire one by one. Nobody sends your change to them.

The recipe for moving notes-api from web-1 to web-2:

  1. A day before, lower the TTL of the A and AAAA records to 300 (5 minutes). Wait at least the old TTL (1 hour) so every cache holds the TTL 300 version.
  2. Change the IP to web-2. Within 5 minutes at most, every resolver follows.
  3. Keep web-1 running until its traffic stops, then raise the TTL back to 3600 so resolvers don't ask too often.

Following a CNAME in code

In the exercise, you write a mini resolver: a zone as an array of records { name, type, value, ttl }, and a function that follows CNAMEs until it finds the type asked for:

function resolveDns(zone, domain, type) {
  const chain = [];
  let current = domain;
  for (let hop = 0; hop < 10; hop++) {
    chain.push(current);
    const direct = zone.find((r) => r.name === current && r.type === type);
    if (direct) return { value: direct.value, ttl: direct.ttl, chain };
    const alias = zone.find((r) => r.name === current && r.type === "CNAME");
    if (!alias) return { error: "NXDOMAIN" };
    current = alias.value;
  }
  return { error: "NXDOMAIN" };
}
Call Result
resolveDns(zone, "notes.example.com", "A") { value: "203.0.113.10", ttl: 3600, chain: ["notes.example.com"] }
resolveDns(zone, "www.notes.example.com", "A") { value: "203.0.113.10", ttl: 3600, chain: ["www.notes.example.com", "notes.example.com"] }
resolveDns(zone, "blog.notes.example.com", "A") { error: "NXDOMAIN" }

⚠️ Records forgotten, late, or in the wrong place

TL;DR: DNS translates domain names into IPs. The main records (lines of DNS data): A (IPv4), AAAA (IPv6), CNAME (an alias for another name, not allowed at the apex, the main domain name such as example.com), MX (the mail server, with a priority), TXT (free text, such as SPF for mail and proof that you own the domain), NS (who the authoritative is, the server with the official answers). A resolver (a server that finds answers for you) searches from the root (the top level of DNS), then the TLD server (the server for a name ending such as .com), then the authoritative. dig +trace (a command that asks DNS) shows that journey. dig @1.1.1.1 name asks the resolver, dig @hera.ns.cloudflare.com name the authoritative. Resolvers keep answers in their cache for the TTL (in seconds), so a change only shows once the old cached answer expires. Lower the TTL to about 300 before moving servers, so the IP change shows quickly.

⚡ A: a name to IPv4

Write:

notes.example.com.      3600  IN  A      203.0.113.10

Meaning: This line is one DNS record, data that translates the name notes.example.com into an IP. The parts are name, TTL (how many seconds the answer may be kept), class (IN = internet), type, and value. An A record points straight at an IPv4 address, here web-1's IP. For IPv6 its partner is AAAA.

⚡ CNAME: an alias for another name

Write:

www.notes.example.com.  3600  IN  CNAME  notes.example.com.

Meaning: This CNAME record means "just use the answer for notes.example.com". So the resolver (the server that finds IPs for you) asks once more, and web-1's IP only has to be written in one place. The main domain name (the apex, such as example.com itself) can't be a CNAME.

⚡ Ask with dig

Write:

dig @1.1.1.1 www.notes.example.com +noall +answer

Meaning: dig asks DNS for a record. @1.1.1.1 asks Cloudflare's resolver, and the resolver follows the CNAME for you: the answer is the CNAME, then the A record of the name it points to. +noall +answer prints only the answer part. dig @hera.ns.cloudflare.com asks the authoritative (the server with the official answers) directly, skipping every cache (answers kept for a while).

⚡ TTL: why a change takes time

Guess first: the A record's TTL is 3600, so a resolver may keep its answer in the cache for 3600 seconds. A resolver kept the answer at 09:50. You change the IP at 10:00. When does that resolver start using the new IP?

At 10:50, when its cached answer expires (09:50 plus 3600 seconds). Until then it answers from its cache without asking again. Nobody sends your change to the resolver.

Meaning: "DNS propagation" is really waiting for the cached answers in thousands of resolvers to expire. A day before moving servers, lower the TTL to 300 (5 minutes) so the IP change shows quickly.

What you will learn