Build Your First Workflow
In this tutorial, you'll build and run your first Temporal application. You'll understand the core building blocks of Temporal and learn how Temporal helps you build crash proof applications through durable execution.
Introduction
Prerequisites
Before you begin, set up your local development environment:
Quickstart Guide
Run through the Quickstart to get your set up complete.
What You'll Build
You’ll build a basic money transfer app from the ground up, learning how to handle essential transactions like deposits, withdrawals, and refunds using Temporal.
Why This Application?: Most applications require multiple coordinated steps - processing payments, sending emails, updating databases. This tutorial uses money transfers to demonstrate how Temporal ensures these multi-step processes complete reliably, resuming exactly where they left off even after any failure.


In this sample application, money comes out of one account and goes into another. However, there are a few things that can go wrong with this process. If the withdrawal fails, then there is no need to try to make a deposit. But if the withdrawal succeeds, but the deposit fails, then the money needs to go back to the original account.
One of Temporal's most important features is its ability to maintain the application state when something fails. When failures happen, Temporal recovers processes where they left off or rolls them back correctly. This allows you to focus on business logic, instead of writing application code to recover from failure.
Download the example application
The application you'll use in this tutorial is available in a GitHub repository.
Open a new terminal window and use git to clone the repository, then change to the project directory.
Now that you've downloaded the project, let's dive into the code.
git clone https://github.com/temporalio/money-transfer-project-template-go
cd money-transfer-project-template-go
git clone https://github.com/temporalio/money-transfer-project-java
cd money-transfer-project-java
git clone https://github.com/temporalio/money-transfer-project-template-python
cd money-transfer-project-template-python
python -m pip install temporalio
git clone https://github.com/temporalio/money-transfer-project-template-ts
cd money-transfer-project-template-ts
npm install
git clone https://github.com/temporalio/money-transfer-project-template-php
cd money-transfer-project-template-php
composer install
git clone https://github.com/temporalio/money-transfer-project-template-dotnet
cd money-transfer-project-template-dotnet
git clone https://github.com/temporalio/money-transfer-project-template-ruby
cd money-transfer-project-template-ruby
bundle install
The repository for this tutorial is a GitHub Template repository, which means you could clone it to your own account and use it as the foundation for your own Temporal application.
Let's Recap: Temporal's Application Structure
The Temporal Application will consist of the following pieces:
- A Workflow written in your programming language of choice and your installed Temporal SDK in that language. A Workflow defines the overall flow of the application.
- An Activity is a function or method that does a specific operation - like withdrawing money, sending an email, or calling an API. Since these operations often depend on external services that can be unreliable, Temporal automatically retries Activities when they fail. In this application, you'll write Activities for withdraw, deposit, and refund operations.
- A Worker, provided by the Temporal SDK, which runs your Workflow and Activities reliably and consistently.

