system-design

Stateless vs Stateful Architecture: Understanding Server State, Sessions & Scalability

Understanding stateless and stateful server architectures is essential for designing scalable distributed systems. Learn how servers manage client sessions, maintain state, handle requests, and achieve horizontal scalability with practical examples and architectural diagrams.

Key concepts
stateless architecturestateful architecturesystem designsystem design interviewserver architecturesession managementdistributed systems
Fundamentals

Understanding Server State

CORE CONCEPT
Core Concept

SERVER STATE refers to information that an application backend maintains or manages to process requests, identify users, and coordinate application operations.

❖

A server does not always process every request independently. Some operations require information from previous interactions, such as user sessions, shopping carts, or ongoing transactions.

❖

The backend may maintain this information in its LOCAL MEMORY or manage it through external storage systems such as DATABASES and REDIS.

❖

This information helps the application maintain continuity across requests and execute business operations correctly.

Application server managing authentication, user sessions, shopping carts, orders, and payment information
The backend manages different types of application information required to process user requests.

In simple terms

Server state = Information the backend needs to manage or access while handling application operations.

Core idea

A server may need information beyond the current request to maintain application continuity.
Understanding State

What Information Does a Server Maintain?

Consider an E-commerce application.

When a user interacts with the application, the backend may need to manage different types of information to support the user's activities.

Application server accessing databases, Redis and external storage to manage different types of application state
Different types of application state can be stored in different storage systems.
CORE CONCEPT
USER INFORMATION

User ID, email address, profile details and account information.

PURPOSE

Identify users and retrieve their account data.

CORE CONCEPT
SESSION INFORMATION

Session ID, authentication status and session expiration.

PURPOSE

Maintain authenticated user interactions.

CORE CONCEPT
SHOPPING CART

Product IDs, quantities, selected variants and cart ownership.

PURPOSE

Preserve the user's shopping selections.

CORE CONCEPT
ORDER INFORMATION

Order ID, order items, order status and delivery details.

PURPOSE

Track the order lifecycle.

CORE CONCEPT
PAYMENT INFORMATION

Transaction ID, payment status and payment references.

PURPOSE

Track and coordinate payment operations.

CORE CONCEPT
WORKFLOW INFORMATION

Current task, processing status and workflow progress.

PURPOSE

Coordinate multi-step business operations.

Architecture Fundamentals

Client State vs Server State

CORE CONCEPT
The Core Difference
An application has two major environments:
CLIENT

The device or application through which the user interacts with the system.

SERVER

The backend that processes requests and manages application operations.

Client-side state and server-side state comparison showing browser UI information and backend business information
Client state primarily supports the user interface, while server state supports backend operations and business data.

CLIENT STATE

CLIENT state refers to information maintained on the user's device or application.

Examples include:

A user selects the blue color filter on a product page. The selected filter can be maintained in the browser without requiring the backend to remember every UI interaction.

SERVER STATE

SERVER state refers to information maintained or managed by the backend.

Examples include:

When a user places an order, the backend stores and manages the order information so that it remains accessible across devices and sessions.

Key Differences

Client StateServer State
Maintained on the user's deviceManaged by the backend
Primarily supports UI interactionsSupports business logic and operations
Can be lost when browser data is clearedPersistence depends on backend storage policies
Generally belongs to a particular clientCan be shared across clients through centralized storage
Selected tab, modal, form valuesOrders, inventory, user accounts
Server Architecture

How Does a Server Handle Information?

We've understood that a server manages different types of application information, such as user details, sessions, shopping carts, and orders.

But how does a server handle this information when a user sends multiple requests?

Consider an E-commerce application where a user logs in, browses products, adds items to a shopping cart, and proceeds to checkout.

Each interaction generates a separate request to the backend. However, the application still needs access to relevant information from the user's previous interactions.

User interacting with an e-commerce application through multiple requests to an application server
Multiple requests from the same user may require access to information from previous interactions.

This introduces an important architectural question:

Should a server remember information from previous requests, or should every request contain or retrieve everything needed for processing?

The ANSWER depends on how the application is designed.

In system design, we commonly discuss two approaches to handling this:

CORE CONCEPT
STATELESS APPROACH

The server does not depend on locally retained client/session state between requests.

CORE CONCEPT
STATEFUL APPROACH

The server maintains client/session-related state across requests.

Architecture Fundamentals

What Is Stateless Architecture?

CORE CONCEPT
Core Concept

