Skip to content
OPQAI.
Sourced intermediate / 💻 Coding Free tools

Ensure Exactly-Once Workflow Execution with Temporal

Job to be done: Ensure durable and exactly-once execution of workflows with side effects despite process crashes

🇳🇬 Ways to use this in Nigeria

Ideas to get you started, adapt to your situation.

  • Student

    Automate grading of Python assignment submissions by running tests exactly once, even if the grading server crashes.

  • 9-5 employee

    Ensure critical financial report generation runs exactly once, preventing duplicate entries if the system restarts.

  • Entrepreneur

    Reliably process customer orders via API, ensuring each payment is captured only once despite server interruptions.

What you’ll get

You will learn how to set up a system that ensures your automated tasks, called workflows, run exactly once, even if the computer running them crashes. This is crucial for tasks that have real-world effects, like sending an email or processing a payment, preventing them from happening multiple times by mistake. This approach uses Temporal, a tool designed for reliable workflow execution.

Tools you need

  • Temporal CLI (free): A command-line tool to run the Temporal server locally on your computer for testing.
  • Python SDK (free): A software development kit that lets you write your workflows using the Python programming language.

Steps

This workflow involves setting up Temporal locally and writing a simple Python script to demonstrate durable execution. The exact code for the workflow and the driver script is provided by the original author, and you would need to adapt it to your specific needs.

  1. Set up Temporal locally: Follow the official Temporal documentation to install the Temporal CLI and start the development server. This typically involves downloading the CLI and running a command like temporal server start-dev. You should see output indicating the Temporal server is running.

  2. Write your workflow code: Create a Python file (e.g., workflow.py) that defines your workflow. This workflow should include activities that represent your side effects. The example uses a simple step activity that writes to a file and then sleeps.

    import asyncio
    import os
    from temporalio import activity, workflow
    
    SIDEFX = "side_effects.txt"
    STEP_SECONDS = 3
    
    @activity.defn
    async def step(order: str, n: int) -> str:
        with open(SIDEFX, "a") as f:
            f.write(f"... step={n} attempt={activity.info().attempt} pid={os.getpid()}\n")
        await asyncio.sleep(STEP_SECONDS)
        return f"step {n} done"
    
    @workflow.defn
    class MyWorkflow:
        @workflow.run
        async def run(self) -> str:
            results = []
            for i in range(1, 11):
                results.append(await workflow.execute_activity(
                    step,
                    args=("main", i),
                    start_to_close_timeout=timedelta(seconds=10),
                ))
            return "\n".join(results)

    You should have a Python file containing the workflow and activity definitions.

  3. Write a driver script: Create another Python file (e.g., driver.py) that starts your workflow and simulates failures. This script will connect to your local Temporal server, start the workflow, and then repeatedly kill the worker process to test durability. The original author’s script includes logic to kill the worker and restart it.

    import asyncio
    import os
    import signal
    from datetime import timedelta
    from temporalio.client import Client
    from temporalio.worker import Worker
    
    from workflow import MyWorkflow, step # Assuming workflow and step are in workflow.py
    
    async def main():
        client = await Client.connect("localhost:7233")
    
        async def run_workflow_and_kill():
            print("Starting workflow...")
            handle = await client.start_workflow(
                MyWorkflow().run,
                id="my-durability-workflow",
                task_queue="my-task-queue",
            )
    
            worker_process = None
            try:
                # Start a worker
                worker = Worker(
                    client=client,
                    task_queue="my-task-queue",
                    workflows=[MyWorkflow],
                    activities=[step],
                )
                worker_process = await asyncio.create_subprocess_exec(
                    "python", "-m", "temporalio.worker",
                    "--host", "localhost:7233",
                    "--task-queue", "my-task-queue",
                    "--activities", "workflow.step",
                    "--workflows", "workflow.MyWorkflow",
                )
                print(f"Worker started with PID: {worker_process.pid}")
    
                # Wait a bit, then kill
                await asyncio.sleep(3)
                print(f"Killing worker {worker_process.pid}...")
                os.kill(worker_process.pid, signal.SIGKILL)
                await asyncio.sleep(2)
    
                # Restart worker and let it continue
                worker_process = await asyncio.create_subprocess_exec(
                    "python", "-m", "temporalio.worker",
                    "--host", "localhost:7233",
                    "--task-queue", "my-task-queue",
                    "--activities", "workflow.step",
                    "--workflows", "workflow.MyWorkflow",
                )
                print(f"Worker restarted with PID: {worker_process.pid}")
    
                # Wait for workflow completion
                result = await handle.result()
                print("Workflow completed:", result)
    
            except Exception as e:
                print(f"An error occurred: {e}")
            finally:
                if worker_process and worker_process.returncode is None:
                    print(f"Terminating worker {worker_process.pid}...")
                    worker_process.terminate()
    
        # The original author repeated this kill cycle multiple times. 
        # For a simple test, we'll run it once.
        await run_workflow_and_kill()
    
    if __name__ == "__main__":
        asyncio.run(main())

    You should have a Python file that can start a workflow and simulate crashes.

  4. Run the driver script: Execute the driver script from your terminal. The script will start the Temporal server (if not already running), start your workflow, run a worker, kill it, restart it, and then wait for the workflow to complete.

    python driver.py

    You should see output indicating the workflow starting, the worker being killed and restarted, and finally the workflow completing. Check the side_effects.txt file to see how many times each step actually ran.

Original source

The original post by remdore on DEV Community explores how Temporal handles workflow execution under pressure. It details an experiment where a workflow was repeatedly interrupted to test its durability and exactly-once execution promise, revealing nuances about activity retries and timeouts.

Notes & variations

  • Free-tier alternatives: Temporal is free and open-source for local development. For production, you would use Temporal Cloud or self-host.
  • Common mistake: If activities run multiple times, it might be due to not properly handling idempotency or not configuring start_to_close_timeout correctly. The original author found that heartbeating could sometimes make duplicates worse if not managed carefully.
  • Tip for better results: Implement idempotency keys for your activities. This means designing your activities so that running them multiple times with the same input has the same effect as running them once. This is the most robust way to ensure side effects happen exactly once.

Keep going

More Coding workflows