Architecture

Stateful vs Stateless Apps: What the Difference Means for Your Servers

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Every application remembers something: who is logged in, what is in a shopping cart, which step of a form a user reached, which files were uploaded. That memory is called state. The real architectural question is not whether your app has state, because it always does, but where that state lives. An app that keeps state inside its own process or on its own disk is stateful. An app that pushes state out to a shared store and treats every request as self-contained is stateless. The choice decides how easily you can restart, replace and multiply your servers.

What "state" actually includes

State is any data that must survive between two requests. Typical examples:

  • Login sessions and CSRF tokens
  • Shopping carts and multi-step form progress
  • Uploaded files and generated images
  • In-memory caches and rate-limit counters
  • WebSocket connections and their subscriptions
  • Background job progress

If any of this lives only in the memory or local disk of one server, that server has become special. Losing it, or sending the next request to a different server, loses the data.

Stateful applications

A stateful application keeps per-user or per-connection data locally. Classic PHP with file-based sessions in /var/lib/php/sessions is stateful. So is a Node.js app that stores sessions in a JavaScript object, or a game server that holds a match in memory.

Stateful designs are not wrong. They are simple, fast (no network hop to fetch a session) and sometimes unavoidable. Databases, message brokers and game servers are stateful by nature. The cost appears when you need more than one instance:

  • A load balancer must send each user back to the same server, which is what sticky sessions do.
  • Restarting or redeploying the server logs users out or drops their work.
  • Scaling down means deciding which users to disconnect.
  • A crashed server takes its users' data with it.

Stateless applications

A stateless application stores nothing that matters between requests. Each request carries what is needed to handle it (a session cookie, a token), and the app looks up everything else in shared services: a database, Redis, object storage. Any instance can answer any request.

Benefits:

  • Add or remove instances freely behind a load balancer.
  • Redeploy with rolling restarts and no logouts.
  • A crashed instance is replaced, not repaired.
  • Containers and autoscaling become straightforward.

The trade-off is that state has not disappeared; it moved to the database, cache and storage layer, which now must be reliable and fast. Stateless application servers are easy to scale precisely because the hard part is concentrated in a few stateful services that you protect carefully.

Comparison

AspectStatefulStateless
Where state livesProcess memory or local diskShared database, cache or storage
Horizontal scalingNeeds sticky routingAny instance serves any request
Restart or deployCan lose sessionsTransparent to users
LatencyLowest for local readsExtra network hop per lookup
Failure impactUsers on that server affectedInstance is simply replaced
Typical examplesDatabases, game servers, legacy PHP sessionsREST APIs, modern web frontends

How to make a web app stateless

1. Move sessions to a shared store

For PHP, install the Redis extension and point sessions to Redis in php.ini:

session.save_handler = redis
session.save_path = "tcp://10.0.0.5:6379"

Laravel does the same with SESSION_DRIVER=redis in .env. Express apps can use connect-redis with express-session. Alternatively, use signed tokens (for example JWT) so the session data travels with the request, keeping in mind that tokens are hard to revoke before they expire.

2. Move uploads off local disk

Write user uploads to S3-compatible object storage or a shared volume instead of ./uploads. Serve them through a CDN or a dedicated path so any instance can generate a URL.

3. Share caches and counters

Rate limits, login attempt counters and cached fragments belong in Redis or Memcached, not in a per-process array, or each instance will count separately.

4. Make background work external

Long jobs should go to a queue with workers, not run inside a request handler that depends on the same process staying alive. Our guide to queue systems covers this pattern.

5. Keep configuration in the environment

Read settings from environment variables or a config file deployed with the release, so every instance starts identical.

A quick test

Run two copies of your app behind a load balancer with round-robin and no stickiness, log in, and click around. Then restart one copy. If nothing breaks, the app tier is stateless.

Where stateful still makes sense

Keep state local when the data is truly per-connection and short-lived (a WebSocket's subscription list), when latency is critical (game simulation), or when you run a single server and have no plans to scale out. Just document it, so the next person adding a second server knows sticky routing or a refactor is needed.

On a single VPS you can run the app, Redis and the database side by side, then split them onto separate servers as traffic grows. Compare plan sizes on the pricing page.

FAQ

Is a REST API always stateless?

REST encourages stateless requests, but an implementation can still keep sessions in local memory. What matters is whether any instance can serve the next request without data held only by another instance.

Does going stateless make my app slower?

Each session lookup adds a network round trip to Redis or the database, usually well under a millisecond on a private network. For most web apps that cost is small compared with the gains in reliability and scaling.

Are JWTs better than server-side sessions?

Neither is strictly better. JWTs avoid a lookup but are hard to revoke and grow with every claim. Server-side sessions in Redis are easy to invalidate. Many teams use short-lived tokens plus a revocation list or refresh tokens.

Related guides