Stateful and Stateless models in Protocols

Stateful and Stateless Protocols

This is the 3rd part of the 4 part series explaining application of stateful and stateless principles. In this post, I’m going to discuss on how stateful and stateless principles are applied in network protocols. The previous two posts can be found below,

  1. Being stateful and stateless
  2. Stateful and stateless programs

If you haven’t read those yet, I suggest you have a quick read for a better context.

We all know what are network protocols. For those who are not familiar with it, here is a common definition.
A network protocol is a set of rules that should be followed by a network to format, transmit and receive data over the network.
You can also find lots of online sources explaining it. May be I should write one of my own someday 🤔. Anyway, there are many protocols out there but for this post, I’ll be discussing only the below ones.

TCP

The Transmission Control Protocol (TCP) is at the transport layer of OSI model. TCP sits above the Internet Protocol (IP). TCP is a stateful protocol as it maintains the state of data transmissions.

What does TCP do?

Imagine you are sending data using TCP. TCP opens up a connection, split the data into packets, adds a sequence number to each packet, sends the packets through the connection and waits for acknowledgment. If any packet is missed, TCP will resend the packet. This makes it more reliable as data loss is minimized and error recovery is provided.

What makes it stateful?

To do what it does, TCP should keep track of things like number packets to be transferred, order of the packets and whether the receiver has received a packet or not. These information are stored as state in TCP.

UDP

User Datagram Protocol (UDP) is also located in the transport layer of OSI model along with TCP. But unlike TCP, UDP is stateless. This is because, UDP doesn’t wait for a connection, doesn’t have error recovery and do not re-transmit failed packets. It doesn’t require a state to maintain track of the transmission. It just sends the packet and forgets about it.

FTP

The File Transfer Protocol (FTP) is at application layer of OSI model. It works over TCP/IP protocols. FTP maintains data of current user session, current working directory, etc. which makes it a stateful protocol.

SMTP

Simple Mail Transfer Protocol (SMTP) is the protocol used by internet systems to transfer mail from one another. This is at the application layer of the OSI model along with FTP and works over TCP/IP protocols. When a user sends mail using SMTP, the client will initiate a request to the server. Once the connection is established, it sends over the email content. The server will respond back to the client whether the mail has been successfully sent or not. If it is unsuccessful, the client might retry or fail depending on the error acknowledgement sent by the server. This makes SMTP a stateful protocol.

HTTP

HyperText Transfer Protocol (HTTP) is at the application layer of OSI model along with FTP and SMTP. This protocol works over TCP/IP protocols but unlike TCP, HTTP is a stateless protocol.

But how?
HTTP uses TCP to open up a connection. HTTP uses this connection to send and retrieve data. More than one HTTP requests can be pipelined over a single TCP connection. Each HTTP request contains enough data required to process that request and is independent from other requests, i.e., doesn’t rely upon data from any other HTTP requests. This makes HTTP a stateless protocol.

The above picture is an attempt to represent how HTTP is stateless. The yellow area represents TCP connections and the green bars are HTTP requests that are pipelined in a connection. You can see that each HTTP request is independent from the other to complete it’s request.

Whats next?

HTTP is a stateless protocol but the servers which use HTTP can be modeled to be stateful. 🤔 How? Find out how in the next post.


I hope this post was helpful 😊. If you find this post informative, support us by sharing this with fellow programmers in your circle 😀.

For any suggestions, improvements, reviews or if you like us to cover a specific topic, please leave a comment.
Follow us on twitter @thegeeksclan and in Facebook.
#TheGeeksClan #DevCommunity

1 thought on “Stateful and Stateless Protocols”

  1. Pingback: Stateful and stateless programs - The Geeks Clan

Comments are closed.

error

Enjoy this blog? Please spread the word :)