Avoiding Reactor meltdown and using Spring WebFlux for live football matchPublished on: Author: Erwin Manders Category: Java & Web
Recently, I looked back at the Spring One Platform (S1P) event in Austin, Texas. This event was hosted by Pivotal and focused primarily on the Pivotal Platform and Spring Boot. A very insightful experience. In this blog, I would like to highlight two interesting developments.
Avoiding Reactor Meltdown
When using Reactive programming, there are a few pitfalls to consider. The simplest variant is calling a service method that is blocking, such as:
String getGreeting() throws Exception;
This method is blocking, because:
A) It doesn’t return a reactive stream such as a Mono
B) It can throw exceptions
To make this method non-blocking, its method description should change to (For example):
This will return either 0 or 1 objects as a stream. By making its implementation to return a Mono stream, regardless of success or error, it can be called in non-blocking fashion.
Besides programmatical method calls, API calls between different applications can also be blocking. For example, your API needs to call an old SOAP service that requires a blocking call. When building a reactive stack, the main purpose is to never call a blocking API in your reactive flow.
There are several ways to deal with this, but I will mention two:
1) Build a separate microservice around the SOAP service that returns the blocking call as a Reactive stream, using Webflux
2) Make sure the blocking call happens outside the threadpool of your reactive flow, but has its own threadpool that doesnt interfere
Last but not least, if you're building a reactive app and are looking for blocking code in your app, try BlockHound: https://github.com/reactor/BlockHound
This tool, once activated, will throw errors when your reactive stack attempts to make a blocking call (don't use it in production though).
Live football match score using Spring WebFlux and Apache Kafka
This demo features a Turkish live football scoring app. Their previous app had very disappointing performances, so they decided to use event streaming to improve it.
The app features a WebFlux-based API to subscribe to a match score. This will add a subscriber to a Kafka topic for a certain match, as long as the match exists. When an event is triggered for this match, such as a goal, the event will be pushed on the Kafka bus for that match's topic. This allows all subscribers to see this event.
Previously, because so many events occurred at hundreds of football matches across the world, it was possible that a 3-1 score would be pushed before the 1-0. Now, the event-streaming architecture ensures the events always occur in correct order.
There is also no need for the publisher of the event to wait for all the subscribers to process, which increases the performance of the app tremendously. This demo specifically has inspired me to start thinking about event-based data streaming using reactive stack in my own work at Rabobank.
Mark it down in your calendar: Next year, Spring One Platform will be held from September 21 to 24 in Seattle!
If you want to know more about Spring One Platform or any of the new developments, don’t hesitate to send Erwin a message at firstname.lastname@example.org or @erwin_manders (Twitter).