Servicios sin Estado
Stateless · Estado externalizado
Escrito por Darjus Kazakevic · revisado el
Any instance can serve any request, because nothing from the last one stayed in memory.
A stateless service still has state. It just keeps it outside the process, where every instance can reach it. The classic leak is a session stored in an in-memory map. It works on one instance and breaks on two, so the load balancer is told to pin users to instances. From then on, a deploy logs users out and a crash loses their carts.
The cost is a round trip. A request that read a local variable now reads Redis or verifies a token, which adds about a millisecond and a dependency. The test is simple: kill any instance at any moment. If no user notices, the service is stateless.
En esencia — Keep state between requests out of the process, in a token or a shared store, so any instance can serve any request and can be killed at any time.
Cuándo recurrir a él
Recurre a él cuando
- A service runs as several instances behind a load balancer
- Instances are replaced often by deploys, autoscaling or crashes
- You want deploys and scale-downs to be safe at any hour
Reconsidéralo cuando
- Long-lived connections, like WebSockets, that are stateful by nature
- Latency budgets too tight for a store round trip on every request
- A tool that runs as one process and will never run twice
Conceptos clave
- Session state
- What a user's next request needs from the last one: who they are, their cart, their language. Keep it in a signed token or a shared store such as Redis, never in a process variable.
- Sticky sessions (Session affinity)
- A load balancer setting that sends a user to the same instance every time. It hides in-memory state instead of removing it, so deploys, crashes and scale-downs still lose that state.
- Externalised state
- Each kind of state goes to a store built for it. Sessions go to Redis, uploads to object storage such as S3, job progress to a database table, and cached data to a shared cache with a TTL.
- Stateful by nature
- Some work holds state by design: WebSocket connections, game servers, stream processors. Keep that in-memory state small, keep the durable part in a store, and let clients reconnect to any instance.
Reglas de diseño
- Pass the kill test
- Kill any instance at any moment. If a user notices, it was holding state. Move sessions to a signed token or a shared store, uploads to object storage, and job progress to a table.
- Don't hide state behind sticky sessions
- Session affinity makes in-memory state work until a deploy, a crash or a scale-down. Use it, if at all, to keep a cache warm, never as the place a user's data lives.
- Write files to object storage
- A container's disk disappears on the next deploy, and the next request lands on another instance. Upload to S3 or similar, and keep only the object key in the database.
- Budget for the round trip
- Shared state puts the session store on every request's critical path. Count that latency in your budget, and treat the store's availability as your own service's availability.
Errores habituales
- A module-level cache with no TTL. Each instance holds a different value, and users see data change between requests.
- Uploads written to the local disk. The next request lands on another instance and gets a 404, and the next deploy deletes the file.
- Long-lived JWTs with no way to revoke them. A stolen token stays valid for days; keep expiry short and refresh it.
- In-memory rate limits or counters. With N instances, each allows the full limit, so the real limit is N times higher.