TCP (Transmission Control Protocol) enables standardized communication between applications and devices such as web servers. As one of the most widely-used protocols today, TCP helps deliver data segments across the network quickly and reliably with built-in error correction. This enables clients and servers to communicate with each other with no packet loss. Because of this, TCP is considered a connection-oriented protocol. 

Created by the United States Department of Defense in 1974 during the internet’s ARPANET days, TCP quickly became the de facto communication protocol after the Network Control Protocol (NCP) was deprecated. It has since continually evolved to meet evolving web standards. 

While it predates the Open Systems Interconnection (OSI) model, TCP is considered a transport layer (Layer 4) protocol. It sits deeper in the stack than a protocol such as HTTP, which instead works at the application layer (Layer 7) to support user interactions across services on the network.

What makes TCP useful?

TCP is a transport layer protocol and therefore excels at successfully moving data from Point A to Point B. As mentioned earlier, TCP provides applications—and users—with the information they need while supporting better user experiences through error handling. However, the TCP protocol offers other key benefits: 

  • Quick and reliable end-to-end data delivery

  • Data is transported in segments, preventing the entire payload from being resent in the event of an error

  • Flow control

  • Widespread compatibility with a number of application types, devices, and network infrastructure components (routers, switches, etc.)

If reliability and compatibility are your chief concerns, then TCP reigns supreme. However, TCP isn't a perfect protocol and does come with some drawbacks—most notably its synchronous processing. If one packet from a data stream is delayed, all other packets afterwards are forced to wait until the problem packet arrives. TCP therefore provides better error handling at the cost of an occasional performance hit in the form of latency.

How does TCP work?

The following process occurs when TCP is used to transmit data: 

  1. TCP opens a connection between the data source and its destination. This is often between two or more applications and devices. 

  2. The TCP protocol receives a data payload then organizes it into individual segments of varying size—each up to 65K. However, packets that large are relatively uncommon. 

  3. This data is sent onwards to its destination, sometimes using a mechanism called TCP tunneling to permeate NAT firewalls that safeguard sensitive networks. 

  4. The data segments are received and reassembled to form a coherent, machine-readable message. 

  5. Assuming that data keeps flowing, the TCP connection will remain open for continued use. 

  6. Once the data stream ends, the TCP protocol will send a FIN/ACK message from one party to the other. The connection will automatically close once an acknowledgement is returned. 

  7. If any data packets encounter errors caught by failed TCP checksums and Cyclic Redundancy Checks (CRCs)—which work together—then the protocol will send that packet again until it arrives as expected.

TCP relies on a three-stage handshake to open, maintain, and close a connection. This uses three separate messages to accomplish this: 

  1. The client sends a SYN flag in the TCP header to the server. This starts with an arbitrary number sequence that helps uniquely identify each open connection to avoid interference or confusion. 

  2. The server sends back its own SYN packet and acknowledges (ACK) the initial request. 

  3. The client sends back an ACK with a value that increases with each successive message until the data stream ends. 

  4. Once the data stream ends, the connection itself will close. The process may then start over if more data needs to be transmitted.

haproxy-tcp-handshake-process-diagram

Since TCP sits a layer deeper within the OSI model, its functions are abstracted even further from the user. For example, HTTP requests can generate status codes to indicate errors (or successes), while TCP does not. The idea is that TCP will offload the burden of managing data delivery issues by handling it automatically—instead of asking users to diagnose or troubleshoot.

Does HAProxy support TCP?

Yes! HAProxy products process incoming network traffic for most applications—a massive portion of which rely on TCP. HAProxy Enterprise can act as a TCP proxy, offering a dedicated operational mode for TCP while carrying any higher-level protocol such as HTTP or FTP with it. 

To learn more about TCP support in HAProxy, check out our brief TCP configuration tutorial or our blog post, Your Comprehensive Guide to HAProxy Protocol Support.