Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are multiple requests handled by single thread in Spring Boot until certain requests threshold is reached?

In my Spring Boot 2.1.6 project (based on Tomcat) I have a rest controller. I added a default constructor to it which prints something. I thought in Tomcat-based servers each request is handled in separate thread. So I expected each request to trigger a new controller object and as a result new print from the constructor. However, I made a test of sending 30 requests to the rest controller and I only see that the print was made once. So as far as I understand the rest controller handles all those requests in one thread.

My question is whether indeed multiple requests are handled in a single thread or maybe there's certain request threshold upon which another thread will be opened? I'm using default Spring Boot configuration perhaps this is controlled somewhere in the config?

This is the code for my controller:

@RestController
public class TrackingEventController {

    public TrackingEventController() {
        System.out.println("from TrackingEventController");
    }

    @RequestMapping(method=GET, path=trackingEventPath)
    public ResponseEntity<Object> handleTrackingEvent(
            @RequestParam(name = Routes.event) String event,
            @RequestParam(name = Routes.pubId) String pubId,
            @RequestParam(name = Routes.advId) String advId) {

        return new ResponseEntity<>(null, new HttpHeaders(), HttpStatus.OK);
    }
}
like image 733
hitchhiker Avatar asked Aug 02 '19 09:08

hitchhiker


People also ask

How does Spring boot handle multiple requests at the same time?

In Spring, every request is executed in a separate thread. For example, when two users want to log in at the same time, the JVM creates two threads: one thread for the first user and another one for the second user. And these threads work with the singleton bean separately.

How many requests can Spring boot handle at a time?

There simply is a maximum number of threads (IIRC the default is 200) and if you do more the extra requests will just timeout after a while.

Does Spring boot use multiple threads?

Multithreading in spring boot is similar to multitasking, except that it allows numerous threads to run concurrently rather than processes. A thread in Java is a sequence of programmed instructions that are managed by the operating system's scheduler.

How many requests can a thread handle?

No, it can't. One thread will work on one request until it's done.


2 Answers

You're mixing two orthogonal concepts:

  1. a thread
  2. a controller instance

A single thread could create and/or use one, or several controller instances.

Multiple threads could also create and/or use one, or several controller instances.

The two are unrelated.

And how it actually works is

  • Spring beans are singletons by default, so Spring creates a single instance of your controller
  • A servlet container uses a pool of threads.
  • Every time a request comes in, a thread is chosen from the pool, and this thread handles the request. If the request is mapped to your controller, then the appropriate method of the unique controller instance is executed by this thread.

If you want to know which thread is handling the current request, add this to your controller method:

System.out.println(Thread.currentThread().getName());
like image 162
JB Nizet Avatar answered Oct 20 '22 17:10

JB Nizet


Spring boot Tomcat thread pool default size is 200. You can make out that different threads server different requests. Put a debug point on some REST endpoint, and call it multiple times from Postman etc. From debugger, check the thread name. s.b.

enter image description here

like image 21
Apurva Singh Avatar answered Oct 20 '22 17:10

Apurva Singh