Community Threads: Is it possible to write a single Workflow with different languages?

In Community Threads, we dig into some of the most frequent and insightful questions that appear in our Temporal Community.

The question

Today’s Thread focuses on a question asked by a Temporal user who wants to know:

Is it possible to write a single Workflow in multiple languages?

comminity threads-image

The answer

While a single Workflow Definition should be consistent in one language, that Workflow can include Activities written in other languages. For each Activity written in separate languages, you will need a separate Worker and different task queue names for the Activities in each language.

Tip: Even when using the same function in different programming languages, use a distinct name for each task queue so that the Worker can identify the appropriate Activity to run. If you use the same name, then the Worker Entity will be unable to identify which queue to execute. Be sure to define a separate Worker initialization for each programming language you intend to use in your Workflow.

If you want to have the Workflow continue the task execution in another language in the case of failure, then you would use the same Workflow (including the type and method name) to complete the Workflow in the secondary language. Use the data types for your programming language of choice when writing the Workflow or Activity code. The Temporal SDK automatically converts the language types for interoperability with other SDKs. For example, a bool value returned from an Activity written in Python will appear as a boolean value in the Java Workflow that calls the Activity in Python.

The context

You can use any of the available Temporal SDKs as you write an Activity Task. Activity names in the Task Queue should be unique. When dealing with multiple languages, supply unique task names so the Worker Entity (the individual Worker within a Worker Process that listens to a specific Task Queue) can identify which Activities to select from the queue.

Tip: We recommend appending the language to the Activity name so you can keep track of the language-specific Activities.

To combine two different actions written in different languages for your Workflow, each of these actions will require a distinct Activity Type that will be called during the Workflow Execution by the language-specific Worker Entity.

Each task in a Workflow should be well-defined to accomplish a single action. That enables the Worker to perform the operation efficiently as specified by the Activity Definition. When the Temporal Worker finds the specified name in the Task Queue, it will begin the task execution until either the operation is completed or an error is returned. When you define Activities in different languages (assuming that each task has a distinct name for the queue), then each Worker can identify and run those operations concurrently. The Activities will be executed according to your timeout and retry policies.

The Temporal Polyglot and Temporal Pendulum applications illustrate how to use multiple languages with Temporal. The Polyglot app demonstrates Temporal’s orchestration capabilities between applications in different languages, whereas the Pendulum app calls Workflows written in different programming languages through visual representations in a graphical interface.

The Pendulum game is written primarily in Java that calls activities written in PHP, Go, and Node (and their corresponding Workers) to populate the user interface and interactive elements. The file uses the WorkflowUtils class to specify the ID and task queue name for the Workflow in each programming language. As a result, the application, written primarily in Java, can call Workflows in each language to move the position of the pendulum. pendulumnew-image

Learn the basics with Temporal 101

Still not sure how Workflows and Activities interact? Temporal 101 introduces these concepts alongside the Temporal SDKs, so you can learn how to configure and environment and manage your application lifecycle with Temporal’s execution model and event history features.

If you’ve already completed Temporal 101 and you’re ready to delve deeper, try Temporal 102.

Join the Temporal user community in Slack

If you found this thread helpful or if you have your own questions about using Temporal, join us in Slack to connect with other Temporal users and team members.