Sub topic 6
How WebFlux runs requests: the event loop and Netty
This chapter explains how WebFlux serves requests with few threads. It assumes you know the thread-per-request model, basic Spring Boot, and the idea of a Mono or Flux from earlier chapters.
From thread-per-request to event loop
Spring MVC on a servlet container gives each request its own thread from a pool. If the handler waits on a database or a remote call, that thread sits idle until the answer comes back. With 200 threads, the 201st concurrent slow request has to wait.
WebFlux runs on a different model. A small, fixed number of threads, called event-loop threads, each handle many connections. An event-loop thread never waits for I/O. It registers interest in an event, such as "bytes arrived on this socket", and moves on to other work. When the operating system reports the event, a loop thread picks it up and runs the next step.
By default, Spring Boot's WebFlux starter uses Netty, an asynchronous network library. Netty creates roughly one event-loop worker thread per CPU core. Each connection is bound to one worker for its lifetime. Because that worker is the only thread touching the connection, no locking is needed.
What happens during a request
- Netty reads bytes from a socket on an event-loop thread and decodes the HTTP request.
- WebFlux finds the matching controller method or router function and calls it on that same thread.
- The method returns a
MonoorFluxalmost immediately. It has only assembled a pipeline. - WebFlux subscribes to the pipeline. When the database driver or
WebClientfinishes an I/O operation, the event loop receives the result and runs the next operator. - Each emitted element is encoded and written to the socket. When the pipeline completes, the response ends.
Nothing in this sequence parks a thread. Waiting is represented as a pending callback, not a blocked thread.
You can see the threads
This controller reports which thread runs it. Run it and call /thread.
@RestController
public class ThreadController {
@GetMapping("/thread")
public Mono<String> thread() {
return Mono.fromSupplier(() -> Thread.currentThread().getName());
}
}With Netty you will see names such as reactor-http-nio-2. The same few names appear again and again, however many requests you send. In Spring MVC the name would come from a Tomcat pool like http-nio-8080-exec-7.
Choosing a server
WebFlux does not require Netty. It also runs on Undertow, Jetty, and Tomcat (through the Servlet 3.1+ non-blocking API). Netty is the default because it is built around the event loop from the start. Switching servers only means changing the starter dependency. Your controllers and pipelines stay the same.
The consequence: never block a loop thread
Because a few threads serve everyone, one blocking call can freeze many requests. If a handler calls Thread.sleep(5000) or a blocking JDBC query on an event-loop thread, every connection assigned to that thread stalls for that time. On a four-core machine with four workers, four such calls stop the whole server.
Typical culprits are:
- JDBC and other blocking database drivers
RestTemplateand other blocking HTTP clientsMono.block()orFlux.blockFirst()inside a handler- Heavy CPU work, such as large JSON transformations or hashing
A later chapter covers how to detect these calls and move them to a suitable Scheduler.
When this model helps
The event loop pays off when requests spend most of their time waiting on I/O, such as calling other services, streaming, or holding many open connections. It does not make CPU-bound code faster. If your workload is simple CRUD on a blocking database, Spring MVC can be the simpler choice.
Exercise
A WebFlux application on Netty runs on an 8-core machine. A handler calls a blocking JDBC query that takes 2 seconds, directly on the event-loop thread. Explain what happens to other requests when 8 of these arrive at the same time.
Show answer
Netty has about 8 event-loop threads. Each of the 8 requests occupies one thread for 2 seconds, so all workers are blocked. Other requests, even trivial ones, cannot be read or answered until a worker frees up. The server appears frozen for about 2 seconds. The fix is to use a non-blocking driver or move the blocking call to Schedulers.boundedElastic().