DEV Community

Cover image for Stateful vs Stateless Architecture
Ritesh Kumar
Ritesh Kumar

Posted on

Stateful vs Stateless Architecture

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

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

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

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

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 πŸ‘‡πŸΌ

Connect me on Twitter :- Twitter 🀝🏻

Do check out my Github for amazing projects:- Github 🀝🏻

Connect me on LinkedIn :- Linkedin 🀝🏻

Read my another article :-
Parallax In Next.js using React-Scroll-Parallax πŸ˜‰

Top comments (2)

Collapse
 
harshitaditya1 profile image
Harshit Aditya

Amazing Blog ✨. This blog is very deep and informative. Awersome work Ritesh πŸ’―.

Collapse
 
nyctonio profile image
Ritesh Kumar

Thank you Harshit