Reactive Programming in Java: A Simplified Guide

Rahul Krishnan
13 min readOct 30, 2024

--

Photo by Robert Stump on Unsplash

1. What is Reactive Programming?

Reactive programming is a paradigm that focuses on data streams and the propagation of change. It’s like subscribing to a news feed: you don’t know when new articles will arrive, but when they do, you’ll be notified instantly. In Java, this means working with asynchronous data streams, where values are emitted over time, and your code reacts to these emissions.

Reactive programming addresses the inefficiencies of traditional polling mechanisms, which constantly check for updates, even when there are none. This can lead to wasted resources and unnecessary network traffic. By subscribing to a data stream, reactive programming ensures that your code is only notified when new data becomes available, significantly improving performance and resource utilization.

While webhooks and Server-Sent Events (SSE) are similar to reactive programming in that they enable real-time communication, they differ in their approach. Webhooks require a predefined endpoint to receive notifications, limiting flexibility. SSE, on the other hand, establish a persistent connection between the client and server, allowing for bidirectional communication. However, both webhooks and SSE can introduce complexity in handling connection management and error handling. Reactive programming, with its declarative style and focus on data streams, provides a more streamlined and efficient solution for real-time applications.

2. Important Considerations for Reactive Programming

Problem: We want to create an application that displays real-time stock price updates for a specific ticker symbol. Traditional polling methods would constantly fetch data from an API, even if there are no changes. This can be inefficient and consume unnecessary resources.

Reactive Solution:

  1. Data Stream: Establish a reactive stream that emits stock price updates from the API. This could be implemented using a library like Project Reactor or RxJava.
  2. Subscription: Subscribe to the data stream in your application. This means your code will be notified whenever a new stock price update is emitted. Hot Observables emit data regardless of whether there are subscribers. They are like a radio broadcast, always transmitting. Once subscribed, a subscriber receives all emitted items, including those emitted before the subscription. Cold Observables only emit data when subscribed. They are like a video on demand, only playing when requested. Each subscriber gets their own independent sequence of emissions. Choose hot Observables for shared data streams like stock prices or sensor readings. Choose cold Observables when each subscriber needs their own personalized sequence, like user interactions or search results. Hot Observables can lead to unexpected behavior if not managed carefully, while cold Observables ensure predictable and controlled data flow.
  3. Processing: When a new update arrives, process it and update the UI to display the latest price.
  4. Error Handling: Implement error handling mechanisms to deal with potential exceptions, such as network failures or API errors. Error handling in reactive streams is crucial to prevent unexpected behavior and ensure system resilience. Strategies include:
  • Error propagation: Allow errors to propagate downstream, allowing consumers to handle them appropriately.
  • Error recovery: Use operators like retry, retryWhen, or onErrorResumeNext to attempt retries or switch to alternative data sources.
  • Error termination: Use onErrorReturn, onErrorReturnItem, or onErrorComplete to terminate the stream with a default value or completion.
  • Error transformation: Use onErrorMap to transform errors into a different type for easier handling.
  • Error isolation: Use onErrorContinue to ignore specific errors and continue processing.
  • Error logging: Use onError to log errors for debugging and monitoring. By carefully considering these strategies, you can effectively manage errors in reactive streams, preventing indefinite propagation and ensuring graceful recovery.

5. Backpressure: If the rate of updates is too high, implement backpressure to control the flow of data and prevent overwhelming the application. Backpressure is a mechanism in reactive programming that allows downstream consumers to signal upstream producers when they are overwhelmed or cannot keep up with the rate of data production. This prevents resource exhaustion by avoiding situations where producers generate data faster than consumers can process it, leading to memory overflow or performance degradation. By implementing backpressure, consumers can effectively control the flow of data, ensuring that producers only generate data at a rate that can be consumed, thereby maintaining a balanced and efficient system.

This reactive approach ensures that the application is only notified when there are actual changes in the stock price, improving efficiency and responsiveness.

/**
* Fetches the latest stock price for the given ticker symbol.
*
* @param tickerSymbol the ticker symbol to fetch the price for
* @return the latest stock price
*/
private StockPrice fetchStockPrice(String tickerSymbol)

/**
* Updates the UI with the latest stock price.
*
* @param price the latest stock price
*/
private void updateUI(StockPrice price)

