# Flow Control
## What is Flow Control
Flow control is a mechanism used in data communications to manage the rate at which data is transmitted between a sender and a receiver. The purpose of flow control is to prevent a fast sender from overwhelming a slow receiver by sending data faster than the receiver can process and store it. When a receiver's buffer becomes full and it cannot accept more data, it must have a way to tell the sender to pause or slow down. Without flow control, the receiver's buffer would overflow, causing data loss that would need to be detected and corrected through retransmission, reducing efficiency.
## Why Flow Control is Needed
Different devices have different processing speeds and different amounts of buffer memory. A high-speed server sending data to a slow printer might produce data much faster than the printer can process it. A fast sender transmitting to a receiver with limited buffer space might fill that buffer before the receiver has processed the already-received data. In both cases, flow control signals allow the receiver to request that the sender pause or reduce its transmission rate until the receiver has caught up.
## Stop-and-Wait Flow Control
In stop-and-wait flow control, the sender transmits one frame and then stops and waits for an acknowledgment from the receiver before sending the next frame. If the receiver sends a positive acknowledgment, the sender proceeds with the next frame. If the receiver sends a negative acknowledgment or no acknowledgment arrives within a timeout period, the sender retransmits the same frame. This simple approach ensures the receiver is never overwhelmed but is very inefficient because the sender spends most of its time idle waiting for acknowledgments.
## Sliding Window Flow Control
Sliding window flow control allows the sender to transmit multiple frames before waiting for acknowledgments, up to a maximum number called the window size. The sender maintains a window of frames that have been sent but not yet acknowledged. As acknowledgments arrive, the window slides forward and new frames can be sent. The receiver controls how fast the sender transmits by advertising its available buffer space, called the receive window. If the receive window shrinks to zero, the sender stops transmitting until the receiver sends an updated window size. TCP implements sliding window flow control to manage the rate of data transfer.
## Buffer Management
Flow control is closely related to buffer management at the receiver. The receiver allocates buffer space to hold received data until the application processes it. The receiver advertises the amount of available buffer space to the sender. As the application consumes data from the buffer, more buffer space becomes available and the receiver can advertise a larger window to the sender, allowing it to send more data. If the application is slow to consume data, the buffer fills up and the receive window shrinks, causing the sender to slow down.Back to Subject