Skip to content

TCP Protocol

Video: TCP vs UDP Comparison | Cisco CCNA 200-301

The Transmission Control Protocol (TCP) is a sophisticated transport protocol that builds reliable communication on top of the simple UDP foundation. TCP demonstrates how to design robust systems by adding layers of reliability, flow control, and congestion management to an unreliable base.

Our aim in this article is to build intuition on how to design robust protocols and programs on top of simple assumptions.

The Problem: Building Reliability on Unreliability

UDP provides a simple, fast transport mechanism but has significant limitations for applications that need reliability. Let's examine the problems and design solutions.

Problem 1: What if we want guaranteed delivery?

The Challenge: UDP packets can be lost due to network congestion, hardware failures, or routing issues. Applications like file transfer, email, or database transactions cannot tolerate data loss. The Solution: Reliable Connection and Acknowledgments

To achieve guaranteed delivery, we need to build a reliable connection on top of UDP. This requires several components:

1. Connection Establishment (Three-Way Handshake) Before we can send data reliably, both sides need to establish a connection and agree on initial parameters. This requires:

  • State Management: Each side maintains connection state (CLOSED, LISTEN, SYN_SENT, SYN_RECEIVED, ESTABLISHED)
  • Synchronization: Ensure both sides are ready to communicate
  • Parameter Negotiation: Exchange capabilities and preferences

2. Acknowledgment System Once connected, we implement a reliable delivery mechanism:

  • Stateful Tracking: Sender maintains a list of sent packets and their acknowledgment status
  • Timeout Mechanisms: Detect lost packets through timeouts
  • Retransmission: Automatically resend lost packets when timeouts occur
  • Incremental Acknowledgments: Receiver can acknowledge multiple packets efficiently

What do we add to UDP for this?

We need to send some additional packets at the moment the connection needs to be established with the receiver. The receiver should also acknolegde that it has received the connection establishment request and is ready to receive data. Finally a third packet has to be sent by the sender to acknowledge that it has received the acknowledgment for the connection establishment request.

This is called the three way handshake protocol implementation. It ensures that both the sender and the receiver are in a ready state to communicate with each other and from thereon, they can send and receive data. The sender can implement timeouts and retransmission if the connection is broken.

Problem 2: What if we want ordered delivery?

The Challenge: UDP packets can arrive out of order due to different network paths or retransmissions. Applications need data in the correct sequence. The Solution: Sequence Numbers and Buffering

Since we need to ensure order, we can attach an integral serial number (called a sequence number) to each packet. This way, if the receiver receives packets in an unexpected sequence, it can detect that and request the sender to retransmit the missing packets. The receiver now needs to:

  • Track Expected Sequence: Maintain the next expected sequence number
  • Buffer Out-of-Order Packets: Store packets that arrive before their turn and only deliver it to the application when the older packets have been delivered
  • Handle Duplicates: Ignore packets with sequence numbers already processed
  • Receipt of packets: Only confirm that a packet has been received when all earlier packets have also been received.

What do we add to UDP for this?

The header will now contain the sequence number that needs to be attached to each packet. The receiver can inform the sender of the missing packets so that the sender can retransmit them.

Sequence numbers are helpful in detecting missing packets. Maybe you want to implement just sequence numbers on top of UDP without reliability requirements to ensure that you are receiving all the packets in the correct sequence. This can be important for applications where ordering is important, but packet loss is acceptable.

Problem 3: What if we want flow control?

The Challenge: A fast sender can overwhelm a slow receiver, causing buffer overflow and packet loss. We need a mechanism for the receiver to control the sender's transmission rate. The Solution: Sliding Window Protocol

Flow control prevents the sender from overwhelming the receiver by:

  • Window-Based Transmission: Sender can limit the number of unacknowledged packets
  • Buffer Monitoring: Receiver tracks its available buffer space
  • Dynamic Window Adjustment: Receiver tells sender how much data it can accept
  • Backpressure Mechanism: Receiver controls sender's transmission rate by telling the sender to reduce its window size when needed.

What do we add to UDP for this?

  • Sliding window mechanism on sender side to track unacknowledged packets
  • Buffer space tracking on receiver side to ensure that it has space for future incoming packets
  • The receiver can advertize its available buffer space to sender during connection establishment handshake.
  • The sender can pace itself by tracking if the number of unacknowledged packets exceeds the receiver's advertised window size. This helps prevent buffer bloating in case the sender is faster than the receiver.

