Client-Server Architecture
# CHAPTER 3
Client-Server Architecture
1. Chapter Introduction
The foundation of the modern internet—and every system design interview—is the Client-Server model. Before you can scale databases or implement microservices, you must profoundly understand how a user's device communicates with a backend system. This chapter breaks down the exact lifecycle of a web request, the crucial role of DNS, and the architectural separation between Web Servers and Application Servers.2. The Client-Server Model
In distributed computing, the architecture is divided into two distinct entities:- The Client (The Requestor): The end-user's device. This could be a web browser (Chrome), a mobile app (iOS/Android), or an IoT device. Clients are responsible for rendering the UI and sending requests.
- The Server (The Provider): A powerful computer (or cluster of computers) hosted in a data center. Servers wait for requests, process business logic, query databases, and return responses back to the client.
*Key Principle:* Clients should NEVER directly access the database. The server acts as the secure middleman.
3. DNS (Domain Name System) Basics
Computers communicate using IP Addresses (e.g.,192.168.1.1 or 2001:0db8::ff00). Humans cannot remember billions of IP addresses.
DNS is the "Phonebook of the Internet." It translates human-readable domain names (e.g., www.google.com) into machine-readable IP addresses.
The DNS Lookup Process:
-
1.
You type
facebook.cominto your browser.
- 2. The browser checks its local cache.
- 3. If not found, it queries an ISP DNS Resolver, which queries Root servers, TLD servers, and finally the Authoritative Name Server.
-
4.
The DNS returns the IP Address (e.g.,
157.240.22.35) back to the browser.
4. The HTTP Request Lifecycle
Once the client has the IP address, the real communication begins. You must be able to whiteboard this flow:- 1. TCP Handshake: The client establishes a reliable TCP connection with the server (SYN, SYN-ACK, ACK).
- 2. HTTP Request: The client sends an HTTP GET/POST request containing headers, cookies, and payloads.
- 3. Processing: The server receives the request, executes business logic, and queries the database.
-
4.
HTTP Response: The server sends back an HTTP response with a status code (e.g.,
200 OK) and data (usually JSON or HTML).
- 5. Rendering: The client's browser parses the JSON/HTML and renders the UI.
5. Web Servers vs. Application Servers
In modern, scalable system design, the "Server" is often split into two distinct tiers:1. The Web Tier (Web Servers):
- *Examples:* Nginx, Apache.
- *Function:* Handles raw HTTP traffic. Serves static content (HTML, CSS, Images). Manages SSL termination and connection handling. If the request requires dynamic logic, it forwards it to the App Server.
2. The Application Tier (App Servers):
- *Examples:* Node.js, Spring Boot (Java), Django (Python).
- *Function:* Executes complex business logic, processes payments, running algorithms, and communicates directly with the database.
*Why split them?* Separation of concerns. Web servers are optimized for handling thousands of concurrent lightweight HTTP connections. App servers are optimized for heavy CPU computation.
6. Architectural Diagram: The 3-Tier Architecture
This is the baseline architecture you will draw in your interviews.7. HR/Interview Perspective: Protocol Knowledge
Interviewers will test your depth by asking, "Why use HTTP over UDP for this request?" You must know that HTTP (which runs on TCP) guarantees packet delivery, ensuring data isn't lost during a checkout process. If you are designing a live video stream, you might suggest UDP, which is faster but allows packet loss (glitches).8. Real-World Scenario: Static Asset Bottleneck
*Scenario:* A startup hosts their marketing website on a single Django (Python) App Server. Every time a user loads the page, Django has to process and serve a 5MB background image. The site slows to a crawl under heavy load. *The Fix:* The engineers place an Nginx Web Server in front of Django. Nginx is configured to serve the 5MB image directly from disk in milliseconds, without ever waking up the Python App Server. The App Server is now free to process dynamic API requests exclusively.9. Mini Project: Trace a Request
Verbally trace exactly what happens when a user clicks "Login" on a website.- 1. Browser resolves DNS.
- 2. Browser sends HTTP POST request with username/password over HTTPS.
- 3. Request hits Load Balancer.
- 4. Request routed to App Server.
- 5. App Server hashes password and queries Database.
- 6. App Server generates JWT (JSON Web Token) and sends HTTP 200 Response.
- 7. Browser stores token and redirects to Dashboard.
10. Common Mistakes
- Assuming direct DB access: Never draw a line directly from the Client (Mobile App) to the Database. This is a massive security violation. All DB access must go through the Application Server.
- Forgetting DNS: In global system design (e.g., Netflix), DNS is used to route users to the data center physically closest to them to reduce latency.
11. Best Practices
- Stateless Architecture: Ensure both your Web and App tiers are stateless. Any server should be able to handle any request.
- HTTPS: Always mention that communication between the Client and the Load Balancer occurs over HTTPS (encrypted), even if internal network traffic is HTTP.
12. Exercises
- 1. What is the difference between a 404 HTTP status code and a 500 HTTP status code?
- 2. If you want to design a multiplayer shooting game, would you prioritize TCP or UDP?
13. MCQs
What is the core definition of the Client-Server model?
Why should a Client (like a mobile app) NEVER communicate directly with the Database?
What is the primary function of DNS (Domain Name System)?
What is the first step that occurs before an HTTP request can be sent from a client to a server?
What is the primary difference between a Web Server and an Application Server?
In a standard 3-Tier Architecture, what are the three tiers?
Which HTTP method is traditionally used to submit data to the server to create a new resource (e.g., submitting a login form)?
Why might Nginx be placed in front of a Node.js application server?
What does an HTTP 200 OK status code indicate?
In global system design, how can DNS help reduce latency for users?
14. Interview Questions
-
Q: "Walk me through exactly what happens when you type
www.amazon.cominto your browser and press Enter, down to the network layer."
15. FAQs
- Q: Are Web Servers and App Servers always on different physical machines?