1. Introduction

A web server is a program that serves incoming HTTP message requests. These requests could be coming from anywhere, connecting to a server that is listening on a specific port.

In this tutorial, we’ll explore how a web server handles multiple requests simultaneously (concurrently) on a single port.

We’ll start with reviewing the web request lifecycle and understanding what a concurrent request is. Next, we’ll explore the types of computer ports and sockets, delving into how they work. Finally, we’ll discuss the mechanism for handling such requests.

2. Web Request Lifecycle

A web request begins when a client sends a message in HTTP format to a web server using a TCP/IP socket and then receives a response:

Web Request Lifecycle

In the diagram above, the web server forwards the request to the web app, which in turn will process the request and compose the response to send back the the client.

Notice that the diagram separates the web server and the web app. We can configure the web app to also process the incoming requests, such as by installing the PHP interpreter as an Apache module. However, it’s not recommended in a production environment for security reasons.

3. Concurrent Requests

Concurrent requests occur when a web server gets hit by more than one request at the same time:

Async Mechanism

We can either process one request at a time (consecutively) or multiple requests simultaneously (concurrently), each with its own advantages.

The diagram above shows how a web server handles concurrent requests by using an asynchronous mechanism. This mechanism means the web app uses non-blocking calls to handle incoming requests.

Let’s analyze how the same port can be hit by multiple requests at the same time.

4. Computer Ports

First, we need to understand what computer ports are.

We can separate computer ports into two types: physical and logical.

Some common physical ports are serial ports, USB ports, Ethernet ports, and so on.

On the other hand, logical ports are software-based endpoints that range from 0 to 65535. We can configure our applications to use a specific port number, however, some ports are reserved.

Logical ports are also called network ports, which we use to distinguish different services or processes on a networked device by combining the device IP and port. For example, we identify a web service at a specific device IP and port 80 or 443. Additionally, the same device may also have other services, such as SSH listening on port 22.

Physical ports like serial and USB typically only have a one-on-one connection. Meanwhile, logical ports (via Ethernet or Wi-Fi) can support many-to-many connections.

5. Handling Concurrent Requests on a Single Port

All networked devices use sockets to communicate within a network.

In socket programming, both the client and server sides need to call a set of socket APIs to communicate. For the server side, those APIs are bind(), listen(), accept(), receive(), send(), and close().

When we run a web server, it creates a socket (with listenfd socket file descriptor) to listen on a port and then waits for incoming connections. Moreover, when the server receives a connection request from a client, it creates another socket (clientfd) to handle the request:

Socket connection mechanism on server side

In case there are concurrent requests, and the server is capable of handling them, then the server will simply create a new socket (clientfd*) for each request:

Concurrent socket connections on server side

A socket file descriptor is an integer value and unique per socket. The operating system associates socket file descriptors with various values in the background, such as a combination of client IP and port, server IP and port, and communication protocol.

As a result, whenever the server wants to communicate with the client, it only needs to pass the socket file descriptor to the socket API, and then the OS will know which channel to use.

6. Conclusion

In this article, we analyzed how a web server can handle multiple requests from clients at the same time on the same port.

First, we reviewed the basic web request lifecycle and concurrent requests scenario. Then, we explored the computer port types and the differences between them. Finally, we studied how a web server handles concurrent requests on the same port.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.