We often hear the terms “stateful” and “stateless”. Understanding these concepts has a critical importance since it is one of the most essential questions when designing and architecting software applications. In today’s blog post, I wanted to talk about these two very basic concepts.
As the simplest definition, stateful application means an application which remembers the past, similarly, stateless application means an application which doesn’t remember the past. The state we have been talking about might be saved in a database, a file on the disk, a cookie in a browser etc. As an example, consider an e-commerce website. When you add items to your shopping cart, the application has to remember what you have added. If you navigate away and then return back, your items are still in the cart. This is a typical example of a stateful application. Stateful applications need to manage and synchronize the state, and handle situations where the state might be lost.
A stateless application, on the other hand, doesn’t remember anything from one implementation to the next. On each restart, it treats each request as an isolated transaction, unrelated to any previous request. For example, consider a search engine. You enter a query, it gives you results. If you enter a new query, it doesn’t care about the previous one. This can be an example to stateless application.
When designing a system, the primary difference between a stateful and a stateless application can be felt when it comes to scalability. Let’s say you have a Docker container which runs a MySQL database and it stores all the data inside the container. You cannot scale it up since each container will store its data on itself and dividing the requests among multiple instances of them will cause an absolute chaos. On the other hand, let’s say you have another Docker container running a Web API which calculates VAT and doesn’t store any data. You can easily scale it up. In other words, you might have hundreds and thousands of instances of the same container, divide requests among them and they will work side-by-side without any issues.
In day-to-day work, we encounter problems requiring both stateful and stateless components co-exist. The primary challenge is how we structure these two categories together? The key to achieve an effective co-existing for both is simply decoupling. Decoupling stateful and stateless components allows each to scale independently according to their specific requirements. By isolating the state management to separate services or components, stateless services can be lightweight and scalable. This approach also simplifies the architecture, making it easier to manage, maintain, and extend.
Scaling stateless components are generally easier to scale horizontally, because any instance can take care of any request. On the other hand, stateful components may require more sophisticated scaling strategies to ensure that state is consistently maintained across multiple instances. For instance, database replication is one of the most uses techniques to scale out a database server, which is the most stateful of all types of applications.
Suleyman Cabir Ataman, PhD
Leave a Reply