/**
* Gets the flux of stock price updates.
*
* @return the flux of stock price updates
*/
public Flux<StockPrice> getPriceFlux()
  1. WebClient to fetch stock prices from the API.
  2. Flux to create a reactive stream that emits stock price updates.
  3. Sinks.Many to multicast the price updates to multiple subscribers.
  4. AtomicReference to store the latest price.
  5. retryBackoff for error handling and backpressure control.
  6. subscribeOn to specify the scheduler for the flux.
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicReference;

import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;

public class StockPriceUpdater {
private final WebClient webClient;
private final Sinks.Many<StockPrice> priceSink;
private final AtomicReference<StockPrice> latestPrice;

public StockPriceUpdater(String apiBaseUrl, String tickerSymbol) {
this.webClient = WebClient.builder()
.baseUrl(apiBaseUrl)
.build();
this.priceSink = Sinks.many().multicast().onBackpressureBuffer();
this.latestPrice = new AtomicReference<>();

// Create a flux that emits stock price updates
Flux<StockPrice> priceFlux = Flux.interval(Duration.ofSeconds(1))
.flatMap(i -> fetchStockPrice(tickerSymbol))
.doOnError(ex -> System.err.println("Error fetching stock price: " + ex.getMessage()))
.retryBackoff(3, Duration.ofSeconds(1), Duration.ofSeconds(10))
.subscribeOn(Schedulers.boundedElastic());

// Subscribe to the flux and update the UI
priceFlux.subscribe(price -> {
latestPrice.set(price);
priceSink.tryEmitNext(price);
updateUI(price);
});
}

private StockPrice fetchStockPrice(String tickerSymbol) {
return webClient.get()
.uri("/stock/{tickerSymbol}", tickerSymbol)
.retrieve()
.bodyToMono(StockPrice.class)
.block();
}

private void updateUI(StockPrice price) {
// Update your UI component with the latest price
System.out.println("Updated stock price: " + price.getSymbol() + " - " + price.getPrice());
}

public Flux<StockPrice> getPriceFlux() {
return priceSink.asFlux();
}

public static class StockPrice {
private String symbol;
private double price;

// Getters and setters
public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}
}

public class Main {
public static void main(String[] args) {
String apiBaseUrl = "https://api.example.com";
String tickerSymbol = "AAPL";

StockPriceUpdater updater = new StockPriceUpdater(apiBaseUrl, tickerSymbol);

// Subscribe to the price flux
updater.getPriceFlux()
.subscribeOn(Schedulers.boundedElastic())
.subscribe(price -> System.out.println("Received updated price: " + price.getSymbol() + " - " + price.getPrice()));
}
}

