All lessons
Web basicsBeginner9 min readIn my stack

Domains & DNS

Registrars, nameservers, records and TTL, explained without the magic.

  • Registrar
  • Nameservers
  • A / CNAME / MX / TXT
  • TTL
Step 1/5
The browser asks a resolver (often your provider's or 1.1.1.1): “where is example.com?”

In 30 seconds

A domain is a name you rent from a registrar. DNS is the global phone book that turns that name into IP addresses and other information, such as where email should go. You control your entries by editing records at your DNS host. Understanding records and TTL makes launches, migrations and email setup calm instead of scary.

Why I use it

  • I connect domains to servers and services regularly, and almost every launch starts with DNS records.
  • I use DNS as the switch in server migrations: copy, verify, then change the record, keeping the old server until everything is confirmed.
  • Email deliverability depends on DNS (SPF, DKIM, DMARC), and clients notice immediately when it is wrong.
  • Knowing TTL lets me plan changes so users move to the new server in minutes, not days.

How it works

01Three different roles: registrar, nameservers, DNS hosting

These are often confused because one company can do all three. The registrar is where you buy and renew the domain (for example Namecheap, Porkbun or Cloudflare Registrar); it talks to the registry that runs the TLD, such as .com. In the registrar panel you set which nameservers are authoritative for your domain. The nameservers belong to a DNS host, and the DNS host is where your actual records live: A, CNAME, MX and so on. So you can buy a domain at one registrar, point its nameservers to Cloudflare, and manage all records in Cloudflare. If you edit records at the registrar while nameservers point elsewhere, nothing happens, which is a very common source of confusion. Analogy: the registrar is the land registry, the nameservers are the sign saying which office keeps the address book, and the DNS host is that office.

  • Registrar: ownership, renewal, nameserver setting, WHOIS/RDAP contact, transfer lock.
  • Nameservers: which DNS provider answers authoritatively for the domain.
  • DNS hosting: the records themselves.

02How a lookup works

Your device asks a recursive resolver (your ISP, 1.1.1.1, 8.8.8.8). If the answer is not cached, the resolver asks a root server where .com lives, asks the .com servers which nameservers serve example.com, then asks those authoritative nameservers for the record. Each answer is cached for its TTL (time to live) in seconds. Next time, anyone using that resolver gets the cached answer instantly. This caching is why DNS scales to the whole internet, and also why changes are not visible everywhere at once.

bash
# Ask a specific resolver, then follow the whole chain
dig example.com A @1.1.1.1
dig +trace example.com
# Which nameservers are authoritative?
dig example.com NS +short
# Ask the authoritative server directly (skips caches)
dig example.com A @ns1.example-dns.net +short

03Record types you will actually use

A maps a name to an IPv4 address, AAAA to IPv6. CNAME makes one name an alias of another name; it cannot sit on the root domain (apex) next to other records, so providers offer ALIAS, ANAME or CNAME flattening for the apex. MX tells the world which servers receive email, with a priority (lower is tried first). TXT holds text, used for domain verification and email security: SPF lists who may send mail for you, DKIM publishes a signing key, DMARC says what receivers should do with mail that fails. NS delegates a zone, CAA limits which certificate authorities may issue certificates for your domain, and SRV describes services with a port. Each record has a name, a type, a value and a TTL.

bash
example.com.        300  IN A      203.0.113.10
example.com.        300  IN AAAA   2001:db8::10
www.example.com.    300  IN CNAME  example.com.
example.com.        3600 IN MX     10 mail.example-mail.com.
example.com.        3600 IN TXT    "v=spf1 include:_spf.example-mail.com -all"
_dmarc.example.com. 3600 IN TXT    "v=DMARC1; p=quarantine; rua=mailto:[email protected]"
example.com.        3600 IN CAA    0 issue "letsencrypt.org"

04TTL and "propagation"

DNS does not really propagate; caches expire. If a record has a TTL of 86400 (one day), a resolver that cached the old value may keep using it for up to a day after you change it. Changing nameservers is slower still, because the delegation at the TLD level often has a TTL of a day or two. The practical recipe for a migration: lower the TTL to 300 seconds at least one old-TTL period before the switch, make the change, verify from several resolvers, and raise the TTL again after things are stable. Some resolvers also cache negative answers ("this name does not exist") according to the SOA record, so create records before you announce them.

  • 300 s for records you might change soon; 3600 s or more for stable ones.
  • Check from multiple resolvers, not only from your own laptop.
  • Browser and OS caches can hold old answers; flush or test with dig.

05Checking records with dig and nslookup

dig is the standard tool on Linux and macOS; nslookup works everywhere, including Windows. Always check the authoritative answer as well as a public resolver, so you know whether the problem is your record or a cache. For email, check MX and the TXT records for SPF, DKIM (on a selector subdomain) and DMARC. For certificates, check CAA. Online tools that query from many countries help confirm that a change has reached most resolvers. The +short flag prints only the answer, which is handy in scripts and runbooks.

bash
dig +short example.com A
dig +short www.example.com CNAME
dig +short example.com MX
dig +short example.com TXT
dig +short selector1._domainkey.example.com TXT
nslookup -type=MX example.com 8.8.8.8
# Windows / PowerShell
Resolve-DnsName example.com -Type A -Server 1.1.1.1

06Production tips

Treat the domain itself as critical infrastructure. Turn on auto-renew and registrar lock, use an account with two-factor authentication, and keep contact email on a mailbox you will still own in five years. Enable DNSSEC if your registrar and DNS host support it, so answers cannot be forged. Keep an exported copy of your zone file. Before moving nameservers, recreate every record at the new host first, especially MX and TXT, or email will silently break. Remove dangling records that point to services you no longer use, because they can be taken over by others.

Use it when

  • Launching a site or moving it to a new server.
  • Setting up business email and making it land in inboxes, not spam.
  • Verifying a domain for Google, Apple, payment providers or certificate issuance.
  • Putting a CDN or proxy such as Cloudflare in front of a site.

Skip it when

  • Do not use DNS round-robin as a real load balancer; it has no health checks and caches ignore failures.
  • Do not rely on very low TTLs for instant failover; some clients cache longer than you ask.

Alternatives

  • Cloudflare DNS

    Free, fast authoritative DNS with proxying, CNAME flattening and DNSSEC.

  • AWS Route 53

    Paid DNS with health checks, latency and geo routing, good inside AWS.

  • Registrar DNS

    Fine for simple sites, usually slower and with fewer features.

Best practices

  1. Lower TTL to 300 s a day before planned changes; raise it again afterwards.
  2. Copy every record to the new DNS host before switching nameservers.
  3. Publish SPF, DKIM and DMARC for every domain that sends email, and SPF "-all" plus DMARC reject for domains that never send.
  4. Use CAA records to limit which certificate authorities can issue for you.
  5. Enable registrar lock, auto-renew, 2FA and DNSSEC.
  6. Delete records that point to resources you no longer control.

Common mistakes

  • Editing records at the registrar while the nameservers point to another provider.
  • Putting a CNAME on the apex domain next to MX records, breaking email.
  • Switching nameservers without copying MX and TXT records first.
  • Forgetting the www record, so www.example.com does not resolve.
  • Letting the domain expire because the renewal email went to a former employee.

Deep dive

07Delegation, glue records and the SOA

A zone is the part of the DNS tree one set of nameservers is responsible for. The .com zone does not hold your A record; it holds NS records that delegate example.com to your nameservers. If your nameservers live inside the same domain (ns1.example.com), there is a chicken-and-egg problem, so the parent zone also stores their IP addresses as glue records, set at the registrar. Every zone starts with an SOA record: the primary nameserver, a contact, a serial number that secondaries use to know when to transfer updates, and the negative-caching TTL. That last value controls how long resolvers remember that a name does not exist. A lame delegation happens when the parent points to nameservers that do not actually answer for the zone, which causes slow or failed lookups for some resolvers.

bash
# Delegation as seen by the .com servers
dig example.com NS @a.gtld-servers.net +norecurse
# SOA: primary NS, serial, refresh, retry, expire, negative TTL
dig example.com SOA +short
# A name that does not exist returns NXDOMAIN plus the SOA
dig nothing-here.example.com A

08DNSSEC: signing the answers

Classic DNS has no way to prove that an answer is genuine, so a cache-poisoning attack could send users to a fake IP. DNSSEC adds signatures. Your DNS host signs every record set (RRSIG records) with a zone key published as a DNSKEY record. The parent zone holds a DS record, a hash of your key, which you add at the registrar. Validating resolvers follow the chain from the root's key, to .com, to your DS, to your DNSKEY, to the signature on the answer. If anything does not match, the resolver returns SERVFAIL instead of a forged answer. The operational risk is that a mismatch also breaks a legitimate domain: moving DNS hosts without removing or updating the DS record first takes the domain offline for every validating resolver. Managed providers handle key rotation; your job is the DS record.

bash
dig example.com DNSKEY +dnssec +short
dig example.com DS +short          # what the parent publishes
dig example.com A +dnssec          # look for the "ad" flag from a validating resolver
# Online: dnsviz.net shows the full chain of trust

09Modern records: HTTPS/SVCB, CAA details and serve-stale

The HTTPS record (RFC 9460, a form of SVCB) lets a domain tell browsers, before connecting, which protocols it supports (for example h3 and h2), alternative ports, IP hints and keys for Encrypted Client Hello. It can also sit on the apex and point to another hostname, which solves part of the old apex-CNAME problem for browsers that support it. Cloudflare and other providers publish it automatically for proxied hostnames. CAA records can also carry iodef (where CAs report refused requests) and issuewild (which CA may issue wildcards). On the resolver side, many implementations follow serve-stale (RFC 8767): if your authoritative servers are unreachable, they may keep serving expired answers for a while. It helps availability during outages but means very short TTLs are never a guarantee of fast failover.

bash
dig example.com HTTPS +short
# 1 . alpn="h3,h2" ipv4hint=203.0.113.10
dig example.com CAA +short
# 0 issue "letsencrypt.org"
# 0 issuewild ";"          <- nobody may issue wildcards
# 0 iodef "mailto:[email protected]"

10Security: takeovers, zone hygiene and anycast

A subdomain takeover happens when a record, usually a CNAME, points to a third-party resource you deleted, such as a cloud storage bucket, a PaaS app or a landing-page service. Someone else claims that resource name and now serves content on your subdomain, with your cookies in scope. Prevent it by deleting DNS records before or together with the resource, and by auditing the zone regularly. Other hygiene rules: never allow open zone transfers (AXFR) to anyone, avoid wildcard records unless you mean them, and keep internal hostnames out of public DNS because they reveal your network map. For resilience, good DNS providers use anycast: the same nameserver IP is announced from many locations, so queries go to the nearest healthy site and large floods are spread out.

Real-world scenarios

01

Email stopped after moving DNS to a new provider

Problem

Nameservers were switched to a new DNS host on Friday. The website works, but since then outgoing mail lands in spam and some incoming mail bounces.

How to solve it

Compare the old zone export with the new zone record by record. Check MX with dig, then SPF on the apex, the DKIM selector TXT records (for example selector1._domainkey) and _dmarc. The usual gap is a missing DKIM selector or an SPF record split into two TXT records, which is invalid. Recreate what is missing exactly, with the same values. If the old nameservers still answer, query them directly to recover lost records. Send a test message to a mailbox and read the authentication results header to confirm spf=pass, dkim=pass and dmarc=pass.

02

The domain went offline after changing DNS hosts

Problem

After pointing nameservers to a new provider, some users get the site and others get a "server can't be found" error. dig against 1.1.1.1 returns SERVFAIL.

How to solve it

This pattern is typical of a DNSSEC mismatch: the registrar still publishes the DS record for the old provider's keys, so validating resolvers reject the new answers. Check with dig example.com DS and dnsviz.net. The fastest fix is to remove the old DS record at the registrar; resolvers recover once the DS TTL expires. Then enable DNSSEC at the new provider and add its DS record. Next time, follow the order: remove DS, wait out its TTL, switch nameservers, then re-enable DNSSEC.

03

A forgotten subdomain starts serving spam

Problem

Security reports that promo.example.com shows a casino page. The marketing campaign it was made for ended a year ago and its hosting account was closed.

How to solve it

This is a subdomain takeover through a dangling CNAME. Delete the record immediately, then check whether the attacker could have set cookies scoped to the parent domain and rotate sessions if so. Audit the whole zone: list every CNAME and A record, and confirm that each target still belongs to you. Add a rule to the offboarding checklist for any service: remove DNS first, then delete the resource. Consider tools that monitor Certificate Transparency logs for unexpected certificates on your domains.

Cheat sheet

Authoritative nameservers
dig example.com NS +short
Answer straight from the authoritative server
dig example.com A @ns1.example-dns.net +norecurse
Remaining TTL in a resolver's cache
dig example.com A @8.8.8.8 | grep -A1 "ANSWER SECTION"
Full resolution path
dig +trace example.com
Zone serial and negative TTL
dig example.com SOA +short
DNSSEC delegation record
dig example.com DS +short
Reverse lookup (PTR)
dig -x 203.0.113.10 +short
Flush DNS cache on macOS
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Flush DNS cache on Ubuntu (systemd-resolved)
resolvectl flush-caches
Block all wildcard certificates
example.com. CAA 0 issuewild ";"

In my real projects

  • I manage DNS for my sites and client sites, with Cloudflare in use for DNS on several of them and a plan to move all domains there.
  • In server migrations I follow inventory, copy, verify, switch DNS, and keep the old server until everything is confirmed.
  • My consolidation plan moves mail to a hosted provider, which means rewriting MX, SPF, DKIM and DMARC records carefully.

Questions & answers

Q1What is the difference between a registrar and a DNS provider?

The registrar is where you register and renew the domain and where you set its nameservers; it reports that delegation to the TLD registry. The DNS provider runs those nameservers and stores the actual records such as A, CNAME, MX and TXT. They can be the same company, but often are not: a domain can be registered at one company and use Cloudflare for DNS. The key practical point is that records only matter at whichever provider the nameservers point to. Editing them anywhere else has no effect, which explains many "I changed the record but nothing happened" support tickets.

Q2Why does a DNS change take time to appear?

Because answers are cached by resolvers, operating systems and browsers for the record's TTL. If the old record had a TTL of one day, some users can see the old IP for up to a day. Nameserver changes take longer because the delegation at the TLD is cached separately, often for 24–48 hours. So it is not propagation in the sense of pushing data; it is caches expiring. I plan for it: lower the TTL well before a migration, switch, verify with dig against several public resolvers and the authoritative server, and keep the old server running until traffic has fully moved.

Q3Can you put a CNAME on the root domain?

Not by the DNS standard. A CNAME means this name is an alias and has no other records, but the apex must have SOA and NS records, and usually MX and TXT. Putting a CNAME there conflicts with them and can break email. Providers solve it with CNAME flattening, ALIAS or ANAME records: you configure an alias, and the DNS server resolves the target itself and returns A and AAAA records. So for a hosting platform that gives you a hostname, use that feature on the apex, or point the apex to IPs and use a CNAME on www.

Q4How do SPF, DKIM and DMARC work together?

SPF is a TXT record listing which servers may send mail for your domain; receivers check the sending IP. DKIM adds a cryptographic signature to each message, and receivers verify it with a public key published in DNS under a selector. DMARC ties them to the visible From domain: it says that at least one must pass and align, defines a policy for failures (none, quarantine, reject) and where to send reports. Together they stop spoofing and improve deliverability. I start DMARC at p=none to collect reports, fix every legitimate sender, then move to quarantine and reject.

Q5How would you migrate a site to a new server with no downtime?

First inventory everything: files, database, cron jobs, environment variables, certificates and DNS records. Lower the TTL of the relevant records to 300 seconds at least one TTL period before. Set up the new server, copy data, and test it directly with a hosts-file entry or curl --resolve. Freeze writes or sync the database for the final cut, then switch the A records. Watch logs on both servers; requests drain from the old one as caches expire. Keep the old server untouched until the new one has run cleanly for a while, then raise the TTL and decommission.

Senior-level questions

S1How would you use DNS for failover, and what are its limits?

Managed DNS can run health checks and change answers when an endpoint fails, for example Route 53 failover routing or a load-balancing feature at a CDN. With TTLs around 60 seconds, most users move within a few minutes. The limits are real: resolvers and clients may cache longer than the TTL, some serve stale answers when they cannot refresh, and long-lived connections keep talking to the old IP until they reconnect. DNS also cannot see application health beyond what the check tests. So I treat DNS failover as a coarse, regional mechanism, and use a load balancer or anycast proxy for fast, per-request failover. Data replication between the two sites is usually the harder part.

S2Should every domain run DNSSEC?

It is worth enabling when your registrar and DNS host support it with automated key management, because it blocks forged answers for validating resolvers and enables features like DANE for mail. The trade-off is operational risk: a wrong or stale DS record turns into a full outage for everyone using validating resolvers, which is a large share of users today. Migrations need a careful order, and some providers do not support multi-signer setups. For a small team, I enable it on a managed provider, document the DS removal step in migration runbooks, and monitor with an external checker. Without automation and a runbook, a misconfigured DNSSEC is worse than none.

S3How do you plan DNS for a multi-tenant SaaS with customer domains?

Customers point a CNAME, such as shop.customer.com, to a stable hostname you control, like tenants.example-saas.com, and the apex needs ALIAS or flattening on their side or an A record to anycast IPs. You verify ownership with a TXT token, then issue certificates automatically, typically with HTTP-01 once traffic arrives or through a CDN's custom-hostname product that handles validation and renewal. Routing uses the Host header to find the tenant. Important risks: a customer removes the CNAME but you keep the mapping, or you delete a tenant while their record still points to you, so monitoring and cleanup jobs are needed. Rate limits on certificate issuance and clear error pages for unverified domains complete the design.

Glossary

Zone
The part of the DNS namespace one set of nameservers is authoritative for, such as example.com and its subdomains.
Apex
The bare domain itself (example.com), also called the root or naked domain.
Recursive resolver
The server that looks up answers on behalf of clients and caches them, such as 1.1.1.1.
Authoritative nameserver
The server that holds the real records for a zone and gives the final answer.
Glue record
An IP address for a nameserver stored in the parent zone, needed when the nameserver is inside its own domain.
NXDOMAIN
The DNS response meaning the name does not exist at all.
DS record
A hash of a zone's DNSSEC key, published in the parent zone to link the chain of trust.
PTR record
Reverse DNS: maps an IP address back to a hostname; important for mail servers.
Anycast
One IP address announced from many locations, so traffic reaches the nearest working server.
Subdomain takeover
When someone claims a deleted third-party resource that your DNS record still points to.

Have a product in mind?Let's build it.

Tell me about your idea, your product or your team. I usually reply within one working day.