Hanpy's Blog

Type at least two characters.

Design · Software Design

Client-Server Model Explained: How Requests and Responses Work

In the client-server model, clients send requests and a server processes them and sends back responses. This post defines the model, runs a 30-line Node.js server with curl to show real requests, responses, shared state and a server outage, and compares client-server with peer-to-peer.

Hanpy13 min read
  • #Client-Server
  • #Software Architecture
  • #HTTP
  • #Node.js
  • #Peer-to-Peer

The client-server model splits a system into two roles. A client asks for something by sending a request, and a server waits for requests, does the work, and sends back a response. The client starts every conversation; the server never calls the client first. Your browser and a web server, a mail app and a mail server, and an app and its API all follow this model.

Think of ordering coffee. You (the client) place an order, and the barista (the server) makes the drink with the shop's beans and machine. You never touch the machine yourself. When many customers arrive at once, they wait in line, and if the barista steps away, nobody gets coffee. Those two everyday facts, waiting in line and depending on one barista, are also the two main weaknesses of the model, as the example below shows.

The roles in one picture

Client-server: many clients, one server that owns the data
  Client A (browser)            Client B (app)
       |      ^                     |      ^
   request  response            request  response
   GET /menu  200 + JSON        POST /orders  201 + JSON
       v      |                     v      |
  +----------------------------------------------+
  |                    Server                    |
  |    listens for requests, runs the rules      |
  +----------------------------------------------+
                      |      ^
                      v      |
              +--------------------+
              |      Database      |
              |   (shared state)   |
              +--------------------+
ClientServer
Starts the conversationYes, sends requestsNo, listens and answers
How manyMany, come and goFew, always on
HoldsThe user interface and its own temporary dataThe shared data and the rules for changing it
Typical examplesBrowser, mobile app, curl, database driverWeb server, API server, mail server, database server

The web itself is built on this model. HTTP is a client-server protocol in which the requests come from the recipient side, usually a web browser. HTTP is also stateless: each request stands on its own, and the server keeps no session data between two requests unless something like cookies adds it. Keep that in mind for the example: the server remembers orders because we store them, not because HTTP remembers anything.

Seeing it work: a 30-line Node.js server

Reading about requests and responses is one thing; watching them is quicker. The server below uses only Node.js's built-in http module, with no packages to install. It sells two drinks, accepts orders, and keeps the orders in memory.

server.mjs
// server.mjs — a tiny coffee-shop server (Node.js built-in http, no packages)
import http from 'node:http'
 
const menu = { americano: 4500, latte: 5000 }
const orders = [] // state lives only on the server
 
const server = http.createServer(async (req, res) => {
  console.log(`${req.method} ${req.url} from port ${req.socket.remotePort}`)
  const send = (status, body) => {
    res.writeHead(status, { 'Content-Type': 'application/json' })
    res.end(JSON.stringify(body))
  }
 
  if (req.method === 'GET' && req.url === '/menu') return send(200, menu)
 
  if (req.method === 'POST' && req.url === '/orders') {
    let raw = ''
    for await (const chunk of req) raw += chunk
    const { item } = JSON.parse(raw || '{}')
    if (!(item in menu)) return send(400, { error: `unknown item: ${item}` })
    const order = { id: orders.length + 1, item, price: menu[item] }
    orders.push(order)
    return send(201, order)
  }
 
  if (req.method === 'GET' && req.url === '/orders') return send(200, orders)
 
  send(404, { error: 'not found' })
})
 
server.listen(3000, () => console.log('listening on http://localhost:3000'))

Start it with node server.mjs, then play the client from another terminal with curl. All output below is real, from Node.js 22.23 and curl 8.5 on Ubuntu 24.04 (Linux 6.18, 2 CPUs).

1. A request and its response

curl -v prints the raw request (lines starting with >) and the raw response (lines starting with <).

Terminal — client
$ curl -v http://localhost:3000/menu
> GET /menu HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.5.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Sun, 22 Nov 2026 06:07:21 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
<
{"americano":4500,"latte":5000}

The request is a method (GET), a path (/menu) and a few headers. The response is a status line (200 OK), headers, and a body. That is the whole contract: the client never sees the menu object inside the server, only the response the server chooses to send.

2. Changing state on the server

Terminal — client
$ curl -s -X POST http://localhost:3000/orders -H 'Content-Type: application/json' \
    -d '{"item":"latte"}' -w '  (HTTP %{http_code})\n'
{"id":1,"item":"latte","price":5000}  (HTTP 201)
 
$ curl -s -X POST localhost:3000/orders -d '{"item":"mocha"}' -w '  (HTTP %{http_code})\n'
{"error":"unknown item: mocha"}  (HTTP 400)

The rules live on the server. The client asked for a mocha, and the server refused with 400 Bad Request because the menu has no mocha. A client can be changed, copied or replaced with a different program, but it cannot skip this check. This is why validation and permission checks belong on the server.

3. Two clients at the same time

Next, two clients send orders at once (& runs both curl commands in parallel), and then a third client reads the list.

Terminal — two clients, then a third
$ curl -s -X POST localhost:3000/orders -d '{"item":"americano"}' & \
  curl -s -X POST localhost:3000/orders -d '{"item":"latte"}' & wait
{"id":2,"item":"latte","price":5000}{"id":3,"item":"americano","price":4500}
 
$ curl -s localhost:3000/orders
[{"id":1,"item":"latte","price":5000},{"id":2,"item":"latte","price":5000},{"id":3,"item":"americano","price":4500}]
Server log
listening on http://localhost:3000
GET /menu from port 58666
POST /orders from port 58670
POST /orders from port 58684
POST /orders from port 58700
GET /orders from port 58706
POST /orders from port 58710

Every request came from a different client port, yet the third client sees all three orders. The clients never talked to each other; they share data only because they all talk to the same server. Notice also that the two parallel orders finished in the opposite order to how they were typed. The server handles requests as they arrive, so the order of IDs is decided on the server, not by the clients.

4. When the server is gone

Stop the server with Ctrl+C and try again:

Terminal — client
$ curl -sS localhost:3000/menu
curl: (7) Failed to connect to localhost port 3000 after 0 ms: Couldn't connect to server

Every client fails at once, with nothing it can do on its own. This is the single point of failure that comes with a central server. Start the server again and ask for the orders:

Terminal — after restarting the server
$ curl -s localhost:3000/orders
[]

The three orders are gone because they lived in the server's memory. Real servers store shared data in a database for this reason, and run more than one server so that one crash does not stop every client.

Client-server vs. peer-to-peer

The usual alternative is peer-to-peer (P2P), where every node can be both client and server and nodes talk to each other directly. (I covered it in a separate post, Peer-To-Peer Pattern, currently in Korean.) Here is how the two compare on the points the example above made visible.

Client-serverPeer-to-peer
Who starts a requestClients onlyAny peer
Where shared data livesOn the server (one place to read and fix)Spread across peers
Where the rules are enforcedOn the server; clients cannot skip themOn every peer; each must be trusted or checked
If the central node stopsEvery client fails (example 4)No central node; the network keeps working with fewer peers
Adding usersServer load grows; add servers and a load balancerEach new peer also brings capacity
Security and updatesPatch and monitor the serverHarder to control every peer
Typical usesWebsites, APIs, email, databases, online bankingFile sharing (BitTorrent), some blockchains, local device sync

Many real systems mix the two. A video call app, for example, may use a server to find the other person and then send the media directly between devices.

광고

Strengths, weaknesses, and how teams soften the weaknesses

The strengths come from having one place in charge: data is consistent because there is one copy to update, rules cannot be bypassed by a modified client, and you can fix a bug for everyone by deploying the server once. Clients stay light; a browser or a curl command is enough.

The weaknesses are the two you saw in the example, plus a third:

  • Single point of failure. Run several server instances behind a load balancer, and keep data in a database with replicas so a restart does not lose it.
  • Bottleneck under load. Requests queue up when one server is busy. Teams add servers horizontally, cache frequent responses, and move slow work to background jobs so the request–response path stays short.
  • Network dependence. Clients cannot work while offline unless they keep a local copy and sync later, which brings some peer-to-peer ideas back into a client-server app.

None of these changes the model itself. A large website is still clients sending requests and servers sending responses; there are simply more servers, and the "server" in the picture becomes a whole group of machines behind one address.

From my experience

I once ran a video-generation API as a single server process. As traffic grew, requests queued up behind the ffmpeg jobs, and whenever that one process restarted, every client got connection errors, the same symptom as the curl: (7) in example 4.

We changed three things. The ffmpeg work moved to a job queue with separate workers. The API ran as several instances behind a load balancer. And job state went to Redis and the database instead of the server's memory.

Summary

  • In the client-server model, clients send requests and a server sends responses. Clients always start; the server listens.
  • The server owns the shared data and the rules. Clients share data only through the server, and a modified client cannot skip server-side checks.
  • HTTP follows this model and is stateless; anything the server remembers, such as orders or sessions, is stored on purpose.
  • The price of a central server is a single point of failure and a bottleneck under load. Load balancers, multiple instances, databases with replicas, caches and background jobs soften both.
  • Peer-to-peer removes the central node but makes control, security and consistency harder.

References


This post was drafted with AI tools and reviewed and edited by the author.

Hanpy백엔드·인프라 엔지니어 · AI 트렌드 정리 중
About 보기