Temporal Workflow message passing - Signals, Queries, & Updates
Workflows can be thought of as stateful web services that can receive messages. The Workflow can have powerful message handlers akin to endpoints that can react to the incoming messages in combination with the current state of the Workflow. Temporal supports three types of messages: Signals, Queries, and Updates. To define these, consider these messages from the perspective of the client making the request:
- Queries are read requests. They can read the current state of the Workflow but cannot block in doing so.
- Signals are asynchronous write requests. They cause changes in the running Workflow, but you cannot await any response or error.
- Updates are synchronous, tracked write requests. The sender of the Update can wait for a response on completion or an error on failure.
How to choose between Signals, Updates, and Queries as a Workflow author?
This section will help you write Workflows that receive messages.
For write requests
Unlike Signals, Updates must be synchronous. That is, they must wait for the Worker running the Workflow to acknowledge the request.
Use Signals instead of Updates when:
- The Workflow's clients want to quickly move on after sending an asynchronous message.
- The clients care more about the latency of sending the message than they do about the latency of the end-to-end operation.
- The clients don't want to rely on the Worker being available.
- Clients need to Signal-With-Start atomically (since there is currently no Update-with-Start operation.)
Use Updates instead of Signals when:
- The Workflow's clients want to track the completion of the message.
- The clients need a result or an exception from your message without having to query subsequently.
- You’d like to “validate” the Update before accepting it into the Workflow and its history.
- The clients want a low-latency end-to-end operation and are willing to wait for it to finish or be validated.
For read requests
You normally want to do a Query, because:
- Queries are efficient–they never add entries to the Workflow Event History, whereas an Update would (if accepted).
- Queries can operate on completed Workflows.
However, because Queries cannot block, sometimes Updates are best. When your goal is to do a read once the Workflow achieves a certain desired state, you have two options:
- You could poll periodically with Queries until the Workflow is ready.
- You could write your read operation as an Update, which will give you better efficiency and latency, though it will write an entry to the Workflow Event History.
For read/write requests
Use an Update for synchronous read/write requests. If your request must be asynchronous, consider sending a Signal followed by polling with a Query.