Temporal lets you write complex, long-running workflows as simple code. Automatic retries, timeouts, and state persistence — your workflow survives crashes, deploys, and network failures.
Core Concept
// workflow.ts — runs reliably even if the server crashes mid-execution
import { proxyActivities, sleep } from '@temporalio/workflow';
const { sendEmail, chargePayment, shipOrder } = proxyActivities({
startToCloseTimeout: '30s',
retry: { maximumAttempts: 3 }
});
export async function orderWorkflow(order: Order): Promise<string> {
// Step 1: Charge payment (auto-retries on failure)
const paymentId = await chargePayment(order);
// Step 2: Wait for warehouse confirmation (can sleep for days)
await sleep('2 hours');
// Step 3: Ship order
const trackingId = await shipOrder(order, paymentId);
// Step 4: Send confirmation email
await sendEmail(order.email, `Shipped! Tracking: ${trackingId}`);
return trackingId;
}
Activities (Side Effects)
// activities.ts — these are the actual implementations
export async function chargePayment(order: Order): Promise<string> {
const result = await stripe.charges.create({
amount: order.total,
currency: 'usd',
source: order.paymentToken
});
return result.id;
}
export async function shipOrder(order: Order, paymentId: string): Promise<string> {
const shipment = await warehouse.createShipment(order.items);
return shipment.trackingId;
}
Start Workflows
import { Client } from '@temporalio/client';
const client = new Client();
// Start a workflow
const handle = await client.workflow.start(orderWorkflow, {
taskQueue: 'orders',
workflowId: `order-${orderId}`,
args: [order]
});
// Query status
const result = await handle.result();
// Signal a running workflow
await handle.signal(cancelOrder);
Why This Matters
- Crash-proof: Workflows resume exactly where they left off
- Long-running: Sleep for minutes, hours, or days
- Automatic retries: Built-in retry policies with backoff
- Visibility: Full execution history and debugging
Need custom workflow automation or distributed systems? I build developer tools and data pipelines. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.
Top comments (0)