Connection, Request and Thread in classical vs reactive approach










0















I'm investigating what reactive means and because it is kind of low level difference, compared to the common non-reactive approach, I'd like to understand what is going on. Let's take Tomcat as a server(I guess it will be different for netty)



Non-reactive



  1. Connection from the browser is created.

  2. For each request thread from thread pool is taken, which will process it.

  3. After the thread finished processing, it returns the result through the connection back to other side.

Reactive???



How is it done for Tomcat or Netty. I cannot find any decent article about how Tomcat supports reactive apps and how Netty does that differently(Connection, Thread, request level explanation)



What bothers me is how reactive is making the webserver unblocking, when you still need to wait for the response. You can get first part of the response quicker maybe with reactive, but is it all? I guess the main point of reactivness is effective thread utilization and this is what I am asking about.










share|improve this question


























    0















    I'm investigating what reactive means and because it is kind of low level difference, compared to the common non-reactive approach, I'd like to understand what is going on. Let's take Tomcat as a server(I guess it will be different for netty)



    Non-reactive



    1. Connection from the browser is created.

    2. For each request thread from thread pool is taken, which will process it.

    3. After the thread finished processing, it returns the result through the connection back to other side.

    Reactive???



    How is it done for Tomcat or Netty. I cannot find any decent article about how Tomcat supports reactive apps and how Netty does that differently(Connection, Thread, request level explanation)



    What bothers me is how reactive is making the webserver unblocking, when you still need to wait for the response. You can get first part of the response quicker maybe with reactive, but is it all? I guess the main point of reactivness is effective thread utilization and this is what I am asking about.










    share|improve this question
























      0












      0








      0








      I'm investigating what reactive means and because it is kind of low level difference, compared to the common non-reactive approach, I'd like to understand what is going on. Let's take Tomcat as a server(I guess it will be different for netty)



      Non-reactive



      1. Connection from the browser is created.

      2. For each request thread from thread pool is taken, which will process it.

      3. After the thread finished processing, it returns the result through the connection back to other side.

      Reactive???



      How is it done for Tomcat or Netty. I cannot find any decent article about how Tomcat supports reactive apps and how Netty does that differently(Connection, Thread, request level explanation)



      What bothers me is how reactive is making the webserver unblocking, when you still need to wait for the response. You can get first part of the response quicker maybe with reactive, but is it all? I guess the main point of reactivness is effective thread utilization and this is what I am asking about.










      share|improve this question














      I'm investigating what reactive means and because it is kind of low level difference, compared to the common non-reactive approach, I'd like to understand what is going on. Let's take Tomcat as a server(I guess it will be different for netty)



      Non-reactive



      1. Connection from the browser is created.

      2. For each request thread from thread pool is taken, which will process it.

      3. After the thread finished processing, it returns the result through the connection back to other side.

      Reactive???



      How is it done for Tomcat or Netty. I cannot find any decent article about how Tomcat supports reactive apps and how Netty does that differently(Connection, Thread, request level explanation)



      What bothers me is how reactive is making the webserver unblocking, when you still need to wait for the response. You can get first part of the response quicker maybe with reactive, but is it all? I guess the main point of reactivness is effective thread utilization and this is what I am asking about.







      multithreading tomcat netty spring-webflux reactive






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 9:19









      ZveratkoZveratko

      7261443




      7261443






















          1 Answer
          1






          active

          oldest

          votes


















          2














          The last point by you : " I guess the main point of reactiveness is effective thread utilization and this is what I am asking about.", is exactly what reactive approach was designed for.



          So how does effective utilization achieved?



          Well, as an example, lets say you are requesting data from a server multiple times.



          In a typical non-reactive way, you will be creating/using multiple threads(may be from a thread-pool) for each of your requests. And job of one particular thread is only to serve that particular request. The thread will take the request, give it to the server and waits for its response till the data is fetched from the server, and then bring that data back to the client.



          Now, in a Reactive way, once there is a request, a thread will be allocated for it. Now if another request comes up, there won't be creation of another thread, rather it will be served by the same thread. How?
          The thread when takes a request to the server, it won't wait for any immediate response from the server, rather it will come back and serve other request.
          Now, when server searches for the data and it is available with the server, an event will be raised, and then the thread will go to fetch that data. This is called Event-loop mechanism as all the work of calling the thread when data is available is achieved by invoking an event.
          Now, there is complexity assigned with it to map exact response to requests.
          And all these complexity is abstracted by Spring-Webflux(in Java).
          So the whole process becomes non-blocking. And as only one thread is enough to serve all the requests, there will be no thread switching we can have one thread per CPU core. Thus achieving effective utilization of threads.



          Few images over the net to help you understand: ->enter image description here



          enter image description here



          enter image description here



          enter image description here






          share|improve this answer

























          • Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

            – Zveratko
            Nov 26 '18 at 10:00











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334774%2fconnection-request-and-thread-in-classical-vs-reactive-approach%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          The last point by you : " I guess the main point of reactiveness is effective thread utilization and this is what I am asking about.", is exactly what reactive approach was designed for.



          So how does effective utilization achieved?



          Well, as an example, lets say you are requesting data from a server multiple times.



          In a typical non-reactive way, you will be creating/using multiple threads(may be from a thread-pool) for each of your requests. And job of one particular thread is only to serve that particular request. The thread will take the request, give it to the server and waits for its response till the data is fetched from the server, and then bring that data back to the client.



          Now, in a Reactive way, once there is a request, a thread will be allocated for it. Now if another request comes up, there won't be creation of another thread, rather it will be served by the same thread. How?
          The thread when takes a request to the server, it won't wait for any immediate response from the server, rather it will come back and serve other request.
          Now, when server searches for the data and it is available with the server, an event will be raised, and then the thread will go to fetch that data. This is called Event-loop mechanism as all the work of calling the thread when data is available is achieved by invoking an event.
          Now, there is complexity assigned with it to map exact response to requests.
          And all these complexity is abstracted by Spring-Webflux(in Java).
          So the whole process becomes non-blocking. And as only one thread is enough to serve all the requests, there will be no thread switching we can have one thread per CPU core. Thus achieving effective utilization of threads.



          Few images over the net to help you understand: ->enter image description here



          enter image description here



          enter image description here



          enter image description here






          share|improve this answer

























          • Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

            – Zveratko
            Nov 26 '18 at 10:00















          2














          The last point by you : " I guess the main point of reactiveness is effective thread utilization and this is what I am asking about.", is exactly what reactive approach was designed for.



          So how does effective utilization achieved?



          Well, as an example, lets say you are requesting data from a server multiple times.



          In a typical non-reactive way, you will be creating/using multiple threads(may be from a thread-pool) for each of your requests. And job of one particular thread is only to serve that particular request. The thread will take the request, give it to the server and waits for its response till the data is fetched from the server, and then bring that data back to the client.



          Now, in a Reactive way, once there is a request, a thread will be allocated for it. Now if another request comes up, there won't be creation of another thread, rather it will be served by the same thread. How?
          The thread when takes a request to the server, it won't wait for any immediate response from the server, rather it will come back and serve other request.
          Now, when server searches for the data and it is available with the server, an event will be raised, and then the thread will go to fetch that data. This is called Event-loop mechanism as all the work of calling the thread when data is available is achieved by invoking an event.
          Now, there is complexity assigned with it to map exact response to requests.
          And all these complexity is abstracted by Spring-Webflux(in Java).
          So the whole process becomes non-blocking. And as only one thread is enough to serve all the requests, there will be no thread switching we can have one thread per CPU core. Thus achieving effective utilization of threads.



          Few images over the net to help you understand: ->enter image description here



          enter image description here



          enter image description here



          enter image description here






          share|improve this answer

























          • Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

            – Zveratko
            Nov 26 '18 at 10:00













          2












          2








          2







          The last point by you : " I guess the main point of reactiveness is effective thread utilization and this is what I am asking about.", is exactly what reactive approach was designed for.



          So how does effective utilization achieved?



          Well, as an example, lets say you are requesting data from a server multiple times.



          In a typical non-reactive way, you will be creating/using multiple threads(may be from a thread-pool) for each of your requests. And job of one particular thread is only to serve that particular request. The thread will take the request, give it to the server and waits for its response till the data is fetched from the server, and then bring that data back to the client.



          Now, in a Reactive way, once there is a request, a thread will be allocated for it. Now if another request comes up, there won't be creation of another thread, rather it will be served by the same thread. How?
          The thread when takes a request to the server, it won't wait for any immediate response from the server, rather it will come back and serve other request.
          Now, when server searches for the data and it is available with the server, an event will be raised, and then the thread will go to fetch that data. This is called Event-loop mechanism as all the work of calling the thread when data is available is achieved by invoking an event.
          Now, there is complexity assigned with it to map exact response to requests.
          And all these complexity is abstracted by Spring-Webflux(in Java).
          So the whole process becomes non-blocking. And as only one thread is enough to serve all the requests, there will be no thread switching we can have one thread per CPU core. Thus achieving effective utilization of threads.



          Few images over the net to help you understand: ->enter image description here



          enter image description here



          enter image description here



          enter image description here






          share|improve this answer















          The last point by you : " I guess the main point of reactiveness is effective thread utilization and this is what I am asking about.", is exactly what reactive approach was designed for.



          So how does effective utilization achieved?



          Well, as an example, lets say you are requesting data from a server multiple times.



          In a typical non-reactive way, you will be creating/using multiple threads(may be from a thread-pool) for each of your requests. And job of one particular thread is only to serve that particular request. The thread will take the request, give it to the server and waits for its response till the data is fetched from the server, and then bring that data back to the client.



          Now, in a Reactive way, once there is a request, a thread will be allocated for it. Now if another request comes up, there won't be creation of another thread, rather it will be served by the same thread. How?
          The thread when takes a request to the server, it won't wait for any immediate response from the server, rather it will come back and serve other request.
          Now, when server searches for the data and it is available with the server, an event will be raised, and then the thread will go to fetch that data. This is called Event-loop mechanism as all the work of calling the thread when data is available is achieved by invoking an event.
          Now, there is complexity assigned with it to map exact response to requests.
          And all these complexity is abstracted by Spring-Webflux(in Java).
          So the whole process becomes non-blocking. And as only one thread is enough to serve all the requests, there will be no thread switching we can have one thread per CPU core. Thus achieving effective utilization of threads.



          Few images over the net to help you understand: ->enter image description here



          enter image description here



          enter image description here



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 at 5:41

























          answered Nov 26 '18 at 9:18









          AshishAshish

          687




          687












          • Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

            – Zveratko
            Nov 26 '18 at 10:00

















          • Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

            – Zveratko
            Nov 26 '18 at 10:00
















          Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

          – Zveratko
          Nov 26 '18 at 10:00





          Is it right that client will wait for the response the same time minus the switching times in reactive implementation? In case of lower load nearly identical. What about DefferedResult it will create other thread to process the request as well. So setting server thread pool to number of cores and such mechanism is nearly identical. I think reactivity will apply the even-loop approach on each level. In my eyes the reactive DB drivers only provides the event-like communication. Unless you are using async I/O there must be thread pool somewhere wrapped with events.

          – Zveratko
          Nov 26 '18 at 10:00



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334774%2fconnection-request-and-thread-in-classical-vs-reactive-approach%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          Category:Rhetoric