Continuations and the JVM

August 04, 2017

Continuations and the JVM

The Corda flow framework provides a lot of functionality for implementing complex multi-party workflows, which are a key part of essentially every blockchain/DLT-based app. The framework is built on top of another open source library called Quasar, produced by a company called Parallel Universe. R3 has a commercial support agreement with Parallel Universe which enables us to request prioritised bug fixes and other forms of technical support.

Quasar implements fibers and continuations on the JVM. This allows Corda developers to write simple, straight-line blocking code and have it transformed into highly scalable asynchronous state machines that checkpoint their state to disk as messages fly around and the workflow progresses. This nearly-transparent approach means developers hardly have to think about writing code to survive node restarts, crashes or transient outages at counterparties: common but tedious and time consuming tasks. This is crucial for making multi-party transactions easy to use and Corda apps pleasant to develop.

In future versions of the framework we will be adding support for waiting on other potentially slow things, like human approvals.

Quasar implements continuations on the JVM by statically analysing and rewriting method bytecodes on the fly. All the developer has to do is add annotations to methods that may be on the stack when a checkpoint occurs (e.g. because a message was sent or received).

In Java 9, new functionality was added to the HotSpot JVM specifically for usage by Quasar that makes the nearly-transparent approach in Java 8 entirely transparent. Once Corda upgrades to Java 9, developers will no longer even have to remember to annotate their methods.

The author of Quasar, Ron Pressler, has recently joined Oracle to work on integrating fibers and continuations more deeply in the JVM. He gave a lightning talk at the 2017 JVM Language Summit. It’s only five minutes and worth watching to get an overview of what fibers and continuations are all about:

These talks are not product announcements and should not be interpreted as a guarantee that things will happen, but the extra support for Quasar that’s shipping in Java 9 is a good start. Now one of the pioneers of Java fibers has literally joined the JVM team, HotSpot’s support for the Corda flow framework can only improve.

It was also nice to see a shoutout to our work during the talk, where Ron observes that serialised continuations can be useful for implementing financial transactions that last for weeks or months. Whilst we don’t recommend authoring Corda flows that block for weeks or months at the moment, this is probably the closest we’ll ever get to the JVM architects stating they’re adding features to Java because of Corda!

The flow framework is a key part of Corda’s value to developers, and Corda Enterprise (not yet available) has some enhancements to it: an IntelliJ plugin that lets you visually explore what state is being captured in checkpoints, and a new section in the Explorer app that lets you visually see what flows are running and which have taken errors. The “flow hospital” as we call it is a key part of how operators can recover or retry workflows that failed.