Push vs Pull — Imperative vs Declarative Thinking
- coding z2m
- Jul 1
- 2 min read
This is the mindset shift at the heart of Reactive Programming.
🧠 Why You Need to Understand This
Before you master Mono, Flux, or Spring WebFlux, you must understand:
How Reactive (Push) differs from Imperative (Pull)
Why Reactive = Declarative + Asynchronous + Lazy
What happens behind the scenes in a stream
🏁 1. Imperative Programming (Traditional Java)
❓ What is it?
You tell the program how to do things step-by-step.
🧾 Example: Fetching User from DB
User user = userRepository.findById(1); // Blocking call
System.out.println(user.getName());
Calls DB
Waits for response (blocking)
Prints result
🧱 You control the flow
Data is pulled when needed
Program is thread-blocking
Synchronous (you wait)
⚡ 2. Reactive Programming (Push-Based)
❓ What is it?
You tell what to do when data arrives
🧾 Example: Same thing using Mono
Mono<User> userMono = userRepository.findById(1); // Non-blocking
userMono
.map(user -> user.getName())
.subscribe(name -> System.out.println(name)); You define a pipeline of actions
Data is pushed when ready
You don’t wait — the system reacts when data arrives
🌀 You write:
“When this happens, do that”
🔁 Pull vs Push: Key Comparison
Feature | Imperative (Pull) | Reactive (Push) |
Control | You control flow | Publisher controls |
Timing | Immediate | Deferred (on demand) |
Threads | Blocking | Non-blocking |
Concurrency | Hard to manage | Built-in |
Flow | Step-by-step | Event-based |
Example | List<String> | Flux<String> |
🔍 Real-World Analogy
Analogy | Imperative | Reactive |
Food delivery | You call restaurant every 5 min to check if food is ready (polling/pulling) | You place order, and they notify you when food arrives (push) |
Movie theater | You go and wait in queue even if the movie hasn’t started | You buy ticket, get notified when show starts |
Java | List<String> – items already in memory | Flux<String> – stream of items that may arrive later |
🎯 In Reactive Thinking:
You don’t wait for data — you react to it.
You build a pipeline (chain of operations).
You don’t execute anything until you subscribe.
🔄 Summary
Reactive Style Key Ideas |
Declarative → You describe what to do |
Push-based → System notifies you |
Non-blocking → Threads aren’t blocked |
Lazy → Nothing happens until you .subscribe() |
Stream-oriented → Works well with multiple items (Flux) |
✅ You Now Understand:
✅ The mental shift from "pull" to "push"
✅ Why reactive pipelines don’t run until you .subscribe()
✅ Why it’s critical for building async APIs with WebClient, MongoDB, etc.
Comments