The standard for WebSockets/HTTPS is port 443 and every single client around the world connects on server port 443. This confused me at first because I was imagining all traffic flowing through a single entity and it would be an insane bottleneck if there were a single entity servicing all web traffic. Then I learned that the "port" is just a number and the actual entity on clients and servers are sockets. Sockets are attached to ports so that the kernel knows which socket to forward incoming traffic to (please correct me if this is wrong). So sockets are a higher-level entity than ports.
My question is whether there is any performance difference at all to having all traffic go to a single port on the server. To properly de-multiplex incoming traffic to a socket, more information must be needed (maybe the client hostname/port?). Whereas if every client were using a unique port on the server side, the server port alone would be sufficient to de-multiplex.
CodePudding user response:
My question is whether there is any performance difference at all to having all traffic go to a single port on the server. To properly de-multiplex incoming traffic to a socket, more information must be needed (maybe the client hostname/port?).
Assuming all the traffic is going to the same program anyway (i.e. to the web server process), then having all the traffic come in on a single port is likely to be more efficient than using multiple ports, since the web server needs only deal with a single socket to accept connections, rather than having to create a separate socket for each of a number of ports.
TCP connections are uniquely identified by a 5-tuple (source IP, source port, destination IP, destination port, protocol), so the amount of work it takes the network stack to de-multiplex incoming traffic will be the same (hashing the 5-tuple) regardless of how many ports are in use.
Whereas if every client were using a unique port on the server side, the server port alone would be sufficient to de-multiplex.
However, doing that would limit the server to a relatively small number of clients, since there are only 65535 ports available on any given host (the port field is 16 bits wide), and many of those ports may already be in use for other services... and the server would have to create one socket per allocated port, which would quickly get unwieldy. So giving every client a unique port would cause problems with port-number contention, while not actually speeding anything up.