Logo

dev-resources.site

for different kinds of informations.

πŸš€How TCP Servers Are Designed to Handle Multiple Requests

Published at
3/22/2024
Categories
concurrency
webserver
java
multithreading
Author
mjsf1234
Author
8 person written this
mjsf1234
open
πŸš€How TCP Servers Are Designed to Handle Multiple Requests

πŸ“š Table of Contents

  1. πŸ–₯️ What Is a Server?
  2. πŸ”„ What Is Multi-Threading?
  3. ❓ Why Do We Need a Multi-threaded Server?
  4. πŸ› οΈ How to Design Multi-Threaded Servers?

πŸ–₯️ What Is a Server?

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?

Multithreading is a programming concept that allows multiple threads of execution to run concurrently within a single process.
Image description

❓ Why Do We Need a Multi-threaded Server?

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

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

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

πŸ› οΈ How to Design Multi-Threaded Servers?

  • 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

  • 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

  • Step 3: Create a New Thread for Each Request
    Image description

  • Step 4: Read the Request and Send the Response
    Image description

  • Step 5: Repeat Steps 2 to 4 an Infinite Amount of Time
    Image description

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! πŸš€

webserver Article's
30 articles in total
Favicon
Nginx Simplified: Technical Insights with Real-World Analogies
Favicon
Need assistance to solve an issue
Favicon
Installing Nginx Web Server on Linux: A Step-by-Step Guide
Favicon
Step-by-Step Guide: Setting Up Apache2 and Deploying an HTML Template on Ubuntu (Using Vagrant on a Local Machine)
Favicon
Path-Based Reverse Proxying with Caddy
Favicon
Transform Your Business with Our ERP CRM Software in Kolkata
Favicon
What is a web server ?
Favicon
Postmortem
Favicon
gondola: Why not use a lightweight and simple YAML-based reverse proxy?
Favicon
Uptime Kuma: Uptime von Services beobachten und tracken πŸ”Ž
Favicon
Nginx-Proxy-Manager: Leichtgewichtiger Reverse-Proxy 🌍
Favicon
Nginx Proxy Manager: Lightweight reverse proxy 🌍
Favicon
Uptime Kuma: Monitor and track the uptime of services πŸ”Ž
Favicon
OpenResty: The Overpowered Web Server Used by 40M Websites (that people rarely talk about)
Favicon
Installing a WebServer on Win Server 2019 VM
Favicon
HOW TO SECURE A WINDOWS SERVER WITH IIS AND AZURE NETWORK SECURITY FEATURES
Favicon
Web Sunucuları: İnternetin Temel Taşları
Favicon
Installing a WebServer on Win Server 2019 VM
Favicon
Advantages of Outsourcing Server Management
Favicon
Deploying a web server on the Linux virtual machine
Favicon
Should I use Nginx or Apache Server?
Favicon
Running a Secure Web Server on AWS EC2
Favicon
Trending In NGINX: Top Trends
Favicon
πŸš€How TCP Servers Are Designed to Handle Multiple Requests
Favicon
Why did Cloudflare Build its Own Reverse Proxy? - Pingora vs NGINX
Favicon
Planning my Linux Homelab
Favicon
Building a Web Server in Go
Favicon
πŸš€Unlocking the Power of NGINX - A Web Server Marvel! πŸš€
Favicon
Local machine as a web server without public IP using Cloudflare tunnel(part-1)
Favicon
How web technology works? - Part 02

Featured ones: