Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 50

Understanding Airflow's Core Concepts: DAGs, Operators, and Tasks

This lesson is built with resources from dataquest.io and airflow.apache.org.

Hello! Welcome back to our module on Data Pipeline Orchestration.

In our last lesson, we explored why orchestration is a critical component of any modern data architecture. We established that it's the control plane that brings reliability, scalability, and observability to our data workflows, moving beyond simple scheduling to manage complex dependencies. You learned that the blueprint for these workflows is often a Directed Acyclic Graph (DAG).

Today, we'll dive into the how by examining the core concepts of Apache Airflow, the industry-standard tool for orchestration. As a software architect, you're used to breaking down complex systems into their fundamental components. We'll do the same for Airflow, giving you the vocabulary and mental model needed to design and understand data pipelines.

Your learning outcome is to explain the core concepts of an orchestration tool like Apache Airflow (DAG, operator, task).

1. The Anatomy of an Airflow Workflow

Apache Airflow is an open-source platform to programmatically author, schedule, and monitor workflows. Its key strength is that workflows are defined as code (specifically, in Python), which allows for dynamic, extensible, and versionable data pipelines.

To begin, let's get a high-level overview of Airflow's main components.

Introduction to Apache Airflow

This article from Dataquest provides an excellent introduction to Airflow's architecture and foundational components. It will ground our discussion in a practical context.

Please read the introduction ('What Workflow Orchestration Is...') and the main section 'Core Airflow Concepts'. Focus on understanding the roles of the DAG, Task, Scheduler, and Executor. Don't worry about mastering the code details just yet; we'll break those down next.

From that reading, you can see that an Airflow system has several moving parts. For this lesson, we will focus on the three concepts you use to define a workflow: DAGs, Tasks, and Operators.

Introduction to Apache Airflow Concepts
This image provides a visual summary of Airflow's key building blocks: the DAG which contains tasks, and Operators which provide the logic for those tasks.

2. The DAG: Your Workflow's Blueprint

As we mentioned, the central concept in Airflow is the Directed Acyclic Graph (DAG). Let's break that down:

  • Graph: It's a structure of nodes (tasks) and edges (dependencies).
  • Directed: The dependencies have a specific direction. Data and control flow from one task to another.
  • Acyclic: The graph cannot have loops. A task cannot have a dependency that eventually leads back to itself. This ensures that your workflow has a clear beginning and end and won't run forever.

In essence, the DAG is a Python script that defines the structure of your pipeline: what tasks to run and in what order. It doesn't perform any data processing itself; it's purely a configuration file that acts as the architectural blueprint for your workflow.

Data Pipeline as a Directed Acyclic Graph (DAG)
This diagram illustrates a simple DAG. The 'Python DAG File' defines four tasks and their dependencies. Task 2 and Task 3 can only run after Task 1 completes, and Task 4 waits for both Task 2 and Task 3 to finish.

3. Tasks and Operators: The Building Blocks

If the DAG is the blueprint, Tasks and Operators are the components specified in that blueprint.

  • Task: A task is a single unit of work in your pipeline. In the diagram above, "Task-1" and "Task-2" are tasks. Each node in the DAG represents one task.

  • Operator: An Operator is a pre-built Python class that acts as a template for a task. It defines the actual logic or action to be performed. When you create a task, you are creating an instance of an operator.

This is analogous to object-oriented programming: an Operator is like a class, and a Task is an object (an instance of that class).

Airflow comes with a rich library of operators. Some of the most common ones are:

  • BashOperator: Executes a bash command.
  • PythonOperator: Calls an arbitrary Python function.
  • DatabricksSubmitRunOperator: Submits a job to a Databricks cluster.
  • SqliteOperator: Executes SQL commands against a SQLite database.

By instantiating these operators, you create the tasks that form your DAG.

To see how these concepts are expressed in code, let's turn to the official Airflow tutorial.

Airflow 101: Building Your First Workflow

The official Airflow documentation provides a clear, code-driven walkthrough of how to define a workflow. This will show you how the concepts of DAG, Operator, and Task translate into a Python script.

Please read the following sections: 'What is a Dag?', 'Understanding Operators', 'Defining Tasks', and 'Setting up Dependencies'. Pay close attention to: How an Operator is a template (like BashOperator). How a Task is a specific instance of an Operator, given a unique task_id. How the bit-shift operators (>> and <<) are used to define dependencies between tasks.

TaskFlow API: A More Pythonic Approach

The tutorial you just read uses Airflow's "classic" syntax, where you explicitly instantiate operator classes (e.g., t1 = BashOperator(...)).

More recent versions of Airflow introduced the TaskFlow API, which provides a more intuitive, "Pythonic" way to author DAGs using decorators. You saw this in the first reading from Dataquest.

With the TaskFlow API:

  • You define a DAG by adding a @dag decorator to a function.
  • You define a Python-based task by adding a @task decorator to a function. This implicitly creates a PythonOperator behind the scenes.
  • Dependencies are set simply by calling the task functions in the desired order.

Here’s a comparison:

Classic Syntax:

from airflow.operators.python import PythonOperator

def my_python_function():
    print("Hello from task!")

with DAG(...) as dag:
    python_task = PythonOperator(
        task_id='python_task',
        python_callable=my_python_function
    )

TaskFlow API Syntax:

from airflow.decorators import dag, task

@dag(...)
def my_dag():
    @task
    def my_python_function():
        print("Hello from task!")
    
    my_python_function()

my_dag()

As an architect, it's valuable to recognize both patterns. The TaskFlow API is the modern standard for new Python-based workflows, while the classic syntax is still widely used, especially for non-Python operators or in legacy codebases.

Test your understanding!

Imagine you need to build a simple data pipeline that does the following:

  1. Downloads a CSV file from a web server.
  2. After the download is complete, validates that the CSV has the correct number of columns.
  3. Also after the download is complete, archives a copy of the raw file to a backup location.

How would you describe this workflow using the concepts of DAG, Task, and Operator? What would the dependencies look like?

Show answer
  • DAG: The entire workflow would be defined as a single DAG.
  • Tasks: There would be three tasks:
    1. download_file
    2. validate_columns
    3. archive_raw_file
  • Operators: Each task would be an instance of an operator.
    • download_file might use a BashOperator to run a curl or wget command.
    • validate_columns would likely use a PythonOperator (or a @task decorator) to execute a Python function that reads the CSV and checks its structure.
    • archive_raw_file could use another BashOperator to run a cp or mv command.
  • Dependencies: The dependency graph would look like this:
    • download_file >> [validate_columns, archive_raw_file]
      This means that validate_columns and archive_raw_file both depend on download_file completing successfully. They can run in parallel with each other after the download is finished.

Conclusion

In this lesson, we dissected the core components used to define a workflow in Apache Airflow. By understanding these fundamentals, you can now read an Airflow DAG file and understand its structure and intent.

Key Takeaways:

  • DAG (Directed Acyclic Graph): The architectural blueprint of your workflow, defined as a Python script. It outlines all the tasks and their dependencies.
  • Operator: A reusable template that defines a type of work (e.g., run a script, execute a SQL query). Airflow provides many pre-built operators.
  • Task: A specific instance of an operator within a DAG. It is a single, executable step in the workflow.
  • Workflows are defined by creating tasks (instantiating operators) and setting dependencies between them within a DAG file.

You now have the foundational knowledge of Airflow's building blocks. In the next lesson, you'll put this theory into practice as we move to the next learning outcome: Create a simple Directed Acyclic Graph (DAG) to define a data workflow. We'll set up a minimal Airflow environment and write our very first DAG file from scratch.