Subjects Questions Quizzes Pricing
Beginner Short answer Free

Stateless Services

What does it mean for an application server to be "stateless"? Why is statelessness a prerequisite for horizontal scaling? Give a concrete example of a stateful design and how you would refactor it to be stateless.

Solution

A stateless application server does not store any per-request or per-user data in its own memory between requests. Each request carries all the information needed to process it, or that information is fetched from a shared external store.

Why it matters for horizontal scaling: When a load balancer routes requests across multiple servers, it may send two requests from the same user to different servers. If user state is stored in Server A's memory, Server B cannot find it. A stateless design moves all state to a shared layer (database, Redis) so any server can handle any request.

Example: Stateful (bad): A Python Flask app stores the logged-in user in a global dictionary keyed by session ID, held in the process's memory. When a second server is added, half the requests get a "not logged in" error.

Stateless (good): Sessions are stored in Redis. On each request, the server reads the session from Redis using the cookie's session ID. All servers share the same Redis instance and can serve the same user.

← Back to System Design Basics practice