Airflow Fundamentals: An Introduction

Apache Airflow is a transformative open-source platform that has become a cornerstone for data engineers looking to orchestrate complex workflows. Whether you’re scheduling ETL pipelines, managing machine learning tasks, or automating batch processes, Airflow allows you to define everything in Python code, offering unmatched control, flexibility, and scalability. This guide is part of the Airflow Fundamentals series on SparkCodeHub, where we’ll dive deep into Airflow’s architecture, core components, use cases, and more. If you’re working with big data, pairing Airflow with tools like Airflow with Apache Spark can take your pipelines to the next level. New to Airflow? This is your starting point—explore further with Airflow Concepts: DAGs, Tasks, and Workflows!


What is Apache Airflow?

Apache Airflow is an open-source tool designed to help you author, schedule, and monitor workflows programmatically using Python. It was originally developed by Airbnb in 2014 to tackle their growing data pipeline needs and was later donated to the Apache Software Foundation, where it’s evolved into a robust, community-driven project. At its core, Airflow organizes workflows as Directed Acyclic Graphs (DAGs)—a structure we’ll unpack in Introduction to DAGs in Airflow—to map out tasks and their dependencies in a clear, executable way. Unlike basic schedulers like cron, Airflow doesn’t just trigger tasks on a timer; it understands how tasks depend on each other and provides a powerful UI to track everything, which you can explore in Airflow Web UI Overview. It also integrates seamlessly with systems like Airflow with PostgreSQL, making it incredibly versatile.

Picture Airflow as the conductor of your data orchestra. It can schedule a task—like running a shell script with BashOperator—and ensure it only starts after its prerequisites are complete. Whether you’re pulling data from a database or processing it with Airflow with Apache Spark, Airflow keeps everything in sync. Ready to set it up? We’ll cover that in Installing Airflow (Local, Docker, Cloud).

Here’s a simple DAG to give you a taste:

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

def say_hello():
    print("Hello from Airflow!")

with DAG(
    dag_id="hello_airflow",
    start_date=datetime(2025, 1, 1),
    schedule_interval="@daily",
    catchup=False,
) as dag:
    hello_task = PythonOperator(
        task_id="hello_task",
        python_callable=say_hello,
    )

This code defines a DAG that runs daily, calling a Python function to print a message. It’s a small glimpse into how Airflow turns workflows into manageable code—learn more in Defining DAGs in Python.

Workflow as Code

Airflow stands out because it lets you write workflows in Python, which brings a ton of benefits. You can version your pipelines with tools like Git, meaning every change is tracked and reversible—just like software development. It also means you can test your workflows before they run, catching errors early, and share them with your team for collaboration. This code-driven approach makes Airflow a dream for developers who want full control over their data processes. Keeping your DAG files organized is crucial to making this work smoothly, and DAG File Structure Best Practices guides you through setting up a clean, efficient structure.

Directed Acyclic Graphs (DAGs)

Every workflow in Airflow is built as a Directed Acyclic Graph, or DAG. This is a graph where tasks are nodes and dependencies are arrows pointing one way—no looping back allowed. That “acyclic” part means your workflow won’t get stuck in an endless cycle; it flows logically from start to finish, like extracting data before transforming it. This structure ensures tasks run in the right order and makes it easier to spot where things might go wrong. You’ll define these DAGs in Python, and DAG Dependencies and Task Ordering shows you how to set up those relationships so your pipeline hums along perfectly.

Scalability

Airflow scales effortlessly, whether you’re running a small script or managing massive data workloads. It’s designed to grow with your needs, pairing beautifully with distributed systems like Airflow with Kubernetes Executor. With Kubernetes, you can spread tasks across multiple machines, tapping into as much computing power as you need. This makes Airflow a go-to for big data projects where you’re processing terabytes or running complex jobs—it won’t flinch no matter how much you throw at it.

Dynamic Scheduling

