Skip to content
Back to log

Workflow DAGs: Tasks and dependencies

Chainabit's Chao · · 9 min read

In short

Workflow DAGs define the order of tasks and the dependencies between them. Explore execution conditions and limits through Airflow and Argo Workflows examples.

Defining tasks and dependencies

Apache Airflow represents a workflow as a Dag. A Dag brings together the information needed for execution: tasks, the dependencies between tasks, and when the workflow runs are among them. Callback actions to perform when the workflow completes and other run parameters can also be part of a Dag.

The term Dag used by Airflow comes from the mathematical concept of a directed acyclic graph. In Airflow, the meaning of the term is not limited to the data structure. A Dag also covers the order and the conditions under which tasks are executed. Tasks, in turn, are separate units of work that run on a worker.

A task can have upstream tasks that come before it and downstream tasks that depend on it. In Airflow, these relationships form the Dag structure. By default, a task waits until all of the upstream tasks it depends on have succeeded. Different execution conditions can be defined with trigger_rule.

In Airflow, tasks usually take the form of an Operator, a Sensor, or a TaskFlow function. An Operator is a predefined task. A Sensor is a special type of Operator that waits for an external event to occur. A TaskFlow function defined with @task packages a custom Python function as a task.

Expressing dependencies

In Airflow, individual task dependencies are expressed with the >> and << operators or with the set_upstream and set_downstream methods. Multiple tasks can be defined after a task. This way, paths that depend on the same task are shown separately in the Dag.

To make a list of tasks depend on the tasks in another list, cross_downstream is used. To chain tasks together, chain is used. Airflow also notes that chain can set up pairwise dependencies between lists of the same length. These pairwise relationships differ from the cross dependencies created with cross_downstream.

In Argo Workflows, a dag template defines tasks as a dependency graph. For each task, the other tasks that must complete before it starts are specified. Tasks with no dependencies run immediately. In Argo's example, A runs first; when A completes, B and C run in parallel. D runs after both B and C have completed.

In this example, dependencies are kept in the dependencies field. The field value is [A] for B and C, and [B, C] for D. The template each task uses is specified with the template field. Task names are given in the name field.

In Argo, the steps template also organizes the execution of tasks. The structure consists of nested lists: the outer lists run sequentially, and the steps in the inner lists run in parallel. when can be used to run a step conditionally. A Synchronization feature is available for running the steps in the inner lists sequentially.

The scope of a run

In Airflow, a Dag can be triggered manually or through the API. If a schedule is defined in the Dag, it can also run according to that schedule. Defining a schedule for a Dag is not mandatory. Each run creates a separate Dag instance, which Airflow calls a Dag Run.

Dag Runs of the same Dag can run in parallel. Each Dag Run has a data interval value. This interval indicates the data period the tasks will work on. When a Dag Run is created, Task Instances are also created for the tasks in the Dag.

For a Dag to run, each Operator or Task must be assigned to a Dag. Airflow can compute this assignment when an Operator is defined inside a with DAG block or inside a @dag decorator. The assignment can also be computed when an Operator is placed on the upstream or downstream side of another Operator that is already assigned to a Dag. In other cases, the Dag is passed to the Operator with dag=.

When creating a Dag in Airflow, default_args can be provided. These arguments are automatically applied to the Operators in the Dag. For example, the retries value can be defined in these shared arguments instead of being specified separately for each Operator.

When loading Dags from Python files, Airflow picks up the top-level Dag objects in the file. A Dag object created inside a function and not present at the top level is not loaded. For the Dag produced by a function defined with @dag to be loaded, the function is called and the result is assigned to a top-level object.

In Argo Workflows, a Workflow both defines the workflow to be executed and holds the workflow's state. The execution definition is in the Workflow.spec field. At the core of this structure are a templates list and an entrypoint. The entrypoint determines the first template to run.

A Container template in Argo schedules the running of a container. A Script template adds the source field to the Container structure; the script defined in this field is saved to a file and executed. In both template types, standard output is passed to an Argo variable.

Branching and task conditions

In Airflow, branching makes it possible to follow one or more paths instead of running all of the dependent tasks. A function marked with @task.branch returns a task ID or a list of task IDs. The specified paths are followed and the others are skipped. If the function returns None, all downstream tasks are skipped.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

The task_id returned by the function must correspond to a task directly on the downstream side of the branching task. Airflow also notes that XComs can be used with @task.branch. This way, information coming from upstream tasks can be used to determine which branch to follow.

A task that is on the downstream side of both the branching task and one of the selected tasks is not skipped. In Airflow's example, join runs even though it is not returned in the branching decision, because it is on the downstream side of the selected branch_a task.

In a task where paths merge after branching, the effect of skipped tasks matters. In Airflow, the all_success and all_failed rules propagate the skipped state to downstream tasks. In one Airflow example, the join task depends on both the selected path and the skipped path. Under the default all_success rule, join is also skipped. In the same example, the intended behavior is achieved by setting none_failed_min_one_success for join.

Among the values of trigger_rule, all_success requires all upstream tasks to have succeeded and is the default rule. all_failed requires all upstream tasks to be in the failed or upstream_failed state. all_done is based on all upstream tasks having finished their execution.

one_failed is based on at least one upstream task having failed; one_success is based on at least one having succeeded. These two rules do not wait for all upstream tasks to finish. one_done is based on at least one upstream task having either succeeded or failed.

Under the none_failed rule, no upstream task is in the failed or upstream_failed state; tasks have either succeeded or been skipped. none_failed_min_one_success additionally requires at least one upstream task to have succeeded. all_skipped requires all upstream tasks to be in the skipped state. always allows the task to run at any time without a dependency condition.

In Airflow, a task can also be made to depend on the success of the same task's run in the previous Dag Run. To do this, the depends_on_past argument on the task is set to True. In the first automatic run of the Dag, since there is no previous run, the task still runs.

Data and visibility between tasks

Airflow uses XComs to pass small pieces of metadata between tasks. Tasks can perform push and pull operations on this information. The TaskFlow API passes data between tasks through implicit XComs. For large files, Airflow also describes the approach of uploading to a storage service and downloading from it.

Airflow submits tasks to run as capacity becomes available on workers. It is not guaranteed that all tasks in the same Dag will run on the same worker or the same machine. In the Airflow interface, the status of Dags and tasks can be viewed, Dag Runs can be triggered, and logs can be inspected.

In the Graph view, a TaskGroup organizes tasks into hierarchical groups. Tasks inside a TaskGroup remain on the same Dag; they follow the Dag settings and pool configurations. A dependency relationship can be applied to all of the tasks inside a TaskGroup with the >> and << operators.

TaskGroup also supports default_args, and values at the group level override the Dag-level default_args values. In Airflow's example, the retries value defined for the group is applied when no separate value is specified on the task. When the task's own retries value is specified, that value is used.

In Airflow's Graph view, labels can also be added to the dependency edges between tasks. Labels can be used with the >> and << operators or with the set_upstream and set_downstream methods. In branching sections, these labels can show under what condition a path may run.

Limits and different execution models

An Airflow Dag is concerned not with what happens inside tasks but with how they are executed. Details such as ordering, the number of retries, and timeouts fall within this execution scope. A dependency between tasks does not by itself make the tasks run on the same machine.

In Dagster, GraphDefinition creates a DAG by connecting multiple op units. An op is a unit that performs computational work, and it must be inside a graph to be run. For the graph to be executed, it must also be part of a job. Dagster notes that when assets are used, it is not necessary to use graphs directly.

Dagster defines an asset as a logical unit of data, such as a table, a dataset, or a machine learning model. Assets can depend on other assets, and these relationships form the lineage structure of the data pipeline. AutomationCondition defines when an asset or asset check is executed, based on the state of the asset and its upstream dependencies.

In Dagster, IOManager defines how data is stored and retrieved between the executions of assets and ops. An executor determines how the steps in a job run are executed. Every job has a default executor; a custom executor can also be specified for a job or a code location.

Ready to start?

Download the app and continue from your phone.

End of Entry