CertSafari
    Databricks Certified Data Engineer Associate· Lessons

    Domain 4 · Lesson 17/33

    Lakeflow Jobs control flow: Run if, retries, If/else and For each

    Implement control flows (retries and conditional tasks such as branching and looping) using Lakeflow Jobs for pipeline orchestration

    19 min read
    4% of exam
    9 sources
    Published 22 Oct 2026
    Docs as of 17 Oct 2026

    What you will be able to do

    • Choose the Run if condition that makes a downstream task run on success, failure, or any outcome of its upstream tasks
    • Predict whether a task runs when an upstream task is excluded, cancelled or disabled
    • Configure a task retry policy with a retry count, an interval and retry-on-timeout
    • Build an If/else condition task on a task value or job parameter and connect tasks to its true and false outcomes
    • Loop a nested task over an input list with a For each task, set its concurrency, and read its results downstream

    Key concept

    Run if dependency — A Run if dependency is the rule on a downstream task that uses the final states of the tasks it depends on to decide whether it runs at all. Retries, branches and loops all produce task outcomes, and these rules read those outcomes.

    1.From a task list to a flow with decisions

    A Lakeflow Job is a graph of tasks joined by dependencies. By default it follows one rule: run a task once everything before it has succeeded. Databricks runs upstream tasks first and runs as many independent tasks in parallel as it can. That works for a straight ingest-transform-publish chain. Real pipelines need more: a cleanup step that runs even after a crash, an alert that fires only on failure, a branch that skips transformation when no rows arrived, and the same notebook applied to forty tables.

    Lakeflow Jobs meets those needs with four tools. Run if conditions decide whether a task runs, based on how its upstream tasks finished. Retries re-run a failed task before its failure becomes final. The If/else condition task branches on a value, such as a count produced earlier in the run. The For each task loops a nested task over a list of inputs. Ordinary task types and basic dependencies are covered in task types and dependencies. Control flow is the logic built on top of them.

    A related control is disabling a task. A disabled task is skipped at run time but keeps its configuration and run history. The tasks after it are still checked against their own Run if conditions. This matters because a disabled or skipped upstream task does not automatically stop everything downstream.

    Checkpoint 1 of 8· Match them up

    A pipeline team has four requirements. Match each Lakeflow Jobs control-flow tool to the requirement it meets.

    Tap a term, then the definition that fits it.

    Sources12

    2.Run if conditions and how upstream states are counted

    Every dependent task has one Run if condition, chosen from six options. The default, All succeeded, is the strictest. The other five let a task react to partial success, to failure, or to the upstream tasks simply finishing. The conditions also differ in what happens when they are not met. That difference is what lets you leave failure-handling tasks in a job permanently.

    The six Run if conditions and the state a task gets when its condition is not met
    Run if conditionThe task runs whenIf the condition is unmet
    All succeeded (default)All dependencies have run and succeededUpstream failed
    At least one succeededAt least one dependency has succeededUpstream failed
    None failedNo dependency failed, and at least one dependency was runUpstream failed
    All doneAll dependencies have run, whatever their statusNot applicable: it runs once its dependencies finish
    At least one failedAt least one dependency failedExcluded (skipped)
    All failedAll dependencies failedExcluded (skipped)

    All done is the condition for work that must happen no matter what. Take a cleanup task that deletes staging files and depends on an ingestion task. Set to All done, it runs whether ingestion succeeded, failed or was skipped, because it only waits for its dependencies to finish. None failed looks similar but is not. It still requires that nothing failed and that at least one upstream task actually ran.

    At least one failed is the condition for alerting. Picture three parallel notebook tasks, each writing a different table, and a Slack notification task that depends on all three. With At least one failed, a single broken write is enough to send the alert. All failed would stay silent through a partial failure. When everything succeeds, the notification task is marked Excluded, which means skipped, not failed. The three writers are unaffected either way, because a Run if condition only controls the task it is set on.

    Three counting rules settle most tricky cases:

    • An upstream task in the Excluded state counts as successful.
    • An upstream task marked Upstream failed or Upstream canceled counts as failed, not skipped. A failure three steps back can therefore still trigger an At least one failed handler at the end of the chain.
    • If every one of a task's dependencies is excluded, the task is excluded too, whatever its condition says.

    The last rule cascades. In a chain A → B → C where A is excluded, B's only dependency is excluded, so B is excluded. C is excluded for the same reason, and none of the three runs. This cascade applies to tasks skipped because a condition was not met. Disabling a task works differently: downstream tasks are still checked against their own Run if conditions, and some of them may also be disabled for that run.

    Cancellation still lets cleanup happen. When you cancel a task run, the cancellation moves through the downstream tasks, but tasks whose Run if condition handles failure still run. So a cleanup task set to All done or At least one failed still runs when someone cancels a run halfway through.

    Checkpoint 2 of 8· Check yourself

    Your nightly job has a cleanup task that deletes staging files and depends only on the ingestion task. The staging files must be removed whether ingestion succeeds or fails. Which Run if condition should the cleanup task use?

    Checkpoint 3 of 8· Exam question

    A Lakeflow Job has an ingestion task followed by a cleanup task that deletes staging files. The team wants the cleanup task to run every time the job executes, whether the ingestion task succeeds, fails, or is skipped. Which Run if condition should be set on the cleanup task's dependency on the ingestion task?

    Checkpoint 4 of 8· Exam question

    A job fans out into three parallel notebook tasks that each write to a different table. A downstream Slack notification task should run only if at least one of the three upstream tasks fails, so the team can be alerted to partial failures. Which Run if condition satisfies this requirement while leaving the other three tasks unaffected?

    Sources1

    3.Retry policies for failed tasks

    A retry re-runs a task that failed with an error. Errors are often transient, such as a dropped connection or a busy source, and a restart clears them. Some Databricks features depend on retries. Schema evolution with Structured Streaming assumes jobs run with retries, so that a restart can reset the environment and let the workflow continue.

    Retries are off by default. In most job configurations, a failed task stays failed unless you add a retry policy. To add one, click Add next to Retries in the task's advanced settings. Not every job configuration supports task retries. The policy has three settings, and people often guess two of them wrong.

    Retry policy settings for a task, with their defaults
    SettingWhat it controlsDefault if not set
    max_retriesMaximum number of times to retry the task if it failsNo retries
    min_retry_interval_millisMinimum interval in milliseconds from the start of the failed run to the retry run0 (immediate retry)
    retry_on_timeoutWhether a task that times out is also retriedfalse

    The interval is measured from the start of the failed run, not from its end. A task that fails after 20 minutes with a 10-minute interval is retried right away, because the interval has already passed. Timeouts apply per attempt. When a task has both a Timeout and Retries, the timeout applies to each retry separately, not to all of them together. A task that times out is retried only if retry-on-timeout is turned on.

    Two kinds of job change these defaults. Serverless jobs auto-optimize retries by default, and auto-optimization can retry a failed task on top of your own retry policy. Disable auto-optimization for a task that must run at most once. Jobs in continuous trigger mode are retried automatically with exponential backoff once consecutive failures exceed a threshold. Restart run cancels the active run, resets the retry period and starts a new run.

    Checkpoint 5 of 8· Check yourself

    A notebook task has a Timeout of 60 minutes and a retry policy with max_retries set to 2. Its first attempt fails with a transient connection error after 45 minutes. What happens to the time limit for the retry?

    Sources2345

    4.Branching with the If/else condition task

    Run if reacts to how tasks finished. The If/else condition task reacts to a value. It holds one boolean expression made of two operands and an operator. It evaluates the expression during the run and sends the job down either the true branch or the false branch. A typical use is to run transformation tasks only when the ingestion task actually added new data.

    An operand can be a literal value or a reference that is filled in at run time:

    • a job parameter, for example {{job.parameters.<name>}}
    • a task parameter variable
    • a task value set by an earlier task, for example {{tasks.<task_name>.values.<value_name>}}

    Task values are key-value pairs that an earlier Python notebook sets with dbutils.jobs.taskValues.set(). Suppose a task named process_records counts invalid rows into a value called bad_records. The condition below sends the run down a quarantine branch whenever that count is above zero.

    If/else condition expression that branches when the upstream task reported bad recordstext
    {{tasks.process_records.values.bad_records}} > 0

    You pick the operator from a fixed list of six: ==, !=, >, >=, < and <=. This task has no other operators. There is no LIKE or IN, no string function such as CONTAINS or STARTSWITH, and no AND or OR to combine clauses. If you need more logic, compute it in an earlier task as a single task value and compare that.

    The operators fall into two groups. == and != compare their operands as strings, so 12.0 == 12 is false. >, >=, < and <= compare them as numbers, so 12.0 >= 12 is true. A task value used as an operand must be a number, a string or a boolean, and any other type makes the condition fail. Booleans become the strings "true" or "false". String equality is what you need to route on a job parameter: {{job.parameters.environment}} == staging is true only for that exact value, and every other value goes to the false branch.

    You connect the branches with the Depends on field. When you add a task under the condition task in the graph, Depends on defaults to <task-name> (true). Choose <task-name> (false) for tasks on the other branch. Each branch can hold several tasks, in series or in parallel. After the run, the job run details show the result and how the expression was evaluated. A disabled upstream task causes a hard failure here: if the task that supplies the condition value is disabled, the If/else task itself fails.

    Checkpoint 6 of 8· Fill the gap

    The task process_records stores its invalid-row count in the task value bad_records as a float, so a clean run reports 0.0. The quarantine branch must run only when at least one bad record was found. Which operator completes the If/else condition?

    {{tasks.process_records.values.bad_records}}  ?  0

    Checkpoint 7 of 8· Exam question

    Which set of comparison operators can an If/else condition task in a Lakeflow Job evaluate when comparing a task value or job parameter against a literal?

    Sources612

    5.Looping with the For each task

    The For each task runs one nested task repeatedly, passing a different input to each iteration. You configure two things: the For each task itself, and the nested task. The nested task can be any standard task type, with its usual options. It cannot be another For each task, so you cannot nest loops. A common use is applying one set of transformations to many tables, with each iteration given one table name.

    The loop is driven by Inputs, a JSON array. Each element goes to one iteration. Elements can be strings, numbers, booleans, key-value objects or arbitrarily complex JSON. In the nested task's parameters, {{input}} is the current element, and {{input.<key>}} reads one field when the elements are objects. Passing [{"table": "sales"}, {"table": "orders"}] gives two iterations: {{input.table}} is sales in the first and orders in the second.

    Concurrency sets how many iterations run in parallel. It defaults to 1, so a loop you leave unchanged runs one iteration at a time. Raise it when iterations don't depend on each other, such as separate per-table copies, so the loop finishes sooner.

    You don't have to type the array in. It can also come from a task value computed by an earlier task, or from a job parameter. Each source has its own size limit.

    Ways to supply a For each task's input array, and their limits
    Input sourceWhat goes in the Inputs boxSize limit
    JSON array typed directlyA literal array such as [{"table": "sales"}, {"table": "orders"}]5,000 characters
    Task value from a preceding task{{tasks.<task_name>.values.<task_value_name>}}48 KB for the referenced value
    Job parameter{{job.parameters.<name>}}10,000 characters for the parameter value
    An upstream Python task sets the list that a For each task will loop overpython
    dbutils.jobs.taskValues.set(key = "countries", value = countries_array)

    If that task is named generate_countries_list, the For each task's Inputs box holds {{tasks.generate_countries_list.values.countries}}. The reference itself is short, but the list it points to can be up to 48 KB. Because the list-producing task runs before the For each task, the same disabled-task rule as If/else applies: a For each task fails if the upstream task that supplies its input is disabled.

    For data larger than 48 KB, pass keys instead of the data itself. Store the full configuration for every iteration in a JSON file. Loop over a short list such as ["table_1","table_2","table_3"], and map {{input}} to a notebook parameter named key. The nested notebook then looks up its own arguments in the file.

    Nested notebook that reads its iteration key and finds its step in the config filepython
    # look up step and arguments
    key = dbutils.widgets.get("key")
    current_step = next((step for step in config['steps'] if step['key'] == key), None)
    if current_step is None:
        raise ValueError(f"Could not find step '{key}' in the configuration")

    Tasks after the loop depend on the For each task, the top-level node. A downstream task cannot set the nested task as its dependency. Reading results works the other way round. To read what the iterations produced, use the nested task's key, not the For each task's key. You get one list ordered by iteration number. An iteration that did not set the key still keeps its place as a null, so if the second of three iterations set nothing, the result is [1, None, 3].

    Reading the value that each iteration of the nested task named process setpython
    results = dbutils.jobs.taskValues.get(taskKey = "process", key = "result")
    # results == [1, 2, 3]

    Reading the combined outputs of all iterations is in Beta. On classic compute it needs Databricks Runtime 15.4 LTS or above. On serverless, dbutils.jobs.taskValues.get needs environment version 5 or above, and earlier versions return only the first iteration's value. The dynamic reference {{tasks.process.values.result}} returns the full list on any compute. In the run view, a For each task's history appears as a table of iterations. The rest of the job runs and is managed like any other job.

    Checkpoint 8 of 8· Fill the gap

    A job has a For each task named process_tables. Its nested task, named process, sets a task value called result on each of three iterations. A summary task runs after the loop and needs every iteration's result as one list. Which task key completes the read in the summary notebook?

    results = dbutils.jobs.taskValues.get(taskKey = " ? ", key = "result")
    # results == [1, 2, 3]

    Sources789

    Sources

    (9)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “Databricks runs upstream tasks before running downstream tasks, running as many of them in parallel as possible.”
      ↩︎ From a task list to a flow with decisions
      “All succeeded: All dependencies have run and succeeded. This is the default setting.”
      ↩︎ Run if conditions and how upstream states are counted
      “All done: The task is run after all its dependencies have run, regardless of the status of the dependent runs.”
      ↩︎ Run if conditions and how upstream states are counted
      “At least one failed: At least one dependency failed. The task is marked as Excluded if the condition is unmet.”
      ↩︎ Run if conditions and how upstream states are counted
      “None failed: None of the dependencies failed, and at least one dependency was run.”
      ↩︎ Run if conditions and how upstream states are counted
      “Upstream tasks marked Upstream failed or Upstream canceled are treated as failed, not as skipped.”
      ↩︎ Run if conditions and how upstream states are counted
      “If all of a task's dependencies are excluded, the task itself is also excluded, regardless of its Run if condition.”
      ↩︎ Run if conditions and how upstream states are counted
      “the cancellation propagates through downstream tasks, and tasks with a Run if condition that handles failure are run”
      ↩︎ Run if conditions and how upstream states are counted
      “If an upstream task is disabled, downstream tasks are evaluated against their Run if condition and may also be disabled for that run.”
      ↩︎ Run if conditions and how upstream states are counted
      “For example, run transformation tasks only if the upstream ingestion task adds new data.”
      ↩︎ Branching with the If/else condition task
      “The Run if dependencies field allows you to add control flow logic to tasks based on other tasks' success, failure, or completion.”
      ↩︎ Key concept
      “Upstream tasks with the Excluded result state are treated as successful when evaluating Run if conditions.”
      ↩︎ Exam trap 1
      “Upstream tasks with the Excluded result state are treated as successful when evaluating Run if conditions.”
      ↩︎ Prediction
    2. 2.
      “you can also create branching flows that include conditional tasks, error correction, or cleanup.”
      ↩︎ From a task list to a flow with decisions
      “Disable a task to skip it at run time without removing it from the job.”
      ↩︎ From a task list to a flow with decisions
      “Retries specify how many times a particular task should be re-run if the task fails with an error message.”
      ↩︎ Retry policies for failed tasks
      “Some features on Databricks, such as schema evolution with Structured Streaming, assume that you run jobs with retries”
      ↩︎ Retry policies for failed tasks
      “You can define If/else conditions against taskValues, job parameters, or dynamic values.”
      ↩︎ Branching with the If/else condition task
      “You can use the If/else task type to specify conditionals based on some value.”
      ↩︎ Checkpoint
    3. 3.
      “For most configurations, the default setting does not retry any tasks on task failure.”
      ↩︎ Retry policies for failed tasks
      “The retry interval is calculated in milliseconds between the start of the failed run and the subsequent retry run.”
      ↩︎ Retry policies for failed tasks
      “If you configure both Timeout and Retries, the timeout applies to each retry.”
      ↩︎ Retry policies for failed tasks
      “If you configure both Timeout and Retries, the timeout applies to each retry.”
      ↩︎ Exam trap 2
      “For most configurations, the default setting does not retry any tasks on task failure.”
      ↩︎ Exam trap 3
    4. 4.
      “An optional policy to specify whether to retry the task when it times out. If not specified, defaults to false.”
      ↩︎ Retry policies for failed tasks
      “auto-optimization can retry a failed task in addition to the retries in your retry policy”
      ↩︎ Retry policies for failed tasks
    5. 5.
      “When consecutive failures of a continuous job exceed a threshold, Lakeflow Jobs uses exponential backoff to retry the job.”
      ↩︎ Retry policies for failed tasks
      “To cancel the active run, reset the retry period, and start a new job run, click Restart run.”
      ↩︎ Retry policies for failed tasks
    6. 6.
      “These tasks consist of a boolean operator and a pair of operands”
      ↩︎ Branching with the If/else condition task
      “The >, >=, <, and <= operators perform numeric comparisons of their operands.”
      ↩︎ Branching with the If/else condition task
      “Only numeric, string, and boolean values are allowed when referencing task values in an operand.”
      ↩︎ Branching with the If/else condition task
      “The Depends on field defaults to <task-name> (true) where <task-name> is the name of the If/else condition task.”
      ↩︎ Branching with the If/else condition task
      “An If/else condition task fails if the upstream task that provides its condition value is disabled.”
      ↩︎ Branching with the If/else condition task
      “The == and != operators perform string comparison of their operands. For example, 12.0 == 12 evaluates to false.”
      ↩︎ Exam trap 4
      “The == and != operators perform string comparison of their operands. For example, 12.0 == 12 evaluates to false.”
      ↩︎ Prediction
    7. 7.
      “You cannot add another For each task as the nested task.”
      ↩︎ Looping with the For each task
      “enter a Concurrency value for the task. The default value is 1.”
      ↩︎ Looping with the For each task
      “For example, if you pass [{"table": "sales"}, {"table": "orders"}], reference the field in the nested task with {{input.table}}.”
      ↩︎ Looping with the For each task
      “Parameters are limited to 5,000 characters in the UI directly, or 48 KB if you use task value references”
      ↩︎ Looping with the For each task
      “The For each task is the top-level task, and downstream tasks can specify it as a dependency.”
      ↩︎ Looping with the For each task
      “A For each task fails if the upstream task that provides its input values is disabled.”
      ↩︎ Looping with the For each task
      “A downstream task cannot set the nested task as a dependency directly”
      ↩︎ Exam trap 5
    8. 8.
      “pass in a lookup key (into the JSON data) through the task input instead of the complete data.”
      ↩︎ Looping with the For each task
    9. 9.
      “use the key of the nested task that runs inside the For each task, not the For each task itself.”
      ↩︎ Looping with the For each task
      “It is enabled by default and requires Databricks Runtime 15.4 LTS or above on classic compute.”
      ↩︎ Looping with the For each task
      “Dynamic value references return the full list on any compute.”
      ↩︎ Looping with the For each task

    Spotted a mistake, or was something unclear? Tell us.