Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 65 ยท Building Blocks of the Web Stack

Stateless vs Stateful Systems

Learn the difference between stateless and stateful services, where session state should live, sticky sessions versus shared stores, with a Python demo.

Does a server remember you between requests? If it does, it is stateful; if every request stands alone, it is stateless. This single property decides how easily a service scales, how it survives failures and how you deploy it. Nearly every scaling recommendation in this course starts with "make the tier stateless".

State means any information the system must remember beyond a single request: a logged-in session, the contents of a shopping cart, a half-uploaded file, a game's current position, an open WebSocket.

Stateless services

A stateless server keeps no per-client memory between requests. Each request carries everything needed to answer it (identity token, parameters, context), or the server fetches what it needs from an external store. Because the server remembers nothing, any server can answer any request.

Benefits:

  • Easy horizontal scaling: add servers; any of them can take any request.
  • Simple load balancing: no need to route a user to a particular machine.
  • Fast recovery: if a server dies, nothing is lost; the next request goes elsewhere.
  • Simple deployments: replace servers freely, roll back easily.

HTTP itself is stateless, which is a large part of why the web scales.

Stateful services

A stateful server holds information about its clients in its own memory or disk. Examples are a database, a cache node, a chat server holding open connections, a game server with the live match, a message broker with unread messages.

Stateful components are necessary (the data has to live somewhere!) but harder:

  • A request must reach the server that has the state.
  • If the server fails, its state may be lost unless it is replicated.
  • Scaling needs the state to be partitioned or copied.
  • Deployments and restarts need care.

Stateless vs stateful

Stateless Remembers nothing. Any server can answer.
Stateful Remembers clients. Must find the right server.
Goal Keep most tiers stateless, push state into a few well-managed stores.

State does not vanish; you decide where it lives.

1 / 2

The session problem

Web login is the classic example. After you sign in, the server must recognize you on later requests. Three designs:

1. Sessions in server memory (stateful, with sticky sessions)

The server creates a session object, stores it in its own memory and gives the browser a session ID in a cookie. Next time, only the server holding that session knows who you are. To make this work behind a load balancer you need sticky sessions (session affinity): always send the same user to the same server.

Problems: uneven load, a server restart logs users out, and a failed server loses its sessions.

2. Sessions in a shared store (stateless app servers)

Servers store the session in a fast shared store such as Redis or a database keyed by the session ID. Any server can look it up, so no stickiness is needed and a server can die without logging anyone out. The cost is one extra network call per request, and the store becomes a critical, replicated component.

3. Self-contained tokens (stateless by design)

The server signs a token, for example a JWT, that holds the user ID and expiry, and the client sends it with every request. Servers verify the signature and need no lookup. Drawbacks: tokens cannot be revoked easily before they expire (you need short lifetimes and refresh tokens, or a revocation list), they get large if you put much in them, and the secret key must be protected.

Approach Server state Scales out Revocation Cost
In-memory + sticky Yes Poorly Easy Uneven load, lost on crash
Shared session store In store Well Easy Extra lookup, store must be HA
Signed token None Very well Hard Token size, key management

Seeing the difference

The simulation sends the same user through a round-robin balancer to three servers. With in-memory sessions the user is logged in only on the server that created the session; with a shared store every server recognizes them.

Python Sessions: in memory vs shared store

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With local memory, requests landing on B or C see "please log in" even though A knows the user. The shared store fixes it. This is the everyday meaning of "make servers stateless".

Tokens in action

A signed token makes the server stateless without a lookup. The example uses an HMAC signature from the Python standard library to show the idea (real systems use a vetted JWT library).

Python A self-contained signed token

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every server holding the same key can verify the token with no shared session store, which is why tokens scale so well. The tampered token fails because changing the payload without the key breaks the signature.

A token is signed, not secret: anyone can read its payload. Do not put passwords or private data in it, always send it over HTTPS, and keep lifetimes short.

Making a system mostly stateless

You cannot remove state; you can concentrate it. The standard pattern:

flowchart LR
  U["Users"] --> LB["Load balancer"]
  LB --> A["App server<br/>(stateless)"]
  LB --> B["App server<br/>(stateless)"]
  LB --> C["App server<br/>(stateless)"]
  A --> R[("Session store / cache")]
  B --> R
  C --> R
  A --> D[("Database")]
  B --> D
  C --> D

Figure: stateless middle tier; the state lives in the shared stores at the bottom, which are replicated and scaled specially.

Checklist for a stateless service:

  • No user data in local memory that cannot be rebuilt; no local files that other servers need.
  • Sessions in a shared store, or in signed tokens.
  • Uploaded files go to object storage, not the local disk.
  • Background work goes to a queue, not an in-process list.
  • Configuration comes from the environment, not hand-edited files.

When stateful is the right choice

Some workloads are inherently stateful and that is fine: databases, caches, WebSocket servers holding live connections, multiplayer game servers, streaming sessions. For these:

  • Route by key (for example consistent hashing) so a client reaches the right node.
  • Replicate state so a node failure loses nothing.
  • Use sticky routing where needed, and handle reconnects: clients must be able to resume on another node.

The same idea applies to load balancing itself: a balancer can be stateless (decides per request from headers or addresses) or stateful (remembers sessions and connections), covered in types of load balancers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do stateless servers scale out easily?

    Because no request depends on what a particular server remembers, a load balancer can send traffic anywhere.
  2. 2. What problem do sticky sessions create?

    Users are tied to one server, so that server's load and its failures directly affect them.
  3. 3. What is a main drawback of signed tokens (JWT-style)?

    The server keeps no record, so you cannot simply delete the session; use short lifetimes and refresh tokens.
  4. 4. Where should uploaded user files go in a stateless web tier?

    Local files are invisible to other servers and vanish when the server is replaced.

Summary

  • Stateless servers remember nothing between requests, so any instance can serve any request.
  • Stateful components such as databases and caches are necessary, but must be replicated and partitioned.
  • For sessions choose sticky in-memory (simple, fragile), a shared store (flexible) or signed tokens (no lookup, hard to revoke).
  • Concentrate state into a few well-run stores and keep the application tier stateless.
  • Files, queues and configuration belong outside individual servers.

Next: Monolith vs Microservices