Friday, November 27, 2026

Towards a fully reactive transactional pipleline

Towards a fully reactive transactional pipleline

In this post I want to discuss a couple of problems integraters hit when using Narayana transactions because of the disconnect between JTA and reactive which boils down to three main obstacles:

  1. The XA participant API is blocking: During the prepare phase of the protocol each participant is asked to prepare to commit and only if they all prepared will phase 2, the commit, be tried, i.e. the protocol blocks by design.
  2. Transactions are thread-bound: The specification requires a transaction context to be associated with a single active thread at a time so that transactional resources can be enlisted transparently, while allowing that context to be suspended and resumed on another thread when needed. In a reactive pipeline this can get quite tricky to manage.
  3. The database, more generally the resource, holds in-doubt state: If it says yes to prepare then the XA spec requires it to remember its decision even in the presence of failures which typically requires the holding of locks on any data touched by the resource during the active phase of the transaction.

The first obstacle is difficult to work around and only partial solutions exist - Narayana provides some features for controlling how prepare, commit and rollback are handled but prepare is still blocking and asynchronous commit does not report heuristics outcomes where not all resources reported success. The blocking nature of prepare is partially addressed with virtual threads - when the property useVirtualThreadsForTwoPhaseCommitThreads is true a participant blocking inside prepare() un-mounts from its platform carrier thread instead of blocking the platform thread (referred to as pinning the thread).

The second obstacle is allready mostly addressed, though only in certain contexts. The Eclipse MicroProfile Context Propagation specification provides a nice solution when MicroProfile is an option and the SmallRye implementation uses Narayana for its TCK. The context is managed by a JtaContextProvider (when the context is “captured” it remembers the transaction; on the target thread, the one that receives the context, it suspends any existing transaction there and tm.resume()’s the captured transaction; and when the call finishes on the target thread it puts the original context back) - it is worth noting that the spec requires the transaction to be active on at most one thread at any given time and will fail with an IllegalStateException (with the transaction marked for rollback-only) if you try to use it on a second thread. But this won’t be of use to applications using other reactive APIs - Quarkus, for example, uses the Mutiny programming library as its primary model for writing reactive applications and a transaction API based on Mutiny is a strong candidate for improving our reactive story, and it just so happens that we have an early prototype for that, but that is a topic for another day. Note also that right now, with CDI, we do provide a ContextPropagationAsyncHandler that holds an @Transactional method’s transaction open until the returned reactive value completes.

The third obstacle, i.e. having to maintain transaction state between prepare and commit, is a problem particularly when scaling to the levels that reactive + virtual threads can facilitate, becomes a real issue for high throughput workloads and should not ignored. There is no general solution but there is a nice optimisation whereby we can skip the prepare phase altogether for one of the datasources, although niche it is a choice that can be of benefit - for example maybe one of the resources is doing the bulk of the work, locking many rows, or simply locking rows that are under high contention and locking becomes a throughput bottleneck, or even just that the datasource is not XA enabled, etc, and in these situations the resource is a candidate for the “single resource optimisation”. To take advantage of this approach the standard solution is LRCO (Last Resource Commit Optimisation) where the resource is committed after the XA resources have prepared (hence the name Last Resource Commit) but this can be dangerous if the Transaction Manager (TM) crashes before durably logging its intention to commit because on restart it will tell the XA resources (technically they are called orphans) to rollback even though the Last Resource has committed, breaking the atomicity property of transactions (the “all-or-nothing” guarantee) and supporting it is not really viable.

But even though LRCO is unsafe it has a close cousin, namely CommitMarkableResouce (CMR), which can be used for datasources but it is only available in WildFly and JBoss Enterprise Application Platform. It would be advantageous to make the option available more generally so that other platforms and runtimes, such as Quarkus, can benefit from it too:

Replacing the prepared resource with a row in the database

During prepare, CMR writes a “marker row” on the same connection as the application data so both writes happen atomically, the consequence is that in recovery situations the presence of the marker row implies that the Last Resource committed successfully and therefore the TM’s decision was to commit which is what allows recovery to resolve the XA participants after a crash even though it has no log - it’s ingenious isn’t it!

We factorised that into a datasource wrapper usable from outside of the application server, and validated it against standard Narayana: the validation kills various components (the application, the TM and the DB) at each step of the protocol (including an XA aware resource participant for verifying that it’s branch is recovered correctly). Under load, the number of prepared transactions always ends at zero so the prototype looks like it works as intended. If there’s any interest then we’ll convert that into a quickstart but we will certainly target Quarkus where we are seeing a lot of demand for LRCO and in the service of safety we’d like to push CMR as the correct way to go.

Consequences

With CMR there are no XA start/end/prepare/commit/rollback calls, which are all blocking, which means every DB call uses standard SQL which reactive drivers can issue without blocking the calling thread.

Thread to transaction association at each step of a reactive pipeline is proven by the SmallRye implementation of the MP Context Propagation specification and the Narayana ContextPropagationAsyncHandler class for reactive return types. Similar techniques could be quickly prototyped for Quarkus using the Mutiny programming library.

The blocking nature of the prepare phase can be partially mitigated against by formalising/extending what we aleady have: virtual threads and/or reactive return types for prepare, commit and rollback that the TM drives without blocking the application thread.

In addition to the blocking nature of participant prepare, it’s important to point out that the TM also blocks at the end of a successful prepare phase when it must write a durable record of its commit decision so that in crash scenarios it can pick up from where it left off on restart. I did not consider the impact of not having a “reactive object store” - when the log is written after prepare the TM must be certain that the log is durable before moving onto the commit phase of the protocol.

And finally, for this post, heuristic outcomes particularly mixed outcomes (some participants commit and others rollback) also requires attention if heuristic reporting is a requirement (i.e. we can’t fire and forget the commit part of the protocol). Narayana does provide heuristic notifications which may be worthwhile exposing in some kind of SPI (mixed outcomes cause the most havoc). And to finish on a negative, note that afterCompletion does not work since that does not report heuristic outcomes in the status.