Temporal Workflow Orchestration
Introduction to Temporal
What is Temporal?
Building applications that run reliably across multiple computers is hard. Servers crash, networks fail, and databases become unavailable. Keeping track of a process through all these potential failures can quickly become a complex mess of retry logic, state management, and error handling code.
Temporal is a system that handles this complexity for you. It's a workflow orchestrator, which is a fancy way of saying it's like a hyper-reliable project manager for your code. It ensures that your multi-step processes, or workflows, complete correctly, no matter what failures happen along the way.
Temporal is a cloud-native workflow engine that helps developers create and execute complex, long-running and fault-tolerant applications.
Instead of you writing code to handle retries or to save the state of a process in a database, Temporal takes care of it. This allows you to write your business logic as if failures don't exist, making your code simpler, cleaner, and much easier to maintain.
Workflows and Activities
Temporal organizes your code into two fundamental building blocks: Workflows and Activities.
Workflows define the sequence and logic of your business process. A workflow is a function that orchestrates the execution of various tasks. The defining feature of a workflow is its durability. It maintains its state and continues executing over long periods, from seconds to years, resilient to any infrastructure failure.
Activities are the individual units of work that a workflow coordinates. An activity is typically a simple function that performs a single, well-defined action, like calling an external API, processing a video, or charging a credit card. This is where the actual work happens. If an activity fails, Temporal can automatically retry it according to your configured policies.
Think of an e-commerce order process. The entire process, from placing the order to shipping it, is the Workflow. Sending the confirmation email, charging the payment, and updating the inventory are all individual Activities. The Workflow calls these Activities in the correct order, and Temporal ensures the whole process completes, even if the payment service is temporarily down.
Durable Execution
The magic behind Temporal's reliability is a concept called durable execution. It means that the state of your workflow is automatically preserved and can survive any process or server failure.
How does this work? The Temporal server doesn't execute your workflow code directly. Instead, it manages a task queue and a detailed event history for each workflow execution. When your workflow code needs to do something, like start an activity or wait for a timer, it sends a command to the Temporal server. The server records this command in the event history and schedules the corresponding task.
The Temporal server persists task execution history by recording task inputs and results, enabling workflows to be reliable and resilient against failures in any component of the Temporal ecosystem.
If the machine running your workflow code crashes, Temporal can seamlessly resume the workflow on another machine. It does this by replaying the event history to bring the workflow back to its exact previous state. This replay mechanism ensures that your workflow's logic continues from where it left off, without you having to write any recovery code.
System Architecture
The Temporal system has two main parts: the Temporal Service (also called the Temporal Cluster) and the Workers that you run.
Temporal Service: This is the heart of the system. It's a scalable, multi-tenant service that maintains the state of all workflow executions, manages task queues, and keeps the event history. You can run this service yourself (self-hosted) or use Temporal's managed cloud offering.
Workers: A Worker is a process that you develop and run in your own infrastructure. Workers host the implementation of your Workflow and Activity code. They poll the Temporal Service for tasks, execute them, and report the results back.
The Temporal Service orchestrates, but your Workers do the actual work.
This separation is powerful. The Temporal Service provides the durability and reliability, while your Workers provide the business logic. You can scale your workers independently based on the load of your application, and you have full control over their environment and dependencies.
What is the primary problem Temporal is designed to solve?
In the Temporal architecture, where does your specific business logic, such as calling an external API or processing a file, actually run?
With this foundation, you can start to see how Temporal simplifies the development of complex, reliable applications.