HTTP Persistent Connections
Jun 14, 2019
This was always a point of confusion for me, so I imagine there are others out there who could use some clarification on this too.
A socket is 1 end of a connection. A lone socket doesn’t do much. I like to think of a connection as a tuple of sockets.
Sockets operate at the top of the transport layer. They are the API through which you can use TCP or another transport layer protocol.
A transport layer protocol is one that allows us to send data from a process running on host A to a process running on host B. Consider that in contrast to a network layer protocol, which only works at the host level—sending data from host A to host B.
TCP, combined with IP (Internet Protocol, a network layer protocol) make the foundation of the internet. Furthermore, TCP is connection-oriented, meaning it first establishes a connection, and only then proceeds to send data.
You navigate to Wikipedia. What is the browser doing? It is…
- Discovering the IP address of the domain name
- (read this slowly…) Establishing a TCP connection (using the 3 way handshake) with a process on the host corresponding to that IP address, particularly the process bound to port 80, the default for HTTP traffic
- Sending something resembling
GET www.wikipedia.com/as an HTTP request
- Killing the TCP connection once the request is complete.
Note that we are ignoring SSL here.
With a **persistent connection**, we just tell Wikipedia via the Connection header to not kill the TCP connection for some longer length of time, say a minute. This reduces strain on the server, and results in the faster loading of a page.
However, it’s important to note that HTTP in and of itself is a connectionless protocol. The way I think about this is that each HTTP request is self-contained. You can text it to your friend, and they can open it on their smartphone, and they will get the exact same result (most of the time, yes there are exceptions).
Yes, HTTP does use TCP under the hood, which is connection-oriented, but that is as far as connections go with HTTP.
We’ll save WebSockets for another day.