STATELESS ARCHITECTURE is a server architecture in which an application server does not depend on locally retained client/session state from previous requests to process a new request. Each request carries the required information or allows the server to retrieve it from an external storage system.

In a Traditional Application, a server may maintain user session information in its own memory and use it when the user sends subsequent requests.

➤ However, in a Stateless Architecture, the server does not need to remember the previous interaction with a particular client in its local memory.

Instead, Every request → Contains the necessary context, such as 🔶 Authentication Credentials, 🔶 Request Parameters, or 🔶 Identifiers required to retrieve application data.

This allows different application servers to process requests from the same user without depending on session information stored locally on another server.
Stateless architecture showing clients sending independent requests to application servers connected to shared storage
Application servers process requests independently while accessing shared application data when required.

In simple terms

Request carries or retrieves required information → Server processes it → Response is returned.

Core principle

A server does not depend on locally retained client/session state from previous requests.

How Does a Stateless Server Handle Requests?

Consider an E-commerce application where a user wants to view their previous orders.

In a Stateless Architecture, the application server does not need to remember which user sent the previous request. Instead, the incoming request carries the authentication information required to identify the user.

Stateless request flow showing client authentication information, application server validation, database retrieval and response
Each request contains the required context, and the server retrieves application data when necessary.
REQUEST
FIRST REQUEST

CLIENT Sends :

GET /api/orders

Authorization: Bearer token

SERVER performs :

Validates the token

Identifies the user

Retrieves the user's orders

Returns the response

REQUEST
SECOND REQUEST

CLIENT Sends :

GET /api/profile

Authorization: Bearer token

SERVER performs :

Validates the token again

Identifies the user

Retrieves profile information

Returns the response

Notice that the second request does not depend on the first application server remembering the user's session in its local memory.

The required authentication information is supplied again with the request.

How Multiple Servers Handle Requests Independently ?

One of the MAJOR ADVANTAGE of stateless application servers becomes clear when an application needs to handle increasing traffic.

Consider an application running behind a load balancer with multiple application server instances.

Load balancer distributing client requests across multiple stateless application servers connected to shared database and Redis
Multiple application servers can handle requests without depending on locally retained session state.

Suppose a user sends three different requests:

A
Application Server A
Processes the first request.
B
Application Server B
Processes the second request.
C
Application Server C
Processes the third request.

Each server can process the request because the required authentication information is included in the request, and application data can be retrieved from shared storage.

Key observation

Request 1 → Server A | Request 2 → Server B | Request 3 → Server C

Where Does Application State Live?

A common Misconception is that STATELESS architecture means the application cannot store user or business information.

This is INCORRECT.

A stateless application can use external storage systems to maintain information required across requests.

Remember

Stateless application servers + Shared storage = Application state without dependence on individual server memory.
Stateless application servers accessing shared database and Redis storage for user data, sessions, orders and application information
Application servers can remain independent while sharing access to centralized state storage.

A STATELESS application server does not depend on locally retained client/session state from previous requests.

Instead, each request carries the necessary context, such as authentication credentials, or allows the server to retrieve the required information from shared storage systems.

This means application data can still be maintained in databases, Redis, and other external storage systems without making individual application servers dependent on their local memory.

What about authentication?

JWT authentication flow showing client sending a signed bearer token, application server validating its signature, expiration and claims, successful validation extracting user ID and role, and invalid tokens returning 401 Unauthorized
JWT validation determines whether a request can proceed or must be rejected.

Applications can use signed tokens such as JWTs to carry verified identity and authorization claims.

A server can validate a token without looking up a traditional server-side session on every request.

However, token revocation, permissions, and other requirements may still require shared storage or additional checks.

Characteristics of Stateless Architecture

↗

Independent Requests

Each request carries or retrieves the context required for processing.

◈

No Local Session Dependency

Servers don't rely on locally retained session state between requests.

⇧

Horizontal Scalability

New application server instances can be added to handle increasing traffic.

▤

Shared Data Access

Multiple servers can access common databases and distributed storage.

⟳

Easier Server Replacement

Servers can restart or be replaced without transferring local session state.

⇄

Flexible Request Routing

Requests can reach different available servers without depending on session affinity.

TRADE-OFFS : Advantages and Disadvantages

ADVANTAGES

✓Easier horizontal scaling across multiple application servers.
✓Requests can be routed to different available servers.
✓Less dependency on individual server memory.
✓Easier replacement and recovery of application server instances.
✓Suitable for distributed application deployments.

LIMITATIONS

