Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 3: DNS, HTTP and HTTPS

    DNS: Turning Names Into IP Addresses

    DNS (Domain Name System) is the internet's distributed directory that turns domain names into IP addresses.

    Chapter 2 of 138 min readOften asked in interviews

    The Plain Picture#

    Your phone's contacts list. You tap "Mom", and the phone dials +91-98450-12345. You remember names; the phone network needs numbers.

    But there are hundreds of millions of website names, changing every minute. No single contacts list could hold them all. So the world uses a chain of directories: a top-level directory that knows who handles .com, .app and .in; a .app directory that knows who handles studybuddy.app; and StudyBuddy's own directory that knows the actual number.

    The Real Names#

    • DNS (Domain Name System) is the internet's distributed directory that turns domain names into IP addresses.

    • A domain name is read right to left, from general to specific:

                     .                ← the ROOT (usually invisible)
                     │
            ┌────────┼────────┐
           com      app       in      ← TOP-LEVEL DOMAINS (TLDs)
            │        │        │
         google  studybuddy   co      ← second-level domains
            │        │        │
          www       api     flipkart  ← subdomains
                                         api.studybuddy.app = api  .  studybuddy  .  app  .  (root)
    • The servers holding the real answers for a domain are its authoritative name servers.

    • The server that does the looking-up for you is a recursive resolver — usually run by your ISP, or public ones like Google's 8.8.8.8 and Cloudflare's 1.1.1.1. Your laptop learned which resolver to use from DHCP (Networking 1: Addresses and Routing Ch 9).

    Under the Hood#

    A full lookup, step by step#

    Your laptop asks for api.studybuddy.app, and nobody has it cached:

      Laptop                Recursive resolver                  The DNS hierarchy
      ──────                ──────────────────                  ─────────────────
        │  "api.studybuddy.app?"   │
        │ ───────────────────────▶ │  1. ask a ROOT server ──────────────▶  "I don't know, but .app is
        │                          │                                         handled by these servers"
        │                          │  2. ask a .app TLD server ──────────▶  "studybuddy.app is handled
        │                          │                                         by ns1.dnsprovider.com"
        │                          │  3. ask the AUTHORITATIVE server ───▶  "api.studybuddy.app is
        │                          │                                         13.235.10.20, TTL 300"
        │ ◀─────────────────────── │  4. cache it for 300 s, reply
        │   "13.235.10.20"         │
    • Queries usually travel over UDP port 53 (Networking 2: TCP, UDP and Ports Ch 3): one small question, one small answer. Big answers fall back to TCP.

    • There are 13 named root server identities (a to m.root-servers.net), but each is really hundreds of machines around the world sharing one address; your query reaches the nearest one. (Many machines sharing one IP address, with routing sending you to the closest, is called anycast.)

    Caching and TTL — why DNS is fast#

    Every answer comes with a TTL (time to live, in seconds — a different TTL from the IP header's hop counter). Everyone along the way caches the answer until the TTL runs out: the browser, the operating system, and the resolver. Most lookups are answered from a cache in a few milliseconds and never reach the root.

    Real output from dig (a DNS lookup tool on Mac and Linux), run from Bangalore:

    Output
    $ dig example.com +noall +answer +stats
    example.com.    278    IN    A    172.66.147.243
    example.com.    278    IN    A    104.20.23.154
    ;; Query time: 8 msec
    ;; SERVER: 192.168.1.1#53(192.168.1.1)
    • 278 is the remaining TTL in seconds.

    • Two A records = two addresses; clients may use either (a simple form of spreading load).

    • SERVER: 192.168.1.1 — the query went to the home router, which forwards to the ISP's resolver.

    • 8 msec — cached nearby. A lookup that walks the whole hierarchy can take 50–200 ms.

    Record types to know#

    TypeMaps a name to…Example
    Aan IPv4 addressstudybuddy.app → 13.235.10.20
    AAAAan IPv6 addressstudybuddy.app → 2600:1f18::1
    CNAMEanother name (an alias); look that one up insteadwww.studybuddy.app → studybuddy.app
    MXthe mail servers for the domainstudybuddy.app → mail.provider.com
    TXTfree text; used to prove you own a domain, and for email security settings"google-site-verification=…"
    NSthe authoritative name servers for the domainstudybuddy.app → ns1.dnsprovider.com

    A real CNAME:

    Output
    $ dig www.github.com +noall +answer
    www.github.com.    3600    IN    CNAME    github.com.
    github.com.        17      IN    A        20.207.73.82

    www.github.com is an alias; the resolver followed it to github.com and returned its A record.

    Gotchas

    • "DNS propagation takes 48 hours" is really about caches. When you change a record, old answers stay in caches until their TTL expires. Before a planned migration, lower the TTL (say to 60 seconds) a day ahead, so the switch takes effect in a minute.

    • A CNAME can't be used at the bare domain (studybuddy.app itself, called the apex) by the DNS rules, because the apex must also hold other records like NS. Cloud DNS services offer workarounds ("ALIAS" records, AWS Route 53's alias).

    • "It's always DNS." A huge share of outages involve DNS: expired domains, wrong records, a DNS provider down. When a name "doesn't work" but its IP does, check DNS first (Networking 1: Addresses and Routing Q3).

    Check yourself

    5 questions
    1

    What's the difference between a recursive resolver and an authoritative name server?

    Show answer

    A recursive resolver looks things up on your behalf and caches answers; an authoritative server holds the real records for a domain.

    2

    Walk through the lookup of api.studybuddy.app when nothing is cached.

    Show answer

    Resolver asks a root server → gets the .app servers → asks a .app server → gets StudyBuddy's authoritative servers → asks one → gets the IP → caches it and replies.

    3

    What does a DNS TTL control?

    Show answer

    How long the answer may be cached.

    4

    What's the difference between an A record and a CNAME record?

    Show answer

    An A record gives an IPv4 address; a CNAME points to another name that must be looked up.

    5

    You're moving StudyBuddy to a new server next week. What should you do with the DNS TTL now, and why?

    Show answer

    Lower it in advance (e.g. to 60 s), so when you change the record, caches forget the old address quickly.