Tomcat vs Netty — What's the Difference?
Feature / Aspect | Tomcat | Netty |
Built for | Servlet-based web apps (Spring MVC) | General-purpose networking (low-level) |
I/O model | Blocking or semi-blocking | Fully non-blocking, event-driven |
Protocols | HTTP/1.1, some HTTP/2 | HTTP/1.1, HTTP/2, WebSocket, TCP, UDP |
Use case | Traditional web servers | Reactive systems, custom protocols |
Abstraction level | High (Servlets, Filters) | Low (ChannelHandler, ByteBuf) |
Default in Spring | ✅ Spring MVC | ✅ Spring WebFlux |
Can be replaced? | Yes — swap Tomcat with Jetty, etc. | Yes — Netty can be replaced with others like Undertow |
Native support for WebFlux | ❌ Not optimal | ✅ Designed for reactive streams |
Java NIO Model (what Tomcat builds on):
Tomcat’s NIO connector does use callback-like logic, especially when working with Selector and SelectionKey in Java NIO.
- Poller thread waits for events using a Selector
- When a socket is ready, it enqueues the work to an Executor (thread pool)
- A worker thread eventually handles the actual HTTP request
Selector selector = Selector.open();
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ);
while (true) {
selector.select(); // blocks until I/O is ready
Set<SelectionKey> keys = selector.selectedKeys();
for (SelectionKey key : keys) {
if (key.isReadable()) {
// 🔁 Callback-like logic: handle read
handleRead(key.channel());
}
}
}
🔹 Selector:
- Think of it like an event dispatcher
- It waits and tells you: “Hey, this socket has something to read!”
🔹 SocketChannel:
- This is Java NIO’s non-blocking version of a Socket
- Used for client connections (e.g., WebSocket client or regular TCP client)
. Non-blocking I/O requires low-level OS integration
- You must use Selector, SocketChannel, SelectionKey, etc.
- You need to manage the readiness state for thousands of sockets
- You need to implement a custom event loop, backpressure, timeout handling, etc.
Why reuse engines like Netty?
- Cross-platform optimizations (uses epoll, kqueue, etc.)
- Battle-tested under massive scale (used by gRPC, Elasticsearch, Play Framework, etc.)
- Avoid reinventing the wheel with socket/event loop/error handling
'개발기술 > Web Dev' 카테고리의 다른 글
파일서빙 전략, 표준 그리고 구현 (0) | 2025.04.15 |
---|---|
Static File Upload (0) | 2025.04.12 |
NGNIX 사용 (0) | 2025.03.07 |
RESTFUL API 설계 (0) | 2024.12.15 |
Interception , Filter (0) | 2024.09.05 |