[PR #24068] [Chore/Refactor] Queue-based Graph Engine Architecture #30489

Closed
opened 2026-02-21 20:47:35 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/24068

State: closed
Merged: No


Closes #24067

Fixes

Can fix almost every common parallel error.

Fixes #18233
Fixes #22340
Fixes #22569
Fixes #22120
Fixes #22096
Fixes #22989
Fixes #25379

Summary

This PR implements a comprehensive refactoring of the workflow graph engine, transforming it from a monolithic architecture to a queue-based, worker pool architecture. The changes improve modularity, scalability, and maintainability while maintaining backward compatibility.

Key Changes

Architecture Improvements

  • Queue-based orchestration: Replaced direct execution with command-based orchestration through channels
  • Worker pool with dynamic scaling: Introduced configurable worker pools that scale based on workload
  • Domain-driven design: Separated domain logic from infrastructure with clear boundaries
  • Event-driven communication: Decoupled components communicate through well-defined events

New Components

  • Command System: Commands flow through channels (Redis or in-memory) for better control
  • Worker Management: Dynamic scaling with activity tracking and configurable thresholds
  • Response Coordinator: Centralized handling of streaming responses and output management
  • State Management: Dedicated managers for node states, edge states, and execution tracking
  • Error Handling: Multiple recovery strategies (retry, default value, fail branch, abort)
  • Layer System: Extensible processing layers (e.g., debug logging layer)

Configuration

Configuration is now managed through environment variables via dify_config:

  • GRAPH_ENGINE_MIN_WORKERS: Minimum workers (default: 1)
  • GRAPH_ENGINE_MAX_WORKERS: Maximum workers (default: 10)
  • GRAPH_ENGINE_SCALE_UP_THRESHOLD: Queue size for scaling up (default: 3)
  • GRAPH_ENGINE_SCALE_DOWN_IDLE_TIME: Idle time before scaling down (default: 5.0)

Test Coverage

The refactoring includes comprehensive test coverage:

  • Unit tests for all new components
  • Integration tests for the complete workflow execution
  • Test fixtures for various workflow scenarios
  • Mock system for testing complex workflows without external dependencies

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/24068 **State:** closed **Merged:** No --- Closes #24067 ## Fixes Can fix almost every common parallel error. Fixes #18233 Fixes #22340 Fixes #22569 Fixes #22120 Fixes #22096 Fixes #22989 Fixes #25379 ## Summary This PR implements a comprehensive refactoring of the workflow graph engine, transforming it from a monolithic architecture to a queue-based, worker pool architecture. The changes improve modularity, scalability, and maintainability while maintaining backward compatibility. ## Key Changes ### Architecture Improvements - **Queue-based orchestration**: Replaced direct execution with command-based orchestration through channels - **Worker pool with dynamic scaling**: Introduced configurable worker pools that scale based on workload - **Domain-driven design**: Separated domain logic from infrastructure with clear boundaries - **Event-driven communication**: Decoupled components communicate through well-defined events ### New Components - **Command System**: Commands flow through channels (Redis or in-memory) for better control - **Worker Management**: Dynamic scaling with activity tracking and configurable thresholds - **Response Coordinator**: Centralized handling of streaming responses and output management - **State Management**: Dedicated managers for node states, edge states, and execution tracking - **Error Handling**: Multiple recovery strategies (retry, default value, fail branch, abort) - **Layer System**: Extensible processing layers (e.g., debug logging layer) ### Configuration Configuration is now managed through environment variables via `dify_config`: - `GRAPH_ENGINE_MIN_WORKERS`: Minimum workers (default: 1) - `GRAPH_ENGINE_MAX_WORKERS`: Maximum workers (default: 10) - `GRAPH_ENGINE_SCALE_UP_THRESHOLD`: Queue size for scaling up (default: 3) - `GRAPH_ENGINE_SCALE_DOWN_IDLE_TIME`: Idle time before scaling down (default: 5.0) ## Test Coverage The refactoring includes comprehensive test coverage: - Unit tests for all new components - Integration tests for the complete workflow execution - Test fixtures for various workflow scenarios - Mock system for testing complex workflows without external dependencies ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:47:35 -05:00
yindo closed this issue 2026-02-21 20:47:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30489