Being Stateful and Stateless

Being Stateful and Stateless – A Design Principle

Most of us would have came across the terms Stateful and Stateless. More than being term, they act as a design principles for various implementations. This is a series of posts where I’ll be discussing about them in detail.

What is a state?

In general, a state is a data or a collection of data about an entity. It is collected by subsequently running processes. This state is used to monitor current condition and/or track the data changes made to the entity. Manipulations can be done to keep the state updated with the current condition(s) of the entity. The state of an entity can be used or manipulated by any subsequent process which has access to it.

That was a self-made definition, hope it was good enough 😅

What is being stateful or stateless?

Being Stateful: A process being stateful means that the it has a track of all changes or interactions that happened in the past. The current process is performed with a context of previous processes.

Being Stateless: A process being stateless means it doesn’t keep track of the changes or interactions made in the past. Each request or interaction is considered to be new.

For example, let us assume that a guy named John is interacting with an AI chat bot Mark to book a movie ticket. Their conversation goes as shown below.

stateful-stateless-example-scenario
Now lets assume that Mark has booked a ticket, John went to the movie, comes back and interacts with Mark again.

This time, if Mark is stateful, this is how he would behave
Stateful Mark will respond to John's greeting by asking whether he enjoyed the movie.
On the other hand, this is how a stateless Mark would behave
Stateless Mark will respond to John's greeting with a reply greeting. He wouldn't know who John is or John went for a movie

Whats the difference here?

The stateful Mark maintained the state (here, their conversation history, ticket booking history, user details, etc). When John came back and interacted again, Mark processed the state information to generate a response based on their previous interactions. Mark might even get further details about John’s liking to the movie to add more information to the state for future conversations.

Meanwhile, the stateless Mark doesn’t have a clue on what John was up to. Stateless Mark might not even know that he booked a movie ticket for John. Stateless Mark might not even know who John is. He doesn’t have to care to track these information as he was designed to be stateless.


For further understanding here are few more examples.

If you are reading this from your computer right now, you must have logged in as a system user. Hence, your computer now is stateful.

It knows,

  • Who you are,
  • What resources should be made accessible to you,
  • Recent files that you opened,
  • What theme should be loaded to you as per your configuration,
  • Which settings should be applied for you, etc.

All these components are few of the states that are maintained by your computer.

This blog post you are reading right now is stateless. This blog doesn’t need to maintain any of it’s reader’s information and doesn’t care who is reading it. It serves the page content to any visitors who is visiting the page.

Implementations

Various areas of software development follow the stateful and stateless principles. Although there are various implementations, I’d be discussing few of them.

They are also applied in Micro Service architectures. Flutter’s stateful and stateless widgets are also implemented based on this principle.


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 “Being Stateful and Stateless – A Design Principle”

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

Comments are closed.

error

Enjoy this blog? Please spread the word :)