Prev Next

Spring / Spring Webflux Interview questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is Reactive Programming?

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.

2. What is Spring Webflux?

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.

3. Is Spring WebClient thread safe?

Yes, WebClient is thread-safe since it is immutable.

4. Is Spring RestTemplate deprecated?

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.

5. What are the benefits of using Spring Webflux?

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.

6. What is bodyToMono?

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.

7. Difference between Mono and Flux.

A Mono, which can either return zero or one result before completing,

And a Flux, which can return zero to many, possibly infinite, results before completing.

8. How to handle errors in Spring WebFlux?

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.
9. Difference between Spring MVC @async and Spring Webflux?

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.

10. What is Reactor netty?

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.

11. What are router functions?

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();
}

12. What does Mono.defer() do?

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.

13. When should we choose Reactor over RxJava?

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.

14. How to handle an exception in a Spring Webflux based application?

Different ways to handle exceptions.

  • onErrorReturn,
  • onErrorResume,
  • onErrorMap,
  • and Handling Errors at a Global Level.
15. What is bodyToFlux?

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);

16. Disadvantages of Using Reactive Streams.

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.

17. Difference between the Web client and Web Test Client?

WebClientWebTestClient
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.
18. Can we use SpringMvc and webflux together?

No. SpringMVC cannot be run on Netty.

19. What are the main components of Spring Webflux?

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.

20. What is the Flux API?

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.

21. What is a Publisher? Explain its main interfaces.

A Publisher is a reactive component that emits a stream of data. The two main interfaces are Publisher and Subscriber.

22. Explain the difference between hot publishers and cold publishers.

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.

23. Explain throttling.

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.

«
»
Apache Shiro Interview questions

Comments & Discussions