0

Typically when separate machines or programs on the same machine send requests to the Internet, they open an ephemeral port where packets can be routed back correctly.

What happens if a single program sends 1000 requests to the same website? Would each request open up a separate ephemeral port?

The issue with this is that if I were to send 2 requests spaced out, then the same ephemeral port is used. Is the OS somehow aware if the response is still ongoing and does it open up new ephemeral ports if the previous ones are in use?

If it's somehow true that 1000 simultaneous requests from a program opens 1000 ephemeral ports, is the number of simultaneous requests capped by the total number of ports?

1
  • The OS is not concerned with “requests”. It is concerned with TCP connections, which are uniquely identified by source IP+port and destination IP+port.
    – Daniel B
    Sep 18, 2020 at 18:33

2 Answers 2

2

What happens if a single program sends 1000 requests to the same website? Would each request open up a separate ephemeral port?

Usually ports aren't associated with requests; they are associated with connections. HTTP uses the TCP transport protocol (that's where ports are implemented), which carries completely free-form bidirectional data streams – it's entirely up to the application protocol whether it'll keep reusing the same transport connection for multiple requests or whether it'll close and open a new one every time.

Speaking about HTTP, the early versions (0.9 and official 1.0) were strictly one request/response per connection – however, persistent connections were added later and became standard with HTTP/1.1.

The more recent HTTP/2 is designed to multiplex many requests in parallel over the same TCP connection, and likewise HTTP/3 makes use of the multiple stream support in QUIC.

The issue with this is that if I were to send 2 requests spaced out, then the same ephemeral port is used. Is the OS somehow aware if the response is still ongoing and does it open up new ephemeral ports if the previous ones are in use?

The OS isn't aware of requests but it is aware of connections. Programs usually don't implement the transport protocol on their own – they use the "sockets" programming interface to access the network, and each TCP connection (with its own port pair) is represented by a socket file descriptor. The program doesn't care about TCP handshakes, nor about ephemeral ports – it just asks the OS to create a socket and connect it.

The OS keeps the TCP connection alive and the port pair reserved for that socket for as long as the program holds it. Only closing it (either explicitly, or by exiting the program) will release the ephemeral port as well as inform the server about closed connection.

Run netstat -n to see a list of connections that the OS is aware of.

This also works the same way with UDP, SCTP, etc. Even though e.g. UDP is connectionless, programs can still have a UDP socket that's bound to a specific port pair and the OS will prevent it from being accidentally reused. So e.g. a program that implements the QUIC or uTP transport protocol all by itself still does so on top of the OS-provided UDP socket for multiplexing.

If it's somehow true that 1000 simultaneous requests from a program opens 1000 ephemeral ports, is the number of simultaneous requests capped by the total number of ports?

Yes. If the program wants to have 1000 simultaneous TCP connections to the same destination, then it's limited to the number of possible ports.

But if the remote address and/or remote port are different, then the local port number can be reused, at least technically.

1

If a single app issued 1000 HTTP requests to the same web server, first, its HTTP client code might try to parallelize all the requests through a limited number of HTTP/2 or QUIC (HTTP/3) connections, or partially parallelize and partially serialize them in a limited number of HTTP 1.1 Keep-Alive connections. So it may only choose to make, say, 4 simultaneous connections, in which case it would only open up 4 ephemeral ports.

If it does choose to open up a huge number of separate connections, then yes, it will use a huge number of ephemeral ports, and yes, you can run out of available ephemeral ports. Since ephemeral ports go from 49152-65535, you can only have about 16,384 TCP sessions open simultaneously from the same client IP address to the same server IP address and port.

To avoid possible problems with reusing port numbers too soon, TCP requires recently-closed ephemeral ports to remain unused for 2 minutes. Ports being held in this 2 minute waiting period are said to be in the TIME_WAIT state. The TCP stack keeps track of which ports are in the TIME_WAIT state even if the process that opened them has exited or is otherwise dead/zombified.

Note that a TCP connection is uniquely identified by the 4-tuple of (destination IP address, destination port, source IP address, source port). So if the web server has more than one reachable IP address, or if the client has more than one usable IP address, then each pair of IP addresses could potentially have its own set of ephemeral ports. However, the APIs available in your HTTP client library or TCP stack might not make it very easy to accomplish this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .