dify icon indicating copy to clipboard operation
dify copied to clipboard

[Refactor/Chore] Every in-flight workflow run polls on a timer: 10 waiting runs cost a tenth of a worker core and 485 Redis commands per second

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

A workflow or chatflow run that is only waiting (on an LLM answer, an HTTP call, a tool) still costs worker CPU and Redis commands for as long as it waits, because three loops poll on a timer for every in-flight run:

Loop Where Rate per in-flight run Cost per tick
Engine workers waiting for a ready node graphon/engine/worker/worker.py, self._ready_queue.get(timeout=0.01) under the pool's shared acquisition lock 100 per second a timed queue wait (a gevent Timeout under the gevent worker)
Engine dispatcher asking for commands graphon/engine/dispatcher.py, self._dispatch_queue.get(timeout=0.1) then fetch_commands() on the channels built in core/app/apps/workflow/app_runner.py and core/app/apps/advanced_chat/app_runner.py 10 per second RedisChannel.fetch_commands (MULTI, LRANGE, DEL, EXEC) plus StopFlagCommandChannel (GET)
Queue manager checking the stop flag AppQueueManager.listen, self._q.get(timeout=1) then _is_stopped() 1 per second GET

The first loop is in the engine package, the second is engine timing multiplied by Dify's command sources, the third is in this repository.

Motivation

Measured on 1.19.0 (the Dify files involved are identical to main; graphon 0.8.0), one Celery worker process, -P gevent -c 100. The workflow is Start → HTTP Request → End where the HTTP endpoint answers after 20 seconds, so during the measurement window every run is doing nothing but waiting. CPU is the worker container's cgroup usage over 10 seconds; Redis is total_commands_processed over the same window. An idle worker uses about 1.5% of a core.

Runs waiting Worker CPU (share of one core) Redis commands per second
10 11.0% 485
50 29.4% 2,204
100 33.1% 4,535

So ten idle runs cost about a tenth of a core, and every in-flight run adds about 45 Redis commands per second for its whole duration. CPU grows less than linearly above 50 runs only because the event loop can no longer keep the timers on schedule.

A py-spy profile of the worker during the waiting phase (100 runs, 10 s) attributes the samples to: StopAwareReadyQueue.get ← Worker._run_tasks 33%, CombinedCommandChannel.fetch_commands 24%, AppQueueManager.listen → _is_stopped 6%, and the rest to gevent timer and hub bookkeeping.

Sizing experiments, each a one-line change to the polling interval, same measurement:

Variant CPU at 10 / 50 / 100 waiting runs Redis commands/s at 10 / 50 / 100
Current 11.0% / 29.4% / 33.1% 485 / 2,204 / 4,535
Redis-backed command sources polled every 0.5 s instead of every dispatcher tick 9.4% / 28.4% / 32.2% 113 / 545 / 1,081
Engine worker ready-queue wait 100 ms instead of 10 ms 6.9% / 25.3% / 29.6% 479 / 2,094 / 3,974
Both 4.5% / 12.6% / 22.2% 111 / 538 / 1,084

What this does and does not affect: with a worker already saturated by real work (a workflow with one streaming LLM node, 100 concurrent, 400 runs per round, 4 rounds per variant) throughput was 14.3 runs/s for the current code and 14.0, 14.6 and 14.8 for the three variants, which is within the spread between rounds. The polling is not what limits peak throughput. It is a standing cost per in-flight run: CPU that a worker spends while its runs wait, and a Redis command rate that scales with the number of runs in flight rather than with what they do.

Proposal:

  1. In this repository: poll the Redis-backed command sources (the engine's RedisChannel and StopFlagCommandChannel) on an interval of their own instead of on every dispatcher tick. With 0.5 s this removes about three quarters of the Redis commands. The stop request a client sees is not slowed by this, because the stream is ended by the queue manager; I measured 0.05 s before and 0.11 s after (median of 8) from the stop call to the end of the stream. I will open a pull request for this part.
  2. In the engine package: replace the 10 ms ready-queue poll with a blocking wait that is woken on stop and pause. That is where most of the CPU is, and it cannot be changed from this repository. I can open an issue in langgenius/graphon with the same data if that is the right place.

Additional Context

  • GRAPH_ENGINE_MAX_WORKERS (10 by default) does not change the ready-queue polling rate: the workers of one run share one acquisition lock, so one of them is polling at any moment as long as at least one is idle.
  • A cheaper per-poll check (for example reading the :pending marker that RedisChannel.send_command still writes) would keep the 100 ms reaction time, but it depends on a wire-format detail the engine keeps only for rolling upgrades, so I did not build on it.
  • Scripts and raw numbers are available if useful.

Theysua avatar Nov 13 '26 11:11 Theysua