Hindhustani Posted January 23, 2023 Report Posted January 23, 2023 (edited) Spring boot @controller @service @repository stereotype are singleton and stateless , and thread safe , correct? If so any thread safe and singleton class are in slow processing? If so how does millions of reaquests are processed and handled ? Let’s say if there are 100 requests coming to REST API method in controller class , since it is singleton in nature only one request handle at a time ? or my understanding is incorrect that 100 threads meaning 100 instance of controller class created and each request handled by each controller instance ? @dasari4kntr @csrcsr @Vaampire Edited January 23, 2023 by Hindhustani 1 Quote
Popular Post IamBhagatSingh Posted January 23, 2023 Popular Post Report Posted January 23, 2023 10 minutes ago, Hindhustani said: Spring boot @controller @service @repository stereotype are singleton and stateless , and thread safe , correct? If so any thread safe and singleton class are in slow processing? If so how does millions of reaquests are processed and handled ? Let’s say if there are 100 requests coming to REST API method in controller class , since it is singleton in nature only one request handle at a time ? or my understanding is incorrect that 100 threads meaning 100 instance of controller class created and each request handled by each controller instance ? @dasari4kntr @csrcsr @Vaampire Even though they are singleton by default, Spring boot uses thread pool to handle the requests parallelly 1 2 Quote
areyentiraidhi Posted January 23, 2023 Report Posted January 23, 2023 Spring doesnt guarantee thread safety. Servlet containers like tomcat handle threads. Tomcat will retain a thread pool ( default is 200) which will serve the requests. so when request comes in , a thread is allocated from the pool and its used during the request duration. every thread can access singleton bean beucase they are on shared memory called heap. its important make sure you deisgn tge app to be stateless to avoid thread related issuess 1 Quote
ramudu Posted January 23, 2023 Report Posted January 23, 2023 20 minutes ago, IamBhagatSingh said: Even though they are singleton by default, Spring boot uses thread pool to handle the requests parallelly 1 minute ago, areyentiraidhi said: Spring doesnt guarantee thread safety. Servlet containers like tomcat handle threads. Tomcat will retain a thread pool ( default is 200) which will serve the requests. so when request comes in , a thread is allocated from the pool and its used during the request duration. every thread can access singleton bean beucase they are on shared memory called heap. its important make sure you deisgn tge app to be stateless to avoid thread related issuess Great answers but I think not really address original question..... Please see the question again Quote
dasari4kntr Posted January 23, 2023 Report Posted January 23, 2023 40 minutes ago, Hindhustani said: or my understanding is incorrect that 100 threads meaning 100 instance of controller class created and each request handled by each controller instance ? YES... controller also a bean.. java heap is...globally shared memory accessible to all the running threads within an application.. when the spring container creates a bean with the singleton scope...the bean is stored in the heap... this way, all the concurrent threads are able to point to the same bean instance... Quote
areyentiraidhi Posted January 23, 2023 Report Posted January 23, 2023 2 minutes ago, ramudu said: Great answers but I think not really address original question..... Please see the question again I already answered In spring, every request will be executed in separate thread. These threads will work with singleton beans separately. Every thread has its own stack. stack. has blocks when methods are called, which has local parameters values , references of objects. This stack memory is thread exclusive, cant be accessed by other threads. since all threads can access singleton beans on heap, very important to be stateless. 1 Quote
Vaampire Posted January 23, 2023 Report Posted January 23, 2023 No exp with springboot. Sorry 1 Quote
ramudu Posted January 23, 2023 Report Posted January 23, 2023 1 hour ago, Hindhustani said: Spring boot @controller @service @repository stereotype are singleton and stateless , and thread safe , correct? If so any thread safe and singleton class are in slow processing? If so how does millions of reaquests are processed and handled ? Let’s say if there are 100 requests coming to REST API method in controller class , since it is singleton in nature only one request handle at a time ? or my understanding is incorrect that 100 threads meaning 100 instance of controller class created and each request handled by each controller instance ? @dasari4kntr @csrcsr @Vaampire I think you got how spring boot internally handle request from other answers , now coming to how single class is not slow (singleton) and process millions of requests -> this is the core of scalability design (refer @dasari4kntr tech learns thread for more details) let's say you want your application to process 100K request per sec , it is very highly impossible for single instance to handle this , so you scale your application into multiple instance , lets say each instance can process 10K , you need 10 different instance to run simultaneously to process 100K request , now as other told , each time a request come to container (tomcat / jobss/ netty) , they create a separate thread and store your request / response information and call your Spring controller (this is singleton) this will go to Controller Stack, controller will take the top request from Stack and process request and call the service layer and immediately available to process next request , meanwhile your service/repository process and send the response back to controller , that will go to controller Stack and wait for controller time , once controller complete current request , it will take the top request from Stack and process it , once process complete it will send that to caller , here the container , container finally send that to caller (actual IP address who send this request) Quote
Kurrodu1 Posted January 23, 2023 Report Posted January 23, 2023 1 hour ago, Hindhustani said: Spring boot @controller @service @repository stereotype are singleton and stateless , and thread safe , correct? If so any thread safe and singleton class are in slow processing? If so how does millions of reaquests are processed and handled ? Let’s say if there are 100 requests coming to REST API method in controller class , since it is singleton in nature only one request handle at a time ? or my understanding is incorrect that 100 threads meaning 100 instance of controller class created and each request handled by each controller instance ? @dasari4kntr @csrcsr @Vaampire Not thread safe unless your design makes sure. Singleton classes are slow? Why? The performance of a service depends on bottlenecks with in. Bottlenecks can be backend database or IO resource availability etc. If your design handles the bottle necks well you can handle any number of requests by scaling your servers horizontally Quote
dasari4kntr Posted January 23, 2023 Report Posted January 23, 2023 6 minutes ago, ramudu said: working bro..did you tag me..? Quote
ramudu Posted January 23, 2023 Report Posted January 23, 2023 Just now, dasari4kntr said: bro..did you tag me..? I was typing the answer but not completed it , see above now Quote
dasari4kntr Posted January 23, 2023 Report Posted January 23, 2023 7 minutes ago, ramudu said: I think you got how spring boot internally handle request from other answers , now coming to how single class is not slow (singleton) and process millions of requests -> this is the core of scalability design (refer @dasari4kntr tech learns thread for more details) AOP is helping there... Quote
ramudu Posted January 23, 2023 Report Posted January 23, 2023 1 minute ago, dasari4kntr said: AOP is helping there... , I didn't get how AOP help here bro? AOP purpose is completely different kadha? Quote
dasari4kntr Posted January 23, 2023 Report Posted January 23, 2023 12 minutes ago, ramudu said: , I didn't get how AOP help here bro? AOP purpose is completely different kadha? spring uses a combination of thread-safety through configuration and AOP... to ensure that singleton beans are thread-safe and can handle multiple requests simultaneously.... the thread-safety through configuration is achieved by allowing each thread... to have its own copy of the thread-local variables and AOP allows spring to add synchronization to specific methods of a singleton bean... so that multiple threads can safely access the bean at the same time.... 1 1 Quote
Hindhustani Posted January 23, 2023 Author Report Posted January 23, 2023 2 hours ago, IamBhagatSingh said: Even though they are singleton by default, Spring boot uses thread pool to handle the requests parallelly Bro , singletons per jvm there will be only one , even if there are thread pools at one layer it may speed little bit but when it comes to actual processing other thread have to wait until current thread finished ? @ramudu @dasari4kntr Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.