Overview
The state of an application (or anything else) is its condition or quality of being at a specific moment in time—its state of being. Whether something is stateful or stateless depends on how long the state of interaction with it is being recorded and how that information needs to be stored. A stateful application retains state or context about its interactions with users, systems, or components. The state is persisted on a durable storage solution, so that the application survives a restart. The main difference between stateful and stateless applications is that stateful applications save past and present information while stateless applications don’t.
What are stateful applications?
Stateful applications and processes maintain their state by storing, recording, and returning to already established information and processes over the internet. In stateful applications, the server keeps track of the state of each session or interaction and maintains that information based on the user's past requests. Those sessions can be returned to again and again, like online banking or email. They’re performed with the context of previous transactions, and the current transaction may be affected by what happened during previous transactions. For these reasons, stateful apps use the same servers each time they process a request from a user.
If a stateful transaction is interrupted, the context and history are stored so you can pick up where you left off. Stateful apps track things like window location, setting preferences, and recent activity. You can think of stateful transactions as an ongoing periodic conversation with the same person.
Use cases for stateful applications
- User-centric applications: User-centric applications like social media apps and e-commerce sites keep track of a logged-in user’s session, including their preferences or the items in their shopping cart.
- IoT systems: An Internet of Things (IoT) system works by continuously sending, receiving, and analyzing data in a feedback loop. Based on historical or real-time data, a device will respond to a changing state over time, like your home thermostat.
- AI/ML model training: Artificial intelligence and machine learning (AI/ML) training models learn from data and remember that data. The maintained state of learning and remembering helps models function at their fullest capacity.
Accelerate your path to cloud-native applications
What are stateless applications?
A stateless application or process does not retain information about the user's previous interactions. There is no stored knowledge of or reference to past transactions. Each transaction is made as if from scratch for the first time. Stateless applications provide one service or function and use a content delivery network (CDN), web, or print servers to process these short-term requests.
An example of a stateless transaction would be doing a search online to answer a question you’ve thought of. You type your question into a search engine and hit enter. If your transaction is interrupted or closed accidentally, you just start a new one. Think of stateless transactions as a vending machine: a single request and a response.
Use cases for stateless applications
- REST APIs: REST application programming interface (APIs) transfer a representation of the state of a resource to the requester or endpoint. Each API request is separate and the server does not store information about the previous request.
- Microservices: Microservices allow each core function within an application to exist independently. Because of that, microservices are well suited to both stateless and stateful applications.
- Serverless architectures: Serverless architectures are designed to respond to events in isolation and do not retain context from previous actions and therefore do not need to maintain state. Serverless architectures are ideal for asynchronous, stateless apps that can be started instantaneously.
Stateful vs. stateless: a comparison
The key difference between stateful and stateless is whether an application retains information about the current state of a user's interactions or if it treats each request as an independent, isolated transaction. However, there are also specific differences, including:
State retention
Stateful applications store information about the interaction, often in a database or in distributed memory. Stateless applications don’t store information about an interaction, so a transaction will need to start over.
Session dependence
With stateful applications, each request depends on data or context from previous interactions and transactions. Stateless applications have more independent sessions since each request is treated as new, but that means that the application must have all the information necessary to process the request.
Storage dependence
Stateful applications require persistent storage (like databases and distributed file systems). Stateful apps must rely on an underlying storage solution, and they need a scheme for synchronizing data between instances.
Resource utilization
Stateless applications often have lower resource utilization because there is no need to store and manage session data. Because stateful applications are more storage dependent, they may require more memory and processing power to handle and maintain session information.
Scalability
Stateless applications are generally more scalable, as each request is independent and can be handled by any available server using load balancing. Stateful applications have tightly coupled instances making it more difficult to scale. They may require more specific instances or pods in Kubernetes to manage state, load balance, and manage sessions.
Fault tolerance
Stateless applications can be more fault-tolerant, as the loss of a server doesn't impact user sessions. In stateful applications, the loss of a server can result in the loss of session data unless additional measures, such as session replication or clustering, are in place.
Development complexity
Stateless applications can be simpler to develop and maintain, as there is no need to manage state across multiple requests. Stateful applications, on the other hand, require careful handling of session data and state management.
Containers and state
The majority of applications we use day to day are stateful, but as technology advances, microservices and containers make it easier to build and deploy applications in the cloud.
As cloud computing and microservices grow in popularity, so too has containerization of applications, whether stateful or stateless. Containers are units of code for an application that are packaged up, together with their libraries and dependencies. With containerized applications, they’re able to be easily moved and can run in any environment, whether on a desktop, traditional IT infrastructure, or on a cloud.
Originally, containers were built to be stateless, as this suited their portable, flexible nature. But as containers have come into more widespread use, people began containerizing (redesigning and repackaging for the purposes of running from containers) existing stateful apps. This gave them the flexibility and speed of using containers, but with the storage and context of statefulness.
Because of this, stateful applications can look a lot like stateless ones and vice versa. For example, you might have an app that is stateless, requiring no long-term storage, but that allows the server to track requests originating from the same client by using cookies.
With the growth in popularity of containers, companies began to provide ways to manage both stateless and stateful containers using data storage, Kubernetes, and StatefulSets. Statefulness is now a major part of container storage and the question has become not if to use stateful containers, but when.
Why choose Red Hat?
When it comes to stateful or stateless applications, Red Hat has you covered. Whether you’re orchestrating stateful containers on our hybrid cloud application platform, Red Hat® OpenShift® , or creating a unified environment for app dev with Red Hat Application Foundations, you’ll be backed by our award-winning support and ecosystem of partners.
Red Hat OpenShift provides a unified, security-focused, hybrid cloud application platform that allows organizations to accelerate innovation by helping modernize their application development and deployment and operational processes. Red Hat OpenShift provides features to support stateful applications like Kubernetes StatefulSets. With Red Hat OpenShift Data Foundation, you can manage stateful data with a storage solution that provides software-defined storage and helps provision persistent volumes. Red Hat OpenShift also integrates stateful applications into DevOps workflows. Red Hat OpenShift Pipelines is designed to run each step of the CI/CD pipeline, which supports testing and deployment of stateful workloads alongside stateless ones.
Red Hat Application Foundations provides service composition and orchestration, application connectivity and data transformation, real-time message streaming, change data capture, and API management—all combined with a cloud-native platform and toolchain to support the full spectrum of modern application development.
Used together, Red Hat OpenShift and Red Hat Application Foundations provide an ideal platform to develop and deliver cloud native applications. Whatever your needs, our products help you build solutions, improve developer productivity, and promote innovation—the open source way.
The official Red Hat blog
Get the latest information about our ecosystem of customers, partners, and communities.