Lesson 02: Multiplexing

Ports & Sockets

IP Addresses deliver a packet to a specific machine. But once the packet arrives, the Operating System must deliver it to the correct application. If you have Spotify, Chrome, and Slack all downloading data simultaneously, the OS needs a way to separate the traffic.

This is where Ports come in. A port is a 16-bit number (from 0 to 65535) that acts as a logical "door" on the machine. Servers listen on "Well-Known" ports: HTTP uses port 80, and HTTPS uses port 443.

The 4-Tuple Socket

A single connection between two applications is uniquely identified by four pieces of information, known as the 4-Tuple:

(Source IP, Source Port, Destination IP, Destination Port)

If you open three tabs in Chrome to the same website, the Destination IP and Destination Port (443) are identical for all three. How does the OS know which tab gets which data? Because of the Source Port.

Ephemeral Ports

When an application makes an outgoing connection, it doesn't usually care which port it sends data from. The OS automatically assigns it a random, unused high-numbered port called an Ephemeral Port.

Connection Multiplexer

Click to open new connections from your browser to Google's HTTPS server. Notice how the OS assigns a unique Source Port to keep each connection distinct.

YOUR MACHINE (192.168.1.50)
GOOGLE SERVER (142.250.190.46)

Because the ephemeral port is unique, the server knows exactly where to send the response, and your OS knows exactly which browser tab to route the incoming packet to.

This raises an interesting scaling limit: since there are only 65,535 ports available, a single IP address can only have about 65,000 simultaneous outgoing connections to the exact same Destination IP and Port. This is called Port Exhaustion, and it frequently brings down large-scale load balancers.

Check yourself

1. What allows your OS to keep track of multiple Chrome tabs open to the same website simultaneously?

Because the Destination IP and Destination Port are identical for all tabs, the OS uses a unique Source Port to multiplex the incoming response data to the correct tab.