MA-NET1 · Module 6
Electronic mail and DNS
Topics: SMTP · IMAP and POP · DNS hierarchy · Caching
Overview
Session 6 — Electronic mail and DNS
Mira has written her first message to a teammate, Teo, who works at a harbour-monitoring partner. She presses Send and, a moment later, wonders how a message typed on her laptop ends up in someone else's inbox, and how her laptop even found the machine called mail.skylark.example in the first place. This session answers both questions: first the path of an email, then the naming system that turns host names into IP addresses.
By the end you should be able to: name the parts of the mail system (user agent, mail server, SMTP); explain why SMTP pushes mail while IMAP and POP pull it; describe the path of a webmail message; explain DNS as a distributed name-to-address service; tell root, TLD, authoritative, and local resolver servers apart; explain how caching speeds lookups; and argue why DNS is not a single central machine.
Scroll Core ideas → worked examples → Quiz. The examples ask you to add up lookup times and to read a short mail dialogue, so keep a calculator and a pencil nearby. Plan on roughly 60 minutes. If you have a terminal, nslookup or dig on a name you are allowed to query makes a nice optional experiment, but nothing here requires one.
Concepts
Core ideas — electronic mail and the Domain Name System
Two of the most ordinary things on the Internet, sending an email and typing a name into a browser, rely on two quiet systems that most people never see. In this lesson we meet both. We follow Mira, a developer at Skylark Forecast, as she sends a message from her laptop and then, separately, watches her browser look up a host name. As always, every name, address, and number here is invented for practice. Take your time: each section builds on the one above it.
The three players in electronic mail
Email looks like one thing from the outside, but it is really a small team of cooperating programs. Three kinds of component appear in nearly every explanation.
The user agent (sometimes called a mail client) is the program Mira actually uses: a desktop mail application, a phone app, or a web page. Its jobs are to let her compose and read messages. It hands finished messages to a mail server and fetches new ones from a mail server.
The mail server is a program on an always-on machine that holds the mailbox of each user it serves and keeps a queue of outgoing messages. For Skylark, one such machine is called mail.skylark.example. Because it is always on and reachable, it can receive messages at any time of day, even when Mira's laptop is closed in a bag.
The Simple Mail Transfer Protocol (SMTP) is the language that mail servers speak to each other when moving a message forward, and the language a user agent speaks when it submits a message for sending. SMTP runs over a reliable transport connection, so the bytes of a message arrive complete and in order. Its conversation is made of short text commands such as MAIL FROM, RCPT TO, and DATA, and numeric replies such as 250, which means "fine, go on."
Putting them together: Mira's user agent hands her message to her own mail server. That server opens an SMTP connection to the recipient's mail server and delivers it. The recipient's server stores it in the recipient's mailbox. Later, the recipient's user agent fetches it. Notice that the message goes server to server directly; it does not wander through a chain of intermediate machines on the way, because SMTP uses a direct connection between the sending and the receiving mail server.
SMTP is a push protocol
The word push describes who starts the transfer. In SMTP, the machine that has the message initiates the connection and pushes the message to the machine that should receive it. The sender is the client in that connection, and the receiving mail server plays the server.
This design fits the mail server's job nicely. A sending server wakes up whenever there is something in its queue and opens a connection to the destination server. If the destination is unreachable, the sender keeps the message and tries again later, rather than losing it.
SMTP is also quite old and plain. Messages must be composed of simple text characters in the protocol's headers, and the end of the message is signalled by a line containing a single full stop. For things that are not plain text, such as pictures and attachments, mail uses a separate set of conventions that encode the binary content into text before sending it.
Where does the sending server find the receiver's mail server? Through the naming system we study in the second half of this lesson: a special kind of DNS record, called an MX record, says which host handles mail for a domain.
Common confusion: SMTP carries a message but says nothing about how the recipient later reads it. Reading is a different job with a different protocol, which is the next topic.
Why reading mail uses a pull protocol, not SMTP
Suppose Teo's laptop is switched off at night. Skylark's partner server has Teo's message waiting in his mailbox. Could that server simply push the message to Teo's laptop with SMTP? It cannot, for two reasons that are worth saying slowly.
First, a laptop is usually not always on, and its address may change from day to day, so a mail server cannot rely on finding it. Second, SMTP is built for server-to-server delivery; it has no notion of logging in, choosing a folder, or marking a message as read.
So the design flips the direction. The user agent pulls messages from the mailbox whenever it wants, by opening a connection to the mail server and asking. Two main protocols do this job:
- IMAP keeps the messages on the server and lets the user agent browse folders, search, and mark messages as read. Because everything stays on the server, Mira can read the same mailbox from her laptop and from her phone and see the same state.
- POP is simpler: the user agent downloads the messages, and in the traditional mode the server may then delete its copies. That suits someone who reads from only one device but gives a different view on each device.
Both require the user to authenticate, because a mailbox is private.
The overall picture has a neat rhythm: push between servers (SMTP), pull from the final server to the reader (IMAP or POP). The submission step from Mira's user agent to her own mail server also commonly uses SMTP, because there Mira's agent is the one holding the message and pushing it along.
Common confusion: "Mira's laptop receives mail with SMTP" is wrong. Her laptop retrieves mail with IMAP or POP. SMTP got the message to the server.
Webmail: the same path with a browser in front
Many people read mail in a browser. Does that change the picture? Only at the edges. With webmail, the user agent is a web page. Mira's browser talks to a web server using HTTP, not SMTP and not IMAP. That web server, which sits close to the mail server or inside the same system, is the one that reads and writes the mailbox.
So when Mira presses Send in a webmail page, her browser uses HTTP to hand the message to the web server. The web server then arranges for the message to reach the mail system, which relays it to the recipient's server using SMTP as usual. When Teo reads the message, his browser fetches it from his webmail server over HTTP; the server reads it from his mailbox on his behalf.
The takeaway is a clean division of work. Between mail servers, SMTP is always the language of delivery. Between the user and "their" mail system, the protocol may be IMAP, POP, or HTTP, depending on the kind of user agent.
Names and numbers: why DNS exists
People prefer names such as mail.skylark.example. Routers and transport software need numeric IP addresses. Something has to translate, and that something is the Domain Name System (DNS).
DNS is two things at once. It is a distributed database, spread across a great many servers worldwide, and it is an application-layer protocol that lets hosts query that database. The protocol commonly runs over a simple, quick transport service, using port 53, because a lookup is a small question and a small answer.
Besides translating names to addresses, DNS offers a few related services. It can say which mail server handles a domain (MX records). It can map an alias name to a "canonical" real name. It can spread load by returning several addresses for one popular name. We will focus on the central job: name to IP address.
Every host that wants to use a name starts with the same question to its local resolver: "what is the address of this name?" The local resolver might be a machine run by the access provider, such as BayLink Access, or by an organisation. It is the helper that does the legwork on Mira's behalf.
The hierarchy: root, TLD, authoritative
Why not put every name on one big server? We will see the reasons shortly. DNS instead arranges its servers in a hierarchy, and a lookup walks down it, following referrals.
At the top are the root servers. They do not know the address of api.skylark.example, but they know who is responsible for each top-level domain (TLD), such as .example, .org, or a country code. Next come the TLD servers. A TLD server does not know the final address either, but it knows which servers are responsible for each domain registered under that TLD, such as skylark.example. At the bottom are the authoritative servers: the servers run by an organisation (or its provider) that hold the real, definitive records for that organisation's names. Skylark's authoritative server knows that api.skylark.example has a particular IP address.
Notice the pattern: each level holds pointers to the next level down, and only the last level holds the final answer.
The local resolver is not part of this tree; it is a helper outside it. It receives Mira's question and then asks the hierarchy.
How one lookup unfolds
Here is the sequence, with Mira's laptop asking for api.skylark.example when nothing is cached yet.
- Her laptop asks the local resolver: "address of
api.skylark.example?" This is a recursive query: the laptop wants a final answer and expects the resolver to do the work. - The resolver asks a root server. The root replies, "I do not know, but here is a TLD server for
.example." That is a referral. - The resolver asks that TLD server. It replies, "I do not know either, but here is the authoritative server for
skylark.example." - The resolver asks the authoritative server. It replies with the address.
- The resolver gives the address to the laptop and keeps a copy.
Steps 2 to 4 are iterative queries: each server returns the next place to look instead of fetching the answer itself. The resolver is the one doing the iterating. In total the lookup costs several round trips, which is why the next idea is so valuable.
Common confusion: the root server does not return the final IP address for ordinary host names. It returns a referral. Likewise the local resolver is not a root, TLD, or authoritative server: it is the client-side helper.
Caching and time-to-live
To avoid repeating this walk for every question, DNS servers use caching. When a resolver learns an answer, it stores it for a limited time. Every record carries a time-to-live (TTL), a number of seconds after which the stored copy must be discarded. Cached information is used for the next questions at once, with no trip into the hierarchy.
Caching works at several levels. The resolver might already know the address of the .example TLD server, so it can skip the root. It might already know the authoritative server for skylark.example, so it can skip the TLD. Or it might remember the whole answer from a recent lookup. Each of these shortcuts saves round trips, and browsers and operating systems often keep their own small caches too.
The TTL is a compromise. A long TTL means fewer lookups and faster responses, but if the owner changes the address, some users keep using the old one until their copy expires. A short TTL keeps answers fresh but increases load. This is a common reason why a change to a site's address "takes a while to spread."
Why not one central server?
It is a fair question: a single server would be simpler to describe. There are at least four strong reasons against it.
One point of failure. If that one machine, or the link to it, went down, nobody could look up any name, and so most of the Internet would appear to be broken.
Traffic volume. The total number of lookups across the world is enormous. One machine would need to handle all of them, and the arithmetic in the worked examples shows how quickly the numbers become silly.
Distance. A single server sits in one place. Users on the other side of the planet would wait for long round trips for every lookup. Distributed servers can be placed near users.
Maintenance and ownership. Every organisation wants to manage its own names. With a hierarchy, Skylark updates its own authoritative server whenever it likes. A central database would force everyone to queue behind a single administrator.
The distributed, hierarchical design handles all four. It is not free: it has more moving parts, and a lookup can take several steps. But caching removes most of that cost in everyday use.
What to carry forward
Mail travels from a user agent to a mail server, then between mail servers by SMTP pushing, and is pulled to the reader by IMAP or POP, or fetched through a web server in the case of webmail. DNS maps names to addresses through a hierarchy of root, TLD, and authoritative servers, with a local resolver doing the legwork and caches shortening almost every lookup. The next session moves down a layer, to the transport services that carry these application messages between processes.
Examples
Worked examples — Mira's mail and Mira's lookups
These five short stories follow Mira through a message she sends to Teo, a contact at a partner called Harbor Nine, and through the DNS lookups her laptop makes along the way. All names, addresses, and timings are invented for practice. Where a problem asks for a lookup time, we add up round-trip times and treat everything else as negligible. Read each Interpretation carefully, because that is where the idea is made explicit.
Example A — Reading an SMTP conversation
Scene. Mira's mail server, mail.skylark.example, has a message in its queue for teo@harbornine.example. Mira's server has already looked up the MX record for harbornine.example and learned that the mail host is mx.harbornine.example. It now opens a connection to that host. Here is the conversation. Lines starting with S: come from the receiving server and lines starting with C: from Mira's server, acting as the client.
S: 220 mx.harbornine.example ready
C: HELO mail.skylark.example
S: 250 hello, mail.skylark.example
C: MAIL FROM:<mira@skylark.example>
S: 250 sender ok
C: RCPT TO:<teo@harbornine.example>
S: 250 recipient ok
C: DATA
S: 354 go ahead, finish with a lone dot
C: From: Mira <mira@skylark.example>
C: To: Teo <teo@harbornine.example>
C: Subject: Tide chart draft
C:
C: Teo, the draft chart is attached to the shared folder.
C: .
S: 250 message queued
C: QUIT
S: 221 goodbye
Calculation (reading it step by step).
220is the greeting. The receiving server announces itself.HELOis Mira's server introducing itself by name. Reply250means "fine."MAIL FROMnames the sender,RCPT TOnames the recipient. Both are answered250.DATAasks permission to send the content. Reply354means "go ahead."- The lines after
DATAare the message itself: headers, a blank line, and a body. The lone.on a line by itself marks the end. 250 message queuedmeans the receiving server accepted responsibility for the message.QUITand221close the conversation.
Count the replies: six 2xx replies (the greeting included) and one 3xx that invites more input. Not one reply says "failed," so the delivery succeeded.
Interpretation. Notice who started the connection: the server that had the message, Mira's. It pushed the message to Teo's server. Notice also what is missing: nothing in this dialogue involves Teo himself. His laptop might be off. The message simply rests in his mailbox until he chooses to pull it.
Takeaway. SMTP is push. The sender's server initiates; the receiver's server accepts and stores.
Example B — Tracing two journeys, labelling every protocol
Scene. Mira wants a clear picture of which protocol is used on every leg, in two situations: (1) she uses a desktop mail application, and (2) she uses a webmail page. In both situations Teo reads the message later in a desktop application.
Calculation (situation 1, desktop application).
- Mira's application to
mail.skylark.example: SMTP (the application holds the message and pushes it to her server). mail.skylark.exampletomx.harbornine.example: SMTP (server pushes to server).- Teo's application from
mx.harbornine.example: IMAP (he pulls it; the message stays on the server so his phone sees it too).
Calculation (situation 2, Mira uses webmail).
- Mira's browser to the webmail server: HTTP (the page is the user agent).
- The webmail server hands the message to the mail system, which sends it from
mail.skylark.exampletomx.harbornine.example: SMTP. - Teo's application from
mx.harbornine.example: IMAP, as before.
Count the legs: both situations use exactly three. Count the SMTP legs: one in the desktop case from the application, one between servers: two. In the webmail case, only the server-to-server leg is SMTP.
Interpretation. Look at the middle leg: it is the same in both situations. That is the stable core of email: mail servers always exchange mail by SMTP. What changes is the leg between a person and their own mail system. Also look at the direction of the last leg. Teo's application starts the connection, which is why that leg is a pull. If it were SMTP, Teo's server would need to push into a laptop that may be offline, which is exactly what the pull design avoids.
Takeaway. SMTP between servers; IMAP or POP (or HTTP for webmail) at the user end.
Example C — One lookup, three different histories
Scene. Mira's laptop needs the address of api.skylark.example. The round-trip times are: laptop to the local resolver run by BayLink Access, 6 ms; resolver to a root server, 30 ms; resolver to the .example TLD server, 22 ms; resolver to Skylark's authoritative server, 18 ms. Treat everything else as negligible. How long does the lookup take in each of three situations?
Calculation, situation 1: nothing is cached.
- Laptop asks resolver: 6 ms.
- Resolver asks root: 30 ms. Root returns a referral to the TLD server.
- Resolver asks TLD: 22 ms. TLD returns a referral to the authoritative server.
- Resolver asks authoritative: 18 ms. It returns the address.
- Total: 6 + 30 + 22 + 18 = 76 ms.
Calculation, situation 2: the resolver already remembers the authoritative server for skylark.example.
- Laptop asks resolver: 6 ms.
- Resolver asks authoritative directly, skipping root and TLD: 18 ms.
- Total: 6 + 18 = 24 ms.
Calculation, situation 3: the resolver has the final answer cached.
- Laptop asks resolver: 6 ms. The resolver answers from memory.
- Total: 6 ms.
Interpretation. Compare the three numbers: 76, 24, and 6. The unlucky first lookup is more than twelve times slower than the lucky cached one (76 / 6 ≈ 12.7). Nobody sees this difference on every lookup, because most questions hit a cache. Notice also that the types of cached knowledge differ: remembering where an authoritative server is shortens the walk, and remembering the final answer eliminates it.
Takeaway. Add one RTT per server actually contacted. Subtract the steps that caches let you skip.
Example D — A TTL, and the cost of changing an address
Scene. The record for api.skylark.example has a TTL of 300 seconds. Mira's resolver fetched it at time (t = 0) s. At (t = 120) s a colleague, Priya, asks the same resolver for the name. At (t = 340) s a third person asks. Meanwhile, at (t = 200) s, Skylark's engineers move the API to a new address and update their authoritative server. Assume the resolver still remembers the authoritative server's location, using the timings from Example C.
Calculation.
- Priya's lookup at 120 s: the stored copy was fetched 120 s ago and has 300 − 120 = 180 s of life left, so it is used. Delay: only her laptop to the resolver, say 6 ms. The answer is the old address.
- The address changes at 200 s. The resolver still holds the old record, which remains valid until 300 s.
- Anyone asking between 200 s and 300 s gets the old address from the cache, even though it is already out of date. That window lasts 300 − 200 = 100 s.
- At 340 s the stored copy has expired (340 > 300). The resolver asks the authoritative server again: 6 + 18 = 24 ms, and receives the new address.
Interpretation. A cached answer is a bargain with a time limit. The longest a resolver can serve a stale answer is the whole TTL: if the change had happened at 1 s, the stale window would have lasted nearly 299 s. That is why engineers who plan a move often lower the TTL to something short in advance, wait for old copies to expire, and then make the change. The trade-off is that a short TTL means more lookups and more load on the authoritative server.
Takeaway. TTL bounds how long a cache may serve an answer. Stale-window length is at most one TTL.
Example E — Could one big server do the job?
Scene. Mira's teammate Kofi proposes, half in jest, that DNS should be replaced by one huge server holding every name. To test the idea, Kofi invents some figures: the world sends 25 billion name queries per day, one machine can answer 50,000 queries per second, and the average user is 140 ms of round-trip time from a single central site, compared with 6 ms from a nearby resolver.
Calculation.
- Seconds in a day: 24 × 60 × 60 = 86,400.
- Queries per second: 25,000,000,000 / 86,400 ≈ 289,000 per second on average. Busy hours are higher.
- Machines needed at one site: 289,000 / 50,000 ≈ 5.8, so at least 6 machines, running flat out, just to cope with the average load.
- Now suppose resolvers cache well and only 5 % of queries need to go upstream. Upstream load: 0.05 × 289,000 ≈ 14,500 per second, which is under a third of one machine's capacity.
- Delay per uncached lookup: 140 ms to the central site, versus roughly 6 ms to a nearby resolver when the answer is cached.
Interpretation. The numbers tell two stories. First, a single site must be sized for the whole world's demand, and it is a single point of failure: if it goes down, no name works anywhere. Second, caching and a hierarchy reduce what any single server must handle by a large factor, and place most answers a few milliseconds from the user. The comparison 140 ms versus 6 ms shows the distance penalty of centralisation. And there is a non-numerical reason as well: each organisation can manage its own authoritative data without asking a central administrator for every change.
Takeaway. Centralising would create a single failure point, a traffic bottleneck, long delays for distant users, and a maintenance queue. Distribution plus caching removes all four problems.
Practice check — electronic mail and DNS
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.Mira's mail server has a message for `teo@harbornine.example`. Which statement about the transfer between the two mail servers is correct?
2.Why is SMTP not used for Teo's laptop to read his mailbox?
3.Mira reads her mail in a browser (webmail). Which protocol does her browser use to talk to the webmail server?
4.In a DNS lookup for `api.skylark.example` with nothing cached, which order of servers does the local resolver typically ask?
5.A resolver has no cache. The RTTs are: laptop to resolver 5 ms, resolver to root 40 ms, resolver to TLD 25 ms, resolver to authoritative 15 ms. How long does the lookup take?
6.Which statement best describes the role of the local resolver?
7.A record has a TTL of 600 s and a resolver cached it at t = 0 s. The owner changes the address at t = 150 s. What is the longest time after the change that this resolver could still hand out the old address?
8.Which is a genuine reason DNS is not implemented as one central server?