Spring

Request 처리 방법(WebFlux VS MVC)

inuma 2021. 3. 18. 23:45

리액티브 프로그래밍이란?

  • 데이터를 처리 할 때, non-blocking, asynchronous, event-driven 한 접근 방식을 가진 프로그래밍 패러다임
  • "reactive" 는 데이터의 변화에 반응하도록 구축된 프로그래밍 모델을 의미한다.
  • publisher-subscriber(observer pattern)으로 구현
  • non-blocking code에서 중요한 점으로는 back pressure(controls of the rate of events)이다.
    (너무 많은 producer가 destination의 처리 성능을 넘어선 event를 생성하지 않도록 하는 것)

 

Spring Web Flux

  • 어떠한 thread도 wait 상태로 대기하지 않는다.
    일반적으로는 request를 받는 1개의 request thread만 존재한다.
  • 모든 request는 event handler와 callback 정보가 주어지고
    request thread는 해당 request를 thread pool에 위임한다.
    thread pool에서는 해당 request를 event handler 함수에게 위임하고 즉시 다른 request를 처리하기 시작한다.
  • event handler가 완료 되었을 때,
    thread pool에서 thread가 response들을 수집하고 callback함수에게 넘긴다.

 

Spring MVC

  • 모든 request에 대해서 servlet thread가 생성되고,
    생성된 servlet thread는 worker thread에게 request를 delegate하게 된다.
  • worker thread가 작업하는 동안, servlet thread는 block되게 된다.
    따라서 synchronous request processing이라 불린다.
  • server는 제한된 수의 request thread(servlet thread)를 가질 수 있기 때문에,
    서버가 최대로 수용할 수 있는 capability를 제한하게 된다.

 

참고