Offload Processing

Offloading processing is the practice of delegating certain tasks or operations to a separate thread pool or executor service in order to free up the main event loop and maintain good performance in a reactive application. By offloading processing to a separate thread pool, the event loop can continue processing other requests without being blocked.

Here are some examples of how to offload processing in a Spring WebFlux application:

  • Using subscribeOn(): You can use the subscribeOn() operator to switch to a separate thread pool for specific parts of the reactive chain. For example, to offload database queries to a separate thread pool, you can use the following code:
Flux.fromIterable(ids)
    .flatMap(id -> Mono.fromCallable(() -> repository.findById(id)).subscribeOn(Schedulers.boundedElastic()))
    .map(entity -> entity.toDto())
    .collectList();
  • Using publishOn(): You can use the publishOn() operator to switch to a separate thread pool for downstream operators. For example, to offload expensive computation to a separate thread pool, you can use the following code:
Flux.range(1, 10)
    .map(i -> {
        // expensive computation
        return i * i;
    })
    .publishOn(Schedulers.boundedElastic())
    .map(i -> i.toString())
    .subscribe(System.out::println);
  • Using flatMap(): You can use the flatMap() operator to perform expensive operations in a separate thread pool. For example, to offload file I/O to a separate thread pool, you can use the following code:
Flux.fromIterable(fileList)
    .flatMap(file -> Mono.fromCallable(() -> {
        // expensive file I/O operation
        return Files.readAllLines(file.toPath());
    }).subscribeOn(Schedulers.boundedElastic()))
    .flatMapIterable(lines -> lines)
    .subscribe(System.out::println);

In each of these examples, we use a separate thread pool to perform expensive or blocking operations, freeing up the main event loop to process other requests. This helps to maintain good performance and responsiveness in a reactive application.

Schedulers