shanmugaprabhu
shanmugaprabhu

Reputation: 184

Is it acceptable to use Mono<Object>.publishOn(Schedulers.elastic) for blocking operations?

I understand when using blocking operations in reactive streams we should use Publisher<Object>.publishOn(Schedulers.elastic).subscribe(//blocking operations go here)

I understand that it makes sense when my publisher publishes a list of items (For ex: Flux) the future items does not have to wait for the current item getting blocked by a blocking operation. But in case of Mono is it necessary ? Because there will be only one item flowing in my pipe.

PS. I am using spring boot 2 reactive flux controller something like this.

@RestController("/item")
public Mono<Response> saveItem(Mono<Item> item) {
  return
  item.publishOn(Schedulers.elastic()) **//Do I need this ?**
      .map(blockingDB.save(item))
      .map(item -> new Response(Item);

}

Upvotes: 2

Views: 2672

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81907

Yes, absolutely!

If you don't do it you are blocking on the main processing/event loop threads. Of these, you should have only as many as your machine has (effective) CPUs.

Let's say that's 8. This means with just 8 concurrent requests that are waiting for the blocking operation you bring your application to a full stop!

Also, make sure to move processing after the blocking operation back to a thread pool intended for CPU intense work.

Upvotes: 3

Related Questions