Workers are not honoring deployment concurrency limits during poll time
Describe the current behavior
When a deployment has a concurrency limit (with the ENQUEUE collision strategy), workers still pick up all of its scheduled runs, not just the ones that can actually start. Prefect only checks the limit afterwards, when each run tries to start. Runs that don't fit get pushed back ~30 seconds and are retried on the next poll.
For example, a deployment with 300 queued runs and 1 free slot hands all 300 to the worker. 1 starts and 299 are rejected and rescheduled. This keeps repeating while the backlog lasts.
In practice this causes:
- A lot of wasted work: repeated poll → reject → reschedule cycles that put load on the API and database, which gets worse as the backlog grows.
- Runs no longer go in order: each rejected run gets a later scheduled time, so newer runs can overtake older ones that have been waiting longer.
ENQUEUEacts more like "retry later" than a first-in-first-out queue.
Describe the proposed behavior
When workers poll for runs, also take each deployment's free concurrency slots into account (the same way work pool and work queue limits are already handled):
- If a deployment has no free slots, its runs aren't handed out.
- If it has some free slots, only that many runs are handed out, oldest first.
The existing check when a run starts should stay as-is. It remains the final safeguard against races between workers. This change just stops workers from picking up runs that are sure to be rejected.
This keeps the deployment concurrency limit doing what users expect: extra runs wait their turn instead of repeatedly bouncing between the worker and the server.
Example Use
We run a self-hosted Prefect 3 server (OSS 3.8.2) with around 200 deployments spread across a few priority levels (one work queue per level). Each deployment has its own concurrency limit with ENQUEUE. When traffic spikes, a few hundred runs build up for some deployments. Today those runs keep getting rejected and rescheduled, and newer runs can go ahead of older ones.
With this change, workers would only pick up runs that can actually start. The backlog would drain in order without the retry storm.
We looked at workarounds, like one work queue per deployment with a queue-level concurrency limit. That reduces the churn, but queue priorities have to be unique, so we'd lose our small set of shared priority levels. There's no configuration-only way to get per-deployment limits, shared priority levels, and in-order processing together.
Additional context
- We discussed this with the Prefect team in our shared Slack channel. They confirmed that checking the limit when a run starts is needed for correctness. They also said that also filtering at poll time would be a reasonable enhancement, as long as the start-time check stays in place.
- Draft PR as a starting point: #23084
Source: PrefectHQ/prefect