Spring WebFlux vs Spring MVC — The Complete Beginner-Friendly Guide

 

Spring WebFlux vs Spring MVC — The Complete Beginner-Friendly Guide

(Event Loop, Reactive Programming, Thread Model, Real Use Case Explained)

Modern backend services frequently need to handle:

  • High concurrency

  • Millions of API calls

  • Slow external dependencies (DB, APIs)

  • Real-time streaming

  • Efficient CPU & memory usage

Traditional Spring MVC works perfectly for small, synchronous web apps.
But when you scale to thousands of simultaneous requests, it starts struggling.

This is why Spring WebFlux exists.

This blog will explain WebFlux in a way even freshers can easily understand.


1. What Is Spring WebFlux?

Spring WebFlux is a non-blocking, asynchronous, reactive web framework in the Spring ecosystem.

It is built on:

  • Project Reactor (Mono, Flux)

  • Reactive Streams specification

  • Netty event loop model (default)

In simple words:

WebFlux = High-performance async API system that uses very few threads.


2. What Is Spring MVC?

Spring MVC is Spring’s traditional, blocking, thread-per-request model.

Uses:

  • Tomcat

  • Jetty

  • Undertow (classic servlet containers)

In simple words:

Spring MVC = One thread handles one request and blocks until completion.


3. Why Blocking Is a Problem?

In MVC:

Thread receives request → Calls DB → waits (blocking) → Calls API → waits → Writes logs → waits → Finally sends response

During each wait, the thread is doing nothing but is still occupied.

If you have 1000 requests → you need ~1000 threads

Expensive and not scalable.


4. How WebFlux Works (Non-Blocking)

WebFlux doesn’t block threads.

When an operation may take time:

  • DB call

  • API call

  • Logging

  • Metrics

  • File IO

Rather than waiting, WebFlux does:

Start async call → Register callback → Free the thread → Continue other work → Resume when result arrives

This is the Event Loop model.


5. The Event Loop Explained (VERY IMPORTANT)

Think of the event-loop like a restaurant waiter:

🍽 Spring MVC (Blocking)

  • 1 waiter serves 1 table

  • Waiter must stand and wait while the food is cooking

  • If there are 50 tables → need 50 waiters

🍽 WebFlux (Non-Blocking Event Loop)

  • 1 waiter can serve 100 tables

  • If food is cooking, waiter moves to the next table

  • Calls come back when food is ready

  • Very few waiters needed

That is what the event loop does.


6. Text Diagram: Event Loop Model in WebFlux

┌──────────────────────────┐ │ Event Loop Thread (1) │ └───────────┬──────────────┘ │ Request A → Authenticate → DB Call (async) → Thread released │ Request B → Auth → Metadata API (async) → Thread released │ Event loop picks next ready callback → Process → Continue

One thread can handle hundreds of concurrent requests because it never waits.


7. Spring MVC vs WebFlux — Visual Comparison

🟥 Spring MVC (Thread Per Request)

Request 1 ── Thread-1 (waiting during IO) Request 2 ── Thread-2 (waiting during IO) Request 3 ── Thread-3 (waiting during IO) ... Request 1000 ─ Thread-1000 (waiting during IO)

Threads get exhausted → slow system → timeouts.


🟦 Spring WebFlux (Event Loop)

EventLoop-1 handles: - Request 1Async DB - Request 2Async API - Request 3Async DB EventLoop-2 handles: - Request 4Async API - Request 5Async DB

Only 10–20 threads required even for thousands of requests.


8. Real Use Case — Product Details API

Your API workflow:

  1. Receive request

  2. Authenticate

  3. Authorize

  4. Query product DB

  5. Query metadata service

  6. Log request

  7. Send metrics

  8. Return response

Let’s compare how MVC and WebFlux handle the flow.


9. Request Flow in Spring MVC (Blocking Model)

Step-by-step:

1. DispatcherServlet assigns Thread-25 2. Auth (fast) 3. Authorization (fast) 4. DB call → thread waits 80ms 5. Metadata API → thread waits 100ms 6. Logging → thread waits 20ms 7. Metrics → thread waits 10ms 8. Send response 9. Thread released

Total wait time: 210ms

Thread was BLOCKED for most of that time.

Imagine 5000 users → need ~5000 threads → impossible.


10. Request Flow in WebFlux (Non-Blocking)

Step-by-step:

1. EventLoop-1 receives request 2. Auth (non-block) 3. Authorization (non-block) 4. DB call → async → thread freed immediately 5. Metadata API call → async → thread freed 6. Logging → async 7. Metrics → async 8. EventLoop-1 resumes when both async results are ready 9. Send response

Thread blockage time: 0ms

The thread is never waiting.


11. Diagram — MVC vs WebFlux for Product API

🟥 SPRING MVC (Blocking)

Request ----┐ ├─ Thread-1 (blocked 210ms) Request ----┘ Total threads required ≈ number of active requests

🟦 SPRING WEBFLUX (Non-Blocking)

EventLoop Thread-1: Request → Auth → DB async → FREE EventLoop Thread-2: Request → Meta async → FREE Callback triggers: EventLoop Thread-1 resumes → Build response → Send

Total threads required ≈ 20–30 (even for 5000 requests)


12. Why WebFlux Is Faster Under Load

✔ Uses async IO

✔ Uses Netty event-loop

✔ No thread sits idle

✔ Threads only used for CPU work, not IO wait

✔ Ideal for microservices calling other microservices


13. When Timeouts Happen in WebFlux

WebFlux doesn’t magically prevent timeouts.

Timeout happens when:

  • DB takes too long

  • Downstream API is slow

  • You configure a timeout

  • Client disconnects

The difference:

MVC → thread blocked until timeout

WebFlux → thread is free, timeout happens via callback


14. When to Use WebFlux

✔ Very high concurrency
✔ Calling many external APIs
✔ Lots of IO-bound operations
✔ Real-time streaming (SSE/WebSockets)
✔ Microservices in distributed systems
✔ Reactive databases (R2DBC, Mongo Reactive)


15. When Not to Use WebFlux

❌ If you use JPA/Hibernate (blocking)
❌ If your use case is low traffic
❌ If team is not comfortable with reactive programming
❌ If CPU-heavy work (pure computation)


16. Final Summary

🔹 Spring MVC

  • Blocking

  • One thread per request

  • Threads wait → not scalable

  • Good for synchronous CRUD apps

🔹 Spring WebFlux

  • Non-blocking

  • Event loop model

  • Handles thousands of requests with few threads

  • Perfect for IO-heavy, high-concurrency systems

  • Ideal for microservices & real-time workloads


One-Line Explanation

WebFlux makes your service handle 10× to 50× more requests by not wasting threads during IO.

No comments:

Post a Comment

How JVM Identifies and Communicates Between Stack, Heap, and Method Area

  How JVM Identifies and Communicates Between Stack, Heap, and Method Area Java has three types of memory involved during execution: 1️⃣ ...

Featured Posts