CS › JVM

Blocking vs. non-blocking I/O, epoll, and Tomcat vs. Netty

Comparing sync/async and blocking/non-blocking I/O models, how Linux epoll scales socket multiplexing, and how Tomcat and Netty differ in Spring Boot.

Blocking vs. non-blocking I/O, epoll, and Tomcat vs. Netty

People often summarize Spring MVC vs. WebFlux as “Tomcat is blocking, Netty is non-blocking,” but Tomcat uses Linux epoll too. This post breaks down the four I/O models at the socket level, how epoll works in the kernel, and where Tomcat and Netty actually diverge.

Based on Linux Kernel 6.x (epoll), Java 21, and Spring Boot 3.x (Tomcat 10.1 / Reactor Netty).

When you choose between Spring MVC and Spring WebFlux, the short label “blocking vs. non-blocking” hides what happens inside the OS kernel. Both Tomcat and Netty rely on non-blocking socket multiplexing via epoll. The real difference shows up in how each server dispatches requests and executes your application code once bytes arrive.

Four combinations of blocking and sync I/O

Blocking vs. non-blocking describes whether a system call returns control to the calling thread right away. Synchronous vs. asynchronous describes who handles completion: either the caller checks and copies the result itself, or the kernel finishes the transfer and notifies the caller afterward.

Reading from a network socket (read()) has two phases: (1) waiting until data arrives in the kernel receive buffer, and (2) copying that data from kernel space into a user-space buffer. Blocking I/O parks the thread through both phases. Non-blocking I/O combined with epoll hands phase 1 over to kernel event monitoring so a thread only wakes up when data is ready.

ModelControl returnCompletion notificationCommon APIs
Sync + BlockingAfter completionCaller waitsInputStream.read(), JDBC
Sync + Non-BlockingImmediately (EAGAIN)Caller checks readinessO_NONBLOCK + epoll, Java NIO
Async + BlockingThread waitsCallee notifiesAsync call followed by Future.get()
Async + Non-BlockingImmediatelyKernel notifies on finishLinux io_uring, Windows IOCP

Java NIO and Netty on epoll fall under Sync + Non-Blocking, not true asynchronous I/O: the kernel notifies the event loop that a socket is readable, and the user-space thread still performs the read and buffer copy itself.

How Linux epoll solves I/O multiplexing

Assigning one OS thread per TCP connection breaks down around 10,000 concurrent connections (the C10K problem) because thread stacks eat memory and context switches dominate CPU time. I/O multiplexing solves this by letting a small pool of threads watch thousands of file descriptors (FDs).

Older mechanisms like select and poll copy the entire FD set into the kernel on every call and scan all of them in O(N) time (select also caps FD count at 1,024). Linux epoll keeps the watched FDs inside the kernel and delivers ready events in O(1) time.

flowchart TD
    A["Tens of thousands of client sockets"] -->|"epoll_ctl register (O(log N))"| B["Kernel epoll red-black tree"]
    B -->|"NIC packet arrival callback"| C["Kernel Ready List"]
    C -->|"epoll_wait returns active FDs"| D["EventLoop / Poller thread"]
    D -->|"Read & write handling"| E["Copy to user buffer & run handler"]

Three system calls drive the whole flow:

  • epoll_create1(): Allocates an epoll instance in the kernel containing a red-black tree for watched FDs and a ready list for active ones.
  • epoll_ctl(): Adds, updates, or removes an FD in the tree (O(log N)) once, eliminating repeated full-set copies on every poll.
  • epoll_wait(): Returns only the K active FDs placed on the ready list by kernel callbacks—O(1) relative to the total N connections.

epoll supports two trigger modes. Level-Triggered (LT, default) keeps notifying you as long as unread bytes remain in the buffer, which is safer and is what Java NIO uses by default. Edge-Triggered (ET) fires only once on a state transition; it cuts redundant wakeups, but your code must drain the socket until EAGAIN (Netty’s native epoll transport supports ET).

Tomcat vs. Netty in Spring Boot

