Chapter 2
The TCP 3-way Handshake process:
- SYN - Client picks a random sequence number x and sends a SYN packet, which may also include additional TCP flags and options
- SYN/ACK - Server increments x by 1, picks own random sequence number y, appends its own flags and options, and dispatches the response
- ACK - Client increments both x and y by one and completes the handshake by dispatching the last ACK packet in the handshake
Flow control
- Flow control is a method for preventing the sender from overloading the receiver with data they may not be able to process
- Each side of the TCP connection advertises its own receive window (rwnd), which communicates the size of the available buffer space to hold the data
- The window size can be changed during a transaction. If the window size changes to 0, this indicates the client cannot receive any more data until it finishes processing the existing buffered data
- Each ACK packet carries the latest rwnd on each side of the connection
TCP Slow Start
TCP slow start is a congestion control mechanism used in TCP (Transmission Control Protocol), which is one of the core protocols of the Internet. The purpose of TCP slow start is to gradually increase the amount of data sent by a sender until it reaches an optimal level that maximizes network utilization without causing congestion. When a TCP connection is established between a client and a server, the sender begins by sending a small number of data packets. During the initial phase, the sender’s transmission rate is low to avoid overwhelming the network or causing congestion. This phase is known as slow start.
Here’s how TCP slow start works:
- Connection Establishment: The TCP connection is established between the sender and the receiver.
- Initial Congestion Window (cwnd): At the beginning of the connection, the sender sets its congestion window (cwnd) to a small value, usually one or two segments worth of data. The congestion window represents the number of unacknowledged packets that the sender can have in flight at any given time.
- Sending Data: The sender starts sending data to the receiver, and it waits for acknowledgments (ACKs) from the receiver for each packet sent.
- Doubling cwnd: For each ACK received, the sender increases its congestion window size by doubling it. This means that with every successful round-trip of ACKs, the sender is allowed to send twice as many packets as before.
- Exponential Growth: As the sender continues to receive ACKs, the congestion window keeps doubling, leading to an exponential growth in the sender’s data transmission rate.
- Congestion Avoidance: Once the congestion window reaches a certain threshold (known as the slow-start threshold), the congestion control mechanism switches from slow start to congestion avoidance. During congestion avoidance, the sender increases the congestion window linearly instead of exponentially.
- Multiplicative Decrease: In case of packet loss, which indicates network congestion, the sender interprets it as a sign of congestion and reduces its congestion window size significantly, implementing a multiplicative decrease.
The purpose of TCP slow start is to allow the sender to probe the available bandwidth and avoid overwhelming the network with a sudden surge of data. It provides a conservative approach to ensure network stability while still enabling the sender to ramp up its transmission speed to make efficient use of available resources. Slow start is essential for achieving fairness and stability in TCP-based communication across the Internet.
Congestion Avoidance
It is important to recognize that TCP is designed to use packet loss as a feedback mechanism to help regulate its performance. Slow start initializes the connection with a conservative congestion window, and for every round-trip, doubles the amount of data in flight until it exceeds the receiver’s flow-control window, a system-configured congestion threshold (ssthresh) or until a packet is lost, at which point the congestion avoidance alogorithm takes over.
Optimizing TCP
Some general guidelines for optimizing TCP on a system:
- Ensure the system is running the latest kernel
- Increase TCP’s Initial Congestion Window to 10
- Disable slow-start after idle to improve performance for long-lived TCP connections, which transfer data in bursts
- Enable Window Scaling to increase the maximum receive window size and allow high-latency connections to achieve better throughput
- Enable TCP Fast Open to allow data to be sent in the initial SYN packet in certain situations.
- Eliminate redundant data transfers. You cannot make the bits travel faster. However, you can reduce the amount of bits that are sent
- Compress transferred data
- Position servers closer to the user to reduce RTT
- Reuse established TCP connections whenever possible
Inspecting open socket statitistics on Linux systems
sudo ss --options --extended --memory --processes --info to see current peers and their respective connection settings