# Statelessness

### Statelessness

In a stateless system, each request from the client must contain all the information the server needs to fulfill that request. The server does not store any state of the client's session. **This is a crucial principle of RESTful APIs**. Characteristics include:

* **No Session Memory**: The server remembers nothing about the user once the transaction ends. Each request is independent.
* **Scalability**: Stateless systems are generally more scalable because the server doesn't need to maintain session information. Any server can handle any request.
* **Simplicity and Reliability**: The stateless nature makes the system simpler and more reliable, as there's less information to manage and synchronize across systems.
* **Example**: An API where each request contains an authentication token and all necessary data, allowing any server instance to handle any request.

<figure><img src="https://1471795080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrtXPLjVTxuTGIysjCx89%2Fuploads%2FuDrzR0gBZe6CPlwbZjwi%2Fimage.png?alt=media&#x26;token=8df03934-1d97-4564-b1de-f78cb624fa1d" alt=""><figcaption><p>Diagram explaining Statelessness</p></figcaption></figure>

In this diagram:

* **Request 1**: The client sends a request to the load balancer.
* **Load Balancer to Server 1**: The load balancer forwards Request 1 to Server 1.
* **Response from Server 1**: Server 1 processes the request and sends a response back to the client.
* **Request 2**: The client sends another request to the load balancer.
* **Load Balancer to Server 2**: This time, the load balancer forwards Request 2 to Server 2.
* **Response from Server 2**: Server 2 processes the request and responds to the client.
* **Statelessness**: Each request is independent and does not rely on previous interactions. Different servers can handle other requests without needing a shared session state.

### **Token-Based Authentication**

Common in stateless architectures, this method involves passing a token for authentication with each request instead of relying on server-stored session data. **JWT (JSON Web Tokens)** is a popular example.