Scheduling in Airflow is a step up from cron’s rigid timers. You can set flexible intervals—like “@daily” for every day or “@hourly” for every hour—and even define custom schedules to match your exact needs. Plus, Airflow lets you backfill past runs, so if you start a DAG today, you can tell it to catch up on last week’s data. This dynamic approach gives you way more control than cron’s basic “run at midnight” setup—DAG Scheduling (Cron, Timetables) dives into all the ways you can make it work for you.

Extensibility

Airflow’s operators—like PythonOperator—are what make it so adaptable. These pre-built tools let you connect to almost anything: databases, cloud services, APIs, or even custom scripts you’ve cooked up. Need to run a Python function? There’s an operator for that. Want to execute a shell command? Another operator’s got you covered. And if the built-in ones don’t cut it, you can create your own, tailoring Airflow to fit your stack perfectly. It’s built to grow with whatever tech you’re using.

Here’s an example of task dependencies:

from airflow import DAG
from airflow.operators.dummy import DummyOperator
from datetime import datetime

with DAG(
    dag_id="dependency_demo",
    start_date=datetime(2025, 1, 1),
    schedule_interval="@daily",
) as dag:
    start = DummyOperator(task_id="start")
    end = DummyOperator(task_id="end")
    start >> end  # Start must finish before end begins

This DAG uses the >> operator to enforce order—start runs first, then end. You can refine this technique in Task Dependencies (set_upstream, set_downstream).


Explain Airflow Fundamentals

Now let’s get into the meat of Airflow—how it works, why it’s worth your time, and how to set it up. Airflow operates through a system of interconnected parts that work together like a well-oiled machine—get the full picture in Airflow Architecture (Scheduler, Webserver, Executor).

DAG Definition

Everything in Airflow starts with defining your workflow as a DAG in a Python script. These scripts live in a folder called dags—usually ~/airflow/dags—where you lay out your tasks, how they connect, and when they should run. You might write a DAG that says, “Run this every day at midnight,” spelling out each step like “fetch data,” “process it,” and “save it.” These scripts are your blueprints, and Airflow reads them to figure out what to do. Writing them is your first hands-on step—Defining DAGs in Python takes you through every part, from imports to task setup, so you can craft workflows that fit your needs perfectly.

Scheduler

The scheduler is the heartbeat of Airflow, running in the background to keep everything on track. It’s always watching your DAGs, checking their schedule_interval—like “@daily” or “0 0 * * *”—to decide when tasks need to start. If a DAG is set to run every day, the scheduler queues it up at the right time, ensuring nothing misses its slot. It’s a tireless worker, parsing your scripts constantly to stay ahead of the game. Without it, your workflows would just sit there—Introduction to Airflow Scheduling shows you how it manages timing and keeps your pipelines flowing.

Executor

The executor is the muscle that actually runs your tasks. Airflow offers different types to fit your setup: LocalExecutor handles tasks on your machine, using its CPU cores to process things in parallel if it can—like running four tasks at once on a four-core system. CeleryExecutor, on the other hand, spreads tasks across multiple worker machines, tapping into a message queue like RabbitMQ to coordinate. It’s more complex to set up but scales way bigger, perfect for heavy workloads. You pick the executor based on how much power you need—Airflow Executors (Sequential, Local, Celery) breaks down each option so you can choose wisely.

Task Execution

When the scheduler says “go,” the executor runs each task’s logic. A task might be a Python function—like one called by PythonOperator—that processes data, or a shell command—like one run with BashOperator—that kicks off a script. Tasks can do anything: pull data from an API, transform a file, or push results to a database. It’s where the real work happens, and you define exactly what each task does when you write your DAG. The executor makes sure it all gets done, whether it’s one task at a time or a dozen in parallel.

Webserver

Airflow’s webserver spins up a browser-based interface—typically at localhost:8080—where you can watch your workflows unfold. It’s your window into what’s happening: you’ll see which tasks succeeded, which failed, and get detailed logs with a click. Need to retry a task? You can do it right there. It’s not just pretty—it’s practical, giving you real-time insight into your pipelines. You’ll spend a lot of time here checking on things—Airflow Web UI Overview walks you through every feature so you can make the most of it.

