Course contents · Lesson 4 of 22

Client and Server: Who Asks and Who Answers Online

How the Internet Works · Lesson 4 of 22 · 8 min Foundations

You fill a shopping basket on your laptop, and next morning the same basket appears in the shop's app on your phone. Neither device kept it: each asked a computer far away, and that computer answered. This lesson explains client and server, the two roles behind every such exchange: who asks, who answers, how one machine plays both, and what happens when too many ask at once.

Client and server are roles, not machines

A client is the side that starts a conversation over a network: it sends a question, called a request, and waits. A server is the side that waits to be asked: it listens on a port, as you saw in lesson 2, and sends back an answer, called a response.

"Server" names a job, not a kind of computer. Strictly, the server is a program, such as a web server program, though people also call the machine it runs on a server. Dedicated server machines stay on day and night, but any computer can run one.

So the test is simple: who spoke first? That side is the client, whatever its hardware.

One machine can be both

Because roles belong to conversations, one machine can hold both. A developer often tests a website by running a web server program on their laptop and opening it in that laptop's browser at 127.0.0.1, the "this same machine" address from lesson 2. The laptop is client and server at once.

Servers are often clients too. To show your order history, the shop's web server asks a database server, a program that stores data and finds it on request; it listens on its own port, such as 5432 for the PostgreSQL database. In that conversation, the web server is the client.

In the diagram, the middle machine is the server in conversation A and the client in conversation B.

Three machines in a row: a browser on your laptop, a web server on port 443 and a database server on port 5432. In conversation A the browser is labelled client and the web server server; message 1, my orders?, goes from the browser to the web server, and message 4, the page, comes back. In conversation B the web server is labelled client and the database server; message 2, Asha's orders?, goes from the web server to the database, and message 3, her orders, comes back. A note says who starts the conversation decides the role
Same machine, two roles: it answers your browser, then asks the database.

You met this in lesson 3: the recursive resolver is a server to your device and a client of the root, TLD (top-level domain) and authoritative servers it asks.

Request and response: the rhythm of the internet

Almost everything you do online is a client sending a request and a server sending a response. Opening a page, refreshing your email, even the DNS (Domain Name System) lookup before each: ask, then answer.

The flowchart shows one round for a web page. The server is listening before anyone arrives. The client opens a connection to the server's IP (Internet Protocol) address and port, sends its request, and waits while the server works, perhaps finding a file or asking a database. Then the response comes back.

Flowchart with the client, your browser, on the left and the server, a web server program, on the right. Step 1: the server listens on port 443 and waits for any client. Step 2: the client opens a connection to 203.0.113.7:443. Step 3: the client sends a request, please send /index.html. Step 4: the server processes the request and finds or builds the page. Step 5: the server sends a response, here is the page. Step 6: more to ask? If yes, the client reuses the open connection and returns to step 3; if no, the connection closes, and either side can end it. Grey arrows are requests, cyan arrows responses
The server waits; the client speaks first, and each request gets one response.

Each request gets one response, even if it is bad news such as "not found"; only a lost connection or a timeout leaves it unanswered. The codes that carry that news come in lesson 6.

After the response, the connection can close or stay open. Setting one up costs a few messages back and forth (lesson 5 shows them), so browsers usually keep it open a while for further requests.

And a chat message that arrives unasked? Your phone opened a connection to a server earlier and kept it open, so the server can send down it. The client still spoke first.

One server, many clients

Unlike our lone shopkeeper, a server program can work on many requests at the same time. As lesson 2 showed, four numbers name each connection: the client's IP address and port, and the server's. Every client shares the server's half, but each connection has its own client half, so conversations never mix.

But the machine's processing power and memory are limited, so the program can only work on so many requests at once. The rest wait in a queue, a waiting line, until it is free.

A short queue goes unnoticed. As it grows, every client waits longer and pages feel slow; some give up and show a timeout. When the queue is full, new arrivals are turned away, or the server replies with an error such as 503 Service Unavailable, meaning "too busy, try later".

The fixes mirror a busy shop: a bigger counter, or more counters. Scaling up means a more powerful machine. Scaling out means more servers running the same program, with a load balancer in front spreading clients between them (lesson 14). Scaling Up vs Scaling Out compares the two.

Browsers, apps and scripts are all clients

Your browser is the client you know best. For one page it sends many requests: the page itself, then its images, styles and other files, often from several servers.

A mobile app, such as a banking app, is a client too. It usually asks its company's server for data, such as your balance, and draws its own screens, so the server sends data rather than a whole web page.

A client does not need a person. A script, a short program that runs by itself, might fetch the weather every hour; a shop's server might ask a payment company to charge a card. Both use an API (Application Programming Interface): the set of requests a server accepts from programs, answered in a form programs can read.

In the diagram, three very different clients use one door, port 443, and the server treats each alike: request in, response back.

Three clients on the left: a browser asking for web pages from 198.51.100.23 port 52114, a phone app asking for your data from 192.0.2.44 port 49733, and a script, an API caller with no person present, from 203.0.113.50 port 55870. Each has a two-way line, request in and response back, to one door, port 443, on a single web server at 203.0.113.7, where one program serves all three at the same time. A note says each client's own address and port keep the replies apart, and all addresses and ports are illustrative
To the server, browsers, apps and scripts are all just clients on port 443.

Check yourself

Your laptop runs a small web server for a project while your browser reads a news site. Is the laptop a client or a server?

Both: a server to whoever connects to its web server, and a client while the browser asks the news site for pages. Each role depends on who starts that conversation.

A shop's web server fetches your order history from a database server. In that exchange, which machine is the client?

The web server, because it starts that conversation; the database listens and answers. It is still the server to your browser.

Tickets go on sale, pages take 30 seconds to load, and some show timeouts. What is happening?

More requests arrive than the server can work on, so they queue and some clients give up. Scaling up or out lets the site handle more at once, shortening the queue.