Sub topic 2
Resource records and TTLs
This chapter assumes you can use a Linux shell and have read the previous chapter on how resolution walks from the root servers to an answer. Here you learn what a DNS server stores and returns, and how long other servers may keep it.
What a resource record contains
DNS data is stored as resource records (RRs). Each record has five fields:
| Field | Meaning | Example |
|---|---|---|
| Owner name | The name the record belongs to | `www.example.com.` |
| TTL | Seconds a cache may keep the record | `3600` |
| Class | Almost always `IN` (Internet) | `IN` |
| Type | What kind of data follows | `A` |
| RDATA | The data, whose format depends on the type | `192.0.2.20` |
In a zone file, one record is one line in that order: www 3600 IN A 192.0.2.20. The TTL and the class may be omitted. The default TTL then comes from the $TTL directive. Zone file syntax gets its own chapter. Here you only need to read the records.
Common record types
| Type | Purpose | RDATA |
|---|---|---|
| A | Name to IPv4 address | One address |
| AAAA | Name to IPv6 address | One address |
| NS | Names a server authoritative for the zone | A host name |
| CNAME | Alias to another name | The canonical name |
| MX | Mail exchanger for the domain | Preference number and host name |
| TXT | Free text, used for SPF, DKIM and verification | One or more quoted strings |
| SOA | Zone metadata: primary server, contact, timers | Seven fields |
| PTR | Address to name, used in reverse zones | A host name |
| SRV | Service location | Priority, weight, port, target |
| CAA | Which CAs may issue certificates | Flag, tag, value |
A short zone shows several of these together:
$TTL 3600
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026112801 7200 3600 1209600 300 )
IN NS ns1.example.com.
IN MX 10 mail.example.com.
IN TXT "v=spf1 mx -all"
ns1 IN A 192.0.2.10
mail IN A 192.0.2.25
www IN A 192.0.2.20
IN AAAA 2001:db8::20
web IN CNAME www.example.com.Three details matter here:
@stands for the zone name,example.com..- A line that begins with whitespace reuses the previous owner name. The AAAA record therefore belongs to
www. - Names that end in a dot are absolute. Names without a dot get the zone name appended.
Record sets
All records with the same owner name, class and type form a record set (RRset). A resolver treats the set as one unit. It returns and caches all of it together, and every record in a set carries the same TTL. If two A records for www had different TTLs, servers would normally adjust them to the lowest value.
A name can have several A records for simple load spreading. Servers usually rotate the order between responses, and clients tend to try the first address. This is not health-checked failover.
Rules that cause real outages
- A CNAME cannot share its name with other records. A name with a CNAME may not also have MX, TXT, NS or any other type. The zone apex always has SOA and NS, so you cannot put a CNAME there. BIND's
named-checkzonerejects this. - NS and MX targets must be host names with address records. They must not be CNAMEs or IP addresses.
MX 10 192.0.2.25is read as a relative name, not an address. - A missing trailing dot appends the zone name. Inside
example.com, the linewww IN CNAME host.example.compoints athost.example.com.example.com.. Writehost.example.com.or justhost. - A name's parents must exist implicitly. A record at
a.b.example.com.makesb.example.com.an "empty non-terminal". It answers NOERROR with no data, not NXDOMAIN. - In-zone NS targets need glue. If
ns1.example.comservesexample.com, the parent zone must also hold the address ofns1, or no one can find your server. You set glue at your registrar.
How TTLs work
The TTL is permission to cache. An authoritative server returns the configured TTL. A resolver stores the answer and counts the TTL down. A client that asks again before it reaches zero gets the cached answer with the remaining time.
You can watch this with dig. Run the query twice against a resolver and compare the TTL column, which is the second field of each answer line:
dig @127.0.0.1 www.example.com A +noall +answer
sleep 10
dig @127.0.0.1 www.example.com A +noall +answerwww.example.com. 3600 IN A 192.0.2.20
www.example.com. 3590 IN A 192.0.2.20Querying the authoritative server directly with +norecurse always shows the full configured TTL. This is a quick way to tell whether you are looking at a cache or at the source.
Choosing TTL values
TTL is a trade-off between load and agility:
| TTL | Effect |
|---|---|
| Short (60 to 300 s) | Changes spread quickly. More queries reach your server. |
| Medium (3600 s) | A common default for ordinary records. |
| Long (86400 s or more) | Little query load. A wrong value stays cached for up to a day. |
Resolvers may clamp what they accept. Unbound has cache-min-ttl and cache-max-ttl, and BIND has max-cache-ttl. Some clients and applications add their own caches that ignore TTLs, so you can never be certain a change has fully propagated.
Planning a change
You cannot recall a cached record, only wait it out. To move www to a new address safely:
- Lower the TTL to 300 seconds at least one old-TTL period before the change. With a 3600 s TTL, do this an hour or more ahead.
- Change the address and increase the SOA serial.
- Confirm the new answer with
dig +norecurseagainst the authoritative server. - After the change settles, restore the longer TTL.
Negative caching
Resolvers also cache failures. When a name does not exist (NXDOMAIN) or has no record of the requested type, the resolver caches that fact. The negative TTL is the smaller of the SOA record's own TTL and the last SOA field, here 300. That field is the "minimum" in the older RFC 1035 and was redefined as the negative caching TTL in RFC 2308.
Practical consequence: if a client queries new.example.com before you create it, resolvers may keep saying "does not exist" for up to five minutes after you add it. A very long negative TTL turns a brief mistake into a long one.
Reading records from a live server
Use dig with a type and short output to check what you serve:
dig @127.0.0.1 example.com MX +short
dig @127.0.0.1 example.com SOA +noall +answer
dig @127.0.0.1 web.example.com A +noall +answer
dig @127.0.0.1 nothere.example.com A +noall +comments +authorityThe last command asks for a name that does not exist. The SOA record appears in the authority section. Its TTL is the negative cache time that resolvers will use.
Exercise
Your zone example.com has blog IN CNAME www.example.com with a TTL of 86400, and you want to move www to a new IP address tomorrow at 09:00. What do you change today, and why does the line as written contain an error?
Show answer
The www A record needs a short TTL (for example 300) well before the move. Its old TTL must elapse first, so lower it now if the current value is 3600 or less, and earlier if it is longer. The CNAME's own TTL does not matter for the address change, because the resolver fetches the A record for www separately. The error is the missing trailing dot. www.example.com without a dot becomes www.example.com.example.com.. It should be www.example.com. or simply www.