Backend developer interview question · 3–5 years
A Spring service bean keeps the current user in a field so its helper methods can read it. Under load, users start seeing each other’s data.
What is wrong, and what is the fix?
The strongest answer
Beans are singletons by default — one instance serves every concurrent request — so a mutable field is shared state; keep the bean stateless and pass the request data in as parameters
Why it works
Exactly — one instance, many threads. Statelessness is the fix; scope tricks only work around a design mistake.
What a good answer covers
- Singleton bean, many threads.
- Keep request state out of fields.
- Pass it in as a parameter.
- Scope tricks hide the design bug.
Answers that fall short
- Close, but not the strongest: That field is not thread-safe, so I would synchronise the methods that touch it
A lock stops the field being corrupted but still lets one request read the value another request put there. - Weak: I would change the bean to prototype scope, so each injection point gets its own instance
Prototype scope creates an instance per injection, not per request, so a singleton holding it still shares one. - Red flag: It only appears under load, so I would add a note to the code review checklist and ship the release
Users seeing each other’s data is not a checklist item; that is an incident waiting for a busy Monday.
Who gets asked this
More Backend developer questions
- You are designing a Python service that fans out to many slow downstreams. What shapes the design?
- Your service calls three internal APIs and one is intermittently slow. What do you build in?
- You are asked to make a payments service active in two regions. What is the first design question?
- Product wants dashboards over the transactional database. When do you say no and build a separate analytical…
- Design multi-tenancy for a product where some tenants are regulated.
- One endpoint renders PDFs and pegs a core for about two seconds. It is stalling an otherwise healthy Node…
- Your worker cannot keep up with the producer and memory climbs. What is the right answer?
- A payment endpoint can be retried by clients. How do you stop double charges?