QA Interview Hub · Knowledge base · Quizzes · Українська

Networking › TCP/IP and the path of a request

DNS: name resolution, records, TTL and cache

DNS turns a name into data (A, CNAME, MX, TXT) through a hierarchy of servers. A recursive resolver asks the root, the TLD and the authoritative server and caches the answer. Every answer has a TTL, and until it expires the cache returns the old address, so changes apply gradually. Discrepancies are diagnosed by comparing answers at different levels with dig, and TTL is lowered before a migration.

Networking · TCP/IP

After a server migration you see the new site and a colleague still sees the old one. Why?

The team moved the app to a new server and changed the A record. You open the site and see the new version, a colleague sees the old one, and CI keeps using the old address for two more minutes. One server, one record, different answers. The reason is that a name becomes an IP through a chain of caches, and each node of the chain has its own moment when the old answer expires. This lesson explains the chain, TTL and records, and shows how to see at which level the old answer got stuck.

How it works and why. DNS is a distributed database "name → data". No single server knows everything, so the system is hierarchical: the root knows who answers for .com, the TLD knows who answers for the domain, and the domain's authoritative server knows the answer. The client does not walk it all itself: it asks a recursive resolver (the ISP's, a corporate or a public one), which performs the walk and caches the result. Caching at every step is what makes DNS fast, and it is also what creates "stale" answers.

Where the answer to a DNS query comes from

1. Caches: browser, OS, hosts filethe fastest path, TTL applies2. Recursive resolver (8.8.8.8, 1.1.1.1)its cache is shared by many clients3. Root, then TLD (.com, .ua)they only point to who knows next4. Authoritative server of the domainthe source of truth: records and TTL
Figure 1. The answer comes from the first cache where it is still alive. A change on the authoritative server does not erase the caches above it.

Every answer has a TTL: how many seconds it may be kept. Until the TTL passes, a cache returns the old address even if the record has changed. Negative answers (NXDOMAIN) are cached as well, so a record that was just created may "not exist" for a while for those who asked earlier. A fresh answer is demanded when the TTL has passed, not when somebody changed the record.

A long or a short TTL

Long TTL (hours)

Fewer queries, faster answers. A record change arrives slowly: some clients keep going to the old address for a long time.

Short TTL (minutes)

Changes apply quickly, handy for migrations and failover. More DNS queries and more risk when a resolver fails.

Figure 2. That is why TTL is lowered in advance before a migration, by at least the previous TTL, and restored once things are stable.

Records you must be able to read.

  • A name → IPv4, AAAA name → IPv6; if both exist, the client chooses, usually trying both (Happy Eyeballs);
  • CNAME an alias to another name (www → app.example.net). A CNAME is not placed at the zone apex, because it cannot coexist with other records, so providers offer "ALIAS" or "CNAME flattening";
  • MX mail servers, TXT free text (SPF, domain ownership checks), NS the domain's authoritative servers;
  • HTTPS and SVCB hints for the client, for example that the server supports HTTP/3.

Example: whose cache is lying. The record was changed but some users are still on the old address. Where the problem is gets decided by comparing answers at different levels:

# output shortened, numbers illustrative
dig +noall +answer app.example.com
# app.example.com. 3 IN A 203.0.113.10      # 3 s of TTL left: a resolver cache

dig +noall +answer app.example.com @ns1.example.com
# app.example.com. 300 IN A 203.0.113.99    # the authoritative server already gives the new one

dig +short app.example.com @1.1.1.1
dig +short app.example.com @8.8.8.8          # compare different resolvers

Read it like this. If the authoritative server returns the new address and the resolver the old one, the resolver cache is the cause and what remains is waiting for the TTL (or for the provider to flush). If the authoritative server returns the old address, the record was simply not changed, or was changed in the wrong zone. If dig shows the new address and the browser the old one, the cause is an OS, browser or per-process cache: the JVM, for example, has a cache of its own (networkaddress.cache.ttl), while Node has none and relies on the OS or on libraries. To flush the OS cache on Windows: ipconfig /flushdns.

Limits and alternatives. A DNS cache is not the only thing that holds an old address. Long-lived connections (keep-alive, WebSocket, a connection pool in a test client) keep talking to the IP they were opened with until they close, even after the TTL ends. So restarting the client solves more than waiting does. Different answers in different regions may not be an error but geo-balancing or a CDN: different resolvers return different addresses by design. DNS over HTTPS (DoH) and TLS (DoT) also exist: they encrypt the query but do not change the cache logic.

Common mistakes.

  • Changing a record and assuming "DNS has updated": the old answer lives until the TTL ends.
  • Diagnosing only from your own machine: ask specific resolvers (dig @1.1.1.1) and the authoritative server itself.
  • Putting a CNAME at the apex next to MX and TXT: the record conflicts with the others.
  • Not linking ENOTFOUND or ERR_NAME_NOT_RESOLVED to DNS: it is a typo, a missing record, an unreachable resolver or a VPN, not "the server is down".
  • Overriding an address in hosts to test and forgetting to remove it: the test then "works" on one machine only.

Check yourself

You changed an A record, TTL was 3600 s. After 10 minutes dig @ns1 shows the new address and dig @1.1.1.1 the old one. What does it say and what do you do?

Answer

The authoritative server already returns the new address, while the public resolver keeps the cache from the previous answer and refreshes it when the TTL ends, so within an hour of its last query at most. This is normal behaviour, not an error: for a fast switch the TTL is lowered in advance, and until then DNS is bypassed with curl --resolve or a hosts entry.

After a deploy a CI test keeps going to the old server for an hour although dig shows the new address. Name two causes that are not the resolver cache.

Answer

A DNS cache in the runner process or OS (its own TTL, flushing needs a restart) and a long-lived connection opened to the old IP that the client pool has not closed. Check by restarting the client or process and by closing the pool explicitly.

In an interview. Junior: DNS turns a name into an IP address. There are A, AAAA, CNAME, MX and TXT records. TTL says how long an answer can be cached, so changes do not apply at once. Middle/Senior: The answer goes through the browser, OS and recursive resolver caches to the authoritative server, so for discrepancies I compare answers at different levels with dig. Before a migration I lower the TTL in advance, and I remember negative caching, a CNAME at the apex and that long-lived connections do not read DNS again.

More articles in this chapter