Send signal to the particular workflow

My workflows start with the WORKWORK_ID_REUSE_POLICY_TERMINATE_IF_RUNNING policy, so new workflows can begin only if no active instances with the same workflow ID exist.

Each workflow sends an event to an external third-party system and then waits for a response. I consume these responses outside of the workflow, and when a response arrives, I need to signal the appropriate workflow based on its ID.

The critical aspect here is ensuring that each response is correlated with the correct workflow execution. Specifically, if several workflow executions have been terminated, their responses should be skipped. However, when I receive a response intended for the currently active workflow execution, I should be able to signal it accordingly.

My initial thought is to use the runId to track and pair requests with responses, but I’ve encountered recommendations against using runId in the business logic due to its possible mutability.

Another idea I’m considering is to generate an ID within the workflow itself and use it to match incoming responses. This way, I will forward each response to the currently active workflow and inside the workflow, skip those that do not match.

I’d appreciate any recommendations on the most appropriate way to handle this within Temporal.

RunID was precisely designed to support your use case. In most cases, there is no need to differentiate among different runs; only WorkflowID is used. But in your case, using runID to ensure that signals are sent to the right workflow invocation is the way to go.

1 Like