Jump to content

SpringBoot question


Hindhustani

Recommended Posts

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 by Hindhustani
  • Like 1
Link to comment
Share on other sites

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

 

  • Thanks 1
Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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. 

  • Upvote 1
Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

12 minutes ago, ramudu said:

sorry , 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....

  • Thanks 1
  • Upvote 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...