Problem 4: What if we want congestion control?

The Challenge: Multiple senders can overwhelm the network and send more data than the wires can physically handle, causing packet loss and poor performance for everyone. We need a mechanism to detect and respond to network congestion. The Solution: Congestion Detection and Response

Congestion control prevents network overload by:

  • Congestion Detection: Use timeouts and duplicate acknowledgments to detect network problems. If acknowledgements are not coming in time and the number of packets in flight are increasing, the sender can decide to pace itself by reducing the number of packets in flight
  • Adaptive Window Sizing: Reduce transmission rate when congestion is detected
  • Gradual Recovery: Increase transmission rate slowly when network conditions improve and acknowledgments are received on time
  • Fairness: Ensure all connections get fair access to network resources

What do we add to UDP for this?

This can be strictly controlled by the sender and doesn't require any mechanism on the receiver side. The sender can dynamically adjust its window size based on the number of acknowledgments received on time and the number of packets in flight. This actually ensures that the network connection is fairly used by everyone else.

Building Your Own Reliable Protocol

The key insight from TCP is that you can build sophisticated, reliable protocols on top of simple, unreliable foundations. Here are the essential building blocks:

1. Reliability Layer

  • Sequence numbers for packet identification
  • Acknowledgments for delivery confirmation
  • Timeouts for loss detection
  • Retransmission for error recovery

2. Ordering Layer

  • Buffering for out-of-order packets
  • Sequence number tracking
  • In-order delivery guarantees

3. Flow Control Layer

  • Window-based transmission control
  • Buffer monitoring
  • Backpressure mechanisms

4. Congestion Control Layer

  • Network state detection
  • Adaptive transmission rates
  • Fairness mechanisms

5. Connection Management Layer

  • State machines for connection lifecycle
  • Parameter negotiation
  • Synchronization protocols

Example: Custom Reliable Protocol

When building your own reliable protocol, you can combine these layers as needed:

  • Basic Reliability: Just add sequence numbers and acknowledgments
  • Ordered Delivery: Add buffering and in-order processing
  • Flow Control: Add window management and buffer monitoring
  • Congestion Control: Add adaptive transmission rates
  • Connection Management: Add state machines and parameter negotiation

Design Principles

1. Layered Design

Build complex systems by layering simple, focused components. Each layer solves one specific problem.

2. State Management

Maintain clear state machines for connection lifecycle and protocol behavior.

3. Adaptive Behavior

Design systems that can adapt to changing conditions (network congestion, receiver capacity).

4. Error Recovery

Always plan for failure. Implement mechanisms to detect and recover from errors.

5. Performance Optimization

Balance reliability with performance. Use techniques like sliding windows and congestion control.

6. Fairness

Consider the impact of your protocol on other users of the shared resource (network).

TCP demonstrates how to build a robust, reliable system by systematically addressing the limitations of a simple foundation. The same principles can be applied to any system design problem where you need to build reliability on top of unreliable components.

Questions

Q: What is the main problem that TCP solves over UDP?

TCP solves the reliability problem over UDP. It provides guaranteed delivery, ordered delivery, and error recovery mechanisms that UDP lacks.

Q: What is the purpose of the three-way handshake in TCP?

The three-way handshake establishes a reliable connection and allows both sides to agree on initial parameters like sequence numbers before data transfer begins.

Q: What is the purpose of sequence numbers in TCP?

Sequence numbers track packet order and detect missing packets. They enable TCP to provide ordered delivery and retransmit lost packets.

Q: What is flow control in TCP?

Flow control prevents the sender from overwhelming the receiver by allowing the receiver to control the sender's transmission rate through window size advertisements.

Q: What is congestion control in TCP?

Congestion control prevents network overload by adapting transmission rates based on network conditions. It uses algorithms like slow start and congestion avoidance.

Q: What is the sliding window protocol used for?

The sliding window protocol implements flow control and allows multiple unacknowledged packets to be in flight, improving throughput while preventing receiver overload.

Q: What is the key insight from TCP's design?

The key insight from TCP's design is that you can build sophisticated, reliable protocols on top of simple, unreliable foundations by systematically addressing limitations.

Q: What is the purpose of acknowledgments in TCP?

Acknowledgments confirm that packets have been received and enable TCP to retransmit lost packets when acknowledgments are not received within a timeout period.