Spring Web Flux - The Non Blocking Asynchronous Functional Reactive Web Framework, Java, Spring 5

Author: Shazin Sadakath


Spring Web Flux Framework can be used with its latest Release Candidate Release 5.0.0.RC3 if you want to try it out. You just need to add the Spring Milestone Repository in your gradle or maven file.

 

    lk.techtalks
    spring-webflux-test
    1.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            5.0.0.RC3
        
        
            org.springframework
            spring-webflux
            5.0.0.RC3
        
        
            org.springframework.boot
            spring-boot-starter-web
            2.0.0.M3
        
    
    
    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/libs-milestone
        
            false
        
    
    

I have written the following service which emulates a delay of maximum 1000 milliseconds in a service to test how the conventional Spring Web MVC vs Spring Web Flux work.

@Service
public class PersonService {

    private final List PEOPLE = new ArrayList<>();

    {
        PEOPLE.add(new Person("Shazin"));
        PEOPLE.add(new Person("Shahim"));
        PEOPLE.add(new Person("Shuhail"));
        PEOPLE.add(new Person("Shuhaib"));
        PEOPLE.add(new Person("Athiyab"));
        PEOPLE.add(new Person("Athif"));
    }

    public List getPeople() {
        try {
            Thread.sleep(new Random(System.currentTimeMillis()).nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Collections.unmodifiableList(PEOPLE);
    }
}



And the following controller implementations one with a Blocking conventional Spring Web MVC Controller method which returns a list to get people and a Non Blocking Spring Web Flux Controller method which returns a Fluxwhich is a defferred result and processed differently.

@RestController
@RequestMapping("/people")
public class PeopleController {

    @Autowired
    private PersonService blockingService;

    @GetMapping
    public Flux getPeople() {
        return Flux.fromIterable(blockingService.getPeople());
    }

    @GetMapping("/blocking")
    public List getPeopleBlocking() {
        return blockingService.getPeople();
    }

}



And Tested sending 1000 concurrent requests to both methods using a Gatling test. The Application was running inside of a Tomcat Container.

Spring Web MVC results were


And Spring Web Flux results were


If you compare the 99th Percentile, Max and Mean response times of the both results you can see Spring Web Flux is considerably faster without any code changes to improve performance. This is a promising sign. The numbers can be improved even more if we use the Netty Reactive Servers instead of conventional Tomcat servers in my understanding.

 



Tags: Spring5 WebFlux Reactive
Views: 733
Register for more exciting articles

Comments

Please login or register to post a comment.


There are currently no comments.