!Every request must carry or retrieve the required context.
!Repeated authentication and validation can add processing overhead.
!Shared databases and caches can become bottlenecks.
!Distributed state management introduces additional design complexity.
!Large tokens or repeated request context can increase network overhead.
Architecture Fundamentals

What Is Stateful Architecture?

CORE CONCEPT
Core Concept

STATEFUL ARCHITECTURE is a server architecture in which an application server retains or maintains client/session state between requests and uses that previously stored information when processing subsequent requests.

The SERVER remembers relevant context from earlier interactions instead of requiring every request to independently provide all of it.

Consider a user logging into an e-commerce application.

In a Stateful Architecture, the application server can create a session after Login and Maintain Information Such as the user's identity, authentication status, and shopping cart context.

When the same user sends subsequent requests, the server uses the previously maintained session information to understand the user's context.

Every subsequent request → Can reference the existing session using a session identifier, such as a cookie.

The server uses that identifier to locate the associated session and continue processing the user's interaction.
Stateful architecture showing a client sending requests with a session identifier to an application server that maintains the user's session state between requests
The application server maintains session context and uses it when handling subsequent requests.

In simple terms

Request arrives → Server retrieves previously maintained session context → Processes request → Returns response.

Core principle

The application server remembers relevant client/session information between requests.

How Does a Stateful Server Handle Requests?

Consider an e-commerce application where a user logs in, views products, adds items to a shopping cart, and proceeds to checkout.

In a Stateful Architecture, the application server can maintain the user's session after login and use it to recognize the user during subsequent requests.

Stateful request flow showing login, session creation, session storage, subsequent requests carrying a session ID, and the application server retrieving the existing session context
The server creates a session and uses the session identifier to retrieve previously maintained user context.
REQUEST
FIRST REQUEST — LOGIN

CLIENT Sends :

POST /api/login

Email + Password

SERVER performs :

Validates user credentials

Creates a user session

Stores session information

Returns a session ID to the client

REQUEST
SECOND REQUEST — VIEW CART

CLIENT Sends :

GET /api/cart

Cookie: SESSION_ID=abc123

SERVER performs :

Reads the session ID

Finds the existing session

Identifies the user and cart context

Returns cart information

Notice that the server uses the session created during the first request to understand the context of the second request.

The client sends the session identifier, and the server uses it to locate the previously maintained session.

Where Does the Server Maintain Session State?

A stateful application server needs a way to maintain session information between requests.

One common implementation is to store session data in the application's own memory.

IN-MEMORY SESSION

•User ID and authentication status
•Shopping cart context
•User preferences and temporary session data

EXTERNAL SESSION STORE

•Shared session information
•Session expiration and metadata
•Session data accessible by multiple servers
Comparison of an application server maintaining session data in its own memory versus storing shared session data in an external session store
Session state can be maintained locally in application memory or in an external shared session store.

Remember

Session ID → Identifies the session → Server retrieves previously maintained user context.

What Happens When Multiple Servers Handle Requests?

A challenge with stateful architecture becomes clear when an application runs across multiple application server instances.

Consider an application with a load balancer distributing incoming requests across three servers.

Stateful application architecture showing a load balancer distributing requests across multiple servers with session information maintained separately in each server's local memory
When sessions are stored locally, each application server maintains its own session information.
Suppose a user logs in and the first request reaches Application Server A.
A
Server A
Has the user's session.
B
Server B
Does not have that local session.
C
Server C
Does not have that local session.
If the next request reaches Server B, it may not find the session created by Server A.

This can cause SESSION LOOKUP FAILURES or make the user appear logged out.


There are two common ways to handle this problem.

1. Sticky Sessions (Session Affinity)

The load balancer routes requests from the same client to the same application server that originally created the session.

Sticky sessions diagram showing a load balancer consistently routing requests from one client to Application Server A, where the client's session is stored
Session affinity keeps a client connected to the application server that holds its local session.

For example:

Request routing

User Request 1 → Server A | User Request 2 → Server A | User Request 3 → Server A

This helps the server find its locally stored session without transferring it to another instance.

2. Shared Session Store

Instead of maintaining session data separately in each server's local memory, the application can store sessions in a centralized or distributed session store such as Redis.

Multiple application servers retrieving and updating the same user session from a shared Redis session store
Multiple application servers can access the same session data through a shared session store.

Now, any application server can retrieve the user's session using the session ID.

Request routing

User Request 1 → Server A | User Request 2 → Server B | User Request 3 → Server C

All three servers can access the same session information from Redis, so the session is not tied to one particular application server.

Characteristics of Stateful Architecture

◈

Session Retention

