∛ Cube Root Money must move once, even when machines fail.
ContactTry the IDE in your browser →
← Articles

Two labels instead of two workflow classes

A payment file, in parallel: its batches together, and in each batch its transfers together.

Cube Root · · 3 min read

A payment file is the everyday unit of bulk payments: a file holds batches, and a batch holds transfers. A bank receiving one wants every transfer handled as fast as the systems behind it allow, which means in parallel, and it wants each transfer to be durable on its own, so that one failure does not undo or repeat the rest.

Here is that workflow written twice: once for Temporal, once in Cube Root.

In Temporal

Temporal's way to run durable work in parallel is child workflows, started asynchronously and joined with a promise. Two levels of parallelism mean two workflow classes:

public class FileWorkflowImpl implements FileWorkflow {
  @Override
  public String process(PaymentFile file) {
    String fileWorkflowId = Workflow.getInfo().getWorkflowId();
    List<Promise<String>> outcomes = new ArrayList<>();
    for (PaymentBatch batch : file.batches()) {
      ChildWorkflowOptions options =
          ChildWorkflowOptions.newBuilder()
              .setWorkflowId(fileWorkflowId + "/batch-" + batch.batchId())
              .build();
      BatchWorkflow child = Workflow.newChildWorkflowStub(BatchWorkflow.class, options);
      outcomes.add(Async.function(child::process, batch));
    }
    Promise.allOf(outcomes).get();
    return "file " + file.fileId() + " processed";
  }
}

public class BatchWorkflowImpl implements BatchWorkflow {
  @Override
  public String process(PaymentBatch batch) {
    List<Promise<String>> outcomes = new ArrayList<>();
    for (Transfer transfer : batch.transfers()) {
      ChildWorkflowOptions options =
          ChildWorkflowOptions.newBuilder()
              .setWorkflowId("transfer-" + transfer.endToEndId())
              .build();
      TransferWorkflow child = Workflow.newChildWorkflowStub(TransferWorkflow.class, options);
      outcomes.add(Async.function(child::transfer, transfer));
    }
    Promise.allOf(outcomes).get();
    return "batch " + batch.batchId() + " processed";
  }
}

This is good, idiomatic Temporal code, written from its documentation. It is also mostly infrastructure: workflow IDs, child options, stubs, async calls, a list of promises and a join. Each of those lines names the engine.

In Cube Root

public class PaymentFileRun {
    private Flow<Transfer, String> transfer;

    public String invoke(PaymentFile file) {
        int n = file.batches().size();
        parallel: for (int i = 0; i < n; i++) {
            PaymentBatch batch = file.batches().get(i);
            int m = batch.transfers().size();
            parallel_transfers: for (int j = 0; j < m; j++) {
                String outcome = transfer.invoke(batch.transfers().get(j));
            }
        }
        return "file " + file.fileId() + " processed";
    }
}

Two loops and two labels. (Java needs the inner label to have a name of its own; any name that starts with parallel asks for the same thing.) The label asks for "together". Each transfer.invoke is a call to the transfer's own flow, a durable conversation of its own, with its own steps, its own undo and its own record.

A hint the compiler checks

A label is not an order to the runtime; it is a hint the compiler checks. parallel: is accepted only if no iteration reads anything an earlier one wrote. If one did, running them together would race, so the compiler refuses the label by name, with its line, and says why. A loop that is safe to parallelize says so in one word; a loop that is not can never be made parallel by mistake.

Temporal Cube Root
Classes for two levels two workflow classes and their interfaces one flow
Asking for parallel child workflow stubs, async calls, a promise join a label on each loop
Each transfer durable on its own a child workflow per transfer a call to the transfer's own flow
A loop that would race found in testing, or in production refused when compiled

What it costs to read

The difference that matters day to day is not the number of lines. It is what a reviewer must hold in their head. In the first version, the business logic (for each batch, for each transfer, send it) is spread between the infrastructure. In the second, it is the code.

Try it

The payment file is a sample in the browser IDE at cube-root.com. Open it and choose View execution graph: the batches side by side, and inside each, its transfers. The comparison of the two versions is also on how it compares.

Next in this series: an AI agent is an ordinary while loop.


cube-root.com · Articles · Contact