One of the topics presented at the Golang meetup *instinctools was the practice of using the popular open source microservices orchestration platform Temporal. Maksadbek Akhmedov, *instinctools Go-developer, told what tasks the platform effectively solves, and also showed how you can set up workflows in Temporal on your own.
What is Temporal?
In other words, Workflow is a code that coordinates work tasks, decides what will be implemented and in what sequence. At the same time, Temporal as a system takes responsibility for reliably executing this code, and also guarantees stability, scalability and durability.
If, for example, your data center fails, then you will have the opportunity to restore the database, immediately after that Temporal will continue to execute the code exactly from the point where the process stopped.
An example is a taxi aggregator company. On each trip, such a company must perform two operations:
- withdraw money from the customer’s account
- send this money to the driver’s account
Two operations sounds like a simple enough task, but if you have millions or even billions of users, it can be a little tricky to implement.
Next, let’s look at an example of how these tasks can be described in Go.
Let’s look at the examples
In example 1, let’s consider sending an email to notify the user once a day, once a week, and once a month.
To do this, let’s create a task with the required time intervals and execution sequence in a For-loop.
First of all, we set the Sleep parameters: day, week and month. In doing so, we use Sleep from the Temporal package. Then we activate the code that should send the letter. In this case, the code is a function that can be conventionally called an Activity. In the figure above, the Activity for sending an email is called “SendMail”.
Various failures can occur at this stage. For example, the service that sends the email is unavailable. Then the operation must be repeated until it is completed successfully.
Temporal takes care of this operation, i.e. if the function returns any error, then the platform will try to repeat the operation after a while. For example, in a minute, a day, or a month, depending on the specified configuration.
Workflow и Activity
The activities that we considered in example 1 are a specific action (short or long), for example, activating another service, transcoding a media file, or sending an email.
Workflow is a code that consists of several activities. The Workflow contains a call to various Activities and ensures that after the completion of this Workflow, all the Activities that should be performed in it will be completed successfully.
Now let’s write “Hello world” like all developers do when faced with new technologies.
Let’s take the Workflow function as an example and call it GreetingWorkflow. In this example, Workflow takes a Name parameter, calls the ComeposeGreeting Activity, and passes the Name parameter data there.
The ComeposeGreeting Activity creates the welcome text, adds a name, and displays the welcome text (example 3).
Another important Temporal concept is Workers. Within the framework of the Workers platform, the given code is implemented by a command from the Temporal server. There may be a lot of them. Workers can be different, some can only perform Workflow, others – only Activities, others – all together. Data transfer between workers takes place over the network, in the code it looks very abstract and simple, as if we were passing a variable. In fact, the default encoding is by json protocol.
Let’s write our own Worker. To do this, we need to create a task sequence, as we did in the GreetingWorkfow and ComposeGreeting examples. First, register the Workflow and Activity, then add the Temporal SDK (Example 3).
In our example, the Worker executes both the Workflow and the Activity. Once activated, it connects to the Temporal server, waits for a command, and executes it. You can use different methods to start Workflow. The Temporal utility is just one of these methods.
Temporal Areas of Use
Temporal is actively used by large IT companies. For example, Snapchat uses Temporal to create reports for advertisers. Netflix choses Temporal to implement the SAGA pattern in Spinnaker for CI/CD. As part of the project, the company launches more than 500,000 Workflows per day. Coinbase uses Temporal to make transactions. For example, during transactions from one wallet to another, as well as when implementing a number of SAGA patterns (uploading bank servers to FTP, subscription and billing). Stripe uses Temporal for tasks related to data processing during transactions. DataDog uses Temporal to achieve database reliability. If there are any failures in the company’s databases, the system executes RunBook.
Temporal in *instinctools projects
Periodical execution of a given code (Distributed Cron) is one of the most common tasks that occurs in almost every *instinctools project. For example, we create a code for calculating bonuses for users. In this case, the number of users can be any. Such a Workflow contains three main Activities. The first gets users out of the database, the second determines which of them to accrue the bonus, the last adds the bonus to the user’s account. There can be a lot of this kind of Workflow. Some work once per hour, others may work once a month/year.
Distributed Cron can be implemented with other utilities, such as Linux Cron, but when the number of such tasks grows, we tend to use Temporal.
Orchestration of microservices. In the example with the taxi aggregator company that we analyzed earlier, several typical tasks are sequentially implemented after each trip. You must have seen this popular pattern (SAGA pattern) before. There are several processes running in it, and, in case of possible failures, a certain number of tasks need to be performed to roll back to the previous operations performed. This can be debiting money from the client’s account and sending it to the driver’s account, creating reports, entering data into the operating system, and other tasks. We often use Tempora to implement the SAGA pattern in various projects.
Another common task is results polling. In case when you provide access to a particular service on a subscription basis, you need to monitor and periodically remind users of the need to renew access. These tasks can be implemented using Workflow, which will execute an Activity after a specified period of time to check for a subscription or notify users.
Uploading files to third-party storage is another popular task we use Temporal for. If your project uses users files or other services, from time to time you need to upload them to a third-party storage, be it Amazon S3 or an ftp server. To upload files, a Workflow is usually created that uploads the desired file, and after completing the necessary operations, deletes it from temporary storage.
CI/CD team infrastructure use. Our infrastructure team often uses Temporal for CI/CD tasks such as database migrations.
Related job openings
The company specializes in specific tech markets, such as the Internet of Things (IoT), domain security, videoconferencing, and energy management.