2

What is the cost (in terms of memory, performance, etc) of having threads block on the result of async operations (e.g. CompletableFuture or RxJava) versus propagating asynchronous promises to the caller of the method?

Why Am I asking this Question

3 months ago I drank the cool-aid and went all-asynchronous using CompletableFuture. I've since come across the following complications:

Without commenting on the severity of the above problems, let's just agree that async code is more complicated than it's sync counterpart.

I'm hoping that it is possible to limit the usage of async code to portions that benefit from it, while keeping the rest of the code synchronous.

Gili
  • 86,244
  • 97
  • 390
  • 689
  • The main point is to be aware of [Amdahl's law](https://en.wikipedia.org/wiki/Amdahl%27s_law) and the [Universal Scalability Law](https://en.wikipedia.org/wiki/Scalability) - your synchronous parts will put a fundamental limit on the parallel parts. If "_most_" of your workflow is synchronous, then you may well gain absolutely no benefit from the parallel parts. – Boris the Spider May 06 '18 at 17:20
  • @BoristheSpider A large portion of my run time consisting of making slow network requests. That part would obviously benefit from some concurrency. The remaining portion (aggregating the data and making decisions on what to do next) is naturally synchronous. The thing is: the synchronous portion makes up most of the code base. The network code might be slow, but it is much smaller and easier to maintain in isolation. I don't want this small amount of code impacting the maintainability of the larger codebase. I hope this makes sense. – Gili May 06 '18 at 17:32
  • I (obviously) don't know much about the problem you are trying to solve, but it sounds to me like you are using a concept that's too low level for the problem you are solving. It seems to me that [reactive](https://en.wikipedia.org/wiki/Reactive_programming) or an [actor system](https://en.wikipedia.org/wiki/Actor_model) would be a better fit. You (seem to) basically have a number of asynchronous pipelines that periodically intersect. Keep fighting the good fight! – Boris the Spider May 06 '18 at 17:36
  • The operation is not asynchronous if you wait for it to finish. So if you have to wait it doesn't make sense to design code in an asynchronous fashion. – a better oliver May 08 '18 at 14:22

0 Answers0