How does the singleton Bean serve the concurrent request?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A singleton bean is a single shared object instance, not a single-threaded object. In a web application, many request threads can call methods on that same bean at the same time, so the real question is not how the bean serves concurrency, but whether the code inside the bean is safe under concurrent access.
What Singleton Scope Actually Means
In frameworks such as Spring, singleton scope usually means one bean instance per application context. The container creates the object once and reuses it for every component that depends on it.
When HTTP requests arrive, the web container assigns each request to a worker thread. If twenty users hit the same endpoint, twenty threads may enter the same singleton service concurrently.
This service is safe because it is stateless. Every value needed for the calculation comes in as a method parameter, and the method uses only local variables.
Why Stateless Beans Work Well
Local variables belong to the executing thread's stack, not to shared object state. That means one request cannot overwrite another request's temporary values when the bean is designed as a stateless service.
This is why singleton scope is the default for many service classes. It avoids repeated object creation and works well as long as the bean does not keep mutable request-specific data in fields.
A second example shows the same pattern:
Even with many concurrent requests, each invocation builds its result independently.
Where Concurrency Problems Begin
Trouble starts when a singleton bean stores mutable state in instance fields. Those fields are shared across all threads.
counter++ is not atomic. Two threads can read the same value, increment it, and both write back the same result. That causes lost updates.
A safer implementation uses an atomic type.
If the bean truly needs shared mutable state, you need thread-safe structures, locking, atomic classes, or an external store.
Spring Singleton Versus EJB Singleton
The word singleton appears in multiple Java ecosystems, but the concurrency model differs.
In Spring, singleton scope mostly describes lifecycle and sharing. The framework does not make your bean thread-safe for you.
In EJB, @Singleton beans can use container-managed concurrency rules. You can mark methods as read or write operations and let the container coordinate access.
That model is useful when shared in-memory state is intentional and controlled.
Choosing a Better Scope for Request Data
If data belongs to one request, it usually should not live in a singleton field at all. In Spring, request-scoped components are a better place for per-request state.
The singleton service can still orchestrate work, but request-specific values stay isolated.
Practical Rule of Thumb
If a singleton bean only reads collaborators, uses method parameters, and creates local variables, it is usually safe. If it mutates shared fields, assume you have a concurrency problem until you prove otherwise.
That rule explains why most service beans work fine under load while a small number of cache-like or counter-like beans create difficult race conditions.
Common Pitfalls
A common misconception is believing that one singleton instance means one request at a time. It does not. Multiple threads may enter the same bean concurrently.
Another mistake is storing request data in instance fields for convenience. That can leak values across users and create nondeterministic bugs.
Developers also sometimes add synchronized everywhere without measuring the cost. That may fix correctness but create a throughput bottleneck if the bean is hot.
Finally, some shared state simply belongs somewhere else. If the data must survive restarts or be shared across multiple application nodes, a database or distributed cache is often the better choice.
Summary
- Singleton scope means one shared instance, not serialized access.
- Many request threads can call the same singleton bean at once.
- Stateless singleton beans are usually safe and are the normal design.
- Mutable instance fields require explicit thread-safety measures.
- Put request-specific data in request scope instead of singleton fields.
Related reading
- How does the ThreadStatic attribute work?
- How does this ConcurrentDictionary LazyTaskT code work?
- How does thread pooling works, and how to implement it in an async/await env like NodeJS?
- How does ThreadPoolExecutor.map differ from ThreadPoolExecutor.submit?
- How find all unused classes in Intellij Idea?
- How good is Java's UUID.randomUUID?
- How efficient is locking an unlocked mutex? What is the cost of a mutex?
- How exactly is a coroutine suspended?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.