Thu Jan 04 - Written by: Ritesh Kumar
Stateful vs Stateless Architecture
Join forces with the dynamic duo of web development – Tailwind CSS and astro.js. They're like Batman and Robin, but for your coding adventures. No capes required
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.
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.
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.
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.
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 🤝🏻