Since I started creating courses a year ago, I got so many messages asking me what are the best practices in Apache Airflow. As engineer, we always seek for the best ways to apply what we learn while being constantly improving ourselves. In this series of tutorial, I would like to share with you everything I learned so far to really make Airflow shine in your data ecosystem. You will learn how to create better DAGs, how to optimise Airflow, what you should care about and much more. If you want to start mastering Airflow, you should definitely take a look my course right here: Apache Airflow: The Hands-On Guide.
PS: This tutorial will evolve in time so don’t forget to check it times to times or to subscribe to my mailing list at the bottom of the page so that you stay up to date.
DAGs correspond to your data pipelines. Following best practices with your DAGs is crucial as they will be daily used. They should be optimized, easy to understand, documented and well organized. You can quickly end up with hundreds of DAGs so don’t underestimate this part. It will save you a lot of pain and troubles. Let’s get started.
Define the clear purpose of your DAG
Before creating a DAG, you should think carefully about what do you expect from it. Here are some questions that can help:
- What is the input?
- What is the output?
- When should it be triggered?
- At which interval of time?
- What are third parties tools it will interact with?
- What will be the tasks? (very important)
- Can make it simpler?
The last question is important. Always ask yourself if you really need to build a DAG to achieve what you want or is there any other solution that could be easier to build and maintain? Do not overcomplicate your data pipeline. A DAG should have a clear purpose like exporting data into your data warehouse or updating your machine learning model in production when needed.
Defining a clear purpose for your tasks
Have a clear idea of tasks you want to execute through your DAG. Try as much as possible to have one task = one job and not one task = multiple jobs. Let me give you an example.
As shown in the example above, it’s tempting to put everything into the PythonOperator but I strongly advise against it. Let’s say the function validating fails whereas wrangling, cleaning and transforming succeed. Because only one function fails, Airflow will consider the whole task as a failure. So, instead of retrying only one function validating, all the functions are retried as you put all the work into one task. That’s why you should have one task, one job, or one task one operator, so that if something fails, only this part will be executed again and not the others. This will prevent from inconsistencies in your data and will make easier to spot what fails and why.
DAGs and context managers
In Python, you can leverage context managers to allocate and release resources precisely when you want to. The most widely used context manager is with. I made a tutorial right here about that in Airflow, so I won’t explain how it works again but let me give you an example.
The with statement makes the code cleaner by removing the needs of assigning the variable dag to each task. By doing this, we can clearly see the tasks belonging to the DAG (if you have python functions in your DAG file, things can quickly become messy) and we let the context manager dealing with the DAG life cycle.
Task’s constructors accept many different arguments such as an email, number of retries, a start date, a queue and so on. I’m pretty sure you’ve already seen many DAGs with a dictionary just before the DAG object definition as shown below:
The purpose of this dictionary, usually called default_args, is to define a set of parameters common to all tasks in your DAG. That allows to avoid repeating the same arguments again and again, making your DAG clearer and less prone to errors. Define a set of default arguments with a dictionary for all of your tasks and if one task needs a specific value, overwrite that argument in the operator’s definition.
The unique identifier or DAG ID
When you instantiate a DAG object you have to specify a DAG ID. The DAG ID must be unique across all of your DAGs. You should never have two DAGs with the same DAG ID otherwise only one DAG will show up and you might get unexpected behaviours. Define a meaningful DAG ID along with a description of your DAG.
Think of when you will have hundreds of different DAGs. You definitely want meaningful DAG Ids and descriptions to quickly know which DAG does what. One more thing, in case you have multiple DAGs more or less related to each other, putting a common prefix for all of them can be a great idea.
The start date
Big topic here. The way DAGs are scheduled in Airflow can be somewhat difficult to understand at first. The start_date defines the date at which your DAG will start being scheduled. One thing you have to know is that each task or operator, can have a different start_date. Yes, tasks within the same DAG can start a different dates. I strongly advise you against this. Do not define different start dates. Keep things simple and homogenous. I don’t even see a use case where you might need to do that.
I will make a tutorial about the start_date as there are many things to know about that parameter which are definitely not obvious. As a best practice, define the start in the default arguments.
In addition, your start date should be static. Do not define a dynamic start date with a function like datetime.now() as it is confusing. Keep in mind that tasks are executed once the start_date + schedule_interval is passed. If the start date is set to now(), in theory, your tasks will never get executed as the date constantly moves forward.
The catchup parameter
Airflow automatically runs non triggered DAG Runs between the latest executed DAG Run and the current date. This is really useful if for some reasons, you had to pause your DAG and want to catch up the delay. Nonetheless, you should be careful with this feature. Indeed, let’s imagine that your DAG has a start_date set to 1 year ago with a schedule interval set to 10 minutes. If you start scheduling that DAG, you will end up with thousands of DAG Runs running at the same time. This could prevent from running other tasks or slow down your Airflow instance. To avoid this, I strongly recommend you to turn off this parameter by default. Either set the catchup parameter to False in the DAG definition or in the configuration file.
Notice that you can still use the command: airflow backfill from the Airflow CLI. In my opinion, that’s the best way to catch up your non triggered DAG Runs.
The schedule interval
The schedule interval defines the interval of time at which your DAG is triggered. Airflow is NOT a data streaming solution so don’t set a schedule interval of 1 second.
I’m pretty sure you already saw the schedule_interval either defined with a CRON expression or with a timedelta object.
Cron expressions are extremely powerful but they can be difficult to understand at first. Take a look at the following website to be sure that the schedule interval is the one you expect.
Now, when should you use a Timedelta object instead of a Cron expression? Certain frequency intervals can’t be expressed with Cron expressions. If you want to trigger a DAG once every three days, you have to define a timedelta object with timedelta(days=3). If you try to do it with a Cron expression, at the end of the month, that DAG will be triggered the 30th or 31th, THEN the 1st of the next month, breaking the interval of 3 days.
To sum up, whenever you need to define a schedule interval in relation with the previous one (not stateless), use a timedelta object.
That’s it for the first part of this series of tutorials. I hope you already learned new best practices. Obviously, there are many other Apache Airflow best practices to discover and I will share them with you at a regular basis. So bookmark this page and if you want to stay in touch, don’t forget to put your email below and start mastering Apache Airflow!