Software Architecture/Engineering  

Apache Airflow Tutorial: Building Production Data Pipelines

Introduction

Modern organizations rely heavily on data for analytics, reporting, machine learning, and business decision-making. As data volumes grow, manually managing data workflows becomes increasingly difficult. Teams need a reliable way to schedule, monitor, and automate data processing tasks across multiple systems.

This is where Apache Airflow comes in.

Apache Airflow is one of the most popular open-source workflow orchestration platforms used for building, scheduling, and monitoring data pipelines. It allows developers and data engineers to define workflows as code, making them easier to maintain, version control, and scale.

Whether you're moving data between systems, running ETL processes, training machine learning models, or generating reports, Apache Airflow provides a powerful framework for orchestrating complex workflows.

In this tutorial, you'll learn how Apache Airflow works, understand its architecture, and build your first production-ready data pipeline.

What Is Apache Airflow?

Apache Airflow is an open-source workflow orchestration platform designed to programmatically author, schedule, and monitor workflows.

Airflow represents workflows as Directed Acyclic Graphs (DAGs).

A DAG defines:

  • Tasks

  • Dependencies

  • Execution order

  • Scheduling rules

Example:

Extract Data
      │
      ▼
Transform Data
      │
      ▼
Load Data

Each step is executed in sequence based on defined dependencies.

Airflow automates the execution and monitoring of these tasks.

Why Organizations Use Apache Airflow

Without workflow orchestration:

Data Sources
      │
      ▼
Manual Scripts
      │
      ▼
Reports

Challenges include:

  • Manual execution

  • Limited monitoring

  • Failure handling issues

  • Lack of visibility

  • Difficult scheduling

With Airflow:

Data Sources
      │
      ▼
Apache Airflow
      │
      ▼
Data Pipeline
      │
      ▼
Analytics Platform

Benefits include:

  • Automation

  • Scheduling

  • Monitoring

  • Retry handling

  • Scalability

Understanding Apache Airflow Architecture

Airflow consists of several core components.

Web Server

Provides the user interface.

Features include:

  • Workflow monitoring

  • Task status tracking

  • DAG management

  • Execution history

Scheduler

The scheduler determines when tasks should run.

Responsibilities include:

  • DAG execution

  • Task scheduling

  • Dependency management

Metadata Database

Stores:

  • DAG information

  • Task history

  • Execution states

  • User settings

Common databases include:

  • PostgreSQL

  • MySQL

Workers

Workers execute tasks.

Scheduler
    │
    ▼
 Workers
    │
    ▼
 Tasks

Multiple workers can process jobs simultaneously.

What Is a DAG?

A Directed Acyclic Graph (DAG) is the fundamental building block of Airflow.

Example:

Download Data
       │
       ▼
Clean Data
       │
       ▼
Generate Report

Characteristics:

  • Directed

  • Ordered

  • No circular dependencies

Each node represents a task.

Installing Apache Airflow

Install Airflow using pip:

pip install apache-airflow

Initialize the database:

airflow db init

Create an administrator account:

airflow users create \
  --username admin \
  --firstname Admin \
  --lastname User \
  --role Admin \
  --email [email protected]

Start the scheduler:

airflow scheduler

Start the web server:

airflow webserver

Open the Airflow dashboard:

http://localhost:8080

Creating Your First DAG

Create a file inside the DAGs folder.

Example:

from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime

with DAG(
    dag_id="sample_pipeline",
    start_date=datetime(2025, 1, 1),
    schedule="@daily",
    catchup=False
) as dag:

    def hello():
        print("Hello Airflow")

    task = PythonOperator(
        task_id="hello_task",
        python_callable=hello
    )

Airflow automatically discovers the DAG.

Understanding Tasks

Tasks represent individual units of work.

Examples include:

  • Data extraction

  • Data transformation

  • File processing

  • API calls

  • Database updates

Example:

def extract_data():
    print("Extracting data")

