what's up y'all this is Hussein Nasser from IGeometry where we discussed
software engineering by example and today's topic is stateful versus
stateless architectural applications so in this video we're gonna quickly
describe an example of a stateful application and advantage or
disadvantage of those and an example of another stateless application and again
stay advantages/disadvantages about so yeah for this example we're gonna take
an authentication example choose that's the that's the easiest thing I could
think of really so basically you would go to your server authenticate yourself
username password login and then view your profile very simple stuff okay it's
really really simple so that's the idea I'm building application that actually
authenticate me and then view is my profile it does more obviously but this
is just for the sake of that example here all right so let's explore the
state full version here I have a server here I know you already written my
application so my application has two pages or two services you can think of
it's called login where basically I visit this page to log in into my
application and to visit this page I would basically provide so I didn't
actually do that so I would provide server one which is basically I say it's
hosted on HTTP so server 1 slash login and then I'm gonna post they use their
name and password so usually you're gonna encrypt this but I'm gonna use get
here so Bob and password is just for the sake of example Bob one right ease is
it's a very bad pass or book yeah you get the idea guys all right yeah so now
I just initiated this request Bob initiated this request right and
then we're gonna visit the page login so what Bob will do is like we're gonna
take the username password and there is some code which will basically take the
username password queers that had a base verify that this guy is who he says he
is and then after we get this piece of information that okay username password
is cool so what are we gonna do in this version of the stateful application head
we're gonna sit a session variable or a global variable and my server which is
server one here so saying that Bob logged version equal true so I just said
same variable here and the server itself changing so think of it as a state so
one moment he was not logged in and then the other he's logged in so now what Bob
does is cool now I can I think view my profile so what he does is literally
views profile he doesn't send anything else just view profile literally just
try to view the profile page and since we go to the profile page and go execute
that script and says is Bob really logged in yes he is because we
remembered this piece information on the server so I know that Bob is logged in
usually what you gonna do is some sort of an array or a session variable that
tells me that hey this user is now logged in using this credential right so
now yeah and we gonna get the view profile and it will be successful so
what is the advantages of this so the first call I've made to login I query
the database I did that and I store that state into my server so next time Bob
calls or make a call to view profile I do not need to query the database thus
during the state right saving me a trouble of querying the database twice
cool alright so this is the stateful architecture or stateful application so
where did where does this break now remember this variable this session
variable stored in the machine so it's in Rena RAM it's in the memory right
it's in an instance so if I literally do that if now Bob tries to access the same
application but from another server he will fail and the reason is because that
server is has not does not have that session variable stored right because we
saved it here we logged in here we changed it here but if you logged in to
the other machine it is not it is not does not remember Bob right it does not
we did not authenticate Bob on the other machine so in this case what happened is
we could not scale horizontally we could not copy and just spin up another
machine and make sure that our code works so in this case Bob has to call
the server to log in and then and provide a username password right so
some of you will say okay I don't usually type in server one or server so
I just type them the site name right so usually what how this works is there is
another machine here in the middle called load balancer and what what it
does it just balances the load if I can rotate that yeah cool that's I can
rotate that so yeah what that's what it does right so usually this is how it
works so Bob does not know about server one server
- Bob knows about LD or machine one right let's go to LV so so Bob makes a
call to Logan lb right so let's go through the same example the stateful
example here with load balancers where it breaks so Bob makes a call log in
provide the obviously the user password goes to lb load balancer just randomly
assigns a machine to that - to Bob to the client in this case and he picks up
server 1 and we do the login and we set the session variable we query the
database and then we returned Bobby hey you cool baobob you can now make a view
profile so Bob nicely make a call to the view profile but now if if he was lucky
and they'll burn sort of talk him to the same machine server one you'll be good
because the logged variable is sat right so that would be okay but if he made it
if the load bus had decided to take him to server to the call will fail because
he is not logged in on server two and that's why I imagine like you would go
into facebook.com and you go to profile and then you refresh the page and you
have to log in again that's just just just that's not really user friendly is
it right so that's the idea of the stateful application few build State for
application you do not scale but you save then and took some calls to the
database so you store the state of that so it depends if you work with a man one
machine you you can't build a stateful application it's not bad right
okay let's take a state less example here by though I'm using a tool a cool
tool called Gliffy you can saw so from there all right so let's take Alice here
Alice in the other end has built or is using a rest for example
representational state transfer and let's let's let's focus on the state
transfer it's very important right right focus on the state transfer we're gonna
talk about it in a minute so yeah so there's a little bit add
machine lb and I'm lazy so I'm gonna copy these puppies here feel like in
coping base of it oh I love collecting man this is really cool alright so yeah
Alice same thing right Alice makes a call says hey lb lug may log me in there
was a tool called load me in back in the 90s I used to use it come on mate come
on mate okay lb log me and so what this application is oh we forgot our database
man we forgot our database so the database is connected obviously on the
two servers here what happens here what happens with a stateless user pass right
all right so yeah alice makes a call user pass lb it takes it to the server
one the application here is written slightly differently write it slightly
different so how this makes a call to login lb takes it to server one doesn't
matter really here in the stateless application now login actually what it
does it it does not still a store any state right what it does I say okay user
passed let me query the database Ellis is lit
alright she's cool let her in but but let me do one thing let me send Alice a
talkin so she can't communicate with us using this token right look at that
mmm oh man this is just awful all right so the server sends back a token it's
usually a very long ass you know the token ID right and you can use that
and now what happens is here now Alice has a token was a very long token right
and this Alice with each request is responsible to send the token with her
that is cool what will happen now if I do view
profile remember she can't just do that she has to send in a Tolkien now right
and their token is ABC whatever all right cool
so she got the tokens he's sending the token now lb will decide to go take her
to server 1 now the view profile function or the script or the service
will take the token and guess what it has to query the database to verify that
the token is cool right it doesn't have to be the same database it could be
another intermediate database right in memory database something like that but
it's it is a persistent store right usually tokens has also some expiration
so you cannot use this talking forever you can use it hey use this for 30
minutes and then sorry you have to request another token so your profile
make a query to the database make that trip make that cost right we are
incurring an extra cost but what do we get here if I view report from server
one I'm cool because I can view the profile if I the load balancer hit
another server I'm also cool right because because the
token will always forget authenticate in each servers the same thing we're gonna
head the database eventually right it is like as if I am real fanta gating
myself with each coal but it says it's a it's a
soft authentic it does that make sense guys alright so this was like a quick
explanation between stateless and stateful so stateful you get less trips
to the database so it's kind I would say faster the backend maybe right but you
can also scale it as easily right horizontally but with the stateless
region example rest right we are transferring the state from the client
to the server does that make sense transfer right token and then bring it
back right so we can scale beautifully here but there is always a cost at the
back end of making extra calls to the database to re-authenticate or to re
pull this information Ram that's why with the stateless you'll you'll heard
about the cache is a level of caching there's a lot of caching going on there
but you have to do it in a very smart man hope you guys enjoyed this episode
please subscribe if you like this video where we explain a lot of concept in
software engineering here and we're gonna see you in the next one
have a great day