Modern applications and legacy applications have one property in common, namely whether they store state or not. Whether you are dealing with monoliths or microservices depends on the needs of the application. Some need to store state (user behavior), while others don't need to care.
So-called stateful applications store the state of client requests on the server itself and use that state to process further requests. They use a database to store data as a backend, but the session information itself is stored on the server. When a user sends a login request, they can log in and the user is authenticated, and on the second request, the user sees the dashboard. Stateful applications do not need to make a second call to the database because the session information is stored on the server itself.
So the advantage of these types of applications is higher processing speed. But they also have disadvantages. For example, in stateful applications there is usually a load balancer (LB) behind which there are two servers running the same application. The first request to log in goes to server 1 and the second request might go to server 2; now, since only the one server has logging in enabled, the user won't be able to log in when the load balancer sends the request to the second server. So it is not possible to scale stateful applications horizontally.
Stateful applications, on the other hand, do not store the state of client requests on the server, but only in a database. This in turn is stateful, i.e. it has a persistent memory.
Typically, a user requests a login with credentials, one of the servers processes the request, generates an auth token, stores it in the database, and returns the token to the client at the front end. The next request is sent along with the token. This means that no matter which server processes the request, in each case the token is matched against the information in the database and the user is granted login. Each request is independent and has no connection to previous or next requests, just like REST.
Although stateful (stateless) applications have additional overhead from calling the database, these applications are surprisingly scalable horizontally, which is critical for modern applications that can have millions of users.
Both stateful and stateless are ubiquitous today. But modern software is usually developed as stateless because, unlike stateful applications, it can scale horizontally - an essential criterion today for the use of microservices in cloud environments, among other things.
The eight main differences between stateful and stateless applications are:
1. Working state: stateful applications respond according to the current state, while stateless applications act independently considering the previous/next request.
2. Stored data: If the web server stores data in the backend and uses it to identify the user as an always connected client, the service is Stateful. Whereas in Stateless, the server stores data but in a database to verify the user/client whenever it needs to connect.
3. Reaction towards clients: In Stateful, the server thinks that the client is just a dumb machine, while in Stateless, the server thinks that the client is an intelligent machine that does not need to depend on any state on the server side.
4. Requests: in Stateless, requests are self-contained, i.e. everything is contained in the request and is processed in two separate phases. ("Request" / "Response") In Stateful, requests are always dependent on server-side state.
5. Generated State: When browsing the Internet, the state is generated and stored somewhere. Although in both types this user behavior is generated when stored on the server, the server generates a session. This is called a stateful application.
6. State stored: when the client's behavior is stored, it generates some data that is used for further requests, although technically it is stateful because it refers to a state. But the state is stored by the client, so it is called stateless.
7. Cookie stores: On the client side, the cookie stores authentication data. On the server side, it creates temporary client data or stores it in a database (this is the typical case). When the user returns to the dashboard to make another payment, it is a cookie that is stored in the browser and establishes the state with the server.
8. User base: Stateful is past when there were monoliths and no dynamic user base. Stateless is future as microservices are around and mostly communicate via REST interfaces and everything is scalable as no state is stored.
Main advantages of Stateless
1. Removes the overhead of creating/using sessions.
2. Scales horizontally for the needs of modern users.
3. New instances of an application are added/removed as needed.
4. Enables consistency across different applications.
5. Statelessness makes an application more convenient and maintainable.
Additional scaling and performance benefits of stateless applications:
1. Reduces memory consumption on the server side.
2. Eliminates the problem of session expiration - Sometimes expiring sessions cause problems that are hard to find and test. Stateless applications do not require sessions & and therefore do not suffer from these problems.
3. From the user's point of view, statelessness allows resources to be linked. When a page is stateless, it ensures that when a user links a friend to that page, the user sees the same view as another user.
Source: Gursimran Singh, Home Healthcare Report, October 2020