Skip to main content

Side by side

TCPvsHTTP

What is the difference between TCP and HTTP?

Updated 2 min read7 differences

In short

TCP delivers a reliable stream of bytes between two programs; HTTP runs on top of it and gives those bytes meaning as requests and responses.

TCP

Transmission Control Protocol

TCP is a core internet protocol that delivers data between two programs reliably and in order, by opening a connection and resending anything that gets lost.

Read the page on TCP

HTTP

Hypertext Transfer Protocol

HTTP is the protocol that browsers, apps, and servers use to exchange web pages and data through a simple cycle of requests and responses.

Read the page on HTTP

TCP and HTTP compared

AspectTCPHTTP
LayerTransport layer (layer 4)Application layer (layer 7)
JobDeliver bytes reliably and in orderDefine requests and responses for web resources
Unit of dataA stream of bytes, sent as segmentsMessages: a start line, headers and an optional body
Knows aboutPorts, sequence numbers and acknowledgmentsMethods, URLs, status codes and headers
Connection setupA three-way handshake: SYN, SYN-ACK, ACKNone of its own; it uses the transport's connection
Runs onIPTCP for HTTP/1.1 and HTTP/2; QUIC over UDP for HTTP/3
Also carriesSSH, email, database protocols and HTTPWeb pages, REST APIs and file downloads

The difference, explained

TCP and HTTP work at different layers, so they are partners rather than alternatives. TCP, the Transmission Control Protocol, is a transport protocol: it opens a connection between two programs and guarantees that bytes arrive complete and in order, resending anything lost. HTTP, the Hypertext Transfer Protocol, is an application protocol: it defines what those bytes mean, as requests with methods and URLs and responses with status codes, headers and bodies.

TCP knows nothing about web pages, and HTTP doesn't deal with lost packets. When a browser loads a page over HTTP/1.1 or HTTP/2, it first opens a TCP connection to the server, usually on port 443 with TLS on top, and then sends HTTP requests through it. One TCP connection can carry many HTTP requests, one after another or, with HTTP/2, interleaved.

A good analogy is a phone line and a conversation. TCP is the line: it connects two parties and makes sure every word gets through in order. HTTP is the etiquette of the conversation: one side asks in an agreed format, and the other answers with a status and the content. Many other protocols use TCP the same way, including SSH, SMTP for email and most database connections.

A common misconception is that HTTP always runs on TCP. HTTP/3 runs over QUIC, a protocol built on UDP that provides its own reliability and encryption, while keeping the same methods, headers and status codes. And few developers program TCP directly: most meet it through HTTP clients, database drivers and other libraries that open the connections for them.

Which one should you use?

Choose TCP when…

  • You are designing your own protocol, such as for a game server or a device link.
  • You need a reliable byte stream without HTTP's headers and request structure.
  • You implement or debug a protocol that already runs on TCP, such as a database or mail protocol.

Choose HTTP when…

  • You build a website, a web API or anything a browser must reach.
  • You want standard methods, status codes, caching and authentication.
  • Proxies, load balancers and firewalls should understand the traffic.

Talking raw TCP vs making an HTTP request (Python)

TCPpython
# TCP: a reliable byte stream; what the bytes mean is up to you
import socket

# Talk to a Redis server, which uses its own text protocol over TCP
with socket.create_connection(("localhost", 6379)) as sock:
    sock.sendall(b"PING\r\n")
    print(sock.recv(64))  # b'+PONG\r\n'
HTTPpython
# HTTP: requests and responses with methods, headers and status codes
import requests

response = requests.get(
    "https://api.example.com/users/42",
    headers={"Accept": "application/json"},
)
print(response.status_code)              # 200
print(response.headers["Content-Type"])  # application/json
print(response.json()["name"])           # Ada

Readers ask

Does HTTP use TCP or UDP?

HTTP/1.1 and HTTP/2 use TCP. HTTP/3 uses QUIC, which runs over UDP and adds its own reliable delivery and encryption.

Is HTTP faster than TCP?

The question doesn't quite apply: HTTP is carried by TCP, so it adds headers and parsing on top. A custom protocol on raw TCP can be leaner, but it loses the tooling, caching and compatibility that HTTP brings.

Which port does HTTP use?

By default, port 80 for plain HTTP and port 443 for HTTPS. These are TCP ports, except with HTTP/3, which uses UDP port 443.

More

Settings