🚀How TCP Servers Are Designed to Handle Multiple Requests
Tags: concurrency, webserver, java, multithreading
Published: 2024-03-22 18:52:59

## 📚 Table of Contents
1. [🖥️ What Is a Server?](#what-is-a-server)
2. [🔄 What Is Multi-Threading?](#what-is-multi-threading)
3. [❓ Why Do We Need a Multi-threaded Server?](#why-do-we-need-a-multi-threaded-server)
4. [🛠️ How to Design Multi-Threaded Servers?](#how-to-design-multi-threaded-servers)

## 🖥️ What Is a Server? <a name="what-is-a-server"></a>
It is the process that listens to the TCP protocol on some port, addresses the client request, and sends the response.

## 🔄 What Is Multi-Threading? <a name="what-is-multi-threading"></a>
Multithreading is a programming concept that allows multiple threads of execution to run concurrently within a single process.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwg6454dew0sd0lbipqs.png)

## ❓ Why Do We Need a Multi-threaded Server? <a name="why-do-we-need-a-multi-threaded-server"></a>
Let's imagine we have a single-threaded server that will process one request at a time. Now, imagine what if millions of requests come in.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jjhwioawpa8i00iwt7j.png)

I've created a server that processes requests one after the other. Each new request waits until the previous one is completed. Take a look at the response times in different terminals:

![Response Timing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9cq4pwc8au3u0p7oiuy.png)

Now, consider a **multithreaded server** architecture. It can handle multiple client connections simultaneously using threads. Each incoming connection gets its own thread, enabling the server to serve multiple clients at once.

In our example, all four clients receive their responses concurrently after four seconds. This demonstrates the efficiency of a multithreaded server in handling multiple requests simultaneously.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttngzwvp8sslve4x4r9i.png)

## 🛠️ How to Design Multi-Threaded Servers? <a name="how-to-design-multi-threaded-servers"></a>
- **Step 1: Open the Socket to Listen to the Port**
Create the `ServerSocket` object that will accept the TCP connection from port `1234`. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/doo01gk86d7e2sngk7iz.png)

- **Step 2: Accept the Client's Connection to That Port**
Accept the client request using the `ServerSocket.accept()`. 
It is a blocking system call meaning it will not move further without accepting the connection. 
`ServerSocket.accept()` will return the `Socket` object specific to that client connection, which will be used for reading requests and sending responses to that client. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86s5bchck98ffel3gw2x.png)

- **Step 3: Create a New Thread for Each Request**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdier1shht35wisgi0xb.png)

- **Step 4: Read the Request and Send the Response**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yohg5h0nibilr6shhmnd.png)

- **Step 5: Repeat Steps 2 to 4 an Infinite Amount of Time**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bld5znun86my75oetwgr.png)

**Disclaimer: 🚨** This article aims to explain multithreaded servers. However, there are practical limitations to this design. **Creating millions of threads to handle millions of requests can strain system resources.** These issues can be addressed by using strategies such as thread pooling.

🌟 Thank you for reading! I appreciate your time and hope you found the article helpful. 📚 I'm open to suggestions and feedback, so feel free to reach out. Let's keep exploring together! 🚀