Prevent Concurrent Workflow Runs Using Redis

This workflow effectively prevents concurrent execution of the same workflow by utilizing Redis for state management, avoiding resource conflicts and data inconsistencies. It supports setting, getting, and deleting state markers, ensuring that only one instance is running at any given time, and provides a timeout mechanism to automatically release the locked state, thereby enhancing the system's stability and reliability. It is suitable for scenarios that require strict control over workflow execution, such as automated task management and status tracking of complex processes.

Workflow Diagram
Prevent Concurrent Workflow Runs Using Redis Workflow diagram

Workflow Name

Prevent Concurrent Workflow Runs Using Redis

Key Features and Highlights

This workflow leverages Redis as a state store to intelligently manage and prevent concurrent executions of the same workflow, thereby avoiding resource conflicts and data inconsistencies. By setting, retrieving, and deleting Redis key-values to mark workflow states, it achieves precise control over workflow execution. It supports a timeout mechanism that automatically releases locked states, enhancing system stability and reliability.

Core Problem Addressed

In automated processes, concurrent executions of identical workflows can lead to resource contention, data conflicts, or logical errors. This workflow uses Redis for state management to ensure that only one instance of the workflow runs at any given time, preventing duplicate and conflicting operations, and improving the safety and accuracy of workflow execution.

Use Cases

  • Business scenarios requiring strict single-instance workflow execution control
  • Automated processes that avoid duplicate handling of the same data during parallel tasks
  • State tracking and execution lock management for complex tasks
  • Process management requiring automatic timeout-based lock release

Main Workflow Steps

  1. Trigger Execution: The workflow can be triggered manually or invoked by other workflows, with input parameters including action (get/set/unset), key, value, and timeout.
  2. State Check: Retrieve the current execution state from Redis to determine if an instance is already running.
  3. State Transition: Based on the action parameter, set the workflow state to “started,” “working,” “loading,” “finishing,” or “finished,” or remove the state marker.
  4. Concurrency Control: If an existing execution instance is detected, immediately halt the current execution and raise an “Already Executing” error to prevent concurrency conflicts.
  5. Timeout Handling: When setting the state, apply a TTL (time-to-live) to prevent stale locks caused by unexpected failures.
  6. Task Execution: After confirming no concurrency conflict, execute the core business logic and subsequent wait steps, then clear the state upon completion.

Involved Systems or Services

  • Redis: High-performance key-value store used for state locking and execution marking.
  • n8n Core Nodes: Including manual trigger, conditional (If), Switch branches, execute workflow nodes, wait nodes, error stop nodes, etc.

Target Users and Value

  • Automation Engineers and Developers: Need to guarantee single-instance workflow execution and avoid risks from duplicate runs.
  • Enterprise IT Operations: Manage complex automated tasks to ensure system stability and sequential task execution.
  • Product Managers and Project Leads: Monitor process states to improve automation reliability and user experience.
  • Any n8n Users Facing Concurrency Challenges: Those automating workflows with n8n who require effective concurrency control.

This workflow features a clear and extensible structure, allowing flexible integration into various automation scenarios. It helps users effectively manage workflow execution states, prevent concurrency issues, and enhance the robustness and reliability of the overall automation system.