Why does SSH feel so much faster than HTTP

httpperformancessh

If I ssh into a server, the time between say, issuing the ls command and getting the result is almost nothing.

If however I issue an http request for a simple html page (containing only the word 'hello') on the same server in the browser, it takes at least a second.

Why is this?

Best Answer

I don't know that much about how SSH works, but from what I understand it basically takes your keyboard input and pipes it into the shell of a remote console. Pretty light-weight once the connection has been established and keys exchanged.

HTTP is a totally different protocol. It works a bit like this, assuming it's a just a static HTML page:

  • Establish TCP/IP session (this could involve DNS lookups)
  • Send HTTP headers
  • Send HTTP request body (GET, POST, PUT, etc)
  • Web server receives HTTP request
  • Web server checks its handler mappings to figure out how to process the request
  • Web server picks up the static HTML file
  • Web server formulates the response headers and sends them down the wire
  • Web server pushes the static HTML file down the wire
  • Your browser receives the headers
  • Your web browser receives the response
  • Your web browser waits for the connection to be closed
  • Your web browser then parses the HTML
  • Your web browser then renders the HTML to the monitor

That's a really basic overview, but there's a lot more that goes on in HTTP. It's not a "light-weight" protocol, unlike SSH.

Also, unlike SSH, most HTTP requests are self-contained and your TCP connections re-established. SSH keeps your TCP session open, meaning that every time you press a key, you don't have to re-establish the connection. That's why your web browser can survive being unplugged for a few minutes, but the moment you lose your connection your SSH session drops.

Related Question