Stateless vs Stateful Servers (with examples)

Ahmed Ossama
3 min readApr 9, 2023

--

In the world of distributed systems, an important design issue is whether the server is stateless or stateful. A stateless server does not keep information on the state of its clients and can change its own state without informing any client. On the other hand, a stateful server maintains persistent information on its clients and requires explicit deletion of information by the server. In this essay, we will explore the differences between stateless and stateful servers, their advantages, disadvantages, and implications for server design.

Stateless Servers

A stateless server, like a web server, does not retain any information on the state of its clients. It simply responds to incoming requests, such as uploading or fetching files, and forgets the client after processing the request. However, in some stateless designs, the server may still maintain information on its clients, but the loss of this information does not disrupt the service offered by the server. For example, a web server may log client requests for replication decisions, but the loss of logs only results in suboptimal performance.

A typical example of a stateless server is a simple web server that serves static web pages. When a client requests a web page, the server processes the request and sends the response back to the client without remembering any information about the client’s past requests. The server does not store any session or user-specific data, and each request is independent of others.

Soft State

A particular form of stateless design is where the server maintains soft state on behalf of the client for a limited time. After the expiration of the time, the server falls back to default behavior and discards any information it kept on account of the client. An example of soft state is a server promising to keep a client informed about updates but only for a limited time, after which the client needs to poll the server for updates. Soft-state approaches originated from protocol design in computer networks but can be applied to server design as well.

Stateful Servers

In contrast, a stateful server retains persistent information on its clients. For example, a file server may allow a client to keep a local copy of a file and maintain a table of (client, file) entries to keep track of update permissions and the most recent version of the file. This can improve the performance of read and write operations as perceived by the client, but it also introduces the challenge of recovery in case of server crashes. A stateful server needs to recover its entire state as it was just before the crash, which can be complex and require special measures.

An example of a stateful server is a web application that requires user authentication. When a user logs in, the server stores information about the user’s session, such as the user ID, in its memory or a database. The server can then use this information to maintain the user’s session state and provide personalized content or services based on the user’s authentication status throughout the user’s session. The server may also store other user-specific data, such as shopping cart items or preferences, and use them for subsequent requests from the same user.

Conclusion

In conclusion, the choice between stateless and stateful servers is an important design decision in distributed systems. Stateless servers do not retain information on the state of clients, while stateful servers maintain persistent information. Each approach has its advantages and disadvantages, and implications for server design.

--

--

Ahmed Ossama

Backend Engineer 👨‍💻 documenting what I learn here..