What You'll Build and Run
The project in this tutorial mimics a "money transfer" application. It is implemented with a single Workflow, which orchestrates the execution of three Activities (Withdraw, Deposit, and Refund) that move money between the accounts.
To perform a money transfer, you will do the following:
-
Launch a Worker: Since a Worker is responsible for executing the Workflow and Activity code, at least one Worker must be running for the money transfer to make progress.
-
Start a Workflow Execution through the Temporal Service: After the Worker communicates with the Temporal Service, the Worker will begin executing the Workflow and Activity code. It reports the results to the Temporal Service, which tracks the progress of the Workflow Execution.
None of your application code runs on the Temporal Server. Your Worker, Workflow, and Activity run on your infrastructure, along with the rest of your applications.
Step 1: Build your Workflow and Activities
Workflow Definition
In the Temporal Go SDK, a Workflow Definition is a Go function that accepts a Workflow Context and input parameters.
This is what the Workflow Definition looks like for the money transfer process:
func MoneyTransfer(ctx workflow.Context, input PaymentDetails) (string, error) {
// RetryPolicy specifies how to automatically handle retries if an Activity fails.
retrypolicy := &temporal.RetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2.0,
MaximumInterval: 100 * time.Second,
MaximumAttempts: 500, // 0 is unlimited retries
NonRetryableErrorTypes: []string{"InvalidAccountError", "InsufficientFundsError"},
}
options := workflow.ActivityOptions{
// Timeout options specify when to automatically timeout Activity functions.
StartToCloseTimeout: time.Minute,
// Optionally provide a customized RetryPolicy.
// Temporal retries failed Activities by default.
RetryPolicy: retrypolicy,
}
// Apply the options.
ctx = workflow.WithActivityOptions(ctx, options)
// Withdraw money.
var withdrawOutput string
withdrawErr := workflow.ExecuteActivity(ctx, Withdraw, input).Get(ctx, &withdrawOutput)
if withdrawErr != nil {
return "", withdrawErr
}
// Deposit money.
var depositOutput string
depositErr := workflow.ExecuteActivity(ctx, Deposit, input).Get(ctx, &depositOutput)
if depositErr != nil {
// The deposit failed; put money back in original account.
var result string
refundErr := workflow.ExecuteActivity(ctx, Refund, input).Get(ctx, &result)
if refundErr != nil {
return "",
fmt.Errorf("Deposit: failed to deposit money into %v: %v. Money could not be returned to %v: %w",
input.TargetAccount, depositErr, input.SourceAccount, refundErr)
}
return "", fmt.Errorf("Deposit: failed to deposit money into %v: Money returned to %v: %w",
input.TargetAccount, input.SourceAccount, depositErr)
}
result := fmt.Sprintf("Transfer complete (transaction IDs: %s, %s)", withdrawOutput, depositOutput)
return result, nil
}
The MoneyTransfer function takes in the details about the transaction, executes Activities to withdraw and deposit the money, and returns the results of the process. The PaymentDetails type is defined in shared.go:
type PaymentDetails struct {
SourceAccount string
TargetAccount string
Amount int
ReferenceID string
}
Activity Definition
Activities handle the business logic. Each Activity function calls an external banking service:
func Withdraw(ctx context.Context, data PaymentDetails) (string, error) {
log.Printf("Withdrawing $%d from account %s.\n\n",
data.Amount,
data.SourceAccount,
)
referenceID := fmt.Sprintf("%s-withdrawal", data.ReferenceID)
bank := BankingService{"bank-api.example.com"}
confirmation, err := bank.Withdraw(data.SourceAccount, data.Amount, referenceID)
return confirmation, err
}
func Deposit(ctx context.Context, data PaymentDetails) (string, error) {
log.Printf("Depositing $%d into account %s.\n\n",
data.Amount,
data.TargetAccount,
)
referenceID := fmt.Sprintf("%s-deposit", data.ReferenceID)
bank := BankingService{"bank-api.example.com"}
// Uncomment the next line and comment the one after that to simulate an unknown failure
// confirmation, err := bank.DepositThatFails(data.TargetAccount, data.Amount, referenceID)
confirmation, err := bank.Deposit(data.TargetAccount, data.Amount, referenceID)
return confirmation, err
}
Workflow Definition
In the Temporal Java SDK, a Workflow Definition is marked by the @WorkflowInterface attribute placed above the class interface.
src/main/java/moneytransferapp/MoneyTransferWorkflow.java
package moneytransferapp;
import io.temporal.workflow.WorkflowInterface;
import io.temporal.workflow.WorkflowMethod;
@WorkflowInterface
public interface MoneyTransferWorkflow {
// The Workflow Execution that starts this method can be initiated from code or
// from the 'temporal' CLI utility.
@WorkflowMethod
void transfer(TransactionDetails transaction);
}
src/main/java/moneytransferapp/MoneyTransferWorkflowImpl.java
package moneytransferapp;
import io.temporal.activity.ActivityOptions;
import io.temporal.workflow.Workflow;
import io.temporal.common.RetryOptions;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
public class MoneyTransferWorkflowImpl implements MoneyTransferWorkflow {
private static final String WITHDRAW = "Withdraw";
// RetryOptions specify how to automatically handle retries when Activities fail
private final RetryOptions retryoptions = RetryOptions.newBuilder()
.setInitialInterval(Duration.ofSeconds(1)) // Wait 1 second before first retry
.setMaximumInterval(Duration.ofSeconds(20)) // Do not exceed 20 seconds between retries
.setBackoffCoefficient(2) // Wait 1 second, then 2, then 4, etc
.setMaximumAttempts(5000) // Fail after 5000 attempts
.build();
// ActivityOptions specify the limits on how long an Activity can execute before
// being interrupted by the Orchestration service
private final ActivityOptions defaultActivityOptions = ActivityOptions.newBuilder()
.setRetryOptions(retryoptions) // Apply the RetryOptions defined above
.setStartToCloseTimeout(Duration.ofSeconds(2)) // Max execution time for single Activity
.setScheduleToCloseTimeout(Duration.ofSeconds(5000)) // Entire duration from scheduling to completion including queue time
.build();
private final Map<String, ActivityOptions> perActivityMethodOptions = new HashMap<String, ActivityOptions>() {{
// A heartbeat time-out is a proof-of life indicator that an activity is still working.
// The 5 second duration used here waits for up to 5 seconds to hear a heartbeat.
// If one is not heard, the Activity fails.
// The `withdraw` method is hard-coded to succeed, so this never happens.
// Use heartbeats for long-lived event-driven applications.
put(WITHDRAW, ActivityOptions.newBuilder().setHeartbeatTimeout(Duration.ofSeconds(5)).build());
}};
// ActivityStubs enable calls to methods as if the Activity object is local but actually perform an RPC invocation
private final AccountActivity accountActivityStub = Workflow.newActivityStub(AccountActivity.class, defaultActivityOptions, perActivityMethodOptions);
// The transfer method is the entry point to the Workflow
// Activity method executions can be orchestrated here or from within other Activity methods
@Override
public void transfer(TransactionDetails transaction) {
// Retrieve transaction information from the `transaction` instance
String sourceAccountId = transaction.getSourceAccountId();
String destinationAccountId = transaction.getDestinationAccountId();
String transactionReferenceId = transaction.getTransactionReferenceId();
int amountToTransfer = transaction.getAmountToTransfer();
// Stage 1: Withdraw funds from source
try {
// Launch `withdrawal` Activity
accountActivityStub.withdraw(sourceAccountId, transactionReferenceId, amountToTransfer);
} catch (Exception e) {
// If the withdrawal fails, for any exception, it's caught here
System.out.printf("[%s] Withdrawal of $%d from account %s failed", transactionReferenceId, amountToTransfer, sourceAccountId);
System.out.flush();
// Transaction ends here
return;
}
// Stage 2: Deposit funds to destination
try {
// Perform `deposit` Activity
accountActivityStub.deposit(destinationAccountId, transactionReferenceId, amountToTransfer);
// The `deposit` was successful
System.out.printf("[%s] Transaction succeeded.\n", transactionReferenceId);
System.out.flush();
// Transaction ends here
return;
} catch (Exception e) {
// If the deposit fails, for any exception, it's caught here
System.out.printf("[%s] Deposit of $%d to account %s failed.\n", transactionReferenceId, amountToTransfer, destinationAccountId);
System.out.flush();
}
// Continue by compensating with a refund
try {
// Perform `refund` Activity
System.out.printf("[%s] Refunding $%d to account %s.\n", transactionReferenceId, amountToTransfer, sourceAccountId);
System.out.flush();
accountActivityStub.refund(sourceAccountId, transactionReferenceId, amountToTransfer);
// Recovery successful. Transaction ends here
System.out.printf("[%s] Refund to originating account was successful.\n", transactionReferenceId);
System.out.printf("[%s] Transaction is complete. No transfer made.\n", transactionReferenceId);
return;
} catch (Exception e) {
// A recovery mechanism can fail too. Handle any exception here
System.out.printf("[%s] Deposit of $%d to account %s failed. Did not compensate withdrawal.\n",
transactionReferenceId, amountToTransfer, destinationAccountId);
System.out.printf("[%s] Workflow failed.", transactionReferenceId);
System.out.flush();
// Rethrowing the exception causes a Workflow Task failure
throw(e);
}
}
}
The TransactionDetails interface:
src/main/java/moneytransferapp/TransactionDetails.java
package moneytransferapp;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(as = CoreTransactionDetails.class)
public interface TransactionDetails {
String getSourceAccountId();
String getDestinationAccountId();
String getTransactionReferenceId();
int getAmountToTransfer();
}
Activity Definition
Activities handle the business logic. Each Activity method calls an external banking service:
src/main/java/moneytransferapp/AccountActivity.java
package moneytransferapp;
import io.temporal.activity.ActivityInterface;
import io.temporal.activity.ActivityMethod;
@ActivityInterface
public interface AccountActivity {
// Withdraw an amount of money from the source account
@ActivityMethod
void withdraw(String accountId, String referenceId, int amount);
// Deposit an amount of money into the destination account
@ActivityMethod
void deposit(String accountId, String referenceId, int amount);
// Compensate a failed deposit by refunding to the original account
@ActivityMethod
void refund(String accountId, String referenceId, int amount);
}
src/main/java/moneytransferapp/AccountActivityImpl.java
package moneytransferapp;
import io.temporal.activity.*;
public class AccountActivityImpl implements AccountActivity {
// Mock up the withdrawal of an amount of money from the source account
@Override
public void withdraw(String accountId, String referenceId, int amount) {
System.out.printf("\nWithdrawing $%d from account %s.\n[ReferenceId: %s]\n", amount, accountId, referenceId);
System.out.flush();
}
// Mock up the deposit of an amount of money from the destination account
@Override
public void deposit(String accountId, String referenceId, int amount) {
boolean activityShouldSucceed = true;
if (!activityShouldSucceed) {
System.out.println("Deposit failed");
System.out.flush();
throw Activity.wrap(new RuntimeException("Simulated Activity error during deposit of funds"));
}
System.out.printf("\nDepositing $%d into account %s.\n[ReferenceId: %s]\n", amount, accountId, referenceId);
System.out.flush();
}
// Mock up a compensation refund to the source account
@Override
public void refund(String accountId, String referenceId, int amount) {
boolean activityShouldSucceed = true;
if (!activityShouldSucceed) {
System.out.println("Refund failed");
System.out.flush();
throw Activity.wrap(new RuntimeException("Simulated Activity error during refund to source account"));
}
System.out.printf("\nRefunding $%d to account %s.\n[ReferenceId: %s]\n", amount, accountId, referenceId);
System.out.flush();
}
}
Workflow Definition
A Workflow Definition in Python uses the @workflow.defn decorator on the Workflow class to identify a Workflow.
This is what the Workflow Definition looks like for this kind of process:
from datetime import timedelta
from temporalio import workflow
from temporalio.common import RetryPolicy
from temporalio.exceptions import ActivityError
with workflow.unsafe.imports_passed_through():
from activities import BankingActivities
from shared import PaymentDetails
@workflow.defn
class MoneyTransfer:
@workflow.run
async def run(self, payment_details: PaymentDetails) -> str:
retry_policy = RetryPolicy(
maximum_attempts=3,
maximum_interval=timedelta(seconds=2),
non_retryable_error_types=["InvalidAccountError", "InsufficientFundsError"],
)
# Withdraw money
withdraw_output = await workflow.execute_activity_method(
BankingActivities.withdraw,
payment_details,
start_to_close_timeout=timedelta(seconds=5),
retry_policy=retry_policy,
)
# Deposit money
try:
deposit_output = await workflow.execute_activity_method(
BankingActivities.deposit,
payment_details,
start_to_close_timeout=timedelta(seconds=5),
retry_policy=retry_policy,
)
result = f"Transfer complete (transaction IDs: {withdraw_output}, {deposit_output})"
return result
except ActivityError as deposit_err:
# Handle deposit error
workflow.logger.error(f"Deposit failed: {deposit_err}")
# Attempt to refund
try:
refund_output = await workflow.execute_activity_method(
BankingActivities.refund,
payment_details,
start_to_close_timeout=timedelta(seconds=5),
retry_policy=retry_policy,
)
workflow.logger.info(
f"Refund successful. Confirmation ID: {refund_output}"
)
except ActivityError as refund_error:
workflow.logger.error(f"Refund failed: {refund_error}")
raise refund_error from deposit_err
# Re-raise deposit error if refund was successful
raise deposit_err
The PaymentDetails dataclass is defined in shared.py:
from dataclasses import dataclass
MONEY_TRANSFER_TASK_QUEUE_NAME = "TRANSFER_MONEY_TASK_QUEUE"
@dataclass
class PaymentDetails:
source_account: str
target_account: str
amount: int
reference_id: str
Activity Definition
Activities handle the business logic. Each Activity method calls an external banking service:
@activity.defn
async def deposit(self, data: PaymentDetails) -> str:
reference_id = f"{data.reference_id}-deposit"
try:
confirmation = await asyncio.to_thread(
self.bank.deposit, data.target_account, data.amount, reference_id
)
"""
confirmation = await asyncio.to_thread(
self.bank.deposit_that_fails,
data.target_account,
data.amount,
reference_id,
)
"""
return confirmation
except InvalidAccountError:
raise
except Exception:
activity.logger.exception("Deposit failed")
raise
Workflow Definition
In the Temporal TypeScript SDK, a Workflow Definition is a regular TypeScript function that accepts some input values.
This is what the Workflow Definition looks like for the money transfer process:
import { proxyActivities } from '@temporalio/workflow';
import { ApplicationFailure } from '@temporalio/common';
import type * as activities from './activities';
import type { PaymentDetails } from './shared';
export async function moneyTransfer(details: PaymentDetails): Promise<string> {
// Get the Activities for the Workflow and set up the Activity Options.
const { withdraw, deposit, refund } = proxyActivities<typeof activities>({
// RetryPolicy specifies how to automatically handle retries if an Activity fails.
retry: {
initialInterval: '1 second',
maximumInterval: '1 minute',
backoffCoefficient: 2,
maximumAttempts: 500,
nonRetryableErrorTypes: ['InvalidAccountError', 'InsufficientFundsError'],
},
startToCloseTimeout: '1 minute',
});
// Execute the withdraw Activity
let withdrawResult: string;
try {
withdrawResult = await withdraw(details);
} catch (withdrawErr) {
throw new ApplicationFailure(`Withdrawal failed. Error: ${withdrawErr}`);
}
// Execute the deposit Activity
let depositResult: string;
try {
depositResult = await deposit(details);
} catch (depositErr) {
// The deposit failed; try to refund the money.
let refundResult;
try {
refundResult = await refund(details);
} catch (refundErr) {
throw ApplicationFailure.create({
message: `Failed to deposit money into account ${details.targetAccount}. Money could not be returned to ${details.sourceAccount}. Cause: ${refundErr}.`,
});
}
throw ApplicationFailure.create({
message: `Failed to deposit money into account ${details.targetAccount}. Money returned to ${details.sourceAccount}. Cause: ${depositErr}.`,
});
}
return `Transfer complete (transaction IDs: ${withdrawResult}, ${depositResult})`;
}
The PaymentDetails type is defined in shared.ts:
export type PaymentDetails = {
amount: number;
sourceAccount: string;
targetAccount: string;
referenceId: string;
};
Activity Definition
Activities handle the business logic. Each Activity function calls an external banking service:
Workflow Definition
In the Temporal PHP SDK, a Workflow Definition is a class marked with the #[WorkflowInterface] attribute.
This is what the Workflow Definition looks like for the money transfer process:
src/Workflow/MoneyTransfer.php
<?php
declare(strict_types=1);
namespace App\Workflow;
use App\Banking\Banking;
use App\Banking\Exception\InsufficientFunds;
use App\Banking\Exception\InvalidAccount;
use App\Banking\PaymentDetails;
use Temporal\Activity\ActivityOptions;
use Temporal\Common\RetryOptions;
use Temporal\DataConverter\Type;
use Temporal\Exception\Failure\ActivityFailure;
use Temporal\Internal\Workflow\ActivityProxy;
use Temporal\Workflow;
use Temporal\Workflow\ReturnType;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;
#[WorkflowInterface]
final class MoneyTransfer
{
private Banking|ActivityProxy $bankingActivity;
public function __construct()
{
$this->bankingActivity = Workflow::newActivityStub(
Banking::class,
ActivityOptions::new()
->withStartToCloseTimeout('5 seconds')
->withRetryOptions(
RetryOptions::new()
->withMaximumAttempts(3)
->withMaximumInterval('2 seconds')
->withNonRetryableExceptions([InvalidAccount::class, InsufficientFunds::class]),
),
);
}
#[WorkflowMethod('money_transfer')]
#[ReturnType(Type::TYPE_STRING)]
public function handle(PaymentDetails $paymentDetails): \Generator
{
# Withdraw money
$withdrawOutput = yield $this->bankingActivity->withdraw($paymentDetails);
# Deposit money
try {
$depositOutput = yield $this->bankingActivity->deposit($paymentDetails);
return "Transfer complete (transaction IDs: {$withdrawOutput}, {$depositOutput})";
} catch (\Throwable $depositError) {
# Handle deposit error
Workflow::getLogger()->error("Deposit failed: {$depositError->getMessage()}");
# Attempt to refund
try {
$refundOutput = yield $this->bankingActivity->refund($paymentDetails);
Workflow::getLogger()->info('Refund successful. Confirmation ID: ' . $refundOutput);
} catch (ActivityFailure $refundError) {
Workflow::getLogger()->error("Refund failed: {$refundError->getMessage()}");
throw $refundError;
}
# Re-raise deposit error if refund was successful
throw $depositError;
}
}
}
Activity Definition
Activities handle the business logic. Each Activity method calls an external banking service:
src/Banking/Internal/BankingActivity.php
<?php
declare(strict_types=1);
namespace App\Banking\Internal;
use App\Banking\Exception\InvalidAccount;
use App\Banking\PaymentDetails;
use Psr\Log\LoggerInterface;
final class BankingActivity implements Banking
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly Service $bank,
) {}
#[\Override]
public function withdraw(PaymentDetails $data): string
{
$referenceId = $data->referenceId . "-withdrawal";
try {
$confirmation = $this->bank->withdraw(
$data->sourceAccount,
$data->amount,
$referenceId,
);
return $confirmation;
} catch (InvalidAccount $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->error("Withdrawal failed", ['exception' => $e]);
throw $e;
}
}
#[\Override]
public function deposit(PaymentDetails $data): string
{
$referenceId = $data->referenceId . "-deposit";
try {
$confirmation = $this->bank->deposit(
$data->targetAccount,
$data->amount,
$referenceId,
);
// In Part 2, uncomment the block below and comment out the line above
/*
$confirmation = $this->bank->depositThatFails(
$data->targetAccount,
$data->amount,
$referenceId
);
*/
return $confirmation;
} catch (InvalidAccount $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->error("Deposit failed", ['exception' => $e]);
throw $e;
}
}
#[\Override]
public function refund(PaymentDetails $data): string
{
$referenceId = $data->referenceId . "-refund";
try {
$confirmation = $this->bank->deposit(
$data->sourceAccount,
$data->amount,
$referenceId,
);
return $confirmation;
} catch (InvalidAccount $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->error("Refund failed", ['exception' => $e]);
throw $e;
}
}
}
Workflow Definition
In the Temporal .NET SDK, a Workflow Definition is marked by the [Workflow] attribute placed above the class.
This is what the Workflow Definition looks like for this process:
MoneyTransferWorker/Workflow.cs
namespace Temporalio.MoneyTransferProject.MoneyTransferWorker;
using Temporalio.MoneyTransferProject.BankingService.Exceptions;
using Temporalio.Workflows;
using Temporalio.Common;
using Temporalio.Exceptions;
[Workflow]
public class MoneyTransferWorkflow
{
[WorkflowRun]
public async Task<string> RunAsync(PaymentDetails details)
{
// Retry policy
var retryPolicy = new RetryPolicy
{
InitialInterval = TimeSpan.FromSeconds(1),
MaximumInterval = TimeSpan.FromSeconds(100),
BackoffCoefficient = 2,
MaximumAttempts = 3,
NonRetryableErrorTypes = new[] { "InvalidAccountException", "InsufficientFundsException" }
};
string withdrawResult;
try
{
withdrawResult = await Workflow.ExecuteActivityAsync(
() => BankingActivities.WithdrawAsync(details),
new ActivityOptions { StartToCloseTimeout = TimeSpan.FromMinutes(5), RetryPolicy = retryPolicy }
);
}
catch (ApplicationFailureException ex) when (ex.ErrorType == "InsufficientFundsException")
{
throw new ApplicationFailureException("Withdrawal failed due to insufficient funds.", ex);
}
string depositResult;
try
{
depositResult = await Workflow.ExecuteActivityAsync(
() => BankingActivities.DepositAsync(details),
new ActivityOptions { StartToCloseTimeout = TimeSpan.FromMinutes(5), RetryPolicy = retryPolicy }
);
// If everything succeeds, return transfer complete
return $"Transfer complete (transaction IDs: {withdrawResult}, {depositResult})";
}
catch (Exception depositEx)
{
try
{
// if the deposit fails, attempt to refund the withdrawal
string refundResult = await Workflow.ExecuteActivityAsync(
() => BankingActivities.RefundAsync(details),
new ActivityOptions { StartToCloseTimeout = TimeSpan.FromMinutes(5), RetryPolicy = retryPolicy }
);
// If refund is successful, but deposit failed
throw new ApplicationFailureException($"Failed to deposit money into account {details.TargetAccount}. Money returned to {details.SourceAccount}.", depositEx);
}
catch (Exception refundEx)
{
// If both deposit and refund fail
throw new ApplicationFailureException($"Failed to deposit money into account {details.TargetAccount}. Money could not be returned to {details.SourceAccount}. Cause: {refundEx.Message}", refundEx);
}
}
}
}
The PaymentDetails record is defined in PaymentDetails.cs:
MoneyTransferWorker/PaymentDetails.cs
namespace Temporalio.MoneyTransferProject.MoneyTransferWorker;
public record PaymentDetails(
string SourceAccount,
string TargetAccount,
int Amount,
string ReferenceId);
Activity Definition
Activities handle the business logic. Each Activity method calls an external banking service:
Workflow Definition
In the Temporal Ruby SDK, a Workflow Definition is a class that extends Temporalio::Workflow::Definition.
This is what the Workflow Definition looks like for the money transfer process:
require_relative 'activities'
require_relative 'shared'
require 'temporalio/retry_policy'
require 'temporalio/workflow'
module MoneyTransfer
# Temporal Workflow that withdraws the specified amount from the source
# account and deposits it into the target account, refunding the source
# account if the deposit cannot be completed.
class MoneyTransferWorkflow < Temporalio::Workflow::Definition
def execute(details)
retry_policy = Temporalio::RetryPolicy.new(
max_interval: 10,
non_retryable_error_types: [
'InvalidAccountError',
'InsufficientFundsError'
]
)
Temporalio::Workflow.logger.info("Starting workflow (#{details})")
withdraw_result = Temporalio::Workflow.execute_activity(
BankActivities::Withdraw,
details,
start_to_close_timeout: 5,
retry_policy: retry_policy
)
Temporalio::Workflow.logger.info("Withdrawal confirmation: #{withdraw_result}")
begin
deposit_result = Temporalio::Workflow.execute_activity(
BankActivities::Deposit,
details,
start_to_close_timeout: 5,
retry_policy: retry_policy
)
Temporalio::Workflow.logger.info("Deposit confirmation: #{deposit_result}")
"Transfer complete (transaction IDs: #{withdraw_result}, #{deposit_result})"
rescue Temporalio::Error::ActivityError => e
Temporalio::Workflow.logger.error("Deposit failed: #{e}")
# Since the deposit failed, attempt to recover by refunding the withdrawal
begin
refund_result = Temporalio::Workflow.execute_activity(
BankActivities::Refund,
details,
start_to_close_timeout: 5,
retry_policy: retry_policy
)
Temporalio::Workflow.logger.info("Refund confirmation: #{refund_result}")
"Transfer complete (transaction IDs: #{withdraw_result}, #{refund_result})"
rescue Temporalio::Error::ActivityError => refund_error
Temporalio::Workflow.logger.error("Refund failed: #{refund_error}")
end
end
end
end
end
The TransferDetails struct is defined in shared.rb:
Activity Definition
Activities handle the business logic. Each Activity class calls an external banking service:
Step 2: Set the Retry Policy
Temporal makes your software durable and fault tolerant by default. If an Activity fails, Temporal automatically retries it, but you can customize this behavior through a Retry Policy.
Retry Policy Configuration
In the MoneyTransfer Workflow, you'll see a Retry Policy that controls this behavior:
workflow.go
// ...
// RetryPolicy specifies how to automatically handle retries if an Activity fails.
retrypolicy := &temporal.RetryPolicy{
InitialInterval: time.Second, // Start with 1 second wait
BackoffCoefficient: 2.0, // Double the wait each time
MaximumInterval: 100 * time.Second, // Don't wait longer than 100s
MaximumAttempts: 500, // Stop after 500 tries (0 = unlimited)
NonRetryableErrorTypes: []string{"InvalidAccountError", "InsufficientFundsError"}, // Never retry these errors
}
options := workflow.ActivityOptions{
StartToCloseTimeout: time.Minute,
RetryPolicy: retrypolicy,
}
// Apply the options.
src/main/java/moneytransferapp/MoneyTransferWorkflowImpl.java
// ...
private static final String WITHDRAW = "Withdraw";
// RetryOptions specify how to automatically handle retries when Activities fail
private final RetryOptions retryoptions = RetryOptions.newBuilder()
.setInitialInterval(Duration.ofSeconds(1)) // Start with 1 second wait
.setMaximumInterval(Duration.ofSeconds(20)) // Don't wait longer than 20s
.setBackoffCoefficient(2) // Double the wait each time (1s, 2s, 4s, etc)
.setMaximumAttempts(5000) // Stop after 5000 tries
.build();
workflows.py
# ...
retry_policy = RetryPolicy(
maximum_attempts=3, # Stop after 3 tries
maximum_interval=timedelta(seconds=2), # Don't wait longer than 2s
non_retryable_error_types=[ # Never retry these errors
"InvalidAccountError",
"InsufficientFundsError"
],
)
src/workflows.ts
// ...
const { withdraw, deposit, refund } = proxyActivities<typeof activities>({
// RetryPolicy specifies how to automatically handle retries if an Activity fails.
retry: {
initialInterval: '1 second', // Start with 1 second wait
maximumInterval: '1 minute', // Don't wait longer than 1 minute
backoffCoefficient: 2, // Double the wait each time
maximumAttempts: 500, // Stop after 500 tries
nonRetryableErrorTypes: ['InvalidAccountError', 'InsufficientFundsError'], // Never retry these errors
},
startToCloseTimeout: '1 minute', // Activity must complete within 1 minute
});
MoneyTransfer.php
// ...
$this->bankingActivity = Workflow::newActivityStub(
Banking::class,
ActivityOptions::new()
->withStartToCloseTimeout('5 seconds')
->withRetryOptions(
RetryOptions::new()
->withMaximumAttempts(3) // Stop after 3 tries
->withMaximumInterval('2 seconds') // Don't wait longer than 2s
->withNonRetryableExceptions([ // Never retry these errors
InvalidAccount::class,
InsufficientFunds::class
]),
),
);
MoneyTransferWorker/Workflow.cs
// ...
// Retry policy
var retryPolicy = new RetryPolicy
{
InitialInterval = TimeSpan.FromSeconds(1), // Start with 1 second wait
MaximumInterval = TimeSpan.FromSeconds(100), // Don't wait longer than 100s
BackoffCoefficient = 2, // Double the wait each time
MaximumAttempts = 3, // Stop after 3 tries
NonRetryableErrorTypes = new[] { "InvalidAccountException", "InsufficientFundsException" } // Never retry these errors
};
workflow.rb
# Temporal Workflow that withdraws the specified amount from the source
# account and deposits it into the target account, refunding the source
# account if the deposit cannot be completed.
class MoneyTransferWorkflow < Temporalio::Workflow::Definition
def execute(details)
retry_policy = Temporalio::RetryPolicy.new(
max_interval: 10, # Don't wait longer than 10s
non_retryable_error_types: [ # Never retry these errors
'InvalidAccountError',
'InsufficientFundsError'
]
)
What Makes Errors Non-Retryable?
Without retry policies, a temporary network glitch could cause your entire money transfer to fail. With Temporal's intelligent retries, your Workflow becomes resilient to these common infrastructure issues.
Don't Retry
- InvalidAccountError - Wrong account number
- InsufficientFundsError - Not enough money
These are business logic errors that won't be fixed by retrying.
Retry Automatically
- Network timeouts - Temporary connectivity
- Service unavailable - External API down
- Rate limiting - Too many requests
These are temporary issues that often resolve themselves.
This tutorial shows core Temporal features and is not intended for production use.
Step 3: Create a Worker file
A Worker is responsible for executing your Workflow and Activity code. It:
- Can only execute Workflows and Activities registered to it
- Knows which piece of code to execute based on Tasks from the Task Queue
- Only listens to the Task Queue that it's registered to
- Returns execution results back to the Temporal Server
func main() {
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create Temporal client.", err)
}
defer c.Close()
w := worker.New(c, app.MoneyTransferTaskQueueName, worker.Options{})
// This worker hosts both Workflow and Activity functions.
w.RegisterWorkflow(app.MoneyTransfer)
w.RegisterActivity(app.Withdraw)
w.RegisterActivity(app.Deposit)
w.RegisterActivity(app.Refund)
// Start listening to the Task Queue.
err = w.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("unable to start Worker", err)
}
}
src/main/java/moneytransferapp/MoneyTransferWorker.java
package moneytransferapp;
import io.temporal.client.WorkflowClient;
import io.temporal.serviceclient.WorkflowServiceStubs;
import io.temporal.worker.Worker;
import io.temporal.worker.WorkerFactory;
public class MoneyTransferWorker {
public static void main(String[] args) {
// Create a stub that accesses a Temporal Service on the local development machine
WorkflowServiceStubs serviceStub = WorkflowServiceStubs.newLocalServiceStubs();
// The Worker uses the Client to communicate with the Temporal Service
WorkflowClient client = WorkflowClient.newInstance(serviceStub);
// A WorkerFactory creates Workers
WorkerFactory factory = WorkerFactory.newInstance(client);
// A Worker listens to one Task Queue.
// This Worker processes both Workflows and Activities
Worker worker = factory.newWorker(Shared.MONEY_TRANSFER_TASK_QUEUE);
// Register a Workflow implementation with this Worker
// The implementation must be known at runtime to dispatch Workflow tasks
// Workflows are stateful so a type is needed to create instances.
worker.registerWorkflowImplementationTypes(MoneyTransferWorkflowImpl.class);
// Register Activity implementation(s) with this Worker.
// The implementation must be known at runtime to dispatch Activity tasks
// Activities are stateless and thread safe so a shared instance is used.
worker.registerActivitiesImplementations(new AccountActivityImpl());
System.out.println("Worker is running and actively polling the Task Queue.");
System.out.println("To quit, use ^C to interrupt.");
// Start all registered Workers. The Workers will start polling the Task Queue.
factory.start();
}
}
import asyncio
from temporalio.client import Client
from temporalio.worker import Worker
from activities import BankingActivities
from shared import MONEY_TRANSFER_TASK_QUEUE_NAME
from workflows import MoneyTransfer
async def main() -> None:
client: Client = await Client.connect("localhost:7233", namespace="default")
# Run the worker
activities = BankingActivities()
worker: Worker = Worker(
client,
task_queue=MONEY_TRANSFER_TASK_QUEUE_NAME,
workflows=[MoneyTransfer],
activities=[activities.withdraw, activities.deposit, activities.refund],
)
await worker.run()
if __name__ == "__main__":
asyncio.run(main())
import { Worker } from '@temporalio/worker';
import * as activities from './activities';
import { namespace, taskQueueName } from './shared';
async function run() {
// Register Workflows and Activities with the Worker and connect to
// the Temporal server.
const worker = await Worker.create({
workflowsPath: require.resolve('./workflows'),
activities,
namespace,
taskQueue: taskQueueName,
});
// Start accepting tasks from the Task Queue.
await worker.run();
}
run().catch((err) => {
console.error(err);
process.exit(1);
});
src/worker.php
<?php
declare(strict_types=1);
namespace App\Worker;
use App\Banking\Internal\BankingActivity;
use App\Banking\Internal\Service;
use Temporal\Worker\FeatureFlags;
use Temporal\Worker\Logger\StderrLogger;
use Temporal\WorkerFactory;
require_once __DIR__ . '/../vendor/autoload.php';
# Configure Worker behavior
FeatureFlags::$workflowDeferredHandlerStart = true;
# Create a Worker Factory
$logger = new StderrLogger();
$factory = WorkerFactory::create();
$worker = $factory->newWorker('default', logger: $logger);
# Register Workflows
$worker->registerWorkflowTypes(\App\Workflow\MoneyTransfer::class);
# Register Activities
$worker->registerActivity(BankingActivity::class, static fn(): BankingActivity => new BankingActivity(
$logger,
new Service('bank-api.example.com'),
));
$factory->run();
MoneyTransferWorker/Program.cs
// This file is designated to run the worker
using Temporalio.Client;
using Temporalio.Worker;
using Temporalio.MoneyTransferProject.MoneyTransferWorker;
// Create a client to connect to localhost on "default" namespace
var client = await TemporalClient.ConnectAsync(new("localhost:7233"));
// Cancellation token to shutdown worker on ctrl+c
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
tokenSource.Cancel();
eventArgs.Cancel = true;
};
// Create an instance of the activities since we have instance activities.
// If we had all static activities, we could just reference those directly.
var activities = new BankingActivities();
// Create a worker with the activity and workflow registered
using var worker = new TemporalWorker(
client, // client
new TemporalWorkerOptions(taskQueue: "MONEY_TRANSFER_TASK_QUEUE")
.AddAllActivities(activities) // Register activities
.AddWorkflow<MoneyTransferWorkflow>() // Register workflow
);
// Run the worker until it's cancelled
Console.WriteLine("Running worker...");
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Worker cancelled");
}
require_relative 'activities'
require_relative 'shared'
require_relative 'workflow'
require 'logger'
require 'temporalio/client'
require 'temporalio/worker'
# Create a Temporal Client that connects to a local Temporal Service, uses
# a Namespace called 'default', and displays log messages to standard output
client = Temporalio::Client.connect(
'localhost:7233',
'default',
logger: Logger.new($stdout, level: Logger::INFO)
)
# Create a Worker that polls the specified Task Queue and can
# fulfill requests for the specified Workflow and Activities
worker = Temporalio::Worker.new(
client:,
task_queue: MoneyTransfer::TASK_QUEUE_NAME,
workflows: [MoneyTransfer::MoneyTransferWorkflow],
activities: [MoneyTransfer::BankActivities::Withdraw,
MoneyTransfer::BankActivities::Deposit,
MoneyTransfer::BankActivities::Refund]
)
# Start the Worker, which will poll the Task Queue until stopped
puts 'Starting Worker (press Ctrl+C to exit)'
worker.run(shutdown_signals: ['SIGINT'])
Step 4: Define the Task Queue
A Task Queue is where Temporal Workers look for Tasks about Workflows and Activities to execute. Each Task Queue is identified by a name, which you will specify when you configure the Worker and again in the code that starts the Workflow Execution. To ensure that the same name is used in both places, this project follows the recommended practice of specifying the Task Queue name in a constant referenced from both places.
Set Your Task Queue Name
To ensure your Worker and Workflow starter use the same queue, define the Task Queue name as a constant:
const MoneyTransferTaskQueueName = "TRANSFER_MONEY_TASK_QUEUE"
src/main/java/moneytransferapp/Shared.java
package moneytransferapp;
public interface Shared {
static final String MONEY_TRANSFER_TASK_QUEUE = "MONEY_TRANSFER_TASK_QUEUE";
}
# ...
MONEY_TRANSFER_TASK_QUEUE_NAME = "TRANSFER_MONEY_TASK_QUEUE"
Using a shared constant prevents typos that would cause your Worker to listen to a different Task Queue than where your Workflow tasks are being sent. It's a common source of "Why isn't my Workflow running?" issues.
Step 5: Execute the Workflow
Now you'll create a client program that starts a Workflow execution. This code connects to the Temporal Service and submits a Workflow execution request:
func main() {
// Create the client object just once per process
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create Temporal client:", err)
}
defer c.Close()
input := app.PaymentDetails{
SourceAccount: "85-150",
TargetAccount: "43-812",
Amount: 250,
ReferenceID: "12345",
}
options := client.StartWorkflowOptions{
ID: "pay-invoice-701",
TaskQueue: app.MoneyTransferTaskQueueName,
}
log.Printf("Starting transfer from account %s to account %s for %d", input.SourceAccount, input.TargetAccount, input.Amount)
we, err := c.ExecuteWorkflow(context.Background(), options, app.MoneyTransfer, input)
if err != nil {
log.Fatalln("Unable to start the Workflow:", err)
}
log.Printf("WorkflowID: %s RunID: %s\n", we.GetID(), we.GetRunID())
var result string
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable to get Workflow result:", err)
}
log.Println(result)
}
The Java template generates random account numbers and transfer amounts each time you run it. Your output values will differ from the examples shown.
import asyncio
import traceback
from temporalio.client import Client, WorkflowFailureError
from shared import MONEY_TRANSFER_TASK_QUEUE_NAME, PaymentDetails
from workflows import MoneyTransfer
async def main() -> None:
# Create client connected to server at the given address
client: Client = await Client.connect("localhost:7233")
data: PaymentDetails = PaymentDetails(
source_account="85-150",
target_account="43-812",
amount=250,
reference_id="12345",
)
try:
result = await client.execute_workflow(
MoneyTransfer.run,
data,
id="pay-invoice-701",
task_queue=MONEY_TRANSFER_TASK_QUEUE_NAME,
)
print(f"Result: {result}")
except WorkflowFailureError:
print("Got expected exception: ", traceback.format_exc())
if __name__ == "__main__":
asyncio.run(main())
import { Connection, Client } from '@temporalio/client';
import { moneyTransfer } from './workflows';
import type { PaymentDetails } from './shared';
import { namespace, taskQueueName } from './shared';
async function run() {
const connection = await Connection.connect();
const client = new Client({ connection, namespace });
const details: PaymentDetails = {
amount: 400,
sourceAccount: '85-150',
targetAccount: '43-812',
referenceId: '12345',
};
console.log(
`Starting transfer from account ${details.sourceAccount} to account ${details.targetAccount} for $${details.amount}`
);
const handle = await client.workflow.start(moneyTransfer, {
args: [details],
taskQueue: taskQueueName,
workflowId: 'pay-invoice-801',
});
console.log(
`Started Workflow ${handle.workflowId} with RunID ${handle.firstExecutionRunId}`
);
console.log(await handle.result());
connection.close()
}
run().catch((err) => {
console.error(err);
process.exit(1);
});
src/transfer.php
<?php
declare(strict_types=1);
namespace App\Worker;
use App\Banking\PaymentDetails;
use App\Workflow\MoneyTransfer;
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\WorkflowClient;
use Temporal\Client\WorkflowOptions;
use Temporal\Common\IdReusePolicy;
use Temporal\Exception\Client\WorkflowFailedException;
require_once __DIR__ . '/../vendor/autoload.php';
# Create client connected to server at the given address
$client = WorkflowClient::create(
ServiceClient::create('127.0.0.1:7233'),
);
$paymentDetails = new PaymentDetails(
sourceAccount: '85-150',
targetAccount: '43-812',
amount: 250,
referenceId: '12345',
);
$workflow = $client->newWorkflowStub(
MoneyTransfer::class,
WorkflowOptions::new()
->withWorkflowIdReusePolicy(IdReusePolicy::AllowDuplicate)
->withWorkflowRunTimeout(20)
->withWorkflowExecutionTimeout(30),
);
try {
$result = $workflow->handle($paymentDetails);
echo "\e[32mResult: $result\e[0m\n";
} catch (WorkflowFailedException $e) {
echo "\e[31mWorkflow failed: {$e->getMessage()}\e[0m\n";
} catch (\Throwable $e) {
echo "\e[31mError: {$e->getMessage()}\e[0m\n";
}
MoneyTransferClient/Program.cs
// This file is designated to run the workflow
using Temporalio.MoneyTransferProject.MoneyTransferWorker;
using Temporalio.Client;
// Connect to the Temporal server
var client = await TemporalClient.ConnectAsync(new("localhost:7233") { Namespace = "default" });
// Define payment details
var details = new PaymentDetails(
SourceAccount: "85-150",
TargetAccount: "43-812",
Amount: 400,
ReferenceId: "12345"
);
Console.WriteLine($"Starting transfer from account {details.SourceAccount} to account {details.TargetAccount} for ${details.Amount}");
var workflowId = $"pay-invoice-{Guid.NewGuid()}";
try
{
// Start the workflow
var handle = await client.StartWorkflowAsync(
(MoneyTransferWorkflow wf) => wf.RunAsync(details),
new(id: workflowId, taskQueue: "MONEY_TRANSFER_TASK_QUEUE"));
Console.WriteLine($"Started Workflow {workflowId}");
// Await the result of the workflow
var result = await handle.GetResultAsync();
Console.WriteLine($"Workflow result: {result}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Workflow execution failed: {ex.Message}");
}
This code uses a Temporal Client to connect to the Temporal Service, calling its Workflow start method to request execution. This returns a handle, and calling result on that handle will block until execution is complete, at which point it provides the result.
Run Your Money Transfer
Now that your Worker is running and polling for tasks, you can start a Workflow Execution.
In Terminal 3, start the Workflow:
The Workflow starter script starts a Workflow Execution. Each time you run it, the Temporal Server starts a new Workflow Execution.
temporal server start-dev
temporal server start-dev
temporal server start-dev
temporal server start-dev
temporal server start-dev
temporal server start-dev
temporal server start-dev
go run worker/main.go
mvn compile exec:java -Dexec.mainClass="moneytransferapp.MoneyTransferWorker" -Dorg.slf4j.simpleLogger.defaultLogLevel=warn
python run_worker.py
npm run worker
./rr serve
dotnet run --project MoneyTransferWorker/MoneyTransferWorker.csproj
bundle exec ruby worker.rb
go run start/main.go
mvn compile exec:java -Dexec.mainClass="moneytransferapp.TransferApp"
python run_workflow.py
npm run client
php src/transfer.php
dotnet run --project MoneyTransferClient/MoneyTransferClient.csproj
bundle exec ruby starter.rb
Starting transfer from account 85-150 to account 43-812 for 250 2022/11/14 10:52:20 WorkflowID: pay-invoice-701 RunID: 3312715c-9fea-4dc3-8040-cf8f270eb53c Transfer complete (transaction IDs: W1779185060, D1779185060)
Worker is running and actively polling the Task Queue. To quit, use ^C to interrupt. Withdrawing $[AMOUNT] from account [SOURCE_ACCOUNT]. [ReferenceId: [UUID]] Depositing $[AMOUNT] into account [DEST_ACCOUNT]. [ReferenceId: [UUID]] [[UUID]] Transaction succeeded.
Result: Transfer complete (transaction IDs: Withdrew $250 from account 85-150. ReferenceId: 12345, Deposited $250 into account 43-812. ReferenceId: 12345)
Transfer complete (transaction IDs: W3436600150, D9270097234)
Result: Transfer complete (transaction IDs: W12345, D12345)
Workflow result: Transfer complete (transaction IDs: W-caa90e06-3a48-406d-86ff-e3e958a280f8, D-1910468b-5951-4f1d-ab51-75da5bba230b)
Initiated transfer of $100 from A1001 to B2002 Workflow ID: moneytransfer-2926a650-1aaf-49d9-bf87-0e3a09ef7b32 Workflow result: Transfer complete (transaction IDs: OKW-100-A1001, OKD-100-B2002)
Check the Temporal Web UI
The Temporal Web UI lets you see details about the Workflow you just ran. Since you're running Temporal locally with temporal server start-dev, the UI is available at http://localhost:8233.
What you'll see in the UI:
- List of Workflows with their execution status
- Workflow summary with input and result
- History tab showing all events in chronological order
- Query, Signal, and Update capabilities
- Stack Trace tab for debugging
Try This: Click on a Workflow in the list to see all the details of the Workflow Execution.

Ready for Part 2?
Continue to Part 2: Simulate Failures