MA-NET1 · Module 5
Application principles and HTTP
Topics: Client-server · Sockets · HTTP · Cookies and caching
Overview
Session 5 — Application principles and HTTP
Meet Mira, a developer who has just joined the team behind Skylark Forecast, a small weather product. Her laptop opens the forecast page every morning, and today she wants to know what actually happens between pressing Enter and seeing the temperature. The page is served from api.skylark.example, and images are delivered through a content network called Northline Relay. Her home connection is provided by BayLink Access.
By the end you should be able to: contrast client–server and peer-to-peer designs; explain what a process and a socket are; list the services an application may ask a transport layer for; read an HTTP request and response; compare persistent and non-persistent connections; count objects on a page; explain why cookies exist; describe the intuition behind web caching; and compute a page-fetch time from DNS and connection round-trip times.
Scroll Core ideas → worked examples → Quiz. You do not need a network lab: a pencil, a calculator, and a willingness to draw timelines will do. Plan on roughly 60 minutes, a little more if you try the optional curl -v experiment mentioned in the concept notes.
Concepts
Core ideas — applications, sockets, and HTTP
Every time you open a weather page, a lot of invisible machinery does its work in a fraction of a second. This lesson starts at the very top of the stack, the application layer, which is the part of a network that people actually meet. We will follow a learner called Mira as she loads the Skylark Forecast page. Along the way you will learn who talks to whom, how programs address each other, what an application may ask the network for, and how the web's protocol, HTTP, turns a click into pictures and numbers. Every name, number, and message in this lesson is invented for practice.
Two ways to arrange who talks to whom
Before any protocol details, ask a simpler question: who is in charge? There are two common answers.
In a client–server design, one side, the server, is always on, has a stable, well-known address, and waits for requests. The other side, the client, starts the conversation whenever it needs something and may disappear again afterwards. Mira's laptop is a client; api.skylark.example is a server. Clients usually do not talk to each other directly. They talk to the server, and the server holds the data that everyone shares.
In a peer-to-peer (P2P) design there is no single machine in charge. Each participant, called a peer, may request things and also serve things to others. Imagine fifty Skylark users on one street sharing the same large radar map file: instead of every laptop pulling it from the company's machine, they could fetch pieces from each other. Peers come and go, and their addresses may change, so P2P systems need extra rules for finding one another.
The trade-off is worth stating plainly. Client–server is simple to reason about, easy to secure and update, but the server becomes a bottleneck and a single point of failure. P2P scales its capacity as more peers join, which is attractive for popular large files, but it is harder to manage, and you must trust strangers' machines a little. Many real systems blend the two.
Common confusion: "client" and "server" describe the role in one conversation, not the type of hardware. A powerful machine can be a client when it asks another machine for something, and a laptop can run a small server for a few minutes.
Processes, not computers, do the talking
When we say "two computers communicate," we are being lazy. What actually communicates are processes: running programs. Mira's browser is one process on her laptop; the program that answers requests on api.skylark.example is another process on a different machine. If two processes live on the same computer, the operating system helps them swap messages. If they live on different computers, they exchange messages across the network.
A process needs a doorway through which it hands messages to the network and receives messages back. That doorway is the socket. You can picture a socket as a mail slot in a front door: the process on the inside writes a letter and pushes it through; the network, on the outside, carries it away. The process does not control what happens beyond the door, other than choosing a few settings such as the kind of transport service behind it.
To deliver a message to the right process, the network needs two pieces of address information. First, the IP address of the host, which says which machine. Second, a port number, which says which process on that machine. A web server conventionally listens on port 80 for plain HTTP; a mail server's submission service uses other numbers. Together, an IP address and a port form the destination that a socket sends to.
Common confusion: a socket is not the network cable, and it is not the program itself. It is the interface between the application process and the transport service underneath.
What an application can ask the transport layer for
Applications differ widely in what they need, and transport services are a menu from which an application picks. Four needs come up again and again.
Reliable data transfer. Some applications cannot tolerate lost bytes. A configuration file that arrives with a hole in the middle is useless, and so is an email missing its last paragraph. These applications want the transport service to guarantee that everything sent arrives, in order. Other applications, such as a live voice call, may happily skip a few lost fragments because a late fragment is worth nothing anyway. These are called loss-tolerant.
Throughput. Some applications need a certain minimum sending rate to work at all, for instance a video stream that falls apart below a given bit rate. Others, called elastic, simply use whatever rate is available. Downloading a forecast file is elastic: faster is nicer, slower still works.
Timing. Interactive applications, such as games or calls, want guarantees that data arrives within a bound, say a few tens of milliseconds. Web browsing cares about timing for comfort, but nothing breaks if a page takes slightly longer.
Security. Applications may want data hidden from eavesdroppers or protected against tampering. Today this is usually provided by extra protocol layers running on top of the basic transport service, not by the basic service alone.
Notice that what an application asks for is separate from what it gets. The basic transport services on the public Internet, which later sessions will cover, offer a reliable one and an unreliable one. They do not guarantee a minimum throughput or a maximum delay. Applications that need those things have to cope on their own, for example by adapting video quality.
Common confusion: an application's needs and the network's guarantees are not the same list. "Wants low delay" does not mean "is promised low delay."
HTTP: a polite request, a polite reply
The HyperText Transfer Protocol (HTTP) defines how a client asks a server for web objects and how the server answers. An object is simply a file that can be addressed: a text page, an image, a style sheet, a script, a data file. A typical web page is a base HTML file plus several referenced objects. Each object is named by a URL, which combines the server's host name and a path, such as api.skylark.example and /v1/forecast/harbor-9.json.
HTTP is a request/response protocol: the client sends one request message, the server sends back one response message. It runs over a reliable transport connection, so the messages arrive complete and in order. A request has three parts: a request line (method, path, protocol version), some header lines, and an optional body. A response has a status line (version, numeric code, short phrase), header lines, and then the body.
A tiny, invented request looks like this:
GET /v1/forecast/harbor-9.json HTTP/1.1
Host: api.skylark.example
Accept: application/json
The first line asks for the object at that path using the GET method, which means "send me this." Other methods exist, for example POST (send data to the server) and HEAD (just the headers, please). The Host header says which site on that machine the request is meant for, which matters because one server can host many sites. The blank line at the end tells the server the headers are finished.
A matching response might be:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 37
{"city":"Harbor Nine","hi":18,"lo":9}
The status code 200 means "found it, here it is." A 404 means the object does not exist, 301 means it has moved permanently, 304 means "your saved copy is still good," and 500 means the server itself had trouble. The Content-Length header tells the client how many body bytes to expect.
An important property of HTTP is that it is stateless: the server does not remember earlier requests from the same client. Each request is judged on its own. That keeps servers simple, but it creates a problem we will solve shortly with cookies.
Connections: non-persistent versus persistent
HTTP runs on top of a reliable transport connection, and opening that connection costs time. Before the first request can even be sent, the two ends must perform a short handshake, which takes one round-trip time (RTT). An RTT is the time for a tiny message to travel from client to server and back. After the handshake, sending a request and receiving the start of the response costs another RTT. So, ignoring the time to push bytes through the link, fetching one object over a fresh connection costs about 2 RTTs.
With non-persistent HTTP, each object gets its own connection, opened for that request and closed afterwards. A page of seven objects therefore needs seven connections, and if they are opened one after another, about (7 \times 2 = 14) RTTs. Browsers often reduce the damage by opening several non-persistent connections in parallel, but each connection still pays its own handshake, and the server must manage many short-lived connections.
With persistent HTTP, the server leaves the connection open after replying, and later requests reuse it. The handshake cost is paid once. Fetching seven objects one after another on one persistent connection costs roughly (1 + 7 = 8) RTTs: one for the handshake and one per object. There is also a further refinement, called pipelining, where the client sends several requests without waiting for each reply. That cuts the per-object cost even further, though in practice other techniques are often used to get the same benefit.
There is one more ordering detail that makes timeline problems tricky. The browser cannot request the images before it has read the base HTML file, because the base file is what names the other objects. So the base file always comes first, and only then can the rest be requested, serially or in parallel.
Common confusion: "persistent" does not mean "forever." The server closes a persistent connection after it has been idle for a timeout. And "parallel" does not mean "free": each parallel non-persistent connection still pays its own handshake RTT.
Cookies: giving a stateless protocol a memory
Because HTTP is stateless, the server cannot tell on its own that two requests came from the same visitor. Yet a forecast product wants to remember Mira's saved city, her choice of Celsius, or whether she is signed in. Cookies are the common answer.
A cookie is a small piece of text with four moving parts. First, the server includes a Set-Cookie header in a response, handing the browser a token such as sid=7f3a. Second, the browser stores that token in a file on Mira's laptop, tagged with the site it came from. Third, on later requests to the same site, the browser automatically adds a Cookie header carrying the token. Fourth, the server keeps a table, keyed by the token, that holds what it knows about Mira. The token is a key; the real data usually stays on the server.
Cookies are useful for sign-in sessions, shopping carts, preferences, and recommendations. They also raise a privacy concern, since a token can let a site, or an advertiser whose content is embedded in many sites, link your visits together. For that reason browsers let you inspect and delete cookies.
Common confusion: cookies do not make HTTP itself stateful. The protocol is still stateless; the application adds memory on top by agreeing to carry a token in headers.
Web caching: keep a copy closer to the reader
Fetching every object from the original server each time is wasteful, especially when thousands of readers want the same radar image. A web cache, also called a proxy server, stores copies of recently requested objects. The browser sends its request to the cache. If the cache holds the object (a hit), it answers immediately. If not (a miss), the cache fetches the object from the origin server, stores a copy, and then passes it along. The cache therefore acts as a server to the browser and as a client to the origin.
A cache helps in two ways. It shortens the response time because the cache is closer than the origin, and it reduces traffic on the expensive link to the origin. Skylark's own delivery partner, Northline Relay, runs many such caches near readers, so Mira's laptop often talks to a nearby Northline server rather than to the distant origin.
A fair worry follows: what if the origin changes the object after it was cached? HTTP handles this with a conditional GET. The cache includes a header such as If-Modified-Since with the date of its stored copy. If the object has not changed, the origin replies 304 Not Modified with no body, which is tiny and quick. If it changed, the origin returns the new copy with 200.
The benefit of a cache can be estimated with a weighted average. If a fraction (h) of requests are hits at delay (d_\text{hit}), and the rest incur delay (d_\text{miss}), the average is (h \cdot d_\text{hit} + (1-h)\cdot d_\text{miss}). We will do this calculation with real numbers in the worked examples.
A recipe for page-fetch timing problems
Timing questions look intimidating, but they follow the same recipe each time. Write the steps in order and count round trips.
- Resolve the name. If the browser does not already know the server's IP address, DNS lookups cost some time. You are usually told the total, or the individual RTTs to add up.
- Handshake and base file. One RTT to open a connection, one RTT for the request and response. The base HTML file must arrive before anything else.
- Fetch the referenced objects. Count them. Decide: new connection each time, or reused? Serial or parallel?
- Add everything up. Unless the problem gives file sizes and link speeds, assume that transmission time is negligible compared to RTTs and say so.
Draw a vertical timeline with the client on the left and the server on the right whenever you are unsure; each arrow pair is one RTT.
What to carry forward
Applications are processes that reach each other through sockets, addressed by IP and port. They pick from a menu of transport services. HTTP is a stateless request/response protocol whose cost is largely made of round-trip times, which is why persistent connections, parallel connections, and caches matter so much. Cookies add memory without changing the protocol's nature. The next session turns to electronic mail and to DNS, the system that supplies the IP addresses we assumed in this lesson.
Examples
Worked examples — Mira loads the Skylark Forecast page
These five short stories follow Mira as she investigates why the Skylark Forecast page sometimes feels slow. All hosts, headers, and numbers are invented for practice. Whenever a problem asks for a fetch time, we treat the time to push bytes through the link as negligible and count only round-trip times (RTTs), and we say so out loud. Read the Interpretation paragraphs carefully, because that is where the lesson sits.
Example A — Reading a request and a response
Scene. Mira opens her browser's developer tools and copies the first request it sent when she visited the page for Harbor Nine. She wants to be able to say, line by line, what each part means.
GET /forecast/harbor-9 HTTP/1.1
Host: api.skylark.example
Accept: text/html
Cookie: sid=7f3a91
The server's reply, with the body shown after the blank line:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 95
<html><body><h1>Harbor Nine</h1><img src="/img/sun.png"><img src="/img/wind.png"></body></html>
Calculation (reading it).
- Request line: method GET, path
/forecast/harbor-9, versionHTTP/1.1. Mira is asking for an object. Host: api.skylark.examplenames the site she wants on that machine.Cookie: sid=7f3a91is a token the browser attached on its own, because the server handed it out earlier.- Status line: code 200, phrase OK. The object was found.
Content-Length: 95says the body is 95 bytes. Counting the characters of the HTML line gives exactly 95, so the browser knows where the response ends.- Reading the body, the browser finds two
<img>tags. Those are two more objects it must fetch:/img/sun.pngand/img/wind.png. This page therefore consists of 1 base file + 2 referenced objects = 3 objects.
Interpretation. Notice that the first response did not contain any pictures. It only named them. That is why a browser must receive the base file first and only then request the others: it cannot ask for what it has not yet heard about.
Takeaway. One request, one response. The number of objects on a page is the base file plus every object it references.
Example B — Non-persistent connections, one after another
Scene. Mira's colleague Teo tests a different page, the radar view, in an old browser that uses non-persistent HTTP and opens one connection at a time. The radar page is a base HTML file plus 6 images, so 7 objects in total. Measurements: the DNS lookup for api.skylark.example takes 87 ms in total, and the RTT between the laptop and the server is 40 ms. How long does the page take to load?
Calculation. Treat transmission time as negligible.
- DNS: 87 ms.
- Each object needs a new connection: 1 RTT for the handshake, 1 RTT for request and response. That is 2 × 40 = 80 ms per object.
- There are 7 objects, fetched in sequence: 7 × 80 = 560 ms.
- Total: 87 + 560 = 647 ms.
Interpretation. Of those 647 ms, 280 ms (7 handshakes × 40 ms) are spent merely opening connections, before a single useful byte of an object is requested. That is the hidden cost of non-persistent HTTP: the handshake is paid again and again.
Takeaway. Non-persistent, serial: total = DNS + (number of objects × 2 RTT).
Example C — Three ways to speed up the same page
Scene. Teo wants to know which change helps most for the same radar page: 7 objects, DNS 87 ms, RTT 40 ms. He compares three options.
Option 1: persistent connection, no pipelining.
- DNS: 87 ms.
- One handshake: 40 ms.
- Base file: 1 RTT = 40 ms.
- Six images, one request after another: 6 × 40 = 240 ms.
- Total: 87 + 40 + 40 + 240 = 407 ms.
Option 2: non-persistent, up to 3 parallel connections.
- DNS: 87 ms.
- Base file alone, new connection: 2 × 40 = 80 ms.
- Six images in two rounds of 3 parallel connections. Each round costs 2 RTT = 80 ms, so 2 × 80 = 160 ms.
- Total: 87 + 80 + 160 = 327 ms.
Option 3: persistent with pipelining.
- DNS: 87 ms.
- Handshake: 40 ms.
- Base file: 40 ms.
- All six image requests sent back-to-back without waiting, so they share about 1 RTT: 40 ms.
- Total: 87 + 40 + 40 + 40 = 207 ms.
Interpretation. The ordering is 647 ms (Example B), 407 ms, 327 ms, 207 ms. Notice that parallel non-persistent beat plain persistent here, because three transfers overlapped; but it also made the server manage more connections. Pipelining wins on time here, and because DNS takes 87 ms regardless, the DNS cost becomes a large share of the final 207 ms. Also notice that in every option the base file is fetched alone first. That ordering never changes.
Takeaway. Fewer handshakes and more overlap both reduce time. Always write the base file as its own step, then the dependent objects.
Example D — Following a cookie across two visits
Scene. Mira visits the Skylark Forecast site for the first time and chooses Celsius. The next morning she returns. She wants to see how the site remembers her without HTTP having any memory.
First visit, the server's response includes:
HTTP/1.1 200 OK
Set-Cookie: sid=c41d02
Content-Type: text/html
On the next visit, her browser's request to the same site contains:
GET /forecast/harbor-9 HTTP/1.1
Host: api.skylark.example
Cookie: sid=c41d02
Calculation (the four parts).
- Header in the response.
Set-Cookie: sid=c41d02hands out the token. - File on the laptop. The browser stores
sid=c41d02, tagged withapi.skylark.example. - Header in the next request. The browser sends
Cookie: sid=c41d02automatically. - Table at the server. The server looks up
c41d02in its own table and finds "unit = Celsius, city = Harbor Nine."
Interpretation. Observe where the preference lives: on the server, under the key c41d02. The cookie is only the claim ticket. If Mira deletes the cookie file, the ticket is gone and the server treats her as a new visitor, even though the table row may still exist. If she opens the site from a different browser, that browser has no ticket either.
Takeaway. Cookies give applications memory; HTTP itself stays stateless.
Example E — How much does an edge cache save?
Scene. Skylark's images are delivered via Northline Relay, a network of caches. The RTT between Mira's laptop and the nearby Northline server is 12 ms. The RTT between that server and the Skylark origin is 48 ms. Without a cache, her laptop would reach the origin directly, which, going through the same path, is about 12 + 48 = 60 ms away. The cache finds the requested image 60 % of the time. Treat each image request as costing one RTT to wherever it is answered from. What is the average delay per image?
Calculation.
- Hit (probability 0.6): answered by Northline at once, delay 12 ms.
- Miss (probability 0.4): the request goes to Northline (12 ms) and on to the origin (48 ms), then the answer returns, so about 60 ms.
- Average: 0.6 × 12 + 0.4 × 60 = 7.2 + 24 = 31.2 ms.
- Without a cache: 60 ms.
- Saving: 60 − 31.2 = 28.8 ms per image, a drop of 28.8 / 60 = 48 %.
Now suppose a cached copy of /img/sun.png is a day old. Northline checks freshness with:
GET /img/sun.png HTTP/1.1
Host: api.skylark.example
If-Modified-Since: Mon, 09 Nov 2026 06:00:00 GMT
and the origin replies:
HTTP/1.1 304 Not Modified
Interpretation. The cache did not need to re-download the image: the 304 reply has no body, so the check was cheap, and the cached copy is served again. If the hit rate rose to 80 %, the average would become 0.8 × 12 + 0.2 × 60 = 9.6 + 12 = 21.6 ms, so a better hit rate directly lowers the average delay. It also lowers the traffic going to the origin, which is the other reason caches exist.
Takeaway. Average delay = hit rate × hit delay + miss rate × miss delay. A conditional GET keeps cached copies honest without sending the whole object again.
Practice check — application principles and HTTP
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.Mira's laptop asks `api.skylark.example` for a forecast. Which description fits the roles in this single conversation?
2.What is a socket, in the sense used in this session?
3.A message must reach the web-server process on a host. Which pair of identifiers is needed to name it?
4.A page has a base HTML file plus 4 images. The RTT to the server is 50 ms, DNS takes 30 ms in total, and transmission time is negligible. Using non-persistent HTTP with one connection at a time, what is the total fetch time?
5.Same page and numbers as before (5 objects, RTT 50 ms, DNS 30 ms), but now one persistent connection is used and requests are sent one at a time without pipelining. What is the total?
6.Why does a site need cookies even though HTTP can carry any information in headers?
7.A Northline Relay cache answers 70 % of image requests in 10 ms. The other 30 % take 90 ms because the origin must be asked. What is the average delay?
8.A cache holds a stored copy of an image and wants to know whether it is still current. It sends a conditional GET and receives `304 Not Modified`. What happens next?