5
  1. If multiple request are hit to a single RestController at the same time in a application, how it is handle for different scenarios (Multiple request to a single endpoints (only GET), or Multiple requests for multiple endpoints(GET, POST, PUT...))
  2. Is multi-threading concept utilized? If yes is it possible to handle the requests in FIFO pattern?
  3. What is the maximum request a RestController can take ?
  4. Does RestController scope affect handling of requests (behavior of request scope with default scope-singleton) ?
  5. Also how it is handle by Application context (example with flow will be helpful)

Considering building Micro-services with Spring Boot 2.

1 Answer 1

6

From the point of view of Spring (Application Context) rest controller is a singleton if not specified otherwise.

So the code of controller must be ready to be invoked by multiple threads simultaneously.

When a new request reaches the server, in a tradition thread-per-request model the web server (like tomcat) is responsible to allocate a thread from the predefined pool of threads to the request. Then the request gets processed by controller in the context of this thread.

The actual thread pool implementation can in general vary from server to server, but in general, its something that can be configured (number of threads per loop, queue size to store requests for future processing if the pool is full, etc.)

Now regarding the Scope of RestController. If the controller is stateless (and it should be for many cases, just keep it singleton). If you need the new Instance of controller to be created per request, than change the scope. Obviously each thread will have to use the same (in case of singleton scope) instance of rest controller or spring mvc will create a new instance of controller if you specify another scope.

All the answer above applies to a "traditional" thread-per-request model.

Note that since spring 5 / spring boot 2 spring also supports "Reactive" model with a webflux. It works on top of netty and doesn't utilize a thread-per-request model. Please specify in the question if you're interested in this model rather than a tradition model that I've tried to briefly describe.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @mark for traditional model brief, in my scenario i am implementing Micro-services with Spring Boot 2 version so I am utilizing "Reactive" model with webflux (not aware how it handles the request). Can you brief a little to this model ?
Also how RestController will behave to request scope as compared it with default scope (singleton)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.