Now let's implement the same using Server Side Events as if the market maker is publishing

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
public class StockPriceSSEEndpoint {
private final StockPriceService stockPriceService;
private final ExecutorService executorService;

@Autowired
public StockPriceSSEEndpoint(StockPriceService stockPriceService) {
this.stockPriceService = stockPriceService;
this.executorService = Executors.newSingleThreadExecutor();
}

@GetMapping("/stock-price-sse/{tickerSymbol}")
public SseEmitter getStockPriceUpdates(@PathVariable String tickerSymbol) {
SseEmitter emitter = new SseEmitter();
executorService.execute(() -> {
try {
// Send initial event
emitter.send(stockPriceService.getInitialStockPrice(tickerSymbol));

// Send updates
stockPriceService.getStockPriceUpdates(tickerSymbol)
.forEach(price -> {
try {
emitter.send(price);
} catch (IOException e) {
// Handle error
emitter.completeWithError(e);
}
});
} catch (IOException e) {
// Handle error
emitter.completeWithError(e);
} finally {
emitter.complete();
}
});
return emitter;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import reactor.core.publisher.Flux;

@Service
public class StockPriceService {
private final WebClient webClient;

@Autowired
public StockPriceService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://api.example.com").build();
}

public StockPrice getInitialStockPrice(String tickerSymbol) {
return webClient.get()
.uri("/stock/{tickerSymbol}", tickerSymbol)
.retrieve()
.bodyToMono(StockPrice.class)
.block();
}

public Flux<StockPrice> getStockPriceUpdates(String tickerSymbol) {
return Flux.interval(Duration.ofSeconds(1))
.flatMap(i -> webClient.get()
.uri("/stock/{tickerSymbol}", tickerSymbol)
.retrieve()
.bodyToMono(StockPrice.class));
}
}
public class StockPrice {
private String symbol;
private double price;

// Getters and setters
public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}
const eventSource = new EventSource('/stock-price-sse/AAPL');

eventSource.onmessage = (event) => {
const stockPrice = JSON.parse(event.data);
console.log(`Updated stock price: ${stockPrice.symbol} - ${stockPrice.price}`);
};

eventSource.onerror = () => {
console.log('Error occurred');
};

eventSource.onopen = () => {
console.log('Connected to SSE endpoint');
};

Lets implement this one more time using Websockets and then compare typical uses

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.stereotype.Component;

@Component
public class StockPriceWebSocketEndpoint extends TextWebSocketHandler {
private final StockPriceService stockPriceService;
private final ExecutorService executorService;

@Autowired
public StockPriceWebSocketEndpoint(StockPriceService stockPriceService) {
this.stockPriceService = stockPriceService;
this.executorService = Executors.newSingleThreadExecutor();
}

@Override
public void afterConnectionEstablished(WebSocketSession session) {
// Send initial stock price
executorService.execute(() -> {
try {
String tickerSymbol = (String) session.getAttributes().get("tickerSymbol");
StockPrice initialPrice = stockPriceService.getInitialStockPrice(tickerSymbol);
session.sendMessage(new TextMessage(initialPrice.toString()));
} catch (IOException e) {
// Handle error
}
});
}

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
// Handle incoming message (e.g., subscribe/unsubscribe)
String tickerSymbol = message.getText();
session.getAttributes().put("tickerSymbol", tickerSymbol);
executorService.execute(() -> {
try {
// Send stock price updates
stockPriceService.getStockPriceUpdates(tickerSymbol)
.forEach(price -> {
try {
session.sendMessage(new TextMessage(price.toString()));
} catch (IOException e) {
// Handle error
}
});
} catch (Exception e) {
// Handle error
}
});
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
// Handle connection closure
}

@Override
public void handleTransportError(WebSocketSession session, Throwable exception) {
// Handle transport error
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import reactor.core.publisher.Flux;

@Service
public class StockPriceService {
private final WebClient webClient;

@Autowired
public StockPriceService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://api.example.com").build();
}

public StockPrice getInitialStockPrice(String tickerSymbol) {
return webClient.get()
.uri("/stock/{tickerSymbol}", tickerSymbol)
.retrieve()
.bodyToMono(StockPrice.class)
.block();
}

public Flux<StockPrice> getStockPriceUpdates(String tickerSymbol) {
return Flux.interval(Duration.ofSeconds(1))
.flatMap(i -> webClient.get()
.uri("/stock/{tickerSymbol}", tickerSymbol)
.retrieve()
.bodyToMono(StockPrice.class));
}
}
public class StockPrice {
private String symbol;
private double price;

// Getters and setters
public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

@Override
public String toString() {
return "StockPrice{" +
"symbol='" + symbol + '\'' +
", price=" + price +
'}';
}
}
const socket = new WebSocket('ws://localhost:8080/stock-price-ws');

socket.onmessage = (event) => {
const stockPrice = JSON.parse(event.data);
console.log(`Updated stock price: ${stockPrice.symbol} - ${stockPrice.price}`);
};

socket.onopen = () => {
socket.send('AAPL'); // Subscribe to AAPL stock price updates
};

socket.onerror = (event) => {
console.log('Error occurred');
};

socket.onclose = () => {
console.log('Connection closed');
};

Server-Sent Events (SSE) Implementation:

Characteristics:

  • Event-driven: SSE is built around the concept of events, where the server sends events to the client.
  • Unidirectional: SSE is a unidirectional protocol, where the server pushes events to the client without expecting a response.
  • Long-lived connection: SSE establishes a long-lived connection between the client and server.
  • Text-based: SSE uses text-based messages to communicate between client and server.

Technical Implementation:

  • SseEmitter: Spring provides the SseEmitter class to handle SSE events.
  • send() method: The send() method is used to send events from the server to the client.
  • try-catch block: Error handling is implemented using a try-catch block.

Reactive Implementation:

Characteristics:

  • Stream-based: Reactive programming is built around the concept of streams, where data is processed in a continuous flow.
  • Asynchronous: Reactive programming is asynchronous, allowing for non-blocking processing.
  • Backpressure: Reactive programming handles backpressure, preventing overwhelming the application with too much data.

Technical Implementation:

