Temporal Explained: Durable Execution for Long-Running Systems
Prank · 2026-06-20 · 8м 6с · 195 просмотров · YouTube ↗
Топики: durable-execution
🎧 Аудио
📝 Summary
model=deepseek-v4-flash · prompt=summary-v7 · 3 387→2 216 tokens · 2026-07-20 15:03:32
🎯 Главная суть
Temporal — открытая платформа для durable execution, которая берёт на себя логику восстановления после сбоев в распределённых системах. Вместо самодельных статусных колонок, очередей и стейт-машин разработчик описывает бизнес-процесс как обычный асинхронный код, а платформа гарантирует его завершение даже при падении узлов или сети. Под капотом — immutable лог событий, детерминированное повторное выполнение workflow и богатый набор примитивов для таймаутов, сигналов, таймеров и вложенных процессов.
Проблема распределённых транзакций и ручного восстановления
Типичная цепочка — списание платежа, выделение ресурса, отправка письма и обновление биллинга. Если третий шаг падает по таймауту, система оказывается в рассогласованном состоянии: деньги списаны, ресурс создан, но пользователь и бухгалтерия не в курсе. Простой повтор всей цепочки грозит двойным списанием. Инженеры вынуждены строить кастомные механизмы: столбцы статусов в БД, опросы (polling), сложные очереди на основе писем и ручные стейт-машины. Temporal заменяет этот ad‑hoc инфраструктурный слой единой платформой.
Архитектура Temporal: workflow, activity, worker и event history
Платформа разделяет логику на три компонента. Workflow — оркестратор, содержащий бизнес-логику и определяющий порядок операций. Activity — единица, выполняющая реальную работу (вызов API, запись в БД). Worker — статически безсостоятельный процесс, вытягивающий задачи из очередей Temporal и выполняющий код. Основа всего — event history: неизменяемый append-only реестр, в который записываются все изменения состояния, запросы на планирование и результаты. Если worker падает в середине выполнения, память теряется. При подхвате задачи новым worker’ом он не восстанавливает снимок памяти, а заново запускает функцию workflow и проигрывает event history с начала. При повторном проигрывании уже завершённые activity не вызываются повторно — Temporal видит, что activity завершена, и подставляет записанный результат из лога. Так workflow после сбоя продолжается ровно с той точки, на которой остановился.
Детерминизм и безопасное версионирование
Чтобы воспроизведение по логу давало тот же результат, код workflow обязан быть детерминированным. Нельзя читать текущее системное время (используйте SDK-функции, возвращающие записанное время), генерировать случайные числа, создавать фоновые потоки или напрямую обращаться к глобальному состоянию и сети — все такие side‑effects должны быть вынесены в activities. Из-за этого меняющийся код workflow может сломать replay и вызвать ошибку недетерминизма. Для безопасного обновления Temporal предоставляет Versioning API: внутри кода прописываются ветвящиеся пути с версионной меткой, которая записывается в event history. Во время replay существующие workflow продолжают следовать своему исходному пути до тех пор, пока все in‑flight экземпляры не завершатся. После этого версионирование удаляется, и новый код становится по умолчанию. Такой подход требует такой же строгой дисциплины управления версиями, как при миграции схемы базы данных.
Таймауты и heartbeat для надёжности активностей
Temporal управляет ненадёжностью внешних систем через четыре типа таймаутов. Schedule‑to‑close timeout — максимальное общее время жизни activity вместе со всеми попытками повторного запуска. Start‑to‑close timeout — ограничение длительности одной попытки. Schedule‑to‑start timeout — максимальное время ожидания задачи в очереди до того, как её подхватит worker. Для долгоиграющих задач (перекодировка видео, миграция данных, обучение модели) используется heartbeat timeout: activity обязана периодически отправлять серверу сигнал, что она ещё работает. Если heartbeat перестают приходить, сервер считает worker упавшим и запускает повторное выполнение activity на другом узле. Эти четыре конфигурации позволяют изолировать сетевые ошибки и не допускать застревания всего workflow из‑за временных сбоев.
Сигналы, таймеры и child workflows для гибкой оркестрации
Signals — механизм асинхронных сообщений от внешних систем к работающему workflow. Используется для встроенных человеческих «гейтов» одобрения: workflow приостанавливается в ожидании внешнего события и не потребляет CPU (может ждать дни и недели). Timers заменяют Thread.sleep() — их состояние сохраняется в БД, поэтому сон переживает перезагрузки worker’a и сервера. Такие durable таймеры позволяют отказаться от централизованных cron‑планировщиков для отложенных задач. Child workflows — способ разбить процесс на более мелкие параллельные выполнения, каждое со своим изолированным event history. Сбой одной child не влияет на родителя и остальные. Назначение стабильного ID дочернему workflow даёт встроенную идемпотентность (executes‑at‑most‑once). Все эти примитивы позволяют реализовывать сложную координацию прямо в коде приложения.
Масштабирование: continue as new и task queues
По мере роста системы event history становится узким местом: повторное проигрывание 50 000 событий или 50 МБ истории вносит заметную задержку. Для процессов, работающих месяцами (например, мониторинг), используют continue as new: текущий workflow завершается, а новый экземпляр с тем же ID запускается с текущим состоянием в качестве входных данных. Практическое правило — сбрасывать историю при достижении ~10 000 событий, чтобы сохранить предсказуемую задержку реплея. Параллельно task queues перенаправляют определённые activity на выделенные пулы worker’ов: тяжёлые вычисления — на GPU-узлы, лёгкие — на обычные. Это упрощает rolling deployments: старые worker’ы дорабатывают свои задачи, а новые начинают подхватывать свежие запросы из очереди.
Примеры использования и ограничения
Temporal эффективен в многошаговых процессах, где важны координация и восстановление. Stripe использует платформу для обеспечения гарантии exactly‑once по финансовым транзакциям. Datadog — для workflow инцидент-менеджмента, которые должны переживать перезагрузки системы в течение нескольких дней. Coinbase мигрировал на Temporal обработку транзакций, ранее требовавшую сложной ручной SAGA‑реализации. Однако платформа — оркестрационный движок, а не высокопроизводительный пайп данных. Она не подходит для стриминга миллионов событий в секунду или замены простых stateless‑запросов. Задержка на round‑trip через task queue делает её неоптимальной для CPU‑интенсивных задач, требующих минимальных накладных расходов на планирование. Там, где главное — корректность и гарантированное завершение, Temporal заменяет ручное конструирование инфраструктуры восстановления формальной моделью durable execution.
📜 Transcript
en · 1 199 слов · 19 сегментов · clean
Показать текст транскрипта
A distributed transaction typically follows a sequence. The system charges a payment, provisions a resource, sends a confirmation email, and updates a billing record. If the third step throws a timeout after the first two succeed, the system enters a fragmented state. The money is gone and the resource exists, but the user and the billing ledger remain unaware. Retrying the entire sequence from the beginning creates the risk of running the first step again, potentially double-charging the customer. To prevent this, engineers often build a custom resumption mechanism to track which specific steps in the chain succeeded. This requires assembling database status columns, custom polling loops, complex dev letter queues, and hand-rolled state machines just to manage the failure model. Temporal is an open-source, durable execution platform designed to replace this ad hoc infrastructure. It allows developers to write standard asynchronous code that the platform guarantees will run to completion, regardless of network partitions or process crashes. This model shifts the responsibility for failure recovery and retry logic from the application code down to the platform layer. The platform architecture relies on separating system logic into three distinct components. The workflow is the orchestrator. It contains the business logic and makes decisions about the order of operations. The activity is the unit that executes work in the outside world, such as calling an API or writing to a database. The worker is a stateless process that pulls Temporal's task queues to execute code. Underpinning these is the event history, an immutable append-only ledger that records every state change, schedule request, and completion. If a worker node crashes mid-execution, the active memory is lost. When a new worker picks up the task, it must reconstruct the workflow state. Rather than loading a memory snapshot, the new worker re-executes the workflow function and replays the event history from the beginning. During this replay, the workflow does not re-invoke completed activities. Instead, Temporal detects that the activity has already finished and injects the recorded result from the ledger. By treating state as a reproducible sequence of past events, the workflow can resume exactly where it left off after a failure. Because the replay engine must retrace its steps exactly, workflow code must be deterministic. This means workflows cannot read the current system time directly, as a replay would result in a different timestamp. Developers must use SDK-specific time functions that return the recorded time. Similarly, workflows cannot generate random numbers or spawn background threads that the platform cannot track. Interactions with global mutable state or direct network queries are also prohibited. These side effects must be isolated within activities. This introduces a deployment risk. Changing the logic of a running workflow will break the replay path, causing a non-determinism error. To update logic safely, the Temporal Versioning API allows you to define branching paths within the code. The version check is recorded in the event history, ensuring that existing workflows continue to follow their original code path during a replay. Once all in-flight executions drain and complete, the versioning logic can be removed, making the new path the default. This requirement means workflow code must be managed with the same versioning discipline as a production database schema. While workflows are deterministic, activities are where the platform handles the unreliability of external systems and side effects. Temporal manages this through timeouts. The schedule to close timeout defines the maximum total lifespan of an activity, including every retry attempt. Within that, the start to close timeout limits how long a single attempt can take before being considered a failure. There is also a schedule to start timeout. which limits how long a task can wait in a queue before a worker picks it up. For long-running tasks, the heartbeat timeout requires the activity to periodically signal the server that it is still making progress. If the server stops receiving these heartbeats, it assumes the worker died and retries the activity on a different node. This pattern is essential for heavy operations, like video encoding, data migrations, or training machine learning models. Configuring these four boundaries allows the system to isolate network failures and prevent transient errors from stalling the entire workflow. Temporal includes native primitives for interacting with active processes. Signals allow external systems to send asynchronous messages to a running workflow. Signals are often used for human-in-the-loop approval gates, where the workflow logic pauses until it receives an external event. While paused, the workflow consumes no CPU resources, allowing it to wait for days or weeks without overhead. Timers provide a replacement for thread sleeps. Because the timer state is persisted in the database, the sleep survives worker or server restarts. These durable timers can eliminate the need for centralized cron job schedulers for delayed tasks. For complex orchestration, child workflows allow a process to be decomposed into smaller, parallel executions. Each child has its own isolated event history. This prevents the failure of a single parallel task from affecting the parent workflow or other children. By assigning a stable ID to a child workflow, you can ensure that it executes at most once, providing a native mechanism for item potency. These primitives allow complex coordination and routing to be handled directly within the application's programming language. As a system grows, the length of the event history becomes a factor. Replaying 50,000 events, or 50 megabytes of history, introduces significant latency. This limit is particularly relevant for perpetual processes, such as monitoring loops that are designed to run for months. To manage this, developers use the continue as new command to reset the event history. This functions like stackless recursion, the current workflow completes, and a fresh instance starts with the same ID, carrying over the current state as input. To maintain predictable replay latency, a common practice is to trigger this reset once the history reaches approximately 10,000 events. Scaling also involves task queues, which route specific activities to designated worker pools. This allows for hardware-specific routing, such as directing compute-heavy activities to workers with GPU resources. It also simplifies rolling deployments, as old workers can drain their tasks while new workers begin picking up fresh requests from the queue. Through the management of event histories and task queues, Temporal allows complex logic to run for long periods while maintaining predictable replay latency. Temporal is effective for multi-step processes where coordination and error recovery are essential. Stripe uses the platform to provide exactly once guarantees for financial transactions. Datadog employs it for incident management workflows that must survive system restarts over several days. Coinbase migrated to Temporal to handle the transaction logic that previously required a complex, manually maintained Sava implementation. However, it is important to recognize where the platform is not the right fit. It is an orchestration engine, not a high-speed data pipe. It is not designed for streaming millions of events per second or for replacing simple, stateless request-response APIs. The round-trip through the task queue adds latency, making it unsuitable for highly optimized CPU-bound tasks that require low scheduling overhead. For systems where correctness and guaranteed completion are the primary requirements, Temporal replaces the manual construction of failure recovery infrastructure with a formal model for durable execution.
⚙️ Pipeline jobs
| Stage | Status | Att. | Updated | Error |
|---|---|---|---|---|
| download | done | 1/3 | 2026-07-20 15:03:02 | |
| transcribe | done | 1/3 | 2026-07-20 15:03:09 | |
| summarize | done | 1/3 | 2026-07-20 15:03:32 | |
| embed | done | 1/3 | 2026-07-20 15:03:33 |
📄 Описание YouTube
Показать
In this video, we break down Temporal from first principles and explain why it has become a popular platform for building reliable long running workflows in distributed systems. You'll learn what durable execution means, how Temporal persists workflow state, and why it helps eliminate common failure scenarios caused by crashes, retries, timeouts, network failures, and service restarts. We cover workflows, activities, event history, deterministic execution, workflow replay, state recovery, and fault tolerance. The video also explores how Temporal differs from traditional cron jobs, message queues, orchestrators, and custom workflow engines. If you are a backend engineer, software architect, distributed systems engineer, platform engineer, or microservices developer, this primer will help you understand the core architecture, trade offs, and operational benefits of Temporal. We also discuss practical use cases such as payment processing, order management, data pipelines, user onboarding, and business process orchestration. By the end, you will understand how Temporal enables reliable workflow orchestration at scale and why many engineering teams use it to simplify complex distributed applications. Temporal is an open-source durable execution platform that keeps your code running through crashes, network failures, and deployments — resuming exactly where it left off. In this video, I break down how Temporal actually works: Workflows, Activities, and Workers, how event history and replay enable durability, the determinism constraint that trips up most new users, retries and heartbeating, Signals for human-in-the-loop processes, Timers for durable sleep, Child Workflows for fan-out, and Continue-As-New for long-running processes. If you've ever duct-taped together a cron job, message queue, and retry loop only to watch it break at 2am, this is the workflow engine that replaces all of it. Companies like Stripe, Netflix, Coinbase, and Datadog run Temporal in production for payments, deployments, and incident management. Topics covered: Temporal workflow engine, durable execution, distributed systems, workflow orchestration, event history, replay, determinism, activities and retries, signals, timers, child workflows, continue-as-new, microservices reliability. #Temporal #DistributedSystems #SystemDesign #BackendEngineering #Microservices #SoftwareArchitecture #WorkflowEngine #DurableExecution #CloudEngineering #ScalableSystems #FaultTolerance #PlatformEngineering #EventDrivenArchitecture #BackendDeveloper #TechArchitecture