SkillByAIOpen interactive version →

Lesson 35 / 35

Reactive Programming Intro

See the problem reactive streams solve — blocking threads under load — and read a minimal non-blocking WebFlux endpoint built on Mono and Flux.

The problem: one thread per request

A traditional (servlet) request handler blocks its thread while waiting on a slow downstream call — under heavy load you run out of threads even though the CPU is idle, just waiting on I/O. Reactive programming (Reactive Streams / Project Reactor) instead registers a callback and frees the thread to serve other work while waiting.

Mono and Flux

Mono<T> represents zero-or-one async value, Flux<T> zero-to-many — both are lazy: nothing runs until something subscribes. Spring WebFlux controllers return them directly instead of blocking for the result.

@RestController
class UserController {
    private final WebClient client;

    @GetMapping("/users/{id}")
    Mono<User> get(@PathVariable String id) {
        return client.get()
            .uri("/legacy-users/{id}", id)
            .retrieve()
            .bodyToMono(User.class);   // non-blocking
    }
}

When it's (not) worth it

Reactive shines for high-concurrency I/O-bound services (gateways, streaming, fan-out to many backends). For typical CRUD apps under modest load, plain blocking code on virtual threads (Java 21+) gives similar scalability with far simpler, easier-to-debug code — reach for reactive only when you've measured a real need.

Final quiz 1 of 8

Final quiz

Quick check: Which feature lets you pass behaviour as an argument?

  • Static imports
  • Annotations
  • Lambda expressions
  • Packages
Answer

Lambda expressions — Lambdas implement functional interfaces.

Final quiz 2 of 8

Final quiz

Quick check: What does Stream.map do?

  • Filters elements
  • Terminates the stream
  • Sorts keys
  • Transforms each element
Answer

Transforms each element — map applies a function to each element.

Final quiz 3 of 8

Final quiz

Quick check: Which class safely handles absent values?

  • Optional
  • Object
  • Void
  • Number
Answer

Optional — Optional makes missing values explicit.

Final quiz 4 of 8

Final quiz

Quick check: What does happens-before guarantee?

  • Faster code
  • One thread's writes are visible to another
  • No exceptions
  • Automatic locks
Answer

One thread's writes are visible to another — It defines memory visibility between threads.

Final quiz 5 of 8

Final quiz

Quick check: Which tool manages thread pools?

  • Scanner
  • StringBuilder
  • ExecutorService
  • Properties
Answer

ExecutorService — Executors decouple tasks from threads.

Final quiz 6 of 8

Final quiz

Quick check: Which Spring concept supplies dependencies to your classes?

  • Reflection only
  • JSP
  • Servlets
  • Dependency injection
Answer

Dependency injection — The IoC container wires beans together.

Final quiz 7 of 8

Final quiz

Quick check: Why hash passwords with bcrypt?

  • So plain text is never stored
  • To make them shorter
  • To speed up login
  • To allow decryption
Answer

So plain text is never stored — Hashing protects users if data leaks.

Final quiz 8 of 8

Final quiz

Quick check: What do virtual threads make cheap?

  • CPU-bound loops
  • Blocking I/O concurrency
  • Memory leaks
  • Compilation
Answer

Blocking I/O concurrency — The JVM parks blocked virtual threads.