Signals, Queries, Updates: Talking to a Running Workflow

A workflow can live for a long time, so you need a way to reach it from outside. For example: approve an expense, cancel an order, add an item to a cart, or ask how far along it is. Temporal gives you three ways to do this. The only real difference between them is what gets saved in the history.

SignalQueryUpdate
What it doesSends data in, without waiting for a replyReads data outSends data in and gets a reply
Does the caller wait?No. It returns as soon as the signal is saved.YesYes
Saved in history?Yes, as WorkflowExecutionSignaledNo. It leaves no trace.Yes, as "accepted" and "completed" events
Can it change state?YesNo. It must have no side effects and must not send any commands.Yes
Works after the workflow ends?No. It is dropped without any error.Yes, as long as the history is keptNo
Use it for"Payment received", "user approved", "cancel""What's the current status?" for a UI"Add this item and tell me the new total"

The query rule makes sense once you remember the Replay part. To answer a query, a worker replays the workflow and then runs your query handler on the rebuilt in-memory state. Nothing from that replay is saved.

So if your handler changed a variable or started an activity, the change would only exist in that one worker's memory, and would then disappear. Worse, it could produce commands that don't match the history, which causes a non-determinism error. That's why the SDK doesn't allow it.

Anything that needs to last must be an event. If something doesn't write to the history, it can't change state. That one rule covers signals, queries, updates, replay, and determinism.

Signal-With-Start

Here's a very common need: "Send this event to user X's workflow. If that workflow isn't running yet, start it first."

If you do this in two steps (check if it's running, then start it), two callers can both decide to start it, and you get duplicate workflows. SignalWithStartWorkflow does it as one step on the server: start the workflow if it isn't running, then send the signal, exactly once. Use it for any long-running workflow that belongs to one thing, like a user's session, a shopping cart, or a device.

  • Guarantees: Once the server accepts a signal for a running workflow, the signal is saved and delivered in history order. A query sees a complete, fully replayed view of the workflow's state.
  • Does not guarantee: That a signal sent to a finishing or finished workflow is delivered. It is dropped. Also, a query shows the state as of the last completed workflow task. It won't include another client's signal that is still being processed.