What is webflux and mono in spring boot?

Spring WebFlux is Spring's reactive, non-blocking web framework built on Project Reactor, designed to handle high volumes of concurrent requests with a small number of threads. Mono is one of WebFlux's two core reactive types, representing an asynchronous stream that emits at most one item or completes empty.

Key Points: • WebFlux runs on Netty by default instead of the servlet-based Tomcat stack used by Spring MVC. • Mono represents zero-or-one results, while Flux represents zero-to-many results in a stream. • Reactive pipelines are composed declaratively using operators like map, flatMap, and filter. • Backpressure lets a subscriber control how much data a publisher sends, avoiding overload. • WebFlux suits I/O-heavy, high-concurrency workloads more than CPU-bound processing.

Example: A service method that fetches a single user by ID returns Mono<User> instead of User, and the caller chains .map() or .flatMap() to transform the result once it becomes available, without blocking the calling thread.

Code Example:

@GetMapping("/users/{id}")
public Mono<User> getUser(@PathVariable String id) {
    return userRepository.findById(id)
            .switchIfEmpty(Mono.error(new UserNotFoundException(id)));
}

Interview Tip: A concise interview answer is:

"WebFlux is Spring's reactive stack for non-blocking, asynchronous web applications, typically running on Netty. Mono represents a stream that emits zero or one value, which I use for operations like fetching a single record, as opposed to Flux, which handles zero-to-many streams."