Tomcat 10.1 in Spring Boot 3.x uses an NIO connector backed by an epoll-based Selector. The architectural split happens after the socket event fires:

  • Tomcat (Spring MVC): Acceptor accepts connections and Poller (epoll) watches for readable sockets, then dispatches each request to a worker thread pool (threads.max: 200 by default) using a thread-per-request model for controllers and database calls.
  • Netty (Spring WebFlux): A Boss group accepts connections, and a small Worker EventLoopGroup (sized to CPU core count) runs both epoll polling and the entire non-blocking pipeline (R2DBC, WebClient, reactive codecs) without switching to a separate request pool.
DimensionTomcat (Spring MVC)Netty (Spring WebFlux)
MultiplexingNIO Poller (epoll)EventLoop (epoll)
Execution modelThread-per-requestNon-blocking reactive pipeline
Default threadsthreads.max: 200Small pool tied to CPU cores
Best fitJPA/JDBC CRUD, transactional appsAPI gateways, streaming, high I/O fan-out

When not to choose WebFlux/Netty: If your service relies on JPA, JDBC, or blocking third-party SDKs, Tomcat with Spring MVC is simpler to debug, easier to trace, and avoids reactive complexity. Reach for Netty when you’re building an API gateway, SSE/WebSocket hub, or high-concurrency I/O fan-out service where every downstream client is non-blocking.

Inspecting Spring Boot threads and Linux sockets

Tomcat separates idle Keep-Alive connection capacity (max-connections) from concurrent request worker threads (threads.max), which is why a single Tomcat node can hold thousands of open connections with 200 workers:

1
2
3
4
5
6
7
8
# src/main/resources/application.yml
server:
  tomcat:
    threads:
      max: 200            # Max worker threads executing requests (default 200)
      min-spare: 10       # Minimum idle worker threads
    max-connections: 8192 # Max TCP connections tracked by Poller (epoll)
    accept-count: 100     # OS listen backlog queue beyond max-connections

On the Linux host, you can inspect the listen backlog, active connections, and the JVM’s epoll FD directly:

1
2
3
4
5
6
7
8
# 1. Check listen backlog queues (Recv-Q / Send-Q) on port 8080
ss -lnt | grep 8080

# 2. Count current ESTABLISHED TCP connections on port 8080
ss -ant state established '( sport = :8080 )' | wc -l

# 3. Verify the JVM's kernel epoll file descriptors
ls -l /proc/<JVM_PID>/fd | grep anon_inode:eventpoll

If Recv-Q climbs close to Send-Q, your application isn’t accepting connections fast enough and clients will start seeing connection timeouts or dropped SYN packets.

EventLoop starvation and production pitfalls

Calling Thread.sleep(), synchronous JDBC/JPA, or Mono.block() on a Netty EventLoop thread can freeze your entire WebFlux server.

On a 4-core container, Netty typically runs just 4 EventLoop threads to serve thousands of sockets. If a handler runs a 3-second JDBC query or Thread.sleep(3000), that thread cannot return to epoll_wait(). Four concurrent requests are enough to pin every EventLoop thread, causing EventLoop starvation where health checks and thousands of unrelated requests time out together.

1
2
3
4
5
// src/main/kotlin/com/example/profile/LegacyProfileService.kt
// Offload unavoidable blocking calls to a dedicated bounded scheduler
fun loadLegacyProfile(userId: Long): Mono<UserProfile> =
    Mono.fromCallable { legacyJdbcRepository.findById(userId) }
        .subscribeOn(Schedulers.boundedElastic())

Isolate unavoidable blocking calls with Schedulers.boundedElastic() and run BlockHound in your test suite so accidental blocking calls fail CI before reaching production. On Tomcat, the equivalent failure mode is worker thread pool exhaustion: when a slow downstream service ties up all 200 workers, set strict connect and read timeouts on every HTTP client and database pool.

  • Have you confirmed whether your downstream drivers (DB, Redis, HTTP) are truly non-blocking before choosing WebFlux?
  • In WebFlux, are legacy blocking calls wrapped in Schedulers.boundedElastic() and guarded by BlockHound tests?
  • In Tomcat, do all outbound HTTP clients and JDBC pools have explicit connect and read timeouts?
  • Are you monitoring Recv-Q vs. Send-Q and active worker threads during load tests?
This post is licensed under CC BY 4.0 by the author.