Let's say we have developed an application. For the sake of simplicity, let's assume that we have only two pages, login page and user profile page.
We should display the profile of the user only when they are logged in. Let's see how stateful application and stateless application handles this.
If we choose to use stateful architecture, we will have to store the state of the user(s) somehow. Usually the state is stored in the server itself.
But what if the server goes down? Since we store the state in the memory of the server, we could possibly lose all the stored states. It's hard to restore the state.
On the upside, we don't have to query the database everytime we receive a request, to check if the user is logged in or not.
What if we decide to have more than one server?, Let's say the user logs in by sending a request to server-1 and the state is stored in server-1. Now the user is trying to access their profile page, due to high traffic the request is directed to server-2. Remember, the state is stored in the memory of server-1 only, so according to server-2, the user is not logged in. This might lead to inconsistency. Hence, stateful application might not go well with horizontal scaling.
No need to query the database.
It is quick, as we don't have to query the database everytime.
If the server goes down, it will be hard to recover.
We can't scale up horizontally.
In case of a stateless architecture, a token is sent back to the client, when the user logs in. This token is stored in client's storage. Then this token is sent along with every request the user makes.
Inorder to make sure that the request is coming from an authenticated user, we check the token sent along with the request, against the data we stored in the database.
Since the token is stored in client's storage, we can easily recover if the server goes down. And horizontal scaling is much easier in stateless architecture.
But the catch here is, we have to query the database everytime we get a request.
Horizontal scaling is easy.
We can easily recover if our sever goes down.
We have to query our database, for every request.
Slower than stateful architecture.
I hope this article helped you. Want to connect with me? my DM is open on dev.to, you can connect with me on twitter