Is there an upper limit on the number of Publishers or Subscribers you can have in a Reactor Java Application?

In a Reactor Java based application we free up threads by creating Publishers to be subscribed on later.

Can I create unlimited number of Publishers to scale my Application? What are the constraints for creating Publishers.

Answer

You can create as many publishers as you like – nothing will happen until you subscribe (for a standard cold publisher at least), so until that point they’re just like any other POJO. Obviously you have to have enough heap space, but that’s about it.

If you’re asking about the upper limit of active publishers (i.e. once you’ve subscribed) – that’s a little more complex. In a traditional per-thread model the upper limit is generally the number of threads you can context switch between (often a few thousand.) Reactive frameworks essentially remove that limit by only using one event loop, which means the practical limit is often given by one of:

  • I/O bandwidth. If you have so many requests you don’t have the network bandwidth to respond to them all in a timely fashion (or at all), then this is your limit.
  • CPU cycles. If you have plenty of network bandwidth, but your publishers are performing CPU intensive operations, then this is likely to be what you run into first – while you no longer have the overhead of context switching, it still takes a certain amount of CPU cycles to process each request, and you only have a finite resource here.

In reality though, both of the above differ per setup – so the only real way to know what limit you’ll run into, and when you’ll run into it, is to benchmark your application & hardware to find out.