Sub topic 1
Clients and servers: the big picture
Every page you open in a browser is the result of a short conversation between two programs. One asks for something and the other answers. This chapter gives you the working model of that conversation. The rest of the tutorial zooms into each step.
Prerequisites
This chapter assumes you have:
- Used a web browser and know what a web page is.
- Seen HTML, even if you cannot write it fluently.
- Run commands in a terminal, such as
curlorls.
You do not need to know networking or any server-side language yet.
The client-server model
The web uses the client-server model. A client is a program that starts a conversation by sending a request. A server is a program that waits for requests and sends back a response.
- The client is usually a browser, but it can also be
curl, a mobile app, or a script. - The server is a program running on a computer that is reachable over a network. It could be a web server such as nginx, or an application you wrote.
- The request and the response are text-based messages in a protocol called HTTP (HyperText Transfer Protocol). A protocol is an agreed format and set of rules that both sides follow.
Two properties of this model shape almost everything later in the tutorial:
- The client speaks first. A server does not normally send anything unless a client asked for it.
- Each request is independent. HTTP is stateless, which means the server does not automatically remember earlier requests from the same client. Features such as login sessions are built on top, using cookies (covered in a later chapter).
Roles, not machines
"Client" and "server" describe a role in one exchange. They do not describe a kind of computer.
- One computer can run a browser and a local web server at the same time. Running
localhost:3000on your laptop does exactly that. - A server can be a client of another server. When a web application calls a payment API, your application is the client for that call, while it is still the server for your browser.
- One server program can serve many clients at once. It handles requests from thousands of browsers concurrently.
- One name can map to many machines. Large sites put many servers behind a single hostname, and a request may be answered by any of them.
Because the role is per exchange, you should always ask "who is the client in this request?" before you debug anything.
From typing a URL to seeing a page
When you type https://example.com/index.html and press Enter, the browser performs a chain of steps. This chapter only names them. Each gets its own chapter.
| Step | What happens | Covered in |
|---|---|---|
| 1. Parse the URL | The browser splits the address into scheme, host, port, path, query, and fragment. | Anatomy of a URL |
| 2. Resolve the name | DNS turns `example.com` into an IP address. | Finding the server: DNS |
| 3. Connect | The browser opens a TCP connection to that address. | Reaching the server: IP and TCP |
| 4. Secure the connection | For `https`, TLS encrypts the connection and verifies the server's identity. | HTTPS and TLS |
| 5. Send a request | The browser writes an HTTP request. | HTTP requests and responses |
| 6. Handle the request | The server picks a handler, builds a response, and sends it back. | What a server does with a request |
| 7. Render | The browser parses the HTML, fetches more resources, and draws the page. | From HTML response to rendered page |
The following diagram shows the order of the network steps.
The IP address in the diagram is only an illustration. Real addresses change over time and differ by location.
Step 7 usually leads back to step 5. The HTML refers to stylesheets, scripts, and images, and each one is fetched with its own request. A single page view commonly produces dozens of requests, sometimes to several different servers.
Watching one exchange
You can see a real exchange from the terminal. The -v flag makes curl print the details of the conversation.
curl -v https://example.com/The output has three kinds of lines:
- Lines starting with
*arecurlreporting its own progress. They show the name lookup, the connection, and the TLS handshake. These are steps 2 to 4 in the table. - Lines starting with
>are what the client sent. You will see a request line such asGET / HTTP/2orGET / HTTP/1.1, followed by headers such asHostandAccept. - Lines starting with
<are what the server sent back. You will see a status line such asHTTP/2 200, then headers, and after a blank line the body.
The exact protocol version shown depends on what the client and server negotiate. A later chapter explains the differences between HTTP/1.1, HTTP/2, and HTTP/3.
The browser does the same work as curl. The difference is what it does afterwards: it renders the body instead of printing it.
What the model does not say
A few cases break the simple "client asks, server answers once" picture. Knowing them early prevents confusion later.
- Server-initiated updates. Chat apps and live dashboards need the server to push data. Techniques such as WebSockets and server-sent events still begin with a client request. After that, the connection stays open and the server can send more data.
- Polling. A simpler alternative is for the client to ask again every few seconds. It works with plain HTTP, at the cost of extra requests.
- Intermediaries. A request often passes through proxies, load balancers, or a CDN (content delivery network) before reaching the program that produces the answer. From the client's side, the thing it talks to is still "the server".
- Caching. Sometimes no request reaches the server at all, because the browser already holds a fresh copy. A page can appear without any network traffic.
- Failure at any step. Each step can fail separately. A name that does not resolve, a refused connection, an expired certificate, and a
500error are different problems with different fixes.
The last point is the main reason to learn the whole chain. Debugging the web is mostly a matter of finding which step failed.
Why the layering matters
The steps above are layered. DNS, TCP, TLS, and HTTP each solve one problem and rely on the layer below:
- DNS answers "where is this name?"
- TCP answers "how do I send bytes reliably to that address?"
- TLS answers "how do I keep those bytes private and know who I am talking to?"
- HTTP answers "what do the bytes mean as a request and a response?"
Because of this layering, you can change one layer without rewriting the others. HTTP/3, for example, changes the transport underneath HTTP, but a request still has a method, a path, and headers. It also means a symptom in one layer can come from another. A "site can't be reached" message may be a DNS problem and have nothing to do with HTTP.
Where to go next
The next chapter takes the first step in detail: how a URL is built and how a browser reads each part. Keep the table above nearby. Each later chapter fills in one row.
Exercise
You run curl -v https://example.com/ and the output ends after the lines starting with * that show a failed TLS handshake. No lines starting with > or < appear. Which steps from the table completed, and which never started? Is this an HTTP problem?
Show answer
Name resolution (step 2) and the TCP connection (step 3) completed, because curl got far enough to start the TLS handshake. The TLS step (step 4) failed, so the HTTP request (step 5) was never sent and no response (step 6) came back. Because no > lines appear, no HTTP message was exchanged. The failure is in the TLS layer, for example an expired or untrusted certificate, and not an HTTP problem.
Exercise
Your web application receives a request from a browser, and to answer it, the application calls an external weather API. Name the client and the server in each of the two exchanges.
Show answer
In the first exchange, the browser is the client and your application is the server. In the second exchange, your application is the client and the weather API is the server. The same program holds both roles, one for each exchange.