Metadata Database

The metadata database is where Airflow keeps all its records—task states (running, failed, success), run history, and more. It uses a database like SQLite for small setups or PostgreSQL for bigger ones to store this info, so you can look back at what happened or recover if something crashes. This persistence is what lets Airflow pick up where it left off or show you that a task failed last Tuesday. Setting it up is a critical step—Airflow Metadata Database Setup guides you through picking a backend and getting it running smoothly.

Why Use Airflow?

Airflow solves problems that basic tools like cron can’t handle—see how it stacks up in Cron Expressions in Airflow. With cron, you set a time—like midnight—and it runs, no questions asked. But if one step fails, the next one still goes, which can wreck your pipeline. Airflow waits: if task A flops, task B holds off, saving you from chaos downstream. It’s also built to scale—hook it up with Airflow with Apache Spark and tackle massive datasets. Plus, you get a UI to see what’s happening—Monitoring Task Status in UI turns guesswork into clarity, making it a must-have for complex workflows.

Configuring Airflow Basics

Getting Airflow running is pretty straightforward—here’s how, with full details in Installing Airflow (Local, Docker, Cloud). Start by opening your terminal and running pip install apache-airflow to grab the core package. You’ll need Python 3.7 or higher, and if you want extras—like PostgreSQL support—use pip install apache-airflow[postgres]. This pulls in everything you need—tweak settings later with Airflow Configuration Options. After installing, run airflow db init to set up the metadata database. It defaults to SQLite, which is great for testing, but you might switch to PostgreSQL for production—Airflow Metadata Database Setup has all the steps.

Next, write your workflows as Python scripts and drop them in ~/airflow/dags. Airflow scans this folder for DAGs, so keep it organized—each file defines a workflow with tasks and schedules. DAG File Structure Best Practices helps you keep it clean. Then launch the webserver with airflow webserver -p 8080 to see the UI, and the scheduler with airflow scheduler to run tasks—use separate terminals or background them with &. You can control them via Airflow CLI: Overview and Usage. Finally, head to localhost:8080 to check the UI, or peek at logs in ~/airflow/logs to track progress—Task Logging and Monitoring dives into how to monitor everything.

Here’s the quick rundown:

pip install apache-airflow
airflow db init
airflow webserver -p 8080 & airflow scheduler

You’re live—visit localhost:8080 to see it in action!


Core Components of Airflow Fundamentals

Airflow’s power comes from its core components, each playing a vital role—get the full scoop in Airflow Concepts: DAGs, Tasks, and Workflows.

DAGs (Directed Acyclic Graphs)

DAGs are the foundation of every Airflow workflow. You define them in Python—like DAG(dag_id="my_workflow")—and they lay out your tasks and how they connect. “Directed” means tasks flow one way—say, from extract to transform—and “acyclic” means no loops, so you don’t get stuck circling back. This keeps your workflow predictable: extract runs, then transform, then load, no funny business. It’s all about clarity and order—learn how to set them up in Introduction to DAGs in Airflow and link tasks with DAG Dependencies and Task Ordering.

Tasks

Tasks are the individual jobs in your DAG—like running a script or querying a database. Each one gets a unique task_id—think “fetch_data” or “process_file”—and does one specific thing. You tie them together with dependencies so they run in sequence or parallel, depending on what you need. They’re the building blocks of your workflow, tracked in Task Instances and States and connected via Task Dependencies (set_upstream, set_downstream).

Operators

Operators are ready-made templates for tasks, saving you time and effort. Airflow offers a bunch—like PythonOperator for Python functions or BashOperator for shell commands. They handle common jobs out of the box: want to run a script? BashOperator’s got it. Need a Python function? PythonOperator’s your pick. And if you need something special, you can build your own—Custom Operator Development shows you how to extend Airflow to fit your needs.

Scheduler

