HTTP Persistent Connections

Alex Woods

Alex Woods

June 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.

Sockets

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.

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.

TCP

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.

A man stumbles onto the web, and navigates to a website…

You navigate to Wikipedia. What is the browser doing? It is…

  1. Discovering the IP address of the domain name www.wikipedia.com
  2. (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
  3. Sending something resembling GET www.wikipedia.com/ as an HTTP request
  4. 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.

Connection: keep-alive

But, HTTP is connectionless

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.

Want to know when I write a new article?

Get new posts in your inbox