Quick revision
The short answer
Before redirecting to the identity provider, Spring Security saves the OAuth2 authorization request, including the state and nonce, in the HTTP session, and checks the callback against it. With four pods and no shared session, the callback lands on a pod without that session about three times in four, which matches the 70 percent failures. The best fix is Spring Session with Redis or JDBC, next a cookie-based AuthorizationRequestRepository, and sticky sessions only as a temporary patch.
What to remember
In this answer, we saw that Spring Security saves the OAuth2 authorization request, with its state and nonce, in the HTTP session and checks the callback against it, so with four pods and local sessions about three logins in four fail. The best fix is a shared session store through Spring Session, which also fixes every other session feature; a signed, encrypted cookie repository suits a sessionless app; and sticky sessions only hide the problem until pods change.
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.
Before redirecting to the identity provider, Spring Security saves the OAuth2 authorization request, including the state and nonce, in the HTTP session, and checks the callback against it. With four pods and no shared session, the callback lands on a pod without that session about three times in four, which matches the 70 percent failures. The best fix is Spring Session with Redis or JDBC, next a cookie-based AuthorizationRequestRepository, and sticky sessions only as a temporary patch.
Why this question is asked
This is one of the most common failures when a web app moves from one VM to several pods, and the error message names a Spring Security internal that many developers have never looked at. Interviewers like it because the fraction of failures is a clue: once we know how the login flow works, 70 percent almost explains itself. In this answer, we will recap the OAuth2 authorization code flow, see exactly where Spring stores the request, do the arithmetic that matches the logs, and then go through the fixes from most to least preferable with configuration for each.
1A Quick Recap of the OAuth2 Login Flow
In the authorization code flow, our app does not see the user's password. It redirects the browser to the identity provider with a few parameters, the user logs in there, and the provider redirects the browser back to our callback URL with a short-lived code. Our app exchanges that code for tokens.
Two parameters protect this flow. The state is a random value our app generates and expects to see again on the callback, which stops an attacker from injecting their own callback (a cross-site request forgery). The nonce, used with OpenID Connect, ties the returned ID token to this login attempt. To check them, our app must remember what it sent.
2Where Spring Security Keeps the Request
Spring Security keeps the outgoing OAuth2AuthorizationRequest, which holds the state, nonce, redirect URI and requested scopes, in an AuthorizationRequestRepository. The default implementation, HttpSessionOAuth2AuthorizationRequestRepository, stores it in the HTTP session. When the callback arrives, the login filter loads it from the session by the state parameter; if it is not there, it rejects the callback with the error code authorization_request_not_found.
On one VM the session lives in that VM's memory, and the callback always returns to the same JVM, so it always works. On four pods, each pod has its own in-memory sessions.
8 messages in order. Play them, or step through them one at a time.
Pod C has never seen session a1, so it cannot match the state.
3Matching the Arithmetic to the Logs
If the load balancer picks a pod at random or in turn, the callback reaches the same pod that started the login with a probability of one in four. So we expect about 75 percent of logins to fail, and 70 percent observed is close to that. The small difference usually comes from users who retry and get lucky, or from connection reuse that sends a browser's next request to the same pod.
When a failure rate matches 1 - 1/N for N replicas, it is a strong hint that some state is local to one replica. Here the state is the session, and the OAuth2 request is simply the first feature to trip over it.
4Fix 1: Share the Session With Spring Session
The preferred fix is to move HTTP sessions out of the pods into a shared store. Spring Session replaces the servlet container's session with one backed by Redis, JDBC or another store, without changing application code. Every pod then sees the same session, so the saved authorization request is found whichever pod receives the callback.
This also fixes every other feature that quietly depends on the session: the SecurityContext of logged-in users, the saved request that sends a user back to the page they wanted, CSRF tokens stored in the session, and flash attributes.
Notice that Redis now holds every session, so it needs a replica and failover, and the session must be serialisable. In return, pods can restart and scale freely without logging anyone out.
5Fix 2: Keep Only the Login Request in a Cookie
If we want the app to keep no server-side session at all, for example because it uses JWTs for everything after login, we can replace the repository. A custom AuthorizationRequestRepository writes the authorization request into a cookie that is signed and encrypted, marked HttpOnly and Secure, with SameSite=Lax so it is sent on the provider's top-level redirect back, and expires after a few minutes. On the callback it reads and verifies the cookie, then deletes it.
The signature stops tampering and the encryption hides the nonce. This fixes the login, but anything else that uses the session, such as the logged-in user's SecurityContext, still needs its own stateless design.
6Fix 3: Sticky Sessions, as a Stopgap
Session affinity at the ingress, for example a cookie that pins a browser to one pod, makes the callback return to the pod that stored the request. It works quickly, which is why it is tempting. But when that pod restarts, is evicted or is removed by scale-in, its sessions vanish and logins in progress fail again; it also spreads load unevenly. I would use it only to stop the bleeding while Spring Session is rolled out.
| Fix | Fixes | Cost |
|---|---|---|
| Spring Session (Redis or JDBC) | login and every session feature | run a shared store, serialisable sessions |
| Encrypted cookie repository | the login flow only | custom code, key management |
| Sticky sessions at ingress | most logins, until pods change | breaks on restarts and scale-in, uneven load |
7Follow-up questions
Which other features of the app probably depend on the session?
The logged-in user's SecurityContext, which Spring Security stores in the session by default, so even after a successful login the next request to another pod may look anonymous. The saved request used to return the user to the page they first asked for. CSRF tokens if the default session-based repository is used. Flash attributes in redirect-after-post flows, and any controller that stores data with @SessionAttributes or HttpSession directly.
Why does the state check exist at all? Could we just disable it?
No. The state ties the callback to a login the same browser started; without it an attacker could make a victim's browser complete a login with the attacker's code and end up signed in to the attacker's account. The nonce plays a similar role for the ID token. Disabling them would trade an availability bug for a security hole.
How would you confirm the diagnosis before changing anything?
Scale the deployment to one pod in a test environment and check that logins all succeed, then scale to four and watch failures return. The pod logs also show the authorization redirect and the failing callback with the same state value on different pod names.
8Conclusion
In this answer, we saw that Spring Security saves the OAuth2 authorization request, with its state and nonce, in the HTTP session and checks the callback against it, so with four pods and local sessions about three logins in four fail. The best fix is a shared session store through Spring Session, which also fixes every other session feature; a signed, encrypted cookie repository suits a sessionless app; and sticky sessions only hide the problem until pods change.
Open with
Spring Security's OAuth2 login saves the authorization request, which holds the state and the nonce, in the HTTP session before redirecting the user to the identity provider. When the provider redirects back, the callback is checked against that saved request.
Then, if they want more
On one VM that always works, but with four pods and in-memory sessions, the callback goes to a random pod, and three times out of four that pod has no such session, so it fails with authorization_request_not_found. That matches the 70 percent we see.
My preferred fix is Spring Session with Redis or JDBC, so all pods share sessions; it also fixes everything else that depends on the session, like the security context and saved requests. If the app should have no server sessions, I would write an AuthorizationRequestRepository that keeps the request in a signed, encrypted, short-lived cookie.
Sticky sessions at the ingress would make the error go away quickly, but they break again when pods restart or scale in, so I would only use them as a temporary measure while the real fix ships.
About this answer
- Last updated
- 13 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 "Explain the failure, and give the fixes from most to least preferable.": https://scrivio-interview-library.netlify.app/architecture/sd-fundamentals/a50857aef3/