Client-server and peer-to-peer architecture
When you send a message on Telegram, it first goes to a server, while when you download a game through a torrent, the pieces of the file arrive at the same time from hundreds of peers. These two paths — client-server and P2P — are the foundation of almost every service on the Internet. Today you will measure in simulators how they are built and when each one wins.
Lesson goals and plan
By the end of the lesson you will…
- explain the components of the client-server architecture and the “request → response” cycle
- explain the concepts of a socket (IP:port), a server program and service ports with examples
- explain the pure and hybrid types of P2P networks and the working principle of BitTorrent (pieces, seeder, tracker)
- compare the two architectures by resource management, scalability, security and field of use
- calculate file distribution time (Dcs and DP2P) with formulas and interpret the result
- justify the choice of a suitable architecture for a real service (Telegram, Zoom, Steam, my.gov.uz…)
Plan (according to the syllabus)
- The structure and working principles of client-server and peer-to-peer network architectures
- Comparing the two architectures by resource management, scalability, security and field of use
In this lesson there are two “metro lines”: one takes every passenger to one central station (the server), and on the other every station carries passengers itself (P2P). Change the numbers in the simulators and find out for yourself when each line gets jammed.
On the official site, one server (or a group of servers) gives the file to everybody: the more downloaders there are, the lower the speed each of them gets. In a torrent, every downloader also passes on the pieces it has received to others: the more people there are, the more distributors there are. The same file, the same Internet — but a different architecture, and the result is completely the opposite. Today we will prove why with formulas and a simulator.
Key terms
First recall the meaning of the term yourself, then click the card to check.
The structure and working principles of client-server and peer-to-peer network architectures
A network architecture is a scheme that determines who contacts whom in programs. Even when the physical cables and routers are the same, applications can choose between two “cultures of communication”: centralized (client-server) or distributed (P2P).
Client-server architecture
The client-server architecture is a model in which the roles of a network application are strictly separated: the server, always switched on, waits for requests at a known address and port, and the clients send it a request when needed and receive a response. Clients usually do not communicate directly with one another.
A real-life example: you log in to HEMIS and look at your grades. Your browser (the client) sends the university server the request “give me this student’s grades”; the server takes the grades from the database and returns them as an HTML page. Your groupmate’s browser does not “know” your browser at all — both of you talk only to the server.
Main features of client-server
Server
A permanent IP address or domain name, always switched on, serves many clients at the same time. In large services a single “server” is actually thousands of computers in a data center.
Client
Connects from time to time, its IP address may change (at home, in the metro, at the university — all different), and it starts the communication itself.
The life path of one request
From the moment you type hemis.adu.uz in the browser and press Enter until the page opens, the following stages take place. Each of them is studied separately in later lessons — for now just look at the overall picture.
- Turning a name into an address (DNS)
The client asks a DNS server for the IP address of the domain name. This is also a small client-server exchange (Lecture 15).
- Establishing a connection (TCP)
The client asks to connect to the server’s port 443. On the client side the operating system chooses a temporary port, for example 51834 (Lecture 12).
- Sending the request
A message like
GET /baholar HTTP/1.1is sent. It is split into packets and reaches the server through routers (Lecture 11). - Processing
The server program reads the request, checks permission, queries the database and prepares the response.
- Returning the response
HTTP/1.1 200 OKand the text of the page return to the client; the browser draws it on the screen.
Sockets and ports: “which door should we knock on?”
A website, a mail service and a database can run on one server computer at the same time. Which program an arriving packet belongs to is determined by the port number. The IP address is the address of a multi-storey building, and the port is the apartment number. The IP + port pair is called a socket; and one TCP connection is identified by four values: client IP, client port, server IP, server port.
| Service | Protocol | Port | Example from life |
|---|---|---|---|
| Web page (unencrypted) | HTTP | 80 | old websites, a router’s settings page |
| Web page (encrypted) | HTTPS | 443 | my.gov.uz, the Payme web account |
| Domain names | DNS | 53 | kun.uz → IP address |
| Remote administration | SSH | 22 | an administrator connecting to a server |
| Sending mail | SMTP | 25 / 587 | sending a letter from a university mailbox |
| File transfer | FTP | 21 | uploading website files to a hosting service |
Two-tier and three-tier structure
In the simple case the client talks directly to the server (two-tier). In large services like Uzum Market there are usually three tiers: (1) the client — an app or browser, (2) the application server — the business logic (the cart, discounts, the order), (3) the database server. The client never sees the database directly — this is important for security. In systems with many users a load balancer is placed in front of the application servers: it hands out requests in turn to several identical servers.
In the code the server waits inside a loop (while True) and answers every request that arrives — real servers also work in exactly this kind of “eternal loop”. 200, 404 and 405 are HTTP status codes; they are studied in detail in practical class 16.
Peer-to-Peer (P2P) architecture
Peer-to-peer (P2P) architecture is one in which the participants (peers) have equal rights, and each of them both gives and receives resources (file pieces, computing power, bandwidth) from the others. Dependence on an always-on central server is absent or minimal.
The best-known example is BitTorrent. The file is split into pieces of equal size (for example, 256 KB each). A newly joined peer downloads pieces from different peers and, as soon as it receives them, starts distributing them to others too. That is why the distribution “power” grows with every person who joins the network.
Pieces
The file is divided into parts; every piece has a hash value — a corrupted or fake piece is detected immediately.
“Rarest first”
(rarest first) A peer first asks for the piece that is least common in the network — that way no piece “disappears”.
Tit-for-tat
A peer gives more to those who give it pieces the fastest. “Take-only” users download more slowly.
Pure and hybrid P2P
| Type | Who knows what? | Examples |
|---|---|---|
| Pure P2P | There is no central server at all; peers find one another by asking each other (a distributed hash table — DHT) | BitTorrent in DHT mode, blockchain networks (Bitcoin nodes) |
| Hybrid P2P | A central server serves only as a “directory”: who is online, who has what. The main data is transferred directly between peers | BitTorrent with a tracker; in video calls, a server for setting up the connection + a media stream sent directly |
When you make a video call through Telegram or WhatsApp, a server is used first to set up the connection, and then, if possible, the audio-video stream is sent directly (P2P) between the two devices. If home routers (NAT) do not allow a direct connection, the stream goes through a relay server. So both architectures live inside a single app.
Simulator: how are the pieces distributed?
Each row is one user, each cell is one piece of the file (8 pieces). The server can send 2 pieces per “tick”. In client-server mode only the server distributes; in P2P mode every peer that has a piece also gives one piece to another peer at each tick. Change the number of users, run both modes and compare the number of ticks.
:ik-pazl: Spread of pieces: is the server alone, or is everyone together?
As you observed, in client-server mode the number of ticks grows in direct proportion to the number of users (close to N·8/2 = 4N), while in P2P mode the number of ticks hardly changes even when N grows. The red cells are the new pieces that arrived on that tick.
Comparing the two architectures by resource management, scalability, security and field of use
The correct answer to the question “which is better?” is “it depends on the task”. Let us compare them by four criteria and even measure scalability with a formula.
| Criterion | Client-server | Peer-to-Peer |
|---|---|---|
| Resource management | Data and computation are at the center; backups, updates and control are in one place, easy | Resources are spread among peers; everyone manages their own computer, there is no single control |
| Scalability | As users grow, the server load grows → new servers, bandwidth and a data center are needed (expensive) | Every new peer brings its own bandwidth to the system too → it “scales by itself” |
| Reliability | If the server goes down, the service stops (a single point of failure); reduced with backup servers | The network keeps working even if several peers leave; but if the owners of a rare file leave, the file may be lost |
| Security | Central authentication, permissions, logs, encryption — easy to manage. But the server is a target of attack (DDoS, break-ins) | No central control: malicious peers, fake pieces, your IP address is visible to others, risk of malware spreading |
| Cost | The service owner pays for servers, the data center and bandwidth | The cost is shared among the users |
| Field of use | Banks, government services, social networks, mail, online shops, learning systems | Distributing large files, game updates, cryptocurrencies, video call streams, local file sharing |
We measure scalability: file distribution time
Suppose a department wants to distribute a teaching video of size F bits to N students. The server’s upload speed is us, each student’s upload speed is u, and the download speed of the slowest student is dmin. The minimum time until all N students have received the whole file is the distribution time D.
Client-server
The server has to send N copies itself: N·F bits, at speed us. In addition, the slowest client receives F bits at speed dmin.
Dcs = max( N·F / us , F / dmin )
When N is large the first term dominates → the time grows linearly with N.
P2P
The server has to send at least one copy (F/us), and the total of N·F bits is spread using the sum of the upload speeds of everyone.
DP2P = max( F/us , F/dmin , N·F / (us + N·u) )
As N grows, the third term approaches F/u → the time is bounded.
These formulas are a simplified form of the classic model given in the textbook by J. Kurose and K. Ross (reference 13, chapter 2): all peers are assumed to have the same upload speed. It is an estimate of the lower bound (the best case).
Graph simulator
Drag the sliders: the graph plots the distribution time for both architectures for N = 1…100. What happens if you bring the peer upload speed close to 0? And if you raise the server speed 10 times?
:ik-grafik: Distribution time: Dcs and DP2P
Now write the same calculation yourself in Python. Pay attention to the units: the file size is in megabytes (MB), the speeds are in megabits per second (Mbit/s) — 1 byte = 8 bits.
Resource management and security — in more depth
:ik-bino: Why will a bank never be P2P?
A money transfer in Payme or Click is an operation that requires a single, trusted ledger (who has how much money). In the client-server model this ledger is kept in one database under control, every operation is written to a log, and the user is checked with a password and an SMS code. In P2P the question arises: “whose word do we trust?” — blockchains solve it with complex consensus algorithms, but this is much slower and more expensive.
:ik-oyin: Why do game updates love P2P?
An update is a large file that is the same for everyone and needed by everyone at the same time. This is exactly the situation where P2P is strongest. That is why help from peers is used to deliver some game and system updates (for example, in Windows, the ability to get updates from computers on the local network — Delivery Optimization).
| Threat | In which architecture is it dangerous | Protection |
|---|---|---|
| DDoS — “choking” the server with requests | Client-server (a single target) | Load balancing, CDN, traffic filtering |
| Breaking into the server’s database and stealing data | Client-server | Encryption, restricting permissions, updates |
| A fake or malicious piece (poisoning) | P2P | Checking every piece with a hash |
| Your IP address being visible to strangers | P2P | A trusted client program, only legal content |
| Spreading malware (disguised as a game “crack”) | P2P | Antivirus, official sources |
Field of use: which service is in which model?
Finding the rare piece (rarest first)
How does a BitTorrent client decide which piece to ask for first? It counts the pieces held by the peers it knows and chooses the least common one. Write this algorithm.
Common mistakes
| Misconception | Why it is wrong | Correct answer |
|---|---|---|
| “A server is a very powerful computer” | A server is first of all a role and a program. A web server can be run even on an ordinary laptop | A server is a program that waits for requests and answers them; the powerful computer set aside for it is also called that |
| “There is no server at all in P2P” | Hybrid P2P has trackers and connection-setup (signaling) servers | There is none in pure P2P; in a hybrid there is a helper server, but the main data is between peers |
| “P2P is something illegal” | The technology is being confused with its misuse | Linux distributions, game updates and legal content are distributed through P2P; the illegality is in the content |
| “A port is a physical socket on a computer, like USB” | A network port is a software number | A port is a number from 0 to 65535 that distinguishes a service |
| “P2P gets slower when users are added” | A new peer adds its upload bandwidth too | P2P distribution time stays bounded around F/u even when N grows |
| “A computer is either only a client or only a server” | A browser (client) and a local web server can run on one computer at the same time | A role belongs to a program, not to a computer |
Reinforcement
// REMEMBER IN 1 MINUTE
- Client-server: the server is always on and waits, the client sends the request; clients do not talk to one another.
- Socket = IP + port; the port distinguishes the service (80, 443, 53, 22, 25).
- P2P: a peer both receives and gives; there are pure (DHT) and hybrid (tracker) types.
- Dcs = max(NF/us, F/dmin) — linear in N; the additional term of DP2P, NF/(us+Nu), is bounded.
- Central control, security, a single ledger needed → client-server; a large identical file for many people → P2P.
Final test
Review questions
What do you understand by the client-server architecture?
A model with strictly separated roles: the server, always on, waits for requests at a known address and port, and clients send requests and receive responses; clients usually do not communicate directly with one another.
List the main differences between a server and a client.
Server: always on, permanent address, serves many clients, waits for requests. Client: connects from time to time, its address changes, it starts the communication itself.
Why are ports and sockets needed?
Several services run on a computer with one IP address; the port shows which program a packet belongs to. IP + port = socket — the exact “door” of the communication.
List the stages of one web request.
Finding the IP address through DNS → TCP connection (port 443) → sending the request → processing on the server → returning the response and drawing the page.
What is the difference between pure and hybrid P2P?
In pure P2P there is no central server at all (peers find one another through DHT). In a hybrid, a central server serves only as an index/directory, and the main data is transferred between peers.
How does BitTorrent work?
The file is split into pieces; a peer finds other peers through a tracker or DHT, downloads pieces from them and distributes the ones it has received; rare pieces are requested first, exchange is encouraged with tit-for-tat, and every piece is checked with a hash.
Why does the client-server distribution time grow linearly with N?
The server has to send N copies itself: N·F bits at a constant speed us — the time is N·F/us.
What are the weaknesses of P2P in terms of security?
No central control, malicious or fake peers, your IP address being visible to others, the spread of malware, and the risk of copyright violation.
What is a “single point of failure” and how is it reduced?
A component whose failure stops the whole system (for example, a single server). It is reduced with backup servers, a load balancer and several data centers.
Give an example of both architectures being used together in one app.
A video call: setting up the connection and the contact list go through a server, while the audio-video stream goes directly between peers (P2P) if possible, otherwise through a relay server.
Independent work
The independent study topic in the course syllabus: “P2P, multimedia, VoIP, RTP/RTSP, CDN and cloud network services.” (6 hours). Analyze the service in your assigned variant and prepare a short report (2–3 pages):
- Which architecture does the service use (client-server, pure/hybrid P2P, mixed)? Justify it by relying on official sources or technical articles.
- Draw a structural diagram of the service (client, servers, CDN, peers; the directions of data flow).
- Calculate Dcs and DP2P with the parameters of the task (you may use the Python calculator from the lesson) and interpret the result.
- Write down two security threats to the service and measures against them.
| Criterion | Points |
|---|---|
| The architecture is correctly identified and justified with sources | 1 |
| The structural diagram is clear, the data flows are shown | 1 |
| Dcs and DP2P are calculated correctly (units MB → Mbit) and interpreted | 2 |
| The security threats and measures are written with sound reasoning | 1 |
Summary
Your lesson results
Your results are saved in this browser. Read all the sections and complete the exercises and assignments.