1. Overview

In computer science, the state of a system is the stored information that determines its current status at a certain point in time.

In this tutorial, we’ll learn about stateful and stateless systems and compare them.

2. Stateless System

A stateless system doesn’t store information about past interactions with the system. Therefore, each interaction with the system is like the first interaction with it.

For example, this class is stateless:

class Echo {
    String echo(String input) {
        return input; 
    }
}

Other examples of stateless systems include the Internet Protocol (IP), which is a network layer protocol in the TCP/IP model used for sending packets from source to destination, and the Hypertext Transfer Protocol (HTTP), which is an application layer protocol in the TCP/IP model used to transfer information between devices in a network.

3. Stateful System

A stateful system remembers previous interactions with the system and stores information about them. As a result, new interactions with the system may produce different outputs than previous interactions.

For example, this class is stateful:

class Counter {
    private int count = 0;

    int count() {
        count++;
        return count;
    }
}

As we can see, the state of the class is kept inside of it as a private variable. Further, every new interaction with the system changes its state and produces a different output.

Other examples of stateful systems include email services (like Gmail) and social media platforms.

4. Which One Is Used by Web Services Today?

Stateful web services hold information about previous user interactions on the server side. However, stateless services don’t hold this information and require the user to hold this data on the client side (usually in the form of web cookies) and remind the server every time they send a request.

In the past, internet services were delivered using stateful architectures. Over the years, as the demand for internet services grew, websites and businesses needed to scale their services. On the other hand, stateful solutions are hard to scale. As a result, stateless architectures slowly replaced stateful architectures.

SOAP (Simple Object Access Protocol), REST (REpresentational State Transfer), and GraphQL are examples of stateless methodologies.

5. Conclusion

In this article, we learned about stateful and stateless systems and saw a few examples of them.