Quick revision
The short answer
A service is stateless when any instance can serve any request, because no user state lives only in one instance's memory or local disk. With several instances behind a load balancer, we keep HTTP sessions in a shared store with Spring Session, backed by Redis or JDBC, or use self-contained tokens such as signed JWTs. We avoid local disk for uploads and per-instance caches that must agree, treat sticky sessions as a weaker option because losing a pod loses its users' state, and coordinate background work such as scheduled jobs across instances.
What to remember
In this answer, we saw that a service is stateless when any instance can serve any request because no user state lives only in one instance. Sessions move to a shared store with Spring Session on Redis or JDBC, or into signed tokens; sticky sessions work but lose users' state when a pod goes away. Uploaded files go to object storage, caches that must agree need sharing or invalidation, and scheduled jobs need a lock or a clustered scheduler so they run once.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A service is stateless when any instance can serve any request, because no user state lives only in one instance's memory or local disk. With several instances behind a load balancer, we keep HTTP sessions in a shared store with Spring Session, backed by Redis or JDBC, or use self-contained tokens such as signed JWTs. We avoid local disk for uploads and per-instance caches that must agree, treat sticky sessions as a weaker option because losing a pod loses its users' state, and coordinate background work such as scheduled jobs across instances.
Why this question is asked
Running a service on one instance hides many assumptions: the session is in memory, the uploaded file is in /tmp, the nightly job runs once. The moment we run three instances behind a load balancer, each assumption becomes a bug, such as users logged out at random or a job that sends every email three times. Interviewers ask this to see whether we know what state is and where it should live. In this answer, we will define stateless, see what goes wrong with in-memory sessions, move sessions to Spring Session or tokens, compare sticky sessions, and deal with files, caches and scheduled jobs.
1A Quick Recap: What Stateless Means
State is anything a request leaves behind that a later request depends on: a logged-in user, a shopping cart, an uploaded file, a progress counter. A service is stateless when it does not keep such state only inside one instance. It can still use state, but the state lives in a shared place, such as a database, Redis or object storage, that every instance can reach.
The benefit is that instances become interchangeable. The load balancer can send any request to any instance, we can add instances when traffic rises, remove them when it falls and restart them during deployments without users noticing.
2What Goes Wrong With In-Memory Sessions
By default, a servlet container such as Tomcat keeps HTTP sessions in the memory of the instance that created them, identified by a JSESSIONID cookie. With one instance that works. With three, the user logs in on instance A, and the next request goes to instance B, which has never heard of that session.
6 messages in order. Play them, or step through them one at a time.
The user is logged out at random, whenever the load balancer picks a different instance.
3Shared Sessions With Spring Session, or Tokens
Spring Session replaces the container's session storage with a shared store, while our code keeps using HttpSession as before. Adding the Redis module to a Spring Boot application is usually enough for Boot to configure it; a JDBC module stores sessions in database tables instead.
Now the session lives in Redis under its id, and any instance that receives the cookie loads it from there. Attributes must be serializable, because they travel over the network, and they should be small: a few ids rather than whole object graphs, since the session is read on every request.
The other option is self-contained tokens. Instead of a session id, the client carries a signed token, such as a JWT, that holds the user's identity and claims. Any instance can verify the signature without a shared store. The trade-off is that a token cannot easily be revoked before it expires, so tokens are usually short-lived, and they should not carry large or frequently changing data such as a cart.
4Sticky Sessions: Simple but Fragile
Sticky sessions, also called session affinity, make the load balancer send all requests with the same session cookie to the same instance. It works without code changes, which is why it is popular for legacy applications.
But the state still lives in one instance. When that pod is restarted during a deployment, evicted by Kubernetes or crashes, all its users lose their sessions. Load also spreads unevenly, because a new instance only receives new users, and scaling down means choosing which users to log out. Sticky sessions can be a temporary step during a migration, not the design we aim for.
| Approach | Survives a pod loss | Cost |
|---|---|---|
| In memory, no affinity | no, and broken with 2+ pods | none |
| Sticky sessions | no, users on that pod lose it | uneven load |
| Spring Session + Redis | yes | one Redis call per request |
| Spring Session + JDBC | yes | database load per request |
| Signed tokens (JWT) | yes, nothing stored | hard to revoke early |
5Files, Caches and Background Jobs
Sessions are the obvious state, but not the only one. Uploaded files written to local disk exist only on one instance, and in a container they vanish when the pod is replaced. We store them in object storage, such as S3 or a compatible service, and keep only the reference in the database.
Local caches are fine for data that may be slightly different between instances for a short time, such as reference data with a short expiry. They are a problem when instances must agree, for example a cache of a user's permissions that one instance invalidates after a change while the others keep serving the old value. Such data needs a shared cache or invalidation messages.
Background work is the last trap. A @Scheduled method runs on every instance, so with three instances the nightly job runs three times. We coordinate with a lock in a shared store, for example ShedLock with a database table, or with a scheduler that supports clustering, such as Quartz with its JDBC job store, so only one instance runs each job.
6Follow-up questions
Is a JWT-based service completely stateless?
Its instances are, because any instance can verify a token. But some state usually remains somewhere: refresh tokens, a revocation list for logged-out or compromised tokens, and the user's data in a database. Statelessness is about instances, not about the system having no state.
Redis or JDBC for Spring Session?
Redis is the common choice for high traffic because session reads are fast and expiry is built in. JDBC avoids running Redis if the application already has a database, at the cost of extra database load and a cleanup job for expired sessions. For a small internal application, JDBC is often enough.
What happens to sessions if Redis goes down?
Requests that need the session fail or treat the user as logged out, so Redis becomes part of the availability path. Run it with replication and failover, keep sessions small, and decide how the application should degrade, for example by allowing browsing but not checkout.
7Conclusion
In this answer, we saw that a service is stateless when any instance can serve any request because no user state lives only in one instance. Sessions move to a shared store with Spring Session on Redis or JDBC, or into signed tokens; sticky sessions work but lose users' state when a pod goes away. Uploaded files go to object storage, caches that must agree need sharing or invalidation, and scheduled jobs need a lock or a clustered scheduler so they run once.
Open with
Stateless means any instance can handle any request, because nothing a later request depends on lives only in one instance's memory or disk. The service can use state, but it keeps it in shared stores.
Then, if they want more
The classic problem is the HTTP session: Tomcat keeps it in memory, so with three instances the user logs in on one and looks logged out on the next. I would fix that with Spring Session, usually backed by Redis, or JDBC for smaller apps.
The code still uses HttpSession, but the session is stored centrally, so attributes must be serializable and small. The alternative is short-lived signed tokens, which need no store but are hard to revoke early.
Sticky sessions work without code changes, but the state is still in one pod, so a deployment or eviction logs those users out and load is uneven; I would only use them as a temporary step.
Then the less obvious state: uploads go to object storage with a reference in the database, caches that must be consistent need a shared cache or invalidation, and scheduled jobs need coordination, for example ShedLock or clustered Quartz, so the nightly job runs once and not once per pod.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What makes a Spring Boot service stateless, and how do you handle HTTP sessions when you...": https://scrivio-interview-library.netlify.app/spring-boot/scalability/454a84454f/