# Client-Server Architecture

The Client-Server architecture is a distributed application structure that partitions tasks or workloads between the providers of a resource or service, called **servers**, and service requesters, called **clients**.

## 1. The Model

*   **Client:** Initiates communication sessions with servers which await incoming requests.
*   **Server:** Provides a function or service to one or many clients.

This model is the foundation of the internet (Web Browser = Client, Web Server = Server).

## 2. Architecture Tiers

Client-server systems are often described by the number of layers (tiers) involved.

### 1-Tier Architecture
The client, server, and database all reside on the same machine.
*   **Example:** A desktop application running locally (e.g., MS Office installed on PC).

### 2-Tier Architecture
The client communicates directly with the database server. The application logic is either inside the client (Fat Client) or the database (Stored Procedures).
*   **Pros:** Simple to set up.
*   **Cons:** Hard to scale, security risks (exposing DB directly), tight coupling.

### 3-Tier Architecture
A middleware layer is added between the client and the database.
1.  **Presentation Layer (Client):** UI/UX.
2.  **Application Layer (Server):** Business logic.
3.  **Data Layer (Database):** Data storage.
*   **Pros:** Scalable, secure (DB is hidden), maintainable.
*   **Note:** This is the standard for most web applications.

### N-Tier Architecture
Extends 3-tier by breaking the application layer into further distributed services (e.g., Microservices).

## 3. Thin vs. Thick Clients

*   **Thin Client:** Relies heavily on the server for processing. The client is mostly for display (e.g., a web browser running a basic HTML page, Chromebooks).
*   **Thick (Fat) Client:** Performs the bulk of the processing locally. It only needs the server for data storage (e.g., a video editing software that syncs to cloud, or a complex Single Page Application).

## 4. Advantages & Disadvantages

| Advantages | Disadvantages |
| :--- | :--- |
| **Centralization:** Data is stored in one place (Server), making backups and management easier. | **Single Point of Failure:** If the server goes down, all clients stop working. |
| **Scalability:** You can upgrade the server to handle more clients. | **Network Dependency:** Requires a reliable network connection. |
| **Security:** Access control is managed centrally. | **Cost:** Servers can be expensive to maintain. |

## 5. Peer-to-Peer (P2P) Architecture

In contrast to the Client-Server model, Peer-to-Peer (P2P) architecture distributes tasks and workloads between peers. Peers are equally privileged, equipotent participants in the application.

*   **No Dedicated Servers:** There is no central server. Every node (peer) acts as both a client and a server.
*   **Decentralization:** Resources are shared among all peers.
*   **Scalability:** As more peers join, the system's capacity increases (unlike Client-Server where more clients slow down the server).
*   **Examples:** BitTorrent (File Sharing), Bitcoin (Blockchain), Skype (VoIP).

### Comparison

| Feature | Client-Server | Peer-to-Peer |
| :--- | :--- | :--- |
| **Service** | Server provides, Client requests | Peers request and provide |
| **Focus** | Information sharing | Connectivity |
| **Bottleneck** | Server bandwidth | Peer bandwidth |
| **Cost** | Expensive (Server maintenance) | Cheap (Users provide resources) |

[[programming/client-vs-backend]]
[[programming/distributed-systems]]
[[programming/rest-apis]]