The application maintains client/session context between requests.

↻

Session-Based Processing

Subsequent requests can use information established during earlier interactions.

▤

Session Storage

Session information can be maintained in local memory or an external store.

⇄

Session Affinity

Local sessions may require requests to reach the same application server.

▦

Context-Aware Requests

The server uses previously established session information to process requests.

⟳

Session Lifecycle

Sessions can be created, updated, expired, and invalidated.

TRADE-OFFS : Advantages and Disadvantages

ADVANTAGES

✓Convenient for applications that require ongoing user sessions.
✓Server can reuse previously maintained context.
✓Can simplify workflows that depend on multi-step interactions.
✓Session data can be updated as the user interacts with the application.
✓Useful when application behavior depends on ongoing session context.

LIMITATIONS

!Local sessions can make horizontal scaling more complicated.
!Sticky sessions can create dependency on a particular server.
!Server failure can cause loss of locally stored session information.
!Shared session stores introduce additional infrastructure and operational complexity.
!Session expiration, synchronization, and consistency need to be managed.
Architecture Comparison

Stateless vs Stateful Architecture

Both Stateless and Stateful Architectures manage application information across user interactions, but they differ in how application servers handle session context between requests.

Let's compare these two approaches across some important architectural characteristics.
S

Stateless

The application server does not depend on locally retained client/session state from previous requests.

REQUEST FLOW

Request → Server → Shared Storage

Each request carries or retrieves the required context.

S

Stateful

The application maintains session context across requests and uses previously established information when processing subsequent interactions.

REQUEST FLOW

Request → Session Lookup → Processing

The server uses previously maintained session context.

Key Architectural Differences

AspectStatelessStateful
Request handlingEach request is processed independently.Requests can use context established by previous interactions.
Session dependencyNo dependency on locally retained client/session state.Depends on previously maintained session context.
Session storageUsually avoids local server-side session storage.Can use local memory or an external session store.
AuthenticationOften uses tokens such as JWTs.Often uses server-side sessions identified by session IDs.
Request routingRequests can reach any suitable available server.Local sessions may require sticky sessions.
Horizontal scalingGenerally simpler because requests are not tied to local sessions.Requires session management across server instances.
Server failureAnother server can handle requests if shared dependencies remain available.Locally stored sessions may be lost if the server fails.
InfrastructureMay use shared databases, caches, and token validation.May require session storage, synchronization, and expiration management.
Typical use casesREST APIs, microservices, and horizontally scaled services.Session-oriented applications and workflows requiring ongoing context.
⚠

An Important Architectural Distinction

Stateful and stateless architecture are not determined simply by where session data is stored.

For example - An Application can maintain server-side sessions in Redis and allow multiple application servers to access them. The Application uses session-based state, but individual application server instances do not need to retain those sessions in their own memory.

Similarly, a stateless application server can still access databases and Redis to retrieve application data.

The key question:

Does the application server depend on session context retained from previous requests, or can it process the request using the context provided or retrieved independently?

Architecture Design

When to Use Which Architecture?

Neither stateless nor stateful architecture is universally better. The right choice depends on how your application handles user context, manages sessions, scales traffic, and processes requests.

Choose Stateless When

Requests should be independently processable, and application servers should not depend on locally retained session context.

❖ High-traffic REST APIs

❖ Microservices

❖ Horizontally scaled web applications

❖ Serverless applications

❖ Public APIs and backend services

Choose Stateful When

The application needs to maintain and reuse ongoing session context across multiple interactions.

❖ Real-time multiplayer games

❖ Persistent WebSocket connections

❖ Collaborative applications

❖ Interactive sessions

❖ Long-running workflows

When Stateless Architecture Is a Good Fit ?

Scenario 1 - REST APIs

Consider an e-commerce application where users request their order history, profile, and product information.

GET /api/profile
GET /api/orders
GET /api/products

Each request carries authentication credentials. The server validates the request and retrieves the required information from shared storage. Any available application server can process it.

Why stateless ? Independent request processing and easier horizontal scaling.

Scenario 2 - Microservices

In a microservices architecture, services such as User, Order, and Payment should ideally process requests without depending on session data stored inside a particular service instance.

API Gateway
→
User Service
Order Service
Payment Service

Each service can scale independently, while shared databases, caches, or other infrastructure maintain the required application data.

Why stateless? Independent deployment, scaling, and request handling.

Scenario 3 - Serverless Applications

Serverless functions may execute on different instances for different requests. Keeping session state inside an individual function instance is unreliable because the instance may be reused, replaced, or unavailable for the next invocation.