The scheduler keeps your workflows on schedule, running behind the scenes to watch your DAGs. You set a schedule_interval—like “@daily” or “0 0 * * *”—and it parses your scripts, queues tasks, and makes sure they start on time. It’s always on, checking every DAG to see what’s due next. Without it, your tasks wouldn’t know when to run—Introduction to Airflow Scheduling explains how it works, and Reducing Scheduler Latency helps you keep it snappy.

Webserver and UI

The webserver fires up a browser-based interface—usually at localhost:8080—where you can see your DAGs in action. It shows task statuses (success, failed, running), lets you retry tasks, and pulls up logs with a click. It’s your control center, giving you a clear view of what’s going on and where things might’ve gone off track. You’ll use it a lot—Airflow Web UI Overview covers every feature, and Customizing Airflow Web UI lets you tweak it to your liking.

Metadata Database

The metadata database stores all your workflow history—task states, run dates, everything. It uses a database—SQLite for small setups, PostgreSQL for bigger ones—to log what happened, so you can review past runs or recover from a crash. This persistence is what keeps Airflow reliable: if a task fails, you can see why and fix it. Setting it up right is key—Airflow Metadata Database Setup walks you through it, and Database Performance in Airflow helps you keep it running fast.

Here’s an example tying them together:

from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

with DAG(
    dag_id="components_demo",
    start_date=datetime(2025, 1, 1),
    schedule_interval="@daily",
) as dag:
    task = BashOperator(
        task_id="say_hi",
        bash_command="echo 'Airflow rocks!'",
    )

This uses a DAG, task, and operator—dig into it with BashOperator.


Common Use Cases of Airflow

Airflow shines in real-world scenarios—check out more in Airflow Use Cases and Examples.

ETL Pipeline Orchestration

Airflow is a master at managing extract, transform, load (ETL) jobs. You can schedule a task to pull data from a source—like a CSV or database—transform it with some logic, and load it into a warehouse. It makes sure each step happens in order: extract first, then transform, then load, no skipping ahead. This is perfect for daily data syncs, keeping your warehouse fresh—use operators like PostgresOperator to connect to databases and see how it’s done in ETL Pipelines with Airflow.

Example:

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

def etl_process():
    print("Extracting, transforming, loading...")

with DAG(
    dag_id="etl_demo",
    start_date=datetime(2025, 1, 1),
    schedule_interval="@daily",
) as dag:
    etl = PythonOperator(
        task_id="etl_task",
        python_callable=etl_process,
    )

Machine Learning Workflow Management

Airflow excels at orchestrating machine learning pipelines, linking steps like data prep, model training, and deployment. You might fetch data, clean it up, train a model with SparkSubmitOperator, and push it live—all on a weekly schedule. Each task waits for the last one to finish, keeping your ML process tight and organized—Machine Learning Pipelines in Airflow walks you through setting it up.

Example:

from airflow import DAG
from airflow.operators.dummy import DummyOperator
from datetime import datetime

with DAG(
    dag_id="ml_demo",
    start_date=datetime(2025, 1, 1),
    schedule_interval="@weekly",
) as dag:
    start = DummyOperator(task_id="start")
    train = DummyOperator(task_id="train")
    deploy = DummyOperator(task_id="deploy")
    start >> train >> deploy

Batch Data Processing

For jobs that need to run on a schedule—like crunching logs or aggregating data—Airflow’s your tool. It can fire off a script every night to process server logs, ensuring it runs smoothly and on time. You might use BashOperator to call a Python script or a Spark job, handling big batches with ease—Batch Processing Workflows has examples to get you started.

Example:

from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

with DAG(
    dag_id="batch_demo",
    start_date=datetime(2025, 1, 1),
    schedule_interval="@daily",
) as dag:
    log_task = BashOperator(
        task_id="log_task",
        bash_command="echo 'Processing logs'",
    )

FAQ: Answers to Common Airflow Fundamentals Questions

