Module 11 · Executable guide
WebFlux vs Vert.x vs Virtual Threads
So sánh abstraction, threading, backpressure, architecture và decision trade-offs.
1. Async abstraction
| Concept | CompletableFuture | Reactor | Vert.x |
|---|---|---|---|
| Single async value | CompletableFuture<T> | Mono<T> | Future<T> |
| Transform | thenApply | map | map |
| Async chain | thenCompose | flatMap | compose |
| Recover | exceptionally | onErrorResume | recover |
| Multi-value | Không phải abstraction chính | Flux<T> | ReadStream/EventBus/API riêng |
2. Threading
| Model | Mindset |
|---|---|
| Spring MVC + Virtual Threads | Imperative blocking code, nhiều lightweight threads. |
| WebFlux | Reactive chain + event-loop/non-blocking I/O. |
| Vert.x | Event-loop + Future/Event Bus. |
3. Backpressure
WebFlux/Reactor đặt Reactive Streams ở trung tâm. Vert.x có flow-control ở nhiều stream APIs nhưng Future/EventBus không đồng nghĩa Reactive Streams.
4. Architecture
Spring WebFlux: Controller → Service → Reactor → R2DBC/WebClient Vert.x: Router → Verticle/Service → Future/EventBus → SQL/WebClient
5. Complexity
- Reactive chains cần operator literacy và debugging tốt.
- Vert.x cần kỷ luật event-loop và explicit architecture.
- Virtual Threads làm imperative blocking style cạnh tranh hơn cho nhiều I/O workloads.
6. Decision matrix
| Tình huống | Cân nhắc |
|---|---|
| Team Spring mạnh, downstream non-blocking | WebFlux. |
| CRUD blocking, ưu tiên simplicity | MVC + Virtual Threads. |
| Event-driven toolkit, EventBus/Verticle phù hợp | Vert.x. |
| Reactive Streams end-to-end | WebFlux/Reactor tự nhiên hơn. |
| Legacy blocking SDK nhiều | Virtual Threads hoặc bounded bridge; tránh “reactive giả”. |