DEV Community

Hardik Shah
Hardik Shah

Posted on

Stateless vs Stateful - Which direction should you take?

Scaling a web app is hard, for it always comes with uncertainties and failures. Even a minor fault can break an application if it’s not built for scalability.

In traditional web development, however, one thing that might help a website scale is –– everything being stateless. Here Statelessness means that the server should not remember the application’s state. Instead, the client should remember all the information necessary for the execution with each request as the server can’t memorize and reuse information from previous requests.

Whenever you have to fetch a few things from a database, you receive a request containing everything you need to know. On answering that request, you don’t care about the client until you receive a new request. This makes work so easy; so convenient to execute as you are ensuring a clean state every time.

In traditional application development, however, everything for a fact is stateful. Things are stored in memory, which gives you a sigh of relief that they will continue to remain there until the app is closed; or unless it is reopened. You are not expected to maintain state across sessions; not forced to think that things can be missed out.

Enter Modern Complex Web apps

Modern web apps, on the contrary, sits in between statelessness and statefulness. Here, your server remains stateless and the frontend is stateful.

Think about a web app such as Trello or Canva where every URL points to a single page. Once you reload the page, all elements must be re-fetched from the server, maintaining the state on the server.

So, how do you decide whether the stateless or stateful approach would help you out?

The Answer is…

Until and unless you need a reason to store a state, going Stateless might help. One thing, however, you need to make sure is to think about the situation where maintaining a state is necessary.

Hugh Mckee, the developer advocate at Lightbend, urges to look into specific circumstances while choosing between Stateless or Stateful approach.

In his blog “How to build successful cloud-native solutions”, Hugh demonstrates the scaling capability of stateful applications by illustrating a shopping cart example.

Hugh concludes his opinion on stateless vs stateful debate with the following quote,

Alt Text

Need help in building complex web apps that scale, you can refer to these scalability tips shared by 25+ experts.

What is your opinion regarding stateful vs stateless debate? I would really love to know in the comments.

Top comments (0)