System designCore12 min
DNS resolution
Every connection to your service starts with a question to DNS: what address is behind this name? The answer is usually a cached copy, which is why DNS is fast, why changes take hours to spread, and why a DNS failure makes healthy servers vanish.
1 · The lookup
Most lookups are answered by a cache near you
A ski resort sells lift tickets at tickets.snowpeak.example. When a skier's phone opens the site, it needs the server's IP address, and it asks in layers, stopping at the first one that knows:
- The browser and the operating system keep recent answers. The part of the operating system that asks is the stub resolver: it doesn't search, it only forwards the question.
- A recursive resolver, run by the internet provider or a public service such as 1.1.1.1 or 8.8.8.8, does the searching. It is shared by thousands or millions of users, so it has usually seen the name already.
- Authoritative servers hold the real records. Only they can answer without asking anyone else.
When the resolver has nothing cached, it walks down the name from the right:
Each answer is cached for its time to live (TTL). The referrals from the root and the top-level-domain (TLD) servers are shared by every name under them, so a busy resolver rarely repeats the first two steps.
The root is not one machine. There are 13 named root server identities, run by 12 independent organizations, and over 2,000 copies of them around the world. All that caching is why a lookup usually costs one short round trip to the resolver, or nothing at all when the device already has the answer, and why the cold walk above is rare.
2 · The records
A domain's DNS is a short table of typed records
Snowpeak's authoritative servers hold a zone: the records for snowpeak.example and every name under it. Each record has a name, a type, a value and a TTL:
Type | Snowpeak's record | Meaning |
|---|---|---|
A | tickets → 198.51.100.20 | An IPv4 address |
AAAA | tickets → 2001:db8::20 | An IPv6 address |
CNAME | www → tickets.snowpeak.example | This name is an alias; go and look up that one |
MX | snowpeak.example → mail.snowpeak.example | Where email for the domain goes |
TXT | snowpeak.example → "verify=…" | Free text: proof of ownership, email sender policies |
NS | snowpeak.example → ns1.dnsco.example | Which servers are authoritative for the zone |
One rule trips up nearly everyone. A name that has a CNAME may have no other records. The bare domain, snowpeak.example, is the top of the zone, called the zone apex, and it must carry NS records (and the zone's start-of-authority record). So the apex can't be a CNAME, and you can't point snowpeak.example at a hosting provider's name that way. DNS providers work around it with alias or flattening records: the provider looks up the target itself and answers with plain addresses.
3 · Changes
A change reaches the world one cache at a time
Two weeks before opening day, Snowpeak moves the ticket site to a new hosting provider. The A record has a TTL of one day, so a resolver that fetched it this morning will keep handing out the old address until tomorrow morning, whatever the authoritative servers now say. Change the record at 9:00, and this is the share of busy resolvers still sending skiers to the old server:
- TTL 1 day
- TTL 5 minutes
Model: each busy resolver last fetched the record at a random moment within one TTL, so the share still holding the old answer is 1 − time since the change ÷ TTL. With a 1-day TTL it takes a full day to reach zero; with 5 minutes, 5 minutes.
The trap is to lower the TTL at the same moment as the change. Resolvers already hold the old record with the old one-day TTL, and they won't ask again until it runs out. The order that works:
- Lower the TTL first
Change it from 1 day to 5 minutes, without touching the address.
- Wait out the old TTL
At least one full day, until every cached copy with the long TTL has expired.
- Switch the address
Within about 5 minutes, almost every resolver has the new one.
- Keep the old server running
Some resolvers hold answers longer than the TTL allows, so forward or serve stragglers for a few days.
- Raise the TTL again
Once the move is stable, go back to a long TTL for faster, cheaper lookups.
Missing names are cached too. If someone looks up lifts.snowpeak.example before it exists, the "no such name" answer is cached for a time set in the zone's start-of-authority record; one to three hours is the recommended range. Create the name a minute later, and those users keep seeing "not found" until that runs out. This is negative caching: create names before anyone, including your own tests, asks for them.
4 · Steering traffic
DNS can steer users, but only as fast as caches forget
Because the authoritative server chooses each answer, it can answer differently for different askers. Managed DNS services offer several policies:
Policy | What the server answers | Used for |
|---|---|---|
Several addresses | A list of addresses, often shuffled | Spreading users over several load balancers |
Weighted | Address A 90% of the time, B 10% | Trying a new site on a slice of traffic |
Latency or location | The region closest to the asker | Sending European skiers to the European region |
Failover | The primary while its health checks pass, else the backup | Surviving the loss of a region |
The limits come from DNS's own design:
- Caching slows every change. Failover takes the health checks' detection time plus up to one TTL, and longer for clients that hold answers.
- The server sees the resolver, not the user. A skier in Austria using a resolver in another country may get that country's region. An extension called EDNS Client Subnet lets resolvers pass along part of the user's address (the first 24 bits of an IPv4 one) to fix this.
- DNS knows nothing about load. It can't tell that the server it named is overloaded.
DNS makes the coarse choice once per TTL; the load balancer makes the fine choice on every connection, and knows which servers are busy.
So DNS chooses the region or the load balancer, and a load balancer chooses the server (Load balancing).
A different trick avoids the caching problem altogether. With anycast, the same IP address is announced from many locations, and internet routing delivers each packet to the nearest one in network terms. The address never changes, so nothing needs to expire. The root servers, large public resolvers and CDNs all use it (Content delivery networks (CDNs)).
5 · When it fails
When DNS fails, healthy servers disappear
If a name can't be resolved, it doesn't matter how healthy the servers behind it are: no one can find them. Two well-known outages:
Year | What happened | Effect |
|---|---|---|
2016 | A botnet of hacked cameras and home routers flooded a major DNS provider in three waves | Twitter, Netflix, Reddit, GitHub and many others unreachable for many users in Europe and North America |
2021 | A capacity-check command disconnected every Facebook data center; its DNS servers, built to withdraw their routes when cut off, did so | The DNS servers were running but unreachable; Facebook's names vanished from the internet for about six hours |
In the first case the sites' own servers were fine; they shared a single DNS provider. In the second, a safety mechanism meant to take one unhealthy DNS location out of service took all of them out at once.
Ways to make DNS less of a single point of failure:
Defense | How it helps | The catch |
|---|---|---|
Two independent DNS providers | Both are listed in the NS records, so either can answer | Keeping both zones in sync |
TTLs that aren't needlessly short | Cached answers keep working while the authoritative servers are down | Changes spread more slowly |
Serve-stale | Resolvers that support it answer with expired records, for up to one to three days, when they can't reach the authoritative servers | Only helps names that were cached |
Checking from outside | Resolve your names from other networks, as users do | Inside your network answers may be cached or local |
6 · Security
Plain DNS can be read on the way and forged by some
Classic DNS sends small, unencrypted messages over UDP on port 53. An answer too large for one UDP message comes back marked as cut short, and the asker retries over TCP; every DNS implementation must support both. Two weaknesses follow from the plain format:
Threat | Defense | What the defense doesn't do |
|---|---|---|
Forged answers: an attacker races the real server with a fake reply, poisoning a resolver's cache | Random source ports on top of the 16-bit query ID, making a correct guess far harder | Prove the answer is genuine |
Forged answers, for a signed zone | DNSSEC: the zone signs its records, and resolvers verify the signatures | Hide anything: DNSSEC adds proof, not privacy, and no protection from floods |
Eavesdropping: anyone on the path sees every name you look up | DNS over TLS or DNS over HTTPS encrypts the hop between you and your resolver | Hide your lookups from the resolver itself |
A forged answer needs the attacker's reply to match the question, come from the right server's address, and carry the right query ID and port. With a 16-bit ID alone, about 32,768 guesses succeed on average; randomizing the port as well multiplies the work by up to tens of thousands. DNSSEC removes the guessing game entirely for signed zones, because a forged record has no valid signature.
7 · In practice
Plan DNS changes a TTL ahead
What to remember when DNS is part of your design:
Situation | What to do |
|---|---|
Moving a service to new addresses | Lower the TTL, wait one old TTL, switch, keep the old servers up for stragglers |
Pointing the bare domain at a provider | Use an alias or flattened record; a CNAME isn't allowed at the apex |
Launching a new name | Create it before anything looks it up |
Failing over between regions | Short TTLs on those records, health checks, and a plan for the clients that cache longer |
Depending on one DNS provider | Add a second, independent one |
A checklist:
- Know your TTLs, and lower them a full TTL before any planned change.
- Use DNS for coarse steering, between regions and load balancers, and load balancers for servers.
- Treat DNS as part of your availability: two providers, sensible TTLs, monitoring from outside.
- Sign your zone with DNSSEC if your provider supports it, and protect the account that controls your domain: whoever can change your records can redirect all of your traffic.
Check yourself
5 questions
Takeaways
Remember this
- DNS answers come from layers of caches; only on a cold cache does a resolver walk from the root to the domain's own servers.
- Every answer is kept for its TTL, so lower the TTL a full old TTL before a change, and expect some clients to hold answers even longer.
- A name with a CNAME can have nothing else, so the bare domain needs an alias or flattened record instead.
- DNS can steer users between regions, but caching slows failover and it knows nothing about load; load balancers pick servers.
- When DNS fails, healthy servers become unreachable: use two providers, sensible TTLs, and watch resolution from outside.