无状态服务不在请求之间存储客户端状态(每个请求都是独立的),而有状态服务维护状态。无状态性对可扩展性很重要——无状态服务非常容易进行水平扩展。
无状态与有状态
text
STATELESS → the service keeps NO client state between requests:
→ each request contains all needed info; any server can handle any request
→ state lives ELSEWHERE (database, cache, client, token) if needed
STATEFUL → the service MAINTAINS state across requests:
→ a specific server holds a client's state (session in memory, etc.)
→ requests must go to the SAME server (or state must be shared/synchronized)
无状态如何帮助可扩展性
text
STATELESS services scale horizontally EASILY:
→ any server handles any request → load balancing is simple (any server works)
→ add/remove servers freely; a failed server doesn't lose state → resilient
→ no sticky sessions or state synchronization needed
STATEFUL services are HARDER to scale:
→ requests tied to specific servers (sticky sessions); a server failure loses its state;
scaling needs state sharing/replication → complexity
→ PREFER STATELESS services for scalability (a key design principle).
