Spring MVC (async) vs Spring WebFlux

前端 未结 2 743
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 19:58

I\'m trying to understand Spring WebFlux. The things I\'ve found so far are reactive at the core, no Servlet API, no thread per request, HTTP 2, server pushes, application/strea

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 20:46

    The Servlet async model introduces an async boundary between the container threads (1 Servlet request/thread model) and the processing of the request in your application. Processing can happen on a different thread or wait. In the end, you have to dispatch back to a container thread and read/write in a blocking way (InputStream and OutputStream are inherently blocking APIs).

    With that model, you need many threads to achieve concurrency (because many of those can be blocked waiting for I/O). This costs resources and it can be a tradeoff, depending on your use case.

    With non-blocking code, you only need a few threads to process a lot of requests concurrently. This is a different concurrency model; like any model, there are benefits and tradeoffs coming with it.

    For more information about that comparison, this Servlet vs. Reactive stacks talk should be of interest.

提交回复
热议问题