Skip to main content
foundations

Stateful vs stateless

8 min read
Fully authored

Why stateless services are cheaper to scale — and where state has to live instead.

A stateless service holds no memory between requests — anything it needs, it fetches from a shared store. A stateful service keeps state in memory (session, cache, WebSocket connection). This distinction decides your entire scaling story.

Scaling comparison — N → N+1

Stateless: add 4th instance
  • Spin up new VM
  • Register with load balancer
  • Instantly serving traffic
  • Time: seconds
Stateful: add 4th instance
  • Spin up new VM
  • Migrate 1/4 of state from existing instances
  • Rebalance routing (consistent hashing)
  • Verify data replicated + consistent
  • Time: minutes to hours

The interview soundbite

“Make the API tier stateless — sessions in Redis, uploads to S3, business logic on demand. State lives in the datastore, and only the datastore. This is the single most important decision for horizontal scaling.”

Where state has to live

Session state

Redis or DynamoDB — client sends session ID, backend fetches on demand.

User preferences

Same as session — fetch on login, cache in-memory per request.

File uploads

S3 with pre-signed URLs — browser uploads directly.

WebSocket state

Can't avoid stateful WS servers. Use sticky routing (Slack team-affinity).

Rate-limit counters

Redis atomic counters — shared across all API instances.

Feature flags

LaunchDarkly / Unleash — fetched once at startup, updated via SSE.

Practice what you just read

Every foundation concept has a companion quiz to close the loop.