Spring / Spring Webflux Interview questions
Reactive programming is a programming paradigm that deals with asynchronous data streams and the specific propagation of change, which means it implements modifications to the execution environment in a certain order.
The primary benefits of reactive programming are, better utilization of computing resources on multicore and multi-CPU hardware and better performance by truncating serialization.
Spring WebFlux is part of Spring 5, it is a parallel version of Spring MVC and supports fully non-blocking reactive streams. It supports the backpressure concept and uses Netty as the inbuilt server to run reactive applications.
Yes, WebClient is thread-safe since it is immutable.
RestTemplate allows you to consume Rest resources in a synchronous manner, which means it will block the thread before it receives a response. RestTemplate has been deprecated since Spring 5.
The spring-webflux is not necessarily faster but it is better in dealing with the issues of scalability and efficient use of hardware resources. It is also better at dealing with the issues of latency.
The method bodyToMono() is extracts the body to a Mono instance. The method Mono.block() subscribes to this Mono instance and blocks until the response is received while Mono.subscribe() method is non-blocking.
A Mono
And a Flux
Handle Errors With,
- onErrorReturn (), to return a static default value whenever an error occurs.
- onErrorResume (), to compute a dynamic fallback value, (or) execute an alternative path with a fallback method, (or) Catch, wrap and re-throw an error.
- Global Level by customizing the Global Error Response Attributes and implement the Global Error Handler.
Spring Async I/O model during its communication with the client is blocking while Spring Webflux doesn't block.
Spring WebFlux supports more Web/App servers such as Netty, Undertow.
Processing the request body in Spring Async is blocking while it is non-blocking with Spring Webflux.
Reactor Netty is an asynchronous event-driven network application framework. It provides non-blocking and backpressure-ready TCP, HTTP, and UDP clients and servers. As the name implies, it's based on the Netty framework.
RouterFunction is a functional alternative to the @RequestMapping and @Controller annotation style used in standard Spring MVC.
We can use it to route requests to the handler functions.
1 2 3 4 5 | @Bean public RouterFunction<ServerResponse> listEmployees(EmployeeService es) { return route().GET("/employee", req -> ok().body(es.findAll())) .build(); } |
Mono. defer(monoSupplier) lets you provide the whole expression that supplies the resulting Mono instance. The evaluation of this expression is deferred until somebody subscribes. Inside of this expression you can additionally use control structures like Mono.
When you run Mono.just() it creates immediately an Observable(Mono)and reuses it but when you use defer it doesn't create it immediately it creates a new Observable in every subscribe.
RxJava support project which is based on JDK8- and Project Reactor supports JDK 8+. RxJava has too many problems which can cause Out of Memory if you can't use it well. It is preferred to use Reactor for JDK8+ projects.
Different ways to handle exceptions.
- onErrorReturn,
- onErrorResume,
- onErrorMap,
- and Handling Errors at a Global Level.
Similar to bodyToMono, however here it is used to to retrieve a collection resource of type Flux from endpoint /stocks.
Flux<Stock> stockFluxObj = client.get() .uri("/stocks") .retrieve() .bodyToFlux(Stock.class); stockFluxObj.subscribe(System.out::println);
Troubleshooting a Reactive application is difficult compared to the regular applications.
There is an extra learning curve when implementing reactive applications.
There is limited support for reactive data stores since traditional relational data stores have yet to embrace the reactive paradigm.
WebClient | WebTestClient |
Web client acts as a reactive client who performs non-blocking HTTP requests. | Webtestclient also acts as a reactive client that can be used in tests.. |
It can handle reactive streams with backpressure. | It can bind directly to WebFlux application by applying mock request and response objects. |
It can take advantage of JAVA 8 Lambdas. | It can connect to any server over an HTTP connection. |
No. SpringMVC cannot be run on Netty.
There are 2 main components of Spring Webflux: RouterFunction and the HandlerFunction. The RouterFunction is responsible for mapping incoming requests to the appropriate HandlerFunction. The HandlerFunction is responsible for handling the request and returning a response.
The Flux API is a reactive programming library for Java that allows developers to easily build asynchronous applications. The library is based on the Reactive Streams specification, and provides a wide variety of operators that can be used to manipulate data streams.
A Publisher is a reactive component that emits a stream of data. The two main interfaces are Publisher and Subscriber.
A hot publisher is a publisher that emits data even if there is no Subscriber subscribed to it. A cold publisher is a publisher that only emits data when there is at least one Subscriber subscribed to it.
Throttling limits the amount of data that can be transferred between two systems in a given period of time. This is often done to prevent one system from overloading another system with too much data, or to prevent one user from consuming too much of a shared resource.