Sub topic 12
Limiting concurrent runs
This chapter assumes you can create a multi-task job and attach a schedule or trigger to it. It covers what happens when a new run is due while an earlier run of the same job is still active, and how to control that behavior.
Why concurrency needs a limit
A job is a definition. A run is one execution of it. Nothing stops two runs of the same job from overlapping unless you configure a limit. Overlap happens in a few common ways:
- A daily job takes 26 hours after the data volume grows, so the next scheduled run starts while the previous one is still going.
- Someone clicks Run now while a scheduled run is in progress.
- A file arrival trigger fires several times in a short burst.
- A repair run is started on an old failed run while a new scheduled run is also active.
Overlapping runs of a pipeline that writes to the same table can corrupt results or cause write conflicts. They also compete for compute and cost more. Setting a concurrency limit makes the behavior predictable.
The max_concurrent_runs setting
The job-level field max_concurrent_runs sets how many runs of this job may be active at once. A run is active from the moment it starts until it reaches a terminal state (success, failed, canceled, or timed out). The default is 1.
{
"name": "daily-sales-pipeline",
"max_concurrent_runs": 1,
"schedule": {
"quartz_cron_expression": "0 0 6 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
}
}With a limit of 1, a run that is due while another is active does not start. What happens to it depends on whether queueing is enabled.
Skipped runs versus queued runs
When the limit is reached, Databricks does one of two things.
| Queueing setting | Behavior when the limit is reached |
|---|---|
| Disabled | The new run is skipped. It appears in the run history with a skipped status and never executes. |
| Enabled | The new run waits in a queue and starts when an active run finishes. |
Skipping is the older behavior. It is safe for jobs where only the latest data matters, because a missed run is simply replaced by the next one. It is dangerous when every run must happen, such as a job that processes one specific day's data. A skipped run is not retried, so the data for that period is never processed unless you launch it yourself.
Queueing is configured in the job settings:
{
"name": "daily-sales-pipeline",
"max_concurrent_runs": 1,
"queue": {
"enabled": true
}
}Queued runs do not wait forever. Databricks drops a run that has been queued for too long (the documented limit is 48 hours). Check the current documentation for per-job and per-workspace queue limits, because these can change.
Setting the limit with the SDK and bundles
You can change the settings on an existing job with the Databricks SDK for Python. The update call merges the fields you give it into the job settings and leaves the others unchanged.
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.jobs import JobSettings, QueueSettings
w = WorkspaceClient()
JOB_ID = 123456789 # replace with your job ID
w.jobs.update(
job_id=JOB_ID,
new_settings=JobSettings(
max_concurrent_runs=1,
queue=QueueSettings(enabled=True),
),
)
job = w.jobs.get(job_id=JOB_ID)
print(job.settings.max_concurrent_runs, job.settings.queue)In a Databricks Asset Bundle, the same fields sit at the job level:
resources:
jobs:
daily_sales:
name: daily-sales-pipeline
max_concurrent_runs: 1
queue:
enabled: true
schedule:
quartz_cron_expression: "0 0 6 * * ?"
timezone_id: UTCChoosing a value
Use the nature of the work to decide.
- Limit of 1 with queueing off. Best for "refresh the latest state" jobs. A late run is replaced by the next one, and no backlog builds up.
- Limit of 1 with queueing on. Best when each run covers a distinct period or input and none may be lost. The runs execute one after another, in order of arrival.
- Limit above 1. Best when runs are independent, for example a job parameterized by customer or region and started by an API call for each one. Each run must write to its own output, or the writes must be safe to interleave.
A limit above 1 is a correctness decision first and a throughput decision second. Before you raise it, check that every task is idempotent, which means running it twice, or two copies at once, gives a correct result. Writing with MERGE, or overwriting a partition keyed by a run parameter, is usually safe. Appending to a shared table from overlapping runs can produce duplicates.
Gotchas
- The limit counts per job, not per workspace. Two different jobs writing to the same table are not coordinated by this setting.
- Repair runs count as active. A repair of an old run occupies a slot. If the limit is 1 and a scheduled run is already going, you may be unable to start the repair until the scheduled run ends.
- Retries stay inside one run. A task retry does not create a new run, so retries do not use extra slots. A long retry sequence does extend the run's duration, which makes overlap with the next schedule more likely.
- A timeout frees the slot. If a run hangs, it holds the slot until it ends. A job-level timeout (covered in the retries and timeouts chapter) is the usual way to prevent a hung run from blocking every later run.
- Skipped is not failed. Failure notifications fire on failures, so a skipped run may not alert anyone. If skips matter to you, check the run history, or configure the notification options available in your workspace for skipped runs.
- Concurrency inside a run is separate. A For each task has its own
concurrencyfield that limits how many iterations run in parallel within one run. It does not interact withmax_concurrent_runs. - Compute limits still apply. Setting a high concurrency limit does not guarantee the runs start together. If each run launches its own job cluster, the cloud provider's instance quotas and workspace limits can delay or fail them.
Diagnosing missing or delayed runs
If a scheduled run did not happen, work through these checks in order:
- Open the job's run history and look for runs with a skipped status around the expected time.
- Compare the duration of the previous run with the gap between schedule times. If the run takes longer than the gap, overlap is the cause.
- Check whether queueing is enabled. If it is and the run is not visible as started, look for a queued state.
- Check for a paused schedule or a paused trigger, which looks similar from the outside.
- If many runs are queued, find the run that is blocking them and decide whether to cancel it.
Exercise
A job runs hourly with maxconcurrentruns set to 1 and queueing disabled. One run takes 150 minutes. Which scheduled runs happen during that time, and what do you see in the run history?
Show answer
The run starts at the top of an hour and is still active at the next two scheduled times, which fall at 60 and 120 minutes. Both of those runs are skipped and appear in the history with a skipped status. The next scheduled time at 180 minutes finds no active run (the 150-minute run has finished), so that run starts normally. Enabling queueing would make the two due runs wait and start one after another instead of being skipped.
Exercise
You want to launch one run of a job per customer through the API, five at a time, and each run writes to a table partition named by a customer_id parameter. Which settings do you choose, and what must you verify first?
Show answer
Set maxconcurrentruns to 5 and enable queueing so extra requests wait instead of being skipped. Verify that the tasks are idempotent and that each run writes only to its own customerid partition (for example with a partition overwrite or a MERGE keyed on customerid), so concurrent runs cannot overwrite each other or create duplicates. Also confirm that your cloud quotas allow five job clusters at once.