Kill -9 and Carry On

Durable Execution
from the Inside

Kill -9 and Carry On · Renato G. Cavalcanti
Kill -9 and Carry On · Renato G. Cavalcanti

Renato Guerra Cavalcanti

  • Most of my career on the JVM: Java and Scala
  • BeScala: the Belgian Scala User Group
  • Event Sourcing, CQRS, Microservices
  • Akka Runtime/SDK and Akka Agentic Platform
Kill -9 and Carry On · Renato G. Cavalcanti

What is Durable Execution?

Kill -9 and Carry On · Renato G. Cavalcanti

What is Durable Execution?

  • Code that survives crashes
  • More domain, less boilerplate.
    Abstracts away some of the plumbing
  • A runtime/platform does the heavy lifting
  • The ingredients:
    state management, pause, retries, recovery, timers
Kill -9 and Carry On · Renato G. Cavalcanti

Durable Execution vs. BPM

  • Same promise: long-running, reliable, stateful
  • BPM: diagrams, owned by analysts
  • Durable execution: code, owned by developers
Kill -9 and Carry On · Renato G. Cavalcanti

Why now?

Kill -9 and Carry On · Renato G. Cavalcanti

Why now?

  • Orchestration sagas are hard
  • AI agents need orchestrations
  • Choreography: head-of-line blocking for agents
  • Restarting from zero is expensive
  • Decision APIs (ie:TypeSafe.ai Jev)
Kill -9 and Carry On · Renato G. Cavalcanti

Choreography vs. Orchestration

Kill -9 and Carry On · Renato G. Cavalcanti

Choreography

queue / partition / journal
For a given stream,
we can only consume one at a time
consumer
offset0
Kill -9 and Carry On · Renato G. Cavalcanti

Orchestration

queue / partition / journal
Kill -9 and Carry On · Renato G. Cavalcanti

Some durable players

Kill -9 and Carry On · Renato G. Cavalcanti

Some durable players

Kill -9 and Carry On · Renato G. Cavalcanti

The use case

Kill -9 and Carry On · Renato G. Cavalcanti

The use case

  • Transfer money from Wallet A to Wallet B
  • Withdraw from A, then deposit into B
  • Fails between withdraw and deposit
  • No idempotency, for simplicity
Kill -9 and Carry On · Renato G. Cavalcanti

Waiver

This is a very naïve implementation of a wallet transfer application.

Its sole purpose is to demo durable execution frameworks.

Do not use it a work!

Kill -9 and Carry On · Renato G. Cavalcanti

Restate

Kill -9 and Carry On · Renato G. Cavalcanti

Restate

Caller JAVA PROCESS APPLICATION REST controller :9000 TransferWorkflow · WalletService RESTATE SDK HTTP server :9080 runs handlers · records steps RESTATE SERVER Ingress :8080 Journal · state timers submit register invoke calls step results
Kill -9 and Carry On · Renato G. Cavalcanti

Restate

@PostMapping("/transfer/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public TransferStarted startTransfer(@PathVariable String id, @RequestBody Transfer transfer) {
  client
    .workflowHandle(TransferWorkflow.class, id)
    .send(TransferWorkflow::run, transfer);

  return new TransferStarted(id);
}
Kill -9 and Carry On · Renato G. Cavalcanti

Restate

@Workflow
public TransferState run(Transfer transfer) {
  var state = TransferState.started(Restate.key(), transfer);
  Restate.state().set(STATE, state);

  Restate.run("withdraw", () -> wallets.withdraw(transfer.from(), transfer.amount()));
  state = state.withStatus(WITHDRAW_SUCCEEDED);
  Restate.state().set(STATE, state);

  // execution will be 'suspended' (actually dropped)
  if (transfer.amount() >= 200) Restate.sleep(Duration.ofSeconds(20));

  try {
    Restate.run("deposit", retryPolicy, () -> wallets.deposit(transfer.to(), transfer.amount()));
    state = state.withStatus(COMPLETED);
  } catch (TerminalException e) {
    Restate.run("deposit", () -> wallets.deposit(transfer.from(), transfer.amount()));
    state = state.withStatus(CANCELLED);
  }

  Restate.state().set(STATE, state);
  return state;
}
Kill -9 and Carry On · Renato G. Cavalcanti

Restate · Threads

  • Virtual threads by default
  • Handler parks on each await
  • Long wait: server asks to suspend
  • Thread unwinds, journal replays later
Kill -9 and Carry On · Renato G. Cavalcanti

Temporal

Kill -9 and Carry On · Renato G. Cavalcanti

Temporal

Caller JAVA PROCESS APPLICATION REST controller :9000 WorkflowClient · stub WORKFLOW WORKER TransferWorkflowImpl ACTIVITY WORKER TransferActivitiesImpl TEMPORAL SERVER Frontend :7233 History · timers task queues start record poll commands poll results
Kill -9 and Carry On · Renato G. Cavalcanti

Temporal

@PostMapping("/transfer/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public TransferStarted startTransfer(@PathVariable String id, @RequestBody Transfer transfer) {
  var options = WorkflowOptions.newBuilder()
    .setWorkflowId(id)
    .setTaskQueue(TransferWorkflow.TASK_QUEUE)
    .build();

  var workflow = client.newWorkflowStub(TransferWorkflow.class, options);
  WorkflowClient.start(workflow::transfer, transfer);

  return new TransferStarted(id);
}
Kill -9 and Carry On · Renato G. Cavalcanti

Temporal

