Sub topic 11
Retries and timeouts
This chapter covers two settings that decide what happens when a task fails or runs too long. I assume you can already build a multi-task job with dependencies and read a Jobs API definition.
Why tasks need a failure policy
Tasks fail for different reasons. Some failures are transient: a source API returns a 503, a cluster node is lost, or a storage call times out. Retrying fixes these. Other failures are permanent, such as a bad column name or a missing table. Retrying only delays the failure. A third kind is a task that never fails but never finishes either. A timeout bounds that case.
Both settings are set per task, and a timeout can also be set on the whole job.
Retries
By default a failed task is not retried. You turn retries on with three task fields.
| Field | Meaning | Default |
|---|---|---|
| `max_retries` | How many times to retry after the first attempt fails. `0` means never. `-1` means retry indefinitely. | 0 |
| `min_retry_interval_millis` | Minimum time, in milliseconds, between the start of the failed attempt and the start of the retry. | 0 (retry immediately) |
| `retry_on_timeout` | Whether an attempt that timed out counts as a failure to retry. | `false` |
A task with max_retries: 2 can run up to three times: the first attempt plus two retries. Each attempt appears on the run page with an attempt number.
Here is the ingest task from the daily pipeline with a retry policy and a timeout.
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"max_retries": 2,
"min_retry_interval_millis": 60000,
"retry_on_timeout": false,
"timeout_seconds": 1800
}Each attempt gets at most 30 minutes. If an attempt fails for any other reason, Databricks starts a new attempt at least 60 seconds after the previous one started.
How the interval is measured
The interval runs from the start of the failed attempt, not from its end. Suppose an attempt runs for 10 minutes and then fails, and the interval is 60 seconds. The 60 seconds have already passed, so the retry starts immediately. The interval only creates a pause when attempts fail quickly. For a task that fails fast against a flaky service, this gives the service time to recover.
What downstream tasks see
While a task is being retried, its downstream tasks keep waiting. They do not start after the first failed attempt. The task counts as failed only after the final attempt fails. At that point the downstream tasks follow their run_if rule. With the default ALL_SUCCEEDED, they are skipped as "upstream failed".
Timeouts
A timeout is a maximum run time. When the limit is reached, Databricks stops the run and marks it as timed out. A value of 0 means no timeout, which is the default.
You can set a timeout at two levels:
timeout_secondson a task limits one attempt of that task.timeout_secondson the job limits the whole run, from start to finish. When it fires, the unfinished tasks are stopped.
Use both. The task timeout catches a single stuck task early. The job timeout is a backstop for the whole pipeline, including time spent waiting for compute.
Timeouts and retries together
A timed-out attempt is not retried unless retry_on_timeout is true. This default is deliberate. A task that hit its limit once may hit it again, and each retry spends more compute. Enable the setting only when a timeout is a symptom of a transient problem, such as a call that hangs on a network partition.
You can bound the worst case with arithmetic. With timeout_seconds: 1800, max_retries: 2 and retry_on_timeout: true, three attempts of 30 minutes each give up to 90 minutes. Add any retry waits. The job-level timeout must be larger than this, or it will cut off the retries.
Choosing values
- Retry for transient, external failures: network calls, API rate limits, lost spot instances. Two or three retries with an interval of one to five minutes is a common starting point.
- Do not retry deterministic failures. A syntax error fails three times and delays the alert by minutes. Handle these in the notebook, for example by catching the known transient exception and raising everything else.
- Avoid
max_retries: -1for batch tasks. It retries forever and can hide a permanent problem while the cluster keeps running. It suits long-lived streaming tasks that must restart. - Set the timeout from history. A common choice is two to three times the normal duration. A tight limit causes false alarms. A loose limit detects hangs too late.
Retries are not repairs
Retries happen automatically within one run, and the run's attempts stay attached to the same task. A repair is a manual action you take after a run has finished with failures. Repair reruns only the failed tasks and their skipped dependents, and it starts a new attempt for them. The two work together: retries absorb brief glitches, and repair handles what remains.
A worked check
Consider the three-task pipeline from earlier in the tutorial. You set ingest to max_retries: 2, min_retry_interval_millis: 60000 and timeout_seconds: 600. You leave retry_on_timeout unset. You set the job's timeout_seconds to 7200.
- Attempt 1 fails after 20 seconds with a connection error. The retry waits until 60 seconds after the start, so about 40 more seconds.
- Attempt 2 hangs. At 600 seconds it times out. Because
retry_on_timeoutisfalse, no third attempt runs. ingestis marked failed.transformandpublishare skipped because theirrun_ifrule isALL_SUCCEEDED.
The third attempt was never used. If hangs on this source are usually transient, setting retry_on_timeout to true would have given it one more chance.
Exercise
A task has maxretries 3, minretryintervalmillis 120000 and timeoutseconds 900. retryon_timeout is false. Attempt 1 runs for 5 minutes and fails with an exception. When does attempt 2 start, and how many attempts can run in total?
Show answer
The interval is measured from the start of the failed attempt, and attempt 1 ran for 5 minutes, which is longer than the 2 minute interval. Attempt 2 therefore starts immediately after the failure. In total up to 4 attempts can run (the first attempt plus 3 retries). However, any attempt that times out at 900 seconds ends the task without a retry, because retryontimeout is false.
Exercise
Your notebook inserts rows into a Delta table with INSERT INTO and then runs a slow validation step that sometimes fails. You want to add max_retries 2. What problem can this cause, and how do you avoid it?
Show answer
Each retry reruns the whole task, so the INSERT runs again and creates duplicate rows even though the earlier attempt already wrote them. Make the write idempotent, for example with MERGE on a key or by overwriting the partition for the run date, or move validation into a separate task so that only the validation is retried.