Why stateless ? Each invocation can operate independently using request context and external storage.

When Stateful Architecture Is a Good Fit ?

Scenario 1 - Multiplayer Games

In a multiplayer game, the server continuously maintains the current game state, including player positions, scores, health, and active game events.

Game ServerCurrent Game State
Player 1
Position
Player 2
Score
Game
Status

The server needs to maintain and update a shared game state as players interact in real time.

Why stateful ? Continuous interactions depend on the current session or game state.

Scenario 2 - WebSocket Applications

Consider a live chat application. A WebSocket connection remains open between the client and server, allowing both sides to exchange messages without establishing a new HTTP request for every message.

Client
Persistent connection
Server

The server tracks active connections and may maintain connection-specific context. If connections are distributed across multiple servers, connection routing and shared coordination need to be designed carefully.

Why stateful ? The application maintains ongoing connection context.

Scenario 3 - Collaborative Applications

In collaborative tools such as shared document editors, multiple users may edit the same document simultaneously. The application coordinates active users, document changes, and synchronization of ongoing updates.

Why stateful ? Maintaining ongoing collaboration context is important.

Decision Guide

Use these questions as a starting point when choosing an architecture.
Can each request be processed independently?
Yes → Stateless is a strong fitNo → Consider Stateful
Does the application need ongoing connection or session context?
Yes → Stateful behavior may be neededNo → Stateless may be simpler
Do you need to scale application servers independently?
Yes → Stateless instances simplify scalingLocal state → Additional routing or shared storage design is needed

Can Both Architectures Be Used Together?

Yes. In fact, many real-world applications combine stateless request processing with stateful components.

E-commerce application architecture showing a client, load balancer, multiple stateless application servers, shared Redis cache, centralized database, and a separate WebSocket real-time service.
Multiple stateless application servers handle user requests independently while sharing session data through Redis and persistent information through a centralized database.

In this design, ordinary API servers can process requests independently, while Redis provides shared state and a real-time service manages persistent connections. The overall application uses both stateless and stateful behavior.

Interview Prep

Interview Questions

Answer
In stateless architecture, an application server does not depend on locally retained client/session state from previous requests. Each request carries or retrieves the required context independently. In stateful architecture, the application maintains session context across requests and uses previously established information when processing subsequent interactions.
Key Takeaway
Stateless servers process requests independently, while stateful systems maintain context across interactions.
Watch Out

Common Mistakes

01

Stateless architecture means the application has no state.

Common Misconception
Applications still maintain business data, user information, orders, and other state in databases, caches, and external storage systems.
Key Idea
Stateless refers to server-side session dependency, not the absence of application data.
02

Stateless means the server never stores anything.

Common Misconception
A stateless application server can still access databases, Redis, caches, and other storage systems. It simply does not depend on locally retained client/session state between requests.
Key Idea
Distinguish persistent application data from locally maintained session context.
03

Using JWT automatically makes an application stateless.

Common Misconception
An application can use JWT authentication while maintaining server-side sessions, token revocation lists, or other session-related dependencies.
Key Idea
JWT is an authentication token format, not an architectural guarantee.
04

Using Redis automatically makes an application stateful.

Common Misconception
Redis can act as shared storage for multiple application servers. Individual servers can access the same session data without maintaining it in their own local memory.
Key Idea
Shared state storage does not necessarily create server-instance dependency.
05

Sticky sessions solve all stateful architecture problems.

Common Misconception
Sticky sessions route requests to the same server, but they can create server affinity, uneven traffic distribution, and potential session loss when a server fails.
Key Idea
Sticky sessions help with routing, not complete session reliability.
06

Stateful applications cannot scale horizontally.

Common Misconception
Stateful applications can scale through shared session stores, session replication, and appropriate routing strategies. However, maintaining session consistency and availability requires additional coordination.
Key Idea
Stateful scaling is possible, but it requires careful session management.
07

A load balancer automatically makes an application stateless.

Common Misconception
A load balancer distributes incoming requests across servers, but it does not change how application servers manage session context. Local sessions may still require sticky sessions or shared storage.
Key Idea
Load balancing and stateless architecture solve different problems.
08

An entire application must be either stateful or stateless.

Common Misconception
A real-world application can use stateless REST APIs alongside stateful WebSocket services, shared session stores, and other state-dependent components.
Key Idea
Choose the appropriate architecture for each component based on its requirements.

Resource Info

Quick reference

Estimated read time

20 min

Difficulty

Beginner

Category

system-design