Stateful vs Stateless Architecture
Tags: webdev, discuss, devops, computerscience
Published: 2022-06-19 20:50:45

Hey everyone today I want to discuss something about Stateful vs Stateless Architecture ok 🤔 so.. if I had to simplify these terms I would say that stateless is something which can survive a system restart. But its not that simple so lets dive deeper and try to learn the thing with an example 😃.

Lets consider that there is a person who wants to order something from an Ecommerce website. So the first thing that he will do is obviously singing in to that website with his web browser and as we all know that his or her browser will send a http request with some username password stuffs to the server of that Ecommerce site. But usually there is a something called as a load balancer sits in front of those servers and decides to send that request to 
any one of the available servers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izwrif62af7f1emx47l0.png)
 
lets consider that it send the request to server1 and then server1 requested the database to check if the username password is correct or not and after that it authenticates the user that yep, you are logged in. and the server stores a session of that user that he is logged in.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sk3vhkjbehz2nrshgsr2.png)
 
Now our user wants to add something to his cart and again his  browser send that http request to the website but this time the load balancer decides to send this request to our server2 but as we know that there is no session for our user in the server2 so server2 responded with 403 (access denied) 😔 and asked our user to login again very frustrating right yep.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2otfas8umpcyw0oco5l.png)

so that was the example of a stateful application but now lets change the situation when our user got authenticated by the server1 the server1 now did not stored a session of that user instead it returned a cookie to that user which our server again can use for the verification that the user was signed in or not obviously that cookie will have some expiration time.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfkohh2m5g08r54bqgmb.png)
 

And now in the case when our user will send another http request for adding something to his cart this time he will also send this cookie with the request so that if the load balancer send this request to any of the available servers now those servers can authorize the user and will not ask the user to authenticate again. So that pretty much it but still its not that easy that which one is better both have there own advantages and disadvantages.

I also explained it on youtube you can also check it out 👇🏼

{% youtube 3XQnymcbjds %}

Connect me on Twitter :- [Twitter 🤝🏻](https://twitter.com/nyctonio)

Do check out my Github for amazing projects:- [Github 🤝🏻](https://github.com/nyctonio/)

Connect me on LinkedIn :- [Linkedin 🤝🏻](https://www.linkedin.com/in/nyctonio)

Read my another article :-
[Parallax In Next.js using React-Scroll-Parallax 😉](https://dev.to/nyctonio/parallax-in-nextjs-using-react-scroll-parallax-2110)