Here’s a deep dive into questions people ask online (e.g., Stack Overflow, Reddit, Airflow docs):

  • How does Airflow differ from cron? Airflow goes way beyond cron by managing task dependencies and giving you a UI. With cron, you might set 0 0 * * * to run a script at midnight, but if it fails, the next job runs anyway—no coordination. Airflow says, “Wait—task A needs to finish before task B starts,” using stuff like task1 >> task2. Plus, you can watch it all in Airflow Web UI Overview—something cron can’t touch. Cron’s fine for simple stuff, but Airflow’s built for pipelines—check cron syntax in Cron Expressions in Airflow.
  • Why use DAGs in Airflow? DAGs give your workflow a clear roadmap—no guesswork, no loops. Without them, tasks might run out of order—like transforming data before extracting it—or get stuck cycling forever. In Airflow, a DAG (e.g., DAG(dag_id="my_job")) says, “Extract, then transform, then load,” and that’s final. This structure makes debugging easy and keeps things logical—master it with Introduction to DAGs in Airflow and DAG Dependencies and Task Ordering.
  • Can Airflow integrate with Spark? Yes—Airflow and Apache Spark are a dream team. You can use SparkSubmitOperator in a DAG to kick off Spark jobs—like processing data or training models. Airflow schedules and monitors, while Spark crunches the numbers. Say you’ve got a daily Spark job: Airflow queues it, tracks it, and logs the results—a perfect combo for big data. Set it up with Airflow with Apache Spark.
  • How do I install Airflow locally? Installing Airflow locally is simple. Run pip install apache-airflow in a Python 3.7+ environment—use a virtualenv to keep it tidy. Then, airflow db init sets up the SQLite database (or swap to PostgreSQL later). Finally, start the webserver (airflow webserver -p 8080) and scheduler (airflow scheduler)—two terminals or background with &. Want PostgreSQL? Add pip install apache-airflow[postgres]. You’re ready—full steps, including Docker, are in Installing Airflow (Local, Docker, Cloud).
  • What’s the difference between LocalExecutor and CeleryExecutor? LocalExecutor runs tasks on your machine, using its CPU cores—say, four tasks at once with four cores. It’s a breeze to set up: tweak airflow.cfg and go. CeleryExecutor spreads tasks across multiple worker machines, using a message queue like RabbitMQ. It’s trickier—needs a broker and workers—but scales huge. Use Local for small jobs, Celery for big ones—compare them in Airflow Executors (Sequential, Local, Celery).
  • Why is my DAG not showing up in the UI? If your DAG’s AWOL, check a few things. Is the file in ~/airflow/dags? Airflow only looks there—fix it with DAG File Structure Best Practices. Any syntax errors—like a typo or missing import? Test it with Python. Is the scheduler running? Use airflow scheduler or check with Airflow CLI: Overview and Usage. Restart the webserver if it’s still hiding.
  • How do I retry a failed task automatically? Add retries to your operator—like PythonOperator(..., retries=3, retry_delay=timedelta(minutes=5)). If it fails, Airflow tries again—three times here, waiting 5 minutes each time. It’s perfect for flaky stuff like network hiccups. Tweak retries or delay per task—set it up with Task Retries and Retry Delays.
  • Can Airflow handle real-time workflows? Airflow’s made for batch, not real-time. It schedules tasks at intervals—say, every minute—but can’t hit sub-second speeds like Apache Kafka. For near-real-time, use short schedules, but true streaming needs another tool. Airflow can still join the party—check Real-Time Data Processing and Airflow with Apache Kafka.

Airflow vs Other Tools

Airflow’s all about orchestration—scheduling and monitoring—while tools like Apache Spark (via Airflow with Apache Spark) handle the heavy lifting. Spark crunches data; Airflow tells it when and watches it go. Together, they’re a powerhouse—boost them with Airflow Performance Tuning.


Conclusion

Airflow’s fundamentals—unpacked in this Airflow Fundamentals guide—offer a scalable, code-driven way to manage workflows. From crafting DAGs in Defining DAGs in Python to tracking runs in Monitoring Task Status in UI, it’s a game-changer. Want more? Dive into Airflow Concepts: DAGs, Tasks, and Workflows!