# Statefulness

### Statefulness

The server stores information about the client's current session in a **stateful system**. This is common in traditional web applications. Here's what characterizes a stateful system:

* **Session Memory**: The server remembers past interactions and may store session data like user authentication, preferences, and other activities.
* **Server Dependency**: Since the server holds session data, the same server usually handles subsequent requests from the same client. This is important for consistency.
* **Resource Intensive**: Maintaining state can be resource-intensive, as the server needs to manage and store session data for each client.
* **Example**: A web application where a user logs in, and the server keeps track of their authentication status and interactions until they log out.

<figure><img src="https://1471795080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrtXPLjVTxuTGIysjCx89%2Fuploads%2F1cYsw3X1sUBVoqQ6aZ8t%2Fimage.png?alt=media&#x26;token=eb570235-0fc3-452d-ac83-e8e2ba41f276" alt=""><figcaption><p>Diagram explaning Statefulness &#x26; Stickiness</p></figcaption></figure>

In this diagram:

* **Initial Request**: The client sends the initial request to the load balancer.
* **Load Balancer to Server 1**: The load balancer forwards the request to Server 1.
* **Response with Session ID**: Server 1 responds to the client with a session ID, establishing a sticky session.
* **Subsequent Requests**: The client sends subsequent requests with the session ID.
* **Load Balancer Routes to Server 1**: The load balancer forwards these requests to Server 1 based on the session ID, maintaining the sticky session.
* **Server 1 Processes Requests**: Server 1 continues to handle requests from this client.
* **Server 2 Unused**: Server 2 remains unused for this particular client due to the stickiness of the session with Server 1.

### Stickiness (Sticky Sessions)

Stickiness or sticky sessions are used in stateful systems, particularly in load-balanced environments. It ensures that requests from a particular client are directed to the same server instance. This is important when:

* **Session Data**: The server needs to maintain session data (like login status), and it's stored locally on a specific server instance.
* **Load Balancers**: In a load-balanced environment, without stickiness, a client's requests could be routed to different servers, which might not have the client's session data.
* **Trade-off**: While it helps maintain session continuity, it can reduce the load balancing efficiency and might lead to uneven server load.

#### Methods of Implementing Stickiness

* **Cookie-Based Stickiness**: The most common method, where the load balancer uses a special cookie to track the server assigned to a client.
* **IP-Based Stickiness**: The load balancer routes requests based on the client’s IP address, sending requests from the same IP to the same server.
* **Custom Header or Parameter**: Some load balancers can use custom headers or URL parameters to track and maintain session stickiness.
