dify icon indicating copy to clipboard operation
dify copied to clipboard

[Refactor/Chore] Node execution persistence writes each finished node twice and takes two thirds of the workflow worker's CPU

Open Theysua opened this issue 3 weeks ago • 0 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for refactors or chores; if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing issues, including closed ones.
  • [x] I confirm that I am using English to submit this report, otherwise it will be closed.
  • [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • [x] Please do not modify this template :) and fill in all the required fields.

Description

With the default SQLAlchemy node execution repository, persisting node executions is the largest cost of running a workflow. A node that does nothing still costs about 9 SQL statements and 4.5 transactions, and the data of a finished node is written to the database twice.

What happens for each node (services/workflow_persistence.py and core/repositories/sqlalchemy_workflow_node_execution_repository.py):

  • Node start, repository.save(): SELECT workflow_runs ... FOR UPDATE (the stop check, full row), SELECT the node row, INSERT, COMMIT.
  • Node finish, _update_node_execution calls two repository methods in a row:
    1. repository.save(): SELECT the node row, UPDATE every column including the complete inputs, process_data and outputs, COMMIT.
    2. repository.save_execution_data(): SELECT the node row again plus SELECT its offload rows, apply truncation and offloading, then session.merge(), which selects the row and its offload rows once more and updates the row when the JSON text differs (save() writes sorted keys, save_execution_data() does not), COMMIT.

So the payload is written in full by step 1 and then read back and written again by step 2. When a value is large enough to be truncated, step 1 has already stored the full value that step 2 then replaces with the truncated one.

I propose a single write at node completion: resolve truncation and offloading first, then update the row once, keeping the stored content byte-for-byte what it is today.

Motivation

Measured on 1.19.0 (the three files involved are identical to main at 4833948da), Celery worker -P gevent -c 100, one worker process, a workflow of Start → 20 Variable Aggregator nodes → End called through POST /v1/workflows/run in streaming mode.

CPU profile of the worker under load (py-spy, 100 Hz, 30 s, 3,007 samples, idle time excluded):

Share of worker CPU (inclusive) Function
66.9% WorkflowPersistenceLayer.on_event (services/workflow_persistence.py)
46.6% _handle_node_succeeded
37.5% SQLAlchemyWorkflowNodeExecutionRepository.save
23.1% SQLAlchemyWorkflowNodeExecutionRepository.save_execution_data
17.5% _handle_node_started

By package, 52% of the samples are inside SQLAlchemy (statement construction, cache keys, ORM), and about 7% in the gevent/psycopg wait path. The nodes themselves are a rounding error.

SQL per run (pg_stat_statements, 30 sequential runs of the 22-node workflow):

Per run
Statements 413
Transactions about 100
Time spent executing them in PostgreSQL 5.8–7.1 ms in total

The database is not the bottleneck; the cost is building and round-tripping the statements in a single-core Python worker. A 22-node run takes about 130 ms of worker CPU, which caps one worker process at 7–8 such runs per second.

Experiment: skip the second write when nothing needs truncation (not a proposed patch, just to size the effect):

Statements per run Transactions per run Runs per second at concurrency 20 (3 rounds of 300 runs) Median latency
Current 413 100 7.65, 7.84, 7.95 2.34–2.48 s
One write at completion 237 61 10.15, 10.09, 9.68 1.78–1.93 s

That is 43% fewer statements and about 28% more throughput from the same worker, with every node execution still stored and succeeded.

Additional Context

  • The Celery repository does not have the double write (save_execution_data is a no-op there), and the LogStore repository only forwards it to SQL in dual-write mode, so this is specific to the default repository.
  • I also tried making the per-node stop check lighter (selecting only status and stop_requested_at instead of the whole workflow_runs row). With this small graph it made no measurable difference (7.8–7.9 runs per second), so I am leaving it out.
  • Profiling data and scripts are available if useful. I will open a pull request for the single write.

Theysua avatar Nov 13 '26 09:11 Theysua