back
3 min read

Multithreaded Web Server

·3 min read·backend, multithreading

A multithreaded web server is a type of server that uses multiple threads to handle multiple client requests concurrently. Each client request is typically handled by a separate thread, allowing the server to manage several requests simultaneously without blocking other connections.

A web server is a software that serves static/dynamic content over the web using protocols such as HTTP (Hypertext Transfer Protocol) or HTTPS. Its primary role is to process requests from clients, such as web browsers or APIs, and deliver web pages, files, or data in response.

A single-threaded server is a type of server architecture where all incoming client requests are handled by a single thread of execution.

  • At one particular time, a single-threaded server can only serve a response to one client. It has to close the connection if another client is requesting data.

What is a thread???

A thread is the smallest unit of a program that can be executed independently. It is a sequence of executable instructions within a process. Threads enable a program to perform multiple tasks concurrently within the same application, sharing the same memory and resources of the process.

What is a Multithreaded Web Server?

A multi-threaded web server is a type of server that uses multiple threads to handle multiple client requests concurrently. Each client request is typically handled by a separate thread, allowing the server to manage several requests simultaneously without blocking other connections.

  • A multi-threaded web server provides better concurrency and responsiveness compared to a single-threaded server.
  • It is suitable for applications with moderate to high traffic but requires careful thread management and synchronization to ensure reliability and performance.
  • Drawback — If the server gets a million requests, (ideally) it should create a million threads, this takes up a lot of space in the memory. So the CPU will be overloaded and the performance may degrade due to high number of threads that are spawned.
  • To fix this issue, we use a concept call Thread Pool — It is a collection of pre-initialized reusable threads maintained by a system or application to efficiently manage and execute tasks. Instead of creating and destroying threads repeatedly for each task, a thread pool maintains a fixed number of threads that can be reused for multiple tasks, reducing overhead and improving performance.

  • Clients (c1, c2, etc.):
    • Multiple clients send requests to the server.
  • Task Queue:
    • Incoming tasks (T1, T2, ..., T30) from clients are added to a queue.
    • The queue serves as a buffer to manage requests before processing.
  • Server:
    • The server processes tasks using multiple threads (T1 to T100).
    • Threads are used to handle tasks concurrently, improving efficiency.
  • Thread Execution:
    • Each thread handles a task (e.g., T1t5, T100t100).
    • Tasks are executed independently, ensuring faster response times for clients.