networking25 min

DNS

Translating human-readable domain names into IP addresses

0/9Not Started

Why This Matters

You type google.com into your browser, not 142.250.80.46. But computers communicate using IP addresses, not words. Something has to translate the human-friendly domain name into a machine-friendly IP address. That something is DNS — the Domain Name System.

DNS is often called the "phone book of the internet." Every web request starts with a DNS resolution step. If DNS is slow or broken, nothing loads. Understanding DNS means understanding how browsers find servers, what DNS records are, and why propagation delays happen when you change hosting providers.

Define Terms

Visual Model

Browser
Recursive Resolver
Root DNS. (dot)
.com TLD
example.comAuthoritative
93.184.216.34IP Address
query
where is .com?
ask .com TLD
ask authoritative
A record
connect

The full process at a glance. Click Start tour to walk through each step.

DNS resolution walks a hierarchy: browser cache, recursive resolver, root server, TLD server, then authoritative server.

Code Example

Code
// DNS lookup in Node.js
const dns = require("dns");
const { promisify } = require("util");

const resolve4 = promisify(dns.resolve4);
const resolveMx = promisify(dns.resolveMx);
const resolveCname = promisify(dns.resolveCname);
const resolveTxt = promisify(dns.resolveTxt);

async function lookupDomain(domain) {
  // A record — IPv4 address
  const ipv4 = await resolve4(domain);
  console.log("A records:", ipv4);
  // e.g. ["93.184.216.34"]

  // MX record — mail servers
  const mx = await resolveMx(domain);
  console.log("MX records:", mx);
  // e.g. [{ exchange: "mail.example.com", priority: 10 }]

  // TXT record — verification, SPF, etc.
  const txt = await resolveTxt(domain);
  console.log("TXT records:", txt);
}

// DNS record types:
// A      — Maps domain to IPv4 address
// AAAA   — Maps domain to IPv6 address
// CNAME  — Alias from one domain to another
// MX     — Mail server for the domain
// TXT    — Arbitrary text (SPF, verification)
// NS     — Name servers for the domain
// TTL    — Time To Live (cache duration in seconds)

lookupDomain("example.com");

Interactive Experiment

Try these on your terminal or browser:

  • Run nslookup google.com in your terminal. What IP addresses come back? Run it again — are they the same? (Google uses many IPs for load balancing.)
  • Run nslookup -type=MX gmail.com to see Gmail's mail servers.
  • Run nslookup -type=TXT google.com to see TXT records used for email authentication (SPF, DKIM).
  • Try dig example.com +trace (macOS/Linux) to see the full resolution chain from root servers down.
  • Change your DNS resolver to 8.8.8.8 (Google) or 1.1.1.1 (Cloudflare) and compare speeds.

Quick Quiz

Coding Challenge

DNS Record Parser

Write a function called `categorizeRecords` that takes an array of DNS record objects (each with `type` and `value` properties) and returns an object grouping them by type. For example, given records with types 'A', 'MX', and 'A', the result should have keys 'A' and 'MX', each containing an array of values.

Loading editor...

Real-World Usage

DNS is the invisible first step behind every internet interaction:

  • CDNs and load balancing: Services like Cloudflare and AWS Route 53 use DNS to direct users to the nearest server. A user in Tokyo gets a different IP than a user in New York.
  • DNS propagation: When you change your domain's DNS records (e.g., switching hosting providers), the change takes time to propagate because resolvers worldwide have cached the old IP. Lowering TTL before a migration minimizes downtime.
  • Email delivery: MX records, SPF (in TXT records), DKIM, and DMARC all live in DNS. Misconfigured DNS records are the number one reason emails land in spam.
  • DNS-over-HTTPS (DoH): Traditional DNS queries are unencrypted. Firefox and Chrome now support DNS-over-HTTPS, which encrypts lookups to prevent ISP snooping.
  • Service discovery: In Kubernetes, internal DNS (CoreDNS) resolves service names like my-api.default.svc.cluster.local to pod IP addresses.

Connections