dify icon indicating copy to clipboard operation
dify copied to clipboard

[Refactor/Chore] Default concurrency settings cap a deployment at 10 in-flight streaming requests and 4 concurrent workflow runs regardless of hardware

Open Theysua opened this issue 3 weeks ago • 1 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 shipped defaults, how many requests a deployment can serve at once is set by two numbers rather than by its hardware, and neither is where an operator looks first:

Gate Default What it limits
SERVER_WORKER_CONNECTIONS 10 (api/docker/entrypoint.sh, docker/envs/core-services/api.defaults) Connections one API process accepts. A streaming response occupies one for the whole run, so an API process serves 10 runs at a time; the 11th client gets no byte until one of them finishes.
CELERY_WORKER_AMOUNT 4 in Docker Compose, 1 when the variable is unset (-c ${CELERY_WORKER_AMOUNT:-1} in the entrypoint) Workflow and chatflow runs one worker executes at a time (streaming runs execute in workflow_based_app_execution_task). With the gevent pool these are greenlets, not processes.

Raising one without the other moves the queue from one place to the other, and raising both exposes the connection-pool problems in #46241, #46253 and #46278, which is probably why reports like #36023 ("after modifying the .env file, the concurrency cannot be improved") end without a resolution.

Motivation

Measured on 1.19.0 with Docker Compose, one API process and one worker container, a workflow of Start → HTTP Request (the endpoint answers after 2 seconds) → End, N streaming requests to POST /v1/workflows/run sent at once. The ideal result is every request finishing in a little over 2 seconds.

Settings N = 10 N = 30 N = 60 Runs per second Median time to first byte at N = 60
Defaults (connections 10, worker 4) 8.4 s 23.7 s 43.4 s 1.2–1.4 21.5 s
Connections 1000, worker 4 7.2 s 19.0 s 40.1 s 1.4–1.6 0.5 s
Connections 10, worker 100 2.5 s 7.8 s 15.3 s 3.8–4.0 7.7 s
Connections 1000, worker 100 3.5 s 4.9 s 9.0 s 6.1–6.7 at N = 30–60 2.2 s
Connections 10, worker 1 (variable unset) 22.9 s 66.8 s not run 0.4

(Cells are the time until all N requests have finished.)

Reading the table:

  • With the defaults, 60 users who each start a 2-second workflow wait 43 seconds in total, and half of them see nothing for 21 seconds. The machine is idle during that time: the limit is the two settings.
  • Opening only the API gate changes nothing in throughput, because four runs execute at a time. Opening only the worker gate helps, but requests then queue in front of the API.
  • With both open, throughput is bounded by actual work. At N = 100 this configuration then fails on unpatched code: 82 to 100 of 100 requests return HTTP 500 from a QueuePool timeout (#46241). With the fix for that issue applied, 100 and 200 concurrent requests all succeed.

Proposal, to be done after the pool fixes (#46244, #46254, #46279) so that higher defaults do not turn queueing into errors:

  1. Raise the default SERVER_WORKER_CONNECTIONS for the gevent worker class (gunicorn's own default is 1000; docker-compose.yaml already uses 1000 for the websocket service).
  2. Raise the default CELERY_WORKER_AMOUNT for the gevent pool, and make the entrypoint fallback match the Compose default instead of 1.
  3. Document the sizing rule next to these variables: concurrent streaming requests = API processes × SERVER_WORKER_CONNECTIONS; concurrent workflow runs = worker containers × CELERY_WORKER_AMOUNT; and each API or worker process can open up to SQLALCHEMY_POOL_SIZE + SQLALCHEMY_MAX_OVERFLOW (40) database connections, which has to fit under POSTGRES_MAX_CONNECTIONS when replicas are added.

I can open a pull request for the defaults and the documentation once the direction is agreed.

Additional Context

  • Blocking requests and chat, completion and agent-chat apps do not go through the worker gate: they run inside the API process, where SERVER_WORKER_CONNECTIONS is the only limit.
  • Scripts and raw numbers are available if useful.

Theysua avatar Nov 13 '26 11:11 Theysua

Two more measurements for this issue, both on 1.19.0 with Docker Compose on a 3-core host.

1. The same gate on a Chat app. The issue's numbers are for a workflow. For POST /v1/chat-messages (model stand-in streaming for 2 seconds, N requests in flight, 6 × N requests per row):

In flight Shipped defaults: median time per answer Both gates raised and the open connection fixes applied (#46244, #46254, #46279 and the rest of #46370)
10 2.2 s 2.2 s
20 4.2 s 2.6 s
30 6.3 s 2.8 s
50 10.4 s 3.7 s

With the defaults the API process completes 4.7 answers per second whatever the load, which is 10 connections divided by a little over 2 seconds, and its CPU is mostly idle. The right-hand column is not the gate alone: on stock 1.19.0, raising it runs into the pool exhaustion described in #46241 and #46278, so the fixes have to come first.

2. One API process is one core. Once the gates and the connection holds are out of the way, every scenario I profiled is bound by a single Python thread: the API process for chat and blocking calls, the worker for streamed workflows. SERVER_WORKER_AMOUNT defaults to 1, so an API container uses one core however many the host has.

Chat, 100 in flight, 400 requests Answers per second (three rounds)
SERVER_WORKER_AMOUNT=1 17.9 / 17.8 / 17.2
SERVER_WORKER_AMOUNT=3 27.1 / 28.7 / 26.7

That is with PostgreSQL, Redis, the plugin daemon and the load generator sharing the same three cores, so it understates what a dedicated host would show.

For sizing, the CPU one request costs with everything above applied: a chat answer of 20 streamed chunks takes about 43 ms of the API process, one of 400 chunks about 165 ms (#46397 is about the part of that which grows with the conversation), and a 20-node workflow about 200 ms of the worker.

So a default-settings change would be three numbers rather than two: the two gates in the issue, and the API process count, which could follow the CPU count the way gunicorn deployments usually do.

Theysua avatar Nov 14 '26 12:11 Theysua