@Override
public TransferState transfer(Transfer transfer) {
  var transferId = Workflow.getInfo().getWorkflowId();
  state = TransferState.started(transferId, transfer);

  // parks the thread and wait completion
  activities.withdraw(transfer.from(), transfer.amount());
  state = state.withStatus(WITHDRAW_SUCCEEDED);

  // parks the thread
  if (transfer.amount() >= 200) Workflow.sleep(Duration.ofSeconds(20));

  // parks the thrad and wait completion
  activities.deposit(transfer.to(), transfer.amount());
  state = state.withStatus(COMPLETED);
  return state;
}

Kill -9 and Carry On · Renato G. Cavalcanti

Temporal · Threads

  • Platform threads by default
  • 600 threads, 600 cached workflows
  • Pool full: evict an idle workflow
  • Evicted workflow replays on next task
  • Virtual threads are opt-in
Kill -9 and Carry On · Renato G. Cavalcanti

Axon Framework

Kill -9 and Carry On · Renato G. Cavalcanti

Axon Framework

Caller JAVA PROCESS APPLICATION REST controller :9000 EventGateway · WalletService WORKFLOW ENGINE TransferWorkflow embedded · runs steps AXON SERVER Event store :8124 events · step history publish events calls step events
Kill -9 and Carry On · Renato G. Cavalcanti

Axon Framework

@PostMapping("/transfer/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
public TransferStarted start(@PathVariable String id, @RequestBody Transfer transfer) {
  events // EventGateway
    .publish(List.of(new TransferRequested(id, transfer.from(), transfer.to(), transfer.amount())))
    .join();

  return new TransferStarted(id);
}
Kill -9 and Carry On · Renato G. Cavalcanti

Axon Framework

@Workflow(
  workflowName = "MoneyTransfer",
  idProperty = "transferId",
  startOnEventClass = TransferRequested.class)
public void transfer(SimpleWorkflowContext context) {
    
  var payload = json.convertValue(context.workflowPayload(), TransferWorkflowPayload.class);
  context.setPayload("initializeTransfer", payload.withStatus(STARTED));

  context.awaitExecute("withdraw", Integer.class,
    () -> wallets.withdraw(payload.from(), payload.amount())); 
  
  context.setPayload("withdrawalSucceeded", payload.withStatus(WITHDRAW_SUCCEEDED));

  if (payload.amount() >= 200)
    context.sleep("pauseAfterWithdraw", step -> step.timeout(Duration.ofSeconds(20)))
    .await();

  context.awaitExecute("deposit", Integer.class,
    () -> wallets.deposit(payload.to(), payload.amount()));
  context.setPayload("transferCompleted", payload.withStatus(COMPLETED));
}
Kill -9 and Carry On · Renato G. Cavalcanti

Axon Framework · Threads

  • Virtual threads by default
  • One parked thread per running workflow
  • No pool limit, no eviction
  • Replays only on restart or failover
Kill -9 and Carry On · Renato G. Cavalcanti

Akka

Kill -9 and Carry On · Renato G. Cavalcanti

Akka

Caller AKKA RUNTIME RUNTIME Executes Effects persistence · timers · calls APPLICATION · USER CODE Endpoint · Workflow · Entity handlers return Effects DATABASE Journal events call Effect events
Kill -9 and Carry On · Renato G. Cavalcanti

Akka

@Post("/transfer/{id}")
public HttpResponse start(String id, Transfer transfer) {
  componentClient
    .forWorkflow(id)
    .method(TransferWorkflow::start)
    .invoke(transfer); // user code always on virtual threads

  return HttpResponses.accepted();
}
Kill -9 and Carry On · Renato G. Cavalcanti

Akka

public Effect<String> start(Transfer transfer) {
  return effects()
    .updateState(TransferState.create(transferId, transfer))
    .transitionTo(TransferWorkflow::withdrawStep)
    .thenReply("transfer started");
}

@StepName("withdraw")
private StepEffect withdrawStep() {
  var from = currentState().transfer().from();
  var amount = currentState().transfer().amount();
  componentClient.forEventSourcedEntity(from)
    .method(WalletEntity::withdraw).invoke(amount);

  if (amount >= 200) {
    return stepEffects()
      .updateState(currentState().withStatus(WITHDRAW_SUCCEEDED))
      .thenPause(pauseSetting(Duration.ofSeconds(20))
        .timeoutHandler(TransferWorkflow::resumeAfterPause));
  }

  return stepEffects()
    .updateState(currentState().withStatus(WITHDRAW_SUCCEEDED))
    .thenTransitionTo(TransferWorkflow::depositStep);
}
Kill -9 and Carry On · Renato G. Cavalcanti

Akka

public Effect<Done> resumeAfterPause() {
  return effects()
    .transitionTo(TransferWorkflow::depositStep)
    .thenReply(Done.getInstance());
}

@StepName("deposit")
private StepEffect depositStep() {
  var to = currentState().transfer().to();
  var amount = currentState().transfer().amount();
  componentClient.forEventSourcedEntity(to)
    .method(WalletEntity::deposit)
    .invoke(amount);

  return stepEffects()
    .updateState(currentState().withStatus(COMPLETED))
    .thenEnd();
}

public ReadOnlyEffect<TransferState> get() {
  return effects().reply(currentState());
}
Kill -9 and Carry On · Renato G. Cavalcanti

Akka · Threads

  • User code on virtual threads
  • Under the hood components are Actors
  • One mailbox per Actor
  • Location transparency built in
  • All calls go through clients
Kill -9 and Carry On · Renato G. Cavalcanti

Wrapping up

Kill -9 and Carry On · Renato G. Cavalcanti

Wrapping up

  • Event sourced execution
  • Steps recorded, replayed after a crash
  • The platform does the heavy lifting
  • Less saga plumbing, more domain
  • Guaranteed to run to completion
Kill -9 and Carry On · Renato G. Cavalcanti

Thank you!

Kill -9 and Carry On · Renato G. Cavalcanti