🍃 Spring Boot Intermediate

What is Spring WebFlux and reactive programming?

Why Interviewers Ask This

This tests whether you can apply Spring Boot knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Spring WebFlux is Spring's reactive web framework introduced in Spring 5, built on Project Reactor and Netty. It supports asynchronous, non-blocking request handling — critical for high concurrency with limited threads. vs Spring MVC: Spring MVC uses blocking I/O (one thread per request); WebFlux uses event-loop model (few threads, non-blocking). WebFlux shines when: making many concurrent external API calls, handling real-time data, WebSocket streams. Core reactive types: Mono<T> — 0 or 1 asynchronous element (like CompletableFuture); Flux<T> — 0 to N asynchronous elements (like a stream). Reactive controller: @RestController @RequestMapping("/api/users") public class UserController { @GetMapping("/{id}") public Mono<User> getUser(@PathVariable Long id) { return userRepository.findById(id); } @GetMapping public Flux<User> getAllUsers() { return userRepository.findAll(); } @PostMapping public Mono<ResponseEntity<User>> createUser(@RequestBody Mono<User> userMono) { return userMono.flatMap(userRepository::save).map(u -> ResponseEntity.status(201).body(u)); } }. Reactive repositories: extend ReactiveCrudRepository or ReactiveMongoRepository (MongoDB natively reactive). Spring Data R2DBC for reactive SQL databases. WebClient (reactive HTTP client): replaces RestTemplate: WebClient.create().get().uri("https://api.example.com/data").retrieve().bodyToMono(Data.class).subscribe(data -> process(data));. When NOT to use WebFlux: CPU-intensive work, blocking libraries (JDBC), simple CRUD — Spring MVC is simpler and performs similarly.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Spring Boot experience.