Card 04/ 11

ConceptDifficulty: Beginner2 min

Stateless Application Servers

A server that remembers nothing between requests can be added, replaced or removed at any time.

An application server is the machine that runs your code: it takes a request, reads and writes data, and builds the response. Keeping it stateless means it holds nothing about a user in its own memory between one request and the next. Every server is then interchangeable, so any of them can answer any request, and you can add or remove them freely.

What state means here. State is anything the server must remember to answer the next request correctly: who is signed in, what is in the basket, which step of a form the user reached. The data does not go away when a server is stateless. It moves to a shared store, such as a database or a cache, that every server can read.

Where the shopping basket lives

Statefulbasket held in server A's memory

The user's next request must reach server A, or the basket looks empty. If server A restarts or is removed, every basket on it is lost. Adding server D does not help users already tied to A.

Statelessbasket in a shared store

Each request carries a session id, and whichever server answers reads the basket from the shared store. Server A can restart, server D can join, and the user notices nothing.

What it allows. Because no server is special, a load balancer can send each request to any of them. A failed server is replaced without losing anything. When traffic climbs, a new copy starts and takes requests at once, without moving any data onto it.

What it costs. Every request that needs the state reads it from the shared store, which adds a round trip, a millisecond or less inside one data centre. The shared store now matters to everything, so it needs a spare of its own.

Where you meet it. Any web framework's process plays this part: a Node.js server, a Spring Boot application, a Django worker. Each is stateless when it keeps sessions and uploads outside its own memory and disk.

So move every piece of user state out of the application server's memory: that one choice is what lets the servers be added, replaced and removed without anyone noticing.