Client and Server: Who Asks and Who Answers Online
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.
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.
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.
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.