Stateless vs Stateful AI Services
You'll learn to
- -Say what counts as state in an AI application, including state on the GPU
- -Explain why keeping state in a shared store lets any worker serve any request
- -Compare sticky routing with a shared store, and say what each does when a worker dies
- -Treat local state as a cache that can be lost, never as the only copy
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.
The model call itself is stateless: the API does not remember your last message, and the application resends the history every time. But the application around it is full of state. The conversation so far, the user's saved preferences, a half-finished upload, an agent's scratchpad, a rate-limit counter. Where that state lives decides whether you can add a worker, restart one, or lose one without anyone noticing.
An Analogy: The Teller Who Remembers You
A bank teller who remembers your whole history is wonderful, until their shift ends, or until the queue sends you to the next window. Now imagine instead that every teller works from the same shared customer file. Any teller can serve you, a teller can go home mid-day, and you never repeat yourself. The remembering teller is a stateful service. The shared file is what makes the others stateless.
What Counts as State
- -Conversation history, user memory and preferences.
- -Work in progress: an upload being chunked, an agent's plan and tool results.
- -Counters and windows: rate limits, token budgets, circuit breaker state.
- -State on the accelerator: the KV cache and prefix cache a GPU server builds while it answers, covered in the inference module.
The simulation below runs three workers behind a router and four users sending six messages each. A turn counts as correct if the worker that handled it could see the whole conversation so far. A stateful worker keeps history in its own memory. A stateless worker reads and writes a shared store. Sticky routing sends each user to the same worker every time.
Stateful workers behind plain round robin fail almost every turn, 4 of 24 correct, because a user's next message lands on a worker that never saw the first one. Sticky routing repairs that, all 24 correct, until worker 0 dies before turn 4. The users who lived there lose their history and only 18 of 24 turns are right. The stateless workers do not care about routing at all. Round robin is fine, and the crash costs nothing, because the history was never in the worker.
The Rule, and the One Good Exception
Put the authoritative copy of anything you cannot afford to lose in a shared store such as a database or a replicated cache. Keep local state only as a cache that can be rebuilt. The shared store becomes a dependency you must keep fast and replicated, and each request pays one read for it, which is the price of being able to scale and recover freely.
The exception is deliberate. A GPU server that has already processed a long shared prompt holds that work in its prefix cache, so routing the next request to the same server is faster. This is stickiness for speed, not for correctness. If the server is gone, the request goes elsewhere and simply recomputes. Lose the cache and you lose time, never data.
A useful test for any piece of local state: if this worker vanished right now, what would the user lose? If the answer is 'only some speed', it is a cache. If the answer is 'their conversation', move it to the shared store.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!