TCP is a STATEful protocol and HTTP is a STATEless protocol, why?

One question i was asked in MCA Class today by a smart guy is

Sir, I have a doubt, HTTP is based on TCP protocol and as we know TCP is a stateful protocol then why HTTP is stateless?


I answered him in HINDI in class room, but just sharing this to you here.

HTTP doesn't 'inherit' from TCP, but rather uses it for a transport. HTTP uses TCP for a stateful connection, but then disconnects. Later it will connect again, if needed. So, while you browse through a web site you create many different connections. Each one of those connections is stateful, but the conversation as a whole is not, since you are dropping the connection with every conversation.

Here is an analogy for you. Consider the phone service to be TCP and consider your relationship with distant family members to be HTTP. You will contact them with the phone service. Each call to them would be a stateful TCP connection. However, you don't constantly stay on the phone with them, as you will disconnect and call them back again at a later time. You would certainly expect them to remember what you talked about on the last call. HTTP in itself does not do that, but it is rather a function of the web server that maintains the state of the overall converstation.

The reason for this is pretty simple. Say you have a popular web server that takes millions of hits per day. Each one of those hits consists of a quick connect/disconnect. If each person using your web site maintained an open connection the whole time that they were there, it could potentially overwhelm your server. Also, there would often be conversations left open when the user has stopped looking at your web page.

All that said, I would agree that this is a bit of a misnomer. HTTP as a protocol could be considered stateful since it is using TCP (and a firewall will recognize each connection as such). However, the application (browser/web server) that is using HTTP is really not stateful.

Hope this helps you to understand the differences.

Comments

  1. Most HTTP conversation does not drop the connection on every request, but rather multiple HTTP requests are pipelined in the same connection. In HTTP 1.0, client can indicate that it wants to send multiple requests in the same connection by using Connection: Keep-Alive header, in HTTP 1.1, keep alive is the default, and client and server only drops connection when explicitly requested and on timeout.

    HTTP is not stateless because it uses new connection for each request, HTTP is stateless because every request is independent of any other requests whether they are transported through the same connection or a different connection. In other words every request contains every information that is needed to process that request. The context that actually go through the connection may just be a short token that represents the full context on the server, but this token is attached to every single requests. On the other hand, a stateful protocol would associate a context with the connection itself, and requests in a stateful protocol are incomplete fragments that can only be fully understood in the context of the connection.

    As a real life analogy, a stateless communication is like radio conversation between pilot and ground controllers, where you're expected to always state your callsign and usually the callsign of the message destination with every spurt of messages. On the other hand, a stateful protocol is like a telephone conversation, where you rely that the person on the other side to remember who you are.

    ReplyDelete
  2. other than TCP, Which protocols are belongs to State-full Protocol

    ReplyDelete

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System