skip to content

Which DNS record types does example.com need to serve a web app and receive email, and what does each one answer?

level: juniorimportance: must knowfreq 74%

answer

  1. one lookup per name and type
  2. two address families
  3. alias versus address
  4. mail has its own lookup
  5. lower number wins

basics

~20 s

A and AAAA map a name to IPv4 and IPv6 addresses, CNAME makes one name an alias of another, MX names the mail servers with a preference (lower first), and TXT carries free-form strings such as verification tokens.

solid answer

~40 s

For a web app plus mail I would publish `A` (IPv4) and `AAAA` (IPv6, RFC 3596) records at the apex `example.com`, and make `www` either its own `A`/`AAAA` pair or a `CNAME` pointing at another name, such as a hosting platform's hostname. Mail is found by a separate lookup: senders ask for the `MX` records of `example.com`, each holding a 16-bit preference and an exchange hostname, and lower preference values are tried first (RFC 1035). That exchange name must have its own address records and must not be an alias (RFC 2181 §10.3). `TXT` records carry arbitrary strings, which is where ownership-verification tokens and mail-policy strings live. Every type is a separate question to the DNS, so the web addresses and the mail servers can point at entirely different hosts.

code

dns · 8 lines
dns
$ORIGIN example.com.
@     3600 IN A     192.0.2.10
@     3600 IN AAAA  2001:db8::10
www   3600 IN CNAME example.com.
@     3600 IN MX    10 mx1.example.com.
@     3600 IN MX    20 mx2.example.net.
mx1   3600 IN A     192.0.2.25
@     3600 IN TXT   "example-verification=4f9c2a71"

go deeper

for a junior

Recall what A, AAAA, CNAME, MX and TXT each answer, and that MX lists mail hosts by name with a preference where the lower value is tried first.

for a middle

Explain that each lookup asks for one name and one type, how a CNAME makes the server restart the query at the target, and why an MX exchange must be a name with its own address records.

for a senior

Show you can lay out a real domain: which names get addresses, which become aliases to platform hostnames, where mail and verification strings go, and how those choices constrain later changes.

for a principal

Frame the record layout as a dependency map: which records point into names other teams or vendors control, what breaks when they change, and how that shapes ownership of the zone.

## What a DNS record type is Every piece of data in the Domain Name System is a **resource record** (RR). Each record has an **owner name** (the name it describes), a **type**, a **class** (almost always `IN`), a **TTL** (how many seconds a cache may keep it) and **RDATA**, the type-specific payload. The type decides what question the record answers. Records that share the same owner name, class and type form an **RRset** (RFC 2181 §5). Two `A` records at `example.com` are one RRset and are always returned together. A resolver asks for exactly one name and one type at a time, so the web lookup and the mail lookup for the same domain are separate questions with separate answers. ## The five types a web-plus-mail domain uses | Type | Type code | RDATA | Question it answers | |---|---|---|---| | `A` | 1 | a 32-bit IPv4 address | Which IPv4 address serves this name? | | `AAAA` | 28 | a 128-bit IPv6 address | Which IPv6 address serves this name? (RFC 3596) | | `CNAME` | 5 | one domain name | This name is an alias: where is the canonical name? | | `MX` | 15 | 16-bit preference + exchange name | Which hosts accept mail for this domain? | | `TXT` | 16 | one or more character strings | What text is published here? Meaning depends on the name | `NS`, `SOA`, `PTR`, `SRV` and `CAA` complete the everyday set, but a first web-plus-mail setup can be explained with these five. ## Walking through the setup 1. **Apex addresses.** Publish `A` and `AAAA` at `example.com`. Both may exist at once; a dual-stack client asks for both types, and adding `AAAA` does not remove or replace `A`. 2. **The `www` name.** Give `www` its own `A`/`AAAA` records, or make it a `CNAME`. With a `CNAME`, a server that finds the alias includes it in the response and restarts the query at the canonical name (RFC 1034 §3.6.2), so the client receives the alias plus the target's records. 3. **Mail.** Publish `MX` records at `example.com`. Each carries a **preference**: RFC 1035 §3.3.9 says lower values are preferred, so preference 10 is tried before 20. The **exchange** is a host name, never an address, and RFC 2181 §10.3 requires that name to have address records of its own and not to be an alias. 4. **Text.** Publish `TXT` records at the apex or at a name a service specifies. RFC 1035 §3.3.14 says only that TXT holds descriptive text whose semantics depend on where it is found; verification tokens and mail-policy strings are both conventions layered on top. ## What these records do not do - **A `CNAME` is not an HTTP redirect.** It changes which name's records the resolver returns; the browser still asks the server for the original host name, and the address bar does not change. - **Mail and web are independent.** The exchange names in the `MX` set can live in another organisation's domain, and changing the apex `A` record does not move mail delivery. - **The DNS TTL is a cache lifetime in seconds.** It has nothing to do with the IP header's TTL, which counts router hops. - **An `MX` record cannot hold an IP address.** Its RDATA is a preference plus a domain name; the address comes from that name's own `A`/`AAAA` records, which servers often include in the additional section (RFC 1035 §3.3.9). ## How interviewers use this question At junior level the interviewer wants the one-line meaning of each type and one correct placement: addresses at the apex, `www` as an alias or its own addresses, `MX` pointing at named mail hosts. Strong answers add the direction of the `MX` preference, the rule that an `MX` target is a name with its own address records, and the separation between a name lookup and the application protocol that follows it. Follow-ups usually move to why the apex itself cannot be a `CNAME`, which is a separate question with its own rule.

  • If www.example.com is a CNAME to example.com, what happens when a client asks for www's AAAA record?
    The server finds no `AAAA` at `www`, sees that the name holds a `CNAME`, includes that record in the answer and restarts the lookup at `example.com` (RFC 1034 §3.6.2). The response carries the alias followed by `example.com`'s `AAAA` records. Only a query for type `CNAME` itself is not restarted.
  • Why must an MX exchange name have its own A or AAAA records rather than being a CNAME?
    RFC 2181 §10.3 says the name inside an `MX` (and inside an `NS`) must not be an alias and must have address records. The sender resolves the exchange name to addresses, and servers add those addresses to the additional section when answering `MX` queries (RFC 1035 §3.3.9). An alias there costs an extra lookup and defeats that shortcut, which is why RFC 2181 says it does not work as well as hoped.

saying these in an interview costs you the question

  • An MX record can point straight at the mail server's IP address.
  • A higher MX preference number means that server is tried first.
  • Publishing an AAAA record replaces the A record for that name.
  • A CNAME works like an HTTP 301 redirect to the target host.
  • The DNS TTL counts router hops, like the TTL in the IP header.