  • Flux: Spring provides the Flux class to handle reactive streams.
  • interval() method: The interval() method is used to create a sequence of numbers at regular intervals.
  • flatMap() method: The flatMap() method is used to transform the sequence into a new sequence of stock prices.

WebSockets Implementation:

Characteristics:

  • Bi-directional communication: Enables real-time communication between client and server.
  • Persistent connection: Establishes a persistent connection between client and server.
  • Low latency: Optimizes for low-latency communication.

Technical Implementation:

  • WebSocket endpoint: Defines a WebSocket endpoint to handle connections.
  • onMessage() method: Handles incoming messages from clients.
  • sendMessage() method: Sends messages to clients.
  • Error handling: Implements error handling mechanisms.

Most Realistic Solution for a Trader:

WebSockets would be the most realistic solution for a trader. Here’s why:

  • Real-time updates: WebSockets enable bi-directional, real-time communication between the client and server, ensuring traders receive instantaneous price updates.
  • Low latency: WebSockets reduce latency compared to traditional HTTP requests, crucial for traders making time-sensitive decisions.
  • Multi-asset support: WebSockets can handle multiple asset price updates simultaneously, ideal for traders monitoring multiple stocks.
  • Error handling: WebSockets provide built-in error handling mechanisms, ensuring traders receive notifications about connection issues or data errors.

Most Suitable Solution for a Stock Exchange to Publish New Prices:

A combination of WebSockets and Server-Sent Events (SSE) would be suitable for a stock exchange to publish new prices. Here’s why:

  • Scalability: WebSockets and SSE can handle a large number of concurrent connections, essential for stock exchanges with numerous subscribers.
  • Real-time updates: Both WebSockets and SSE enable real-time price updates, ensuring subscribers receive timely information.
  • Reliability: WebSockets and SSE provide mechanisms for error handling and reconnection, ensuring reliable data delivery.
  • Standardization: SSE is a standardized protocol, making it easier for stock exchanges to integrate with various clients.

Why not Webhooks?

While webhooks can provide real-time updates, they may not be the best fit for stock exchanges due to:

  • Connection overhead: Webhooks require a new HTTP connection for each update, increasing overhead and latency.
  • Limited scalability: Webhooks may struggle with a large number of subscribers, leading to performance issues.

Why not Reactive Implementation?

While reactive implementations (e.g., RxJava, Reactor) provide efficient data processing, they may not be directly applicable for stock exchanges publishing prices due to:

  • Network considerations: Reactive implementations focus on in-memory data processing, whereas stock exchanges need to consider network transmission and scalability.

In summary:

  • WebSockets are suitable for traders requiring real-time updates and low latency.
  • A combination of WebSockets and SSE is suitable for stock exchanges publishing new prices, offering scalability, reliability, and standardization.

3. When to Use Reactive Programming

  • I/O-bound applications: When your application heavily relies on network or disk operations.
  • Event-driven systems: For applications that need to react to continuous streams of events.
  • High-concurrency scenarios: When you need to handle many concurrent requests efficiently.
  • Real-time applications: For systems that require low-latency responses.

Reactive programming is particularly well-suited for I/O-bound applications due to its asynchronous nature. Unlike traditional blocking I/O, reactive programming allows the application to continue processing other tasks while waiting for I/O operations to complete. This non-blocking approach significantly improves scalability and responsiveness, especially in scenarios where I/O operations can be time-consuming. By leveraging reactive streams and operators, developers can effectively manage asynchronous workflows, ensuring that the application remains responsive and efficient even under heavy I/O loads.

Event-driven systems rely on asynchronous communication to handle continuous streams of events. Reactive programming provides a natural framework for building such systems. By representing events as Observables, developers can easily compose and transform event streams using reactive operators. This enables the creation of complex event processing pipelines, where events can be filtered, mapped, aggregated, and reacted to in a declarative manner. Additionally, reactive programming’s backpressure mechanism helps manage the flow of events, preventing overload and ensuring that the system can handle incoming events efficiently.

High-concurrency scenarios often pose significant challenges for traditional programming models. Reactive programming, with its non-blocking and asynchronous nature, is well-equipped to handle such workloads. By avoiding blocking operations, reactive applications can efficiently manage concurrent requests, preventing resource contention and ensuring responsiveness. Additionally, reactive programming’s backpressure mechanism helps regulate the flow of requests, preventing overload and ensuring that the system can handle incoming requests in a controlled manner. This makes reactive programming an ideal choice for applications that need to handle a large number of concurrent users or requests.

Real-time applications require low-latency responses and the ability to process data in a timely manner. Reactive programming’s asynchronous and non-blocking nature aligns well with the requirements of real-time systems. By avoiding blocking operations and leveraging non-blocking I/O, reactive applications can minimize latency and ensure that data is processed efficiently. Additionally, reactive programming’s event-driven model allows for rapid responses to incoming data, making it suitable for applications that need to react quickly to changes in their environment.

4. When Not to Use Reactive Programming

  • Simple, synchronous tasks: If your application doesn’t involve asynchronous operations or complex data flows.
  • Legacy systems: Integrating reactive components into existing synchronous systems might be challenging.
  • Performance-critical, CPU-bound tasks: Reactive programming might not provide significant benefits for CPU-intensive workloads.

Reactive programming is not well-suited for legacy systems due to their often monolithic and blocking nature. Legacy systems typically rely on synchronous, blocking I/O operations, which can introduce performance bottlenecks and hinder scalability. Additionally, integrating reactive components into a legacy system can be challenging due to the differences in programming paradigms and architectural styles. The tight coupling and lack of modularity in many legacy systems can make it difficult to introduce reactive patterns without significant refactoring or rewriting. As a result, adopting reactive programming in legacy systems often requires a substantial investment of time and effort, and may not be feasible in all cases.

Reactive programming, while excellent for I/O-bound tasks, is less ideal for CPU-bound tasks. In CPU-bound scenarios, the bottleneck lies in the CPU’s processing power, not in waiting for external resources like I/O. Reactive programming’s asynchronous nature, while beneficial for I/O, can introduce overhead in CPU-bound tasks due to context switching and scheduling. This overhead can sometimes outweigh the benefits of non-blocking operations, especially when the CPU is already heavily utilized.

Additionally, reactive programming often involves functional programming concepts and paradigms, which can introduce performance overhead due to the creation of intermediate data structures and function calls. While these techniques can be powerful for expressing complex logic, they can also impact performance, especially in CPU-intensive scenarios.

Therefore, for performance-critical, CPU-bound tasks, traditional imperative programming approaches or specialized performance optimization techniques might be more suitable.

5. Java Virtual Threads vs. Reactive Programming

  • Java Virtual Threads: A lightweight thread implementation that simplifies asynchronous programming. They can be used to handle many concurrent tasks efficiently without blocking the main thread.
  • Reactive Programming: A programming paradigm that focuses on data streams and asynchronous operations. It requires more careful consideration of backpressure, error handling, and observability.
  • Key Difference: While Java Virtual Threads can simplify asynchronous programming, reactive programming provides a more declarative and composable approach for handling data streams and events.

6. Alternatives to Reactive Programming

  • Traditional thread-based programming: Using threads directly for asynchronous operations.
  • Callback-based programming: Using callbacks to handle asynchronous results.
  • Futures and CompletableFutures: Java’s built-in mechanisms for asynchronous computations.
  • Choosing the right approach: Consider factors like complexity, scalability, and maintainability. Reactive programming is often preferred for complex, scalable systems.

7. Typical Tools and Technology Stack

  • Reactive Streams: A specification for asynchronous data streams.
  • Project Reactor: A popular reactive programming library for Java.
  • RxJava: Another widely used reactive programming library.
  • Vert.x: A toolkit for building reactive applications.
  • Spring WebFlux: A reactive web framework built on Project Reactor.

8. Why Reactive Programming Can Be Difficult

  • New mindset: Shifting from imperative to declarative programming can be challenging.
  • Complexity: Understanding concepts like backpressure and error handling requires practice.
  • Debugging: Debugging reactive systems can be more complex due to asynchronous nature.
  • Simplification: Breaking down complex problems into smaller, reactive components can help.

9. Observability Considerations

  • Logging: Use structured logging to capture relevant information.
  • Metrics: Monitor key performance indicators like throughput, latency, and error rates.
  • Tracing: Track the flow of requests through your reactive system.
  • Distributed tracing: For microservices architectures, use tools like Zipkin or Jaeger.

10. Conclusion

Reactive programming offers a powerful approach for building scalable, responsive applications in Java. By understanding its principles and leveraging the right tools, you can create efficient and maintainable systems that excel in handling asynchronous data streams.

--

--

Rahul Krishnan

Senior Solutions Designer and Architect. Likes to write about Technology , Economics and Literature. Football is life !