Stateful architecture Vs stateless architecture

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 applicat…


This content originally appeared on DEV Community and was authored by Saran Chakravarthi

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.

Stateful:

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.

Advantages:

No need to query the database.

It is quick, as we don't have to query the database everytime.

Disadvantages:

If the server goes down, it will be hard to recover.

We can't scale up horizontally.

Stateless:

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.

Advantages:

  • Horizontal scaling is easy.

  • We can easily recover if our sever goes down.

Disadvantages:

  • We have to query our database, for every request.

  • Slower than stateful architecture.


This content originally appeared on DEV Community and was authored by Saran Chakravarthi


Print Share Comment Cite Upload Translate Updates
APA

Saran Chakravarthi | Sciencx (2021-07-16T09:44:18+00:00) Stateful architecture Vs stateless architecture. Retrieved from https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/

MLA
" » Stateful architecture Vs stateless architecture." Saran Chakravarthi | Sciencx - Friday July 16, 2021, https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/
HARVARD
Saran Chakravarthi | Sciencx Friday July 16, 2021 » Stateful architecture Vs stateless architecture., viewed ,<https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/>
VANCOUVER
Saran Chakravarthi | Sciencx - » Stateful architecture Vs stateless architecture. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/
CHICAGO
" » Stateful architecture Vs stateless architecture." Saran Chakravarthi | Sciencx - Accessed . https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/
IEEE
" » Stateful architecture Vs stateless architecture." Saran Chakravarthi | Sciencx [Online]. Available: https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/. [Accessed: ]
rf:citation
» Stateful architecture Vs stateless architecture | Saran Chakravarthi | Sciencx | https://www.scien.cx/2021/07/16/stateful-architecture-vs-stateless-architecture/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.