Each task executes independently.

Task Dependencies

Airflow allows defining execution order.

Example:

extract >> transform >> load

Visualization:

Extract
   │
   ▼
Transform
   │
   ▼
Load

Dependencies ensure tasks execute in the correct sequence.

Building a Simple ETL Pipeline

Let's create a basic ETL workflow.

Extract Task

def extract():
    print("Reading source data")

Transform Task

def transform():
    print("Transforming records")

Load Task

def load():
    print("Loading into warehouse")

Define tasks:

extract_task = PythonOperator(
    task_id="extract",
    python_callable=extract
)

transform_task = PythonOperator(
    task_id="transform",
    python_callable=transform
)

load_task = PythonOperator(
    task_id="load",
    python_callable=load
)

Set dependencies:

extract_task >> transform_task >> load_task

This creates a complete ETL pipeline.

Scheduling Pipelines

Airflow supports flexible scheduling.

Daily:

schedule="@daily"

Hourly:

schedule="@hourly"

Weekly:

schedule="@weekly"

Custom cron schedule:

schedule="0 2 * * *"

This runs every day at 2 AM.

Retry Handling

Failures are inevitable in production systems.

Airflow supports automatic retries.

Example:

from datetime import timedelta

default_args = {
    "retries": 3,
    "retry_delay": timedelta(minutes=5)
}

Benefits:

  • Reduced manual intervention

  • Improved reliability

  • Automatic recovery

Monitoring Pipelines

The Airflow UI provides visibility into:

  • Running tasks

  • Failed tasks

  • Historical runs

  • Task durations

  • Retry attempts

Pipeline monitoring helps teams quickly identify issues.

Example dashboard view:

DAG
 │
 ├── Success
 ├── Running
 └── Failed

This visibility is one of Airflow's strongest features.

Working with External Systems

Airflow integrates with numerous platforms.

Common integrations include:

  • AWS

  • Azure

  • Google Cloud

  • Snowflake

  • PostgreSQL

  • MySQL

  • Databricks

  • Apache Spark

  • Kubernetes

Example PostgreSQL task:

from airflow.providers.postgres.operators.postgres import PostgresOperator

This allows workflows to interact with external systems seamlessly.

Production Use Cases

Apache Airflow is widely used for:

Data Warehousing

Automating ETL and ELT pipelines.

Machine Learning

Training and deploying ML models.

Business Reporting

Generating scheduled reports.

Data Migration

Moving data across platforms.

Cloud Automation

Managing infrastructure workflows.

Data Lake Processing

Coordinating large-scale data transformations.

Airflow vs Traditional Cron Jobs

FeatureCron JobsApache Airflow
SchedulingYesYes
Dependency ManagementNoYes
MonitoringLimitedExtensive
Retry HandlingManualBuilt-In
Workflow VisualizationNoYes
ScalabilityLimitedHigh
Pipeline ManagementDifficultEasy

Airflow provides significantly more capabilities for enterprise workflows.

Best Practices

Keep Tasks Small

Each task should perform one specific responsibility.

Use Version Control

Store DAGs in Git repositories.

Implement Retries

Always configure retry policies.

Monitor Pipeline Health

Use Airflow dashboards regularly.

Secure Credentials

Store secrets using secure connection managers.

Avoid Complex DAG Logic

Keep workflows readable and maintainable.

Use Modular Design

Break large workflows into smaller reusable components.

Conclusion

Apache Airflow has become the industry standard for workflow orchestration and data pipeline automation. By allowing developers to define workflows as code, Airflow provides flexibility, scalability, and reliability for modern data engineering workloads.

Whether you're building ETL pipelines, machine learning workflows, reporting systems, or cloud automation processes, Apache Airflow offers a powerful platform for scheduling, monitoring, and managing complex workflows. Its rich ecosystem, extensive integrations, and production-ready architecture make it an essential tool for organizations building modern data platforms.