Temporal: How Code Resumes After a Worker Dies

Outline

Transcript

0:00 Temporal coordinates multi-step application work so another process can take over after a crash. The problem is that restarting from the beginning can repeat work you've already finished. We'll follow one Python operation through a process crash, see where its old result comes from, then change the code and test whether it can still continue. We're using Temporal's official documentation, its versioned Python implementation and a small local experiment. Links to the primary sources are in the description.

0:27 For example, our operation has three steps: prepare, wait for a message, then finish. The workflow is the code coordinating that whole sequence. And the process running that sequence is our worker. When the workflow needs something done separately, like a service call, it requests an activity. Here, each activity just writes a diagnostic line to a local file and returns its step name plus the process number, so there's no real payment or message being sent. Once prepare finishes and its result is recorded, we kill the worker, taking its Python variables with it.

1:00 What can a fresh process use to recover that result? The Temporal service, the server assigning work and keeping a record of the operation, is still alive, so losing the worker hasn't erased what the service knows. Now connect that surviving record to our workflow's first assignment, which calls prepare as an activity and awaits its result, pausing until the answer becomes available. Which leaves prepared waiting for a value. Once it arrives, the following step waits for a signal, a recorded message sent to this running operation.

1:30 Yes, the function handling that signal sets released to true, letting the code reach the final activity, finish. Until that message arrives, finish has no reason to run. While we're waiting, the service keeps event history, an ordered record of what happened that includes prepare being requested and then completing with its returned data. The worker is doing a different job, executing our code. A task queue is the named place workers ask for assignments, and the replacement keeps asking that same queue for work.

2:00 Take the case of a worker that dies during this wait. The next one has no prepared variable. What, exactly, survives for it to use? The history in the service, including prepare's returned data. I think we should follow that value into the new process, because its variables have to be rebuilt. Replay does that rebuilding by running compatible workflow code against the saved events, so the replacement executes the function again and reaches that first await. Starting at the top sounds dangerously close to doing prepare again.

2:33 The code still asks for the activity. What changes when that activity already has a recorded result? The request can be matched to the old record. A command is the workflow asking Temporal to schedule something, and here the matching completion supplies the answer, letting our assignment finish without repeating prepare. Now follow that recorded answer into the fresh worker, still carrying our first worker's process number. How does that data reach a new Python variable? Through the Python library, the Temporal code our application imports, which keeps a handle, an object representing the activity result we're waiting for.

3:09 That handle belongs to an activity request. In the first excerpt, the number identifies that request, so the lookup finds its waiting result. Yes, and the answer arriving with this work is the saved prepare result. The second excerpt decodes it into a Python value and supplies it to the handle. That fills prepared in the replacement process. For example, the result contains prepare and the earlier worker's number, because it's the value saved when that activity completed. So I can get a value describing a process that's already dead.

3:42 Where does this leave the rest of the function? It reaches the wait for our signal, having run again far enough to reconstruct that state. The separately executed prepare activity hasn't needed a second execution on this path. I think the distinction matters, because the workflow code runs again while a completed activity's answer comes from history. Yeah, and we can check that distinction against the processes and file output, rather than relying on the source diagram alone. First, the left column shows the original worker and its prepare result.

4:13 Once completion is recorded, our test controller, the program managing this experiment, forcibly kills it and starts another worker process. The right column has a different process number but the exact same prepare result, including the old number. A debug flag, a value from the Temporal library showing whether this entry into the function is replaying, now says true. Now we send the signal, which releases the workflow's wait and lets finish run in the replacement worker, returning that new process number.

4:42 And the file has exactly two activity lines: prepare from the old process, finish from the new one. There isn't another prepare line hiding between them. The history shows those activities being scheduled and completed, with the signal between them, while the controller tells us which process died. The death stays outside the history column because there isn't a worker-died event in this record, and the service stayed running throughout. So in that test, the application code was unchanged. What happens if we put changed code into use and it asks for something different at the start? We can test that against the same saved history by inserting a wait before prepare, changing the first command rather than just a comment or variable name.

5:24 A timer is a scheduled wait, and our changed version adds the 1-second sleep before prepare, so its first command now asks for a timer. But the first scheduled operation in this history is the prepare activity, so the highlighted event records an activity being scheduled while the new code is following a timer path. The replay test rejects it because the component tracking this timer's status can't accept that saved activity event. That's what the diagnostic means by timer machine. And the diagnostic says it does not handle this event.

5:55 In the matching code, written in Rust, the accepted events belong to timers, so this activity event reaches the error branch instead. Deterministic here means making compatible workflow decisions for the same recorded history, which doesn't mean every line of source must stay frozen forever. Suppose I change an ordinary calculation without changing the workflow's requests. Is that automatically the same error as inserting this timer? Not automatically. We tested one by counting the characters in the two activity names before running the original body, and that added calculation passes replay without adding a request to schedule work.

6:31 So this history constrains which commands the code can issue. The calculation can change without adding a new request, while a changed command can need versioning, which keeps existing executions on compatible code. That's what this comparison tests: the original code and the added calculation pass, while the added timer fails against the same history. So can I use those replay results to check an update before putting it into the live application, without executing the activities again? Yes, those tests use the saved history, and our two-line file stays unchanged.

7:04 Now take an activity calling an outside service, then dying before reporting success. Would this test prove that outside action couldn't happen twice? No, because we killed our worker after its activity completion was recorded. An outside action without recorded completion is a different case; retrying it can repeat the effect, so the outside operation needs protection against duplicates. Right, so restarting this workflow means reconstructing its state from compatible code and recorded results, then continuing with new work.

7:35 Thanks for listening to Learning Podcasts.