0
0
Apache Airflowdevops~10 mins

DAG versioning strategies in Apache Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - DAG versioning strategies
Create DAG code
Save DAG file in repo
Commit changes with version tag
Deploy DAG to Airflow environment
Airflow scheduler loads DAG
Run DAG with specific version
Monitor and maintain versions
Update DAG code for new version
Back to Save DAG file
This flow shows how DAG code is created, versioned, deployed, and run in Airflow, with updates leading to new versions.
Execution Sample
Apache Airflow
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from datetime import datetime

def create_dag(version):
    dag = DAG(
        dag_id=f"example_dag_v{version}",
        start_date=datetime(2023, 1, 1),
        schedule_interval='@daily'
    )
    start = DummyOperator(task_id='start', dag=dag)
    return dag

dag_v1 = create_dag(1)
dag_v2 = create_dag(2)
Defines two DAG versions with different version numbers in their IDs.
Process Table
StepActionDAG ID CreatedVersion UsedResult
1Call create_dag(1)example_dag_v11DAG object for version 1 created
2Call create_dag(2)example_dag_v22DAG object for version 2 created
3Deploy DAGs to Airflowexample_dag_v1, example_dag_v21 and 2Both DAG versions available in Airflow
4Run DAG example_dag_v1example_dag_v11Runs tasks of version 1 DAG
5Run DAG example_dag_v2example_dag_v22Runs tasks of version 2 DAG
6Update DAG code to version 3example_dag_v33New DAG version created and deployed
7Run DAG example_dag_v3example_dag_v33Runs tasks of version 3 DAG
💡 Execution stops after running the latest DAG version (3) and all versions are deployed.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 6Final
dagNoneexample_dag_v1example_dag_v2example_dag_v3example_dag_v3
versionNone1233
Key Moments - 3 Insights
Why do we create separate DAG IDs for each version instead of overwriting the same DAG?
Each DAG version has a unique ID (see execution_table steps 1 and 2) so Airflow can track and run multiple versions independently without conflicts.
How does Airflow know which DAG version to run?
Airflow runs the DAG by its unique ID (execution_table steps 4 and 5), so specifying the version in the DAG ID directs Airflow to the correct version.
What happens when we update the DAG code to a new version?
A new DAG object with a new version ID is created and deployed (step 6), allowing Airflow to run the updated workflow alongside older versions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the DAG ID created at step 2?
Aexample_dag_v2
Bexample_dag_v3
Cexample_dag_v1
Dexample_dag
💡 Hint
Check the 'DAG ID Created' column at step 2 in the execution_table.
At which step does the new DAG version 3 get deployed?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the step where 'Update DAG code to version 3' happens in the execution_table.
If we did not use version numbers in DAG IDs, what would happen?
AAirflow would run all versions separately.
BOlder DAG versions would be overwritten and lost.
CAirflow would create duplicate DAGs automatically.
DNo effect; Airflow handles versions internally.
💡 Hint
Refer to key_moments about unique DAG IDs and versioning.
Concept Snapshot
DAG Versioning Strategies in Airflow:
- Use unique DAG IDs with version numbers (e.g., example_dag_v1)
- Create and deploy each version as a separate DAG file
- Airflow runs DAGs by their IDs, enabling multiple versions
- Update DAG code by creating new versioned DAGs
- Maintain old versions for history or rollback
Full Transcript
This visual execution shows how to manage DAG versions in Airflow by creating separate DAG IDs with version numbers. We start by defining DAGs with different version numbers, deploy them, and run each version independently. Updating the DAG means creating a new version with a new ID. This approach prevents overwriting and allows running multiple versions side by side. Key points include unique DAG IDs, deployment steps, and running specific versions. The quiz tests understanding of DAG IDs, deployment steps, and consequences of not versioning.