principleMajorpending
Principle: Prefer stateless services
Viewed 0 times
statelessscalinghorizontalsessionexternal-storecloud
Problem
Stateful services are hard to scale horizontally, hard to recover from failures, and create sticky sessions that complicate load balancing.
Solution
Design services to be stateless — all state lives in external stores:
Bad: In-memory session store (lost on restart)
Good: Redis, database, or JWT tokens
Bad: Local filesystem (not available on other instances)
Good: S3, GCS, or MinIO
Bad: In-process cache (different per instance)
Good: Redis/Memcached (shared cache)
Bad: In-memory job list (lost on restart)
Good: Redis Queue, Celery, SQS
Bad: Local config file modified at runtime
Good: Environment variables, config service
Benefits of stateless:
Exception: Some state is OK to keep in-process:
- Session state -> External store:
Bad: In-memory session store (lost on restart)
Good: Redis, database, or JWT tokens
- File uploads -> Object storage:
Bad: Local filesystem (not available on other instances)
Good: S3, GCS, or MinIO
- Cache -> External cache:
Bad: In-process cache (different per instance)
Good: Redis/Memcached (shared cache)
- Background jobs -> Job queue:
Bad: In-memory job list (lost on restart)
Good: Redis Queue, Celery, SQS
- Configuration -> Environment or config service:
Bad: Local config file modified at runtime
Good: Environment variables, config service
Benefits of stateless:
- Scale horizontally: add instances behind load balancer
- Recover from crashes: restart anywhere, nothing lost
- Deploy easily: any instance can handle any request
- Test simply: no state to set up or clean up
Exception: Some state is OK to keep in-process:
- Read-only cached data with TTL
- Connection pools (to databases, not to clients)
- Compiled templates or regex patterns
Why
Stateless services are cloud-native. They can be scaled, restarted, and deployed independently without data loss or session disruption.
Revisions (0)
No revisions yet.