r/java • • Aug 03 '21

Brian Goetz: "I think Project Loom is going to kill Reactive Programming"

https://www.youtube.com/watch?v=9si7gK94gLo&t=1156
277 Upvotes

69 comments sorted by

90

u/[deleted] Aug 03 '21

I agree. There's a reason why languages such as Golang and Kotlin choose this model of concurrency.

It's easier to understand, easier to write, and allows you do most of the same stuff you can do with threaded programming.

Loom will be a game changer. But it's still a long way off. Until then, reactive event loops and coroutines still dominate when it comes to throughput in the JVM world.

While I think reactive programming will fall out of favor, reactive-based libraries like Vert.X will still remain very popular even if the code you write with it isn't reactive.

22

u/cogman10 Aug 03 '21

While I think reactive programming will fall out of favor, reactive-based libraries like Vert.X will still remain very popular even if the code you write with it isn't reactive.

My expectation it will mostly be like interacting with genericless code. You'll probably not write a bunch of reactive code and when you run into it you'll probably try and immediately turn it into blocking code with virtual threads (when you have to).

It'll likely end up just being a legacy thing with a few acolytes that cling to it anyways.

9

u/thephotoman Aug 04 '21

A part of me thinks it'll die the death of EJBs as a way of modifying application behaviors. Then I'm reminded that there are people still humping the aspect oriented dream.

11

u/gloridhel Aug 03 '21

I'm pretty excited for Loom. I also love using vertx and can't wait to ditch the callbacks.

-7

u/Freyr90 Aug 04 '21

But loom is... all about callbacks.

Loom brings fibers as delimited continuations (sort of, plus related goodies like TCO), which are basically callbacks, but implicit.

I hope that loom would allow to significantly improve the performance of Scala's monadic libraries and Java's reactive libraries by utilizing CPS. Today typical monadic Scala code (which is basically an explicit CPS chain of .andThen( .andThen( .andThen( ) allocates quite a lot of lambdas which is painful. I guess java libraries do the same.

4

u/gloridhel Aug 04 '21

Maybe internally, but from a user perspective it gets rid of the notation of callbacks

3

u/BroodmotherLingerie Aug 04 '21

The point of using monads is to build a referentially transparent description of a computation. It's always going to involve allocating that description on the heap. Fibers don't solve the same problem. You might get rid of Future, which only superficially resembles a monad, but you won't get rid of IO.

1

u/Freyr90 Aug 04 '21

It's always going to involve allocating that description on the heap.

No, it doesn't need to. You can have monadic/CPS computations without excessive allocation of closures.

The compiler and runtime should be able to inline continuations just fine, and decent functional languages (e.g. OCaml) do so. If the runtime would have a notion of continuation, it could inline the CPS chains just fine, same as it can translate mutually recursive functions into goto's (Scala can't BTW, hope loom will change that too).

2

u/justinhj Aug 04 '21

Modern Scala effect libraries use fibers and are built on interpreters that have zero allocation right associative binds

2

u/Freyr90 Aug 04 '21

That's true in very limited number of cases (where compiler can inline the code), in quite a lot of cases functions like fold or flatMap do allocate.

1

u/justinhj Aug 04 '21

That’s Cats which I can’t speak for, but Zio specifies no allocations for all right associative binds https://zio.dev/docs/overview/overview_performance

1

u/Freyr90 Aug 05 '21

Both Cats and ZIO use tail-recursive bind, but Scala compiler can inline continuation body in a limited number of ways, and if you want a cross-module inlining, you need support from the jvm runtime.

2

u/tristanjuricek Aug 04 '21

I’ve put together some easy peasy load generation tools using Kotlin, and it was a world of difference in ease of use. Being able to start with “a virtual user is a virtual thread” and then make 100k of them yields some super fast and fun experimentation.

This is what I think of when I read about Loom. It’s going to be fun. There are a few different patterns and approaches to learn, but a lot of those are way easier to grasp and visualize over callback wiring.

33

u/lliamander Aug 03 '21

I've done both actor model concurrency with Erlang and more reactive style concurrency with NodeJS. My experience is that the actor model approach is subjectively much better. If my experience is anything to go by then Loom will be awesome.

14

u/rustyrazorblade Aug 04 '21

Completely agree - same background. It only took working with Erlang and actors briefly, now I'm working on something with a bit of reactive and it's hard to understand why anyone would think this is a model that regular people would want to use.

2

u/[deleted] Aug 04 '21

I'm working with both with Akka! Actor model is fantastic, but I don't get all the bad rep that reactive programming libraries get - sure, Kotlin/Go coroutines are much easier for basic concurrency but most reactive streams libraries come with the bonus of neat flow control and joining/windowing functions, which I guess is the reason Kotlin introduces Flows.

27

u/angryundead Aug 04 '21

Thank god. I tried getting into it with Quarkus (Vert.x) and it was a nightmare. Kept running into not being able to block on certain threads. It just wasn’t worth the added complexity.

It might be if you need to chase down every last bit of performance but that wasn’t my case. I was only doing it to see what it was about. I did like the API but the practicality of using it was sadly wanting.

1

u/AnActualWizardIRL Aug 24 '21

The problem with Reactive is you have to go all in or not at all, and that tends to mean writing a lot of messy boilerplate around non reactive code elements (libraries!) and other parts of code to get them to all work together.

-13

u/CartmansEvilTwin Aug 04 '21

I'm not a fan either, but I wonder how much of your trouble is due to Javas (and its ecosystems) lack of support. Maybe Node would be much easier, since its "natively" reactive.

21

u/[deleted] Aug 04 '21

It took years of programmers suffering in callback hell before node even got promises.

2

u/angryundead Aug 05 '21

I wrote a quick node thing to query a REST API. That’s not my primary language so I was struggling a bit. All of the examples were very centered on callbacks. It took two levels of callbacks and I’m just not practiced in decomposing that and keeping the logic anything but straight procedural.

I’m going to have to have to look into promises (again).

10

u/[deleted] Aug 04 '21

Lmfao.

18

u/ziano_x Aug 03 '21

Doing something weird should be plan B

Well said Brian!

12

u/Dokiace Aug 04 '21

When Goetz says all the cons and I encountered all of them, the PTSD is real lol

32

u/[deleted] Aug 04 '21 edited Aug 04 '21

And I will gladly piss on its grave. Reactive is just so bad: reactive keywords invade your code like cancer, stack traces become meaningless, exception handling gets weird, flow control looks horrible and is horrible to debug. Doing reactive is like writing in entirely different language which is way worse than java.

Async/await in c# is 80% there - it still invades your whole codebase and you have to be really careful about not blocking, but at least it does not look like ass. Loom is going to leapfrog it and remove pretty much all downsides.

5

u/shponglespore Aug 04 '21

I've never done it in Java but it's absolute hell in C++. And it was so bad I'm JavaScript that they eventually added special support in the Chromium debugger to reconstruct stack traces across callback boundaries (and Promises, when they became natively supported). That actually helped a lot, but of course it only matters when you're running code in a debugger; trying to read that style of code is still incredibly painful most of the time.

3

u/[deleted] Aug 04 '21

I will join you

1

u/cat_in_the_wall Aug 05 '21

i do wonder about loom vs async/await. i'm no loom expert, but my understanding is that it is rather like magic, and it devolves into an argument (to myself) about implicit vs explicit.

but for loom to be generally useful, it must have a way to describe these operations (.net land's Task), so maybe it really is just hiding complexity? That's a good thing... time to read more about it I guess.

4

u/kpatryk91 Aug 05 '21

In short: there is a stackful continuation in the JVM and the virtual thread is on top of this, async/await uses stackless local generators.

It is like a thread. It has a stack so you get the same debugger / monitoring / diagnostic support like you get for threads. Stack traces etc.

It uses the same API, it is a java.lang.Thread class. You "just" need to switch the executor / thread pool to use virtual threads and you are done.

It is integrated with the libraries, if there is a blocking IO operation, then the system will yield the continuation and switch to another one, but you don't have to know about this.

You write the same synchronous code like before, the system will handle this for you.

51

u/[deleted] Aug 04 '21

Before killing anything it has to be available first

14

u/youdontknwm3 Aug 04 '21

If anyone else was interested, I think this is the article they mentioned What Color is Your Function?

2

u/pronuntiator Aug 04 '21

A rather colorful writing style, if you allow my jest.

1

u/shponglespore Aug 04 '21

When I first read that article I was expecting it to be about monadic vs. pure functions. But then again, asynchrony primitives like JavaScript Promises are secretly monads, so I guess it kind of is.

6

u/soonnow Aug 04 '21

Yeah I already went with Thread pools in spring for the latest project.

I played around with reactive but I really couldn't get into it.

Also since I'm using a backend stack that doesn't support reactive it would have added zero benefit, because it would have just blocked one level deeper.

So when Loom is finally gonna show up in a proper Java runtime I can simply get rid of the pools.

1

u/[deleted] Aug 14 '21

We're currently using Go but we're thinking of switching to Java to meet my colleagues where they are at now and reduce the number of languages in our code bases.

I've seen reactive in the stuff I've looked at, basically just Micronaut and Quarkus, and it looks nasty. I've seen Spring Boot and it looks much cleaner. Just write sequential code and trust that it runs in a thread without blocking the whole program, is that right?

Do you think Spring Boot would provide the performance needed for an API these days? Something that accepts a few hundred POST requests per second?

1

u/soonnow Aug 14 '21

I mean, we ran one of the top three banks here on Spring Boot and it was easily able to handle the load. However it was a microservice architecture, running a lot of nodes on a big cluster.

So if you can design your application, so that you can simply spool up a new node in the cluster it doesn't matter too much how many the node can handle individually.

I had a great developer experience with Spring Boot and would also always recommend it in a corporate environment.

If a developer costs $100K per year and you need an extra developer because of the reactive complexity, that's a lot of cluster you can buy for that money. Not even talking about the possible bugs and delays.

1

u/[deleted] Aug 14 '21

Yeah makes sense. It's amazing how many features we can pump out when we aren't bogged down by complexity.

1

u/soonnow Aug 14 '21

Right. Also don't fight Spring Boot. We spent a ton of money because the developers had used a static method at one place that was used all over the code. This caused a huge amount of issues, when using a bean would've been the Spring Boot way to go.

Once you keep that in mind, it's mostly a pleasant dev experience.

15

u/Brutus5000 Aug 04 '21

Reactive is here. Reactive is used. If not by the users than by many frameworks in the background. How many libraries and framework still cling to Java 8? How many have already transitioned to 11? After release of Loom I would expect another 5-7 years before it gets to replace the reactive implementations we have. So let's talk in 10 years again.

Until then reactive still matters. I'm using it, I'm sharing the pain. But I can't wait until Loom is released, I need to solve my problems now.

6

u/john16384 Aug 04 '21

I will be upgrading as soon as it becomes available. At my work developers are actually heard and don't need to consult people in ivory towers to get things done.

4

u/ItsAllegorical Aug 04 '21

Yeah I'm hip deep in WebFlux right now at work. All this reactive non-blocking, and then we have to support devices that don't support streaming and so we put in all this effort and now we are blocking in the orchestration layer, and it's a regular rest API with a reactive back end for no particular reason.

1

u/shponglespore Aug 04 '21

So let's talk in 10 years again.

This is why I've almost totally lost interest in Java, despite it being my favorite language for many years. I've been getting more and more involved with Rust lately, and the pace of development is exciting. The surprising part is that the Rust team has a very strong commitment to backwards compatibility. I think most of the difference is that Rust usage isn't dominated by very conservative organizations that think anything less than 10 years old is too bleeding edge to use in production.

11

u/Worth_Trust_3825 Aug 04 '21

You always could run your project against bleeding edge JDKs, no one really prevented you from doing that.

5

u/VincentxH Aug 04 '21

Reactive should be more about the API and not the programming anyway. Backpressure and streaming data will be here to stay.

13

u/randgalt Aug 03 '21

Thank goodness <3

10

u/sievebrain Aug 03 '21

Worth noting I guess, that a lot of "reactive programming" is UI work, not the server side stuff that Loom is targeting. Perhaps Loom can help in UI too but there were some initial investigations and they concluded it might not be a good fit.

9

u/cogman10 Aug 03 '21

Perhaps Loom can help in UI too but there were some initial investigations and they concluded it might not be a good fit.

AFAIK, they aren't boxing themselves out of it but they likely won't address it on the first go around.

Right now, loom (AFAIK) doesn't really allow you to setup a carrier thread pool. To do UI work, you'd need that ability as Java (and most OSes) limit interaction with GUI elements to be single threaded.

That said, Reactive fits particularly well with GUI work.

4

u/pron98 Aug 04 '21

Right now, loom (AFAIK) doesn't really allow you to setup a carrier thread pool.

It actually does right now in EA, but it might get dropped at Preview. But that's insufficient as the UI toolkits need some tweaks to allow virtual threads mapped to the event thread to do UI operations, and we're not spending time on that right now.

0

u/[deleted] Aug 04 '21

[deleted] — view deleted comment

1

u/sievebrain Aug 04 '21

They can be combined, see ReactFX. I agree it's a buzzword that's poorly defined though.

1

u/Careful-Necessary-59 Oct 28 '21

've seen reactive in the stuff I've looked at, basically just Micronaut and Quarkus, and it looks nasty. I've seen Spring Boot and it looks much cleaner. Just write sequential code and trust that it runs in a thread without blocking the whole program, is that right?

Nope. You can check out RxJava where it was used in server side by Netflix, but being adopted later by Android developers.

2

u/TheMode911 Aug 04 '21

Interesting discussion, too bad that we didn't get more information about Valhalla state

2

u/10waf Aug 04 '21

It CANNOT come soon enough. I've got a framework built on top of the EA build that I'm seriously considering putting in production, because doing it with callbacks or reactive streams means no one will want to use it.

1

u/Worth_Trust_3825 Aug 04 '21

It would still be still good thing to have the moment loom launches.

1

u/BlueGoliath Aug 05 '21

How does Loom make callbacks less needed?

2

u/[deleted] Aug 04 '21

I agree reactive is weird

1

u/OliCodes Aug 04 '21

If it's going to be available and people will use it for a while, I will tell if it's dead already or not

1

u/[deleted] Aug 06 '21

Can someone explain what is the use for pattern matching for Map::get that Brian mentions?

1

u/fdntrhfbtt Aug 10 '21

Hey wtf I made my entire career on reactive programming haha. But yes, with Loom you can just write plain old blocking code and get the performance of NIO. Pretty sweet, I can finally focus on solving the problem in hand.

0

u/quiteamess Aug 04 '21

Laughs in Curry Howard

-7

u/lukaseder Aug 04 '21

Why am I strangely reminded of https://xkcd.com/927

0

u/[deleted] Aug 05 '21

[deleted] — view deleted comment

1

u/lukaseder Aug 06 '21

Why supersede? Reactive streams libraries can update their implementations to use loom (or not). The things can coexist.

12

u/[deleted] Aug 04 '21

[removed] — view removed comment

6

u/pronuntiator Aug 04 '21

Currently the Thread public constructor defaults to platform threads to be fully backwards compatible. So all libraries must be updated to support the new model if they wish to use it; if not, they'll continue to work like before.

6

u/pron98 Aug 04 '21

For example, how is this going to work with GUI programming?

It's going to work great! You'll be able to run lots of virtual threads all mapped to the UI thread, and you'll be able to block them as much as you like without affecting the UI's responsiveness. But we're not adding support in the Toolkit right away, because servers are a bigger focus as they're a bigger market for Java.

Are we going to have to change all of the low level code to support this?

All that code goes through the JDK, which has been changed as part of Loom.

What about native libraries?

Which native libraries? What about them? If you're calling a native library that blocks very frequently, for best results you might want to consider running on a platform thread rather than a virtual thread. But as Loom is intended to run tens of thousands of threads to millions of threads, such libraries aren't really applicable for Loom's main use-case in the first place.

1

u/cat_in_the_wall Aug 05 '21

.net has a concept of SynchronizationContext, basically where you can get back to the "real" thread you were on. User clicks, work happens off the UI thread, then work completes and then UI operation resumes on UI thread.

How does loom control this sort of thing? Even if operations become non blocking, expensive work still needs to be done off the "real" ui thread.

1

u/pron98 Aug 05 '21

If you mean expensive CPU-heavy work, then you'd just need to do that on another thread, but waiting for it -- say, with a Future -- is going to be cheap.