Skip to main content
LabsThis feature is in Labs. Contact your account team to enable this feature.
Astronomer Cosmos is an open source tool, maintained by Astronomer, that renders dbt Core projects as Airflow Dags and task groups. Cosmos Boost is an Astro-only set of optimizations that reduces the time Airflow spends parsing those Dags. It works with your existing dbt project, Cosmos configuration, and Dag code, and it doesn’t change your Dag structure, task behavior, task output, or schedule. This document explains what Cosmos Boost changes, the results to expect, and how to set it up. To learn how to use Cosmos itself, see the Cosmos documentation. Cosmos Boost only speeds up Dag parsing. You can still tune parsing yourself, for example by choosing a different load mode, and you can speed up task execution, for example by choosing a different execution mode. See Optimize performance in the Cosmos documentation.

How Cosmos Boost works

Airflow parses every Dag file repeatedly: the Dag processor parses each file again on an interval, and workers parse a Dag file again before they run one of its tasks. For a Dag built with Cosmos, each parse turns a dbt project into Airflow tasks, which includes reading the project, loading the manifest, and applying your select and exclude filters. For large dbt projects, this work can dominate parse time, delay task scheduling, and increase memory use. If a Dag file takes longer to import than the dagbag_import_timeout setting allows, Airflow reports a timeout import error for the file, and its Dags can fail to appear or update in the Airflow UI. Cosmos Boost reduces this work on Astro. If an optimization can’t apply, for example because of an unsupported Cosmos version or a missing file, Cosmos Boost logs a warning and falls back to standard Cosmos parsing for that parse. The expected worst case is that a parse isn’t accelerated.

Results

In Deployments, Cosmos Boost parses dbt projects up to six times faster. Results vary by project and configuration. Projects benefit most when they do at least one of the following:
  • Contain hundreds or thousands of dbt models and load them from a manifest.
  • Use complex select or exclude filters with the manifest load method.
  • Use the dbt ls load method.
  • Render the same dbt project in many DbtTaskGroup instances within one Dag.
  • Declare many DbtDag instances in a single Dag file. These projects can also benefit from the opt-in magic loop optimization.

Requirements

Set up Cosmos Boost

After your account team enables Cosmos Boost, complete the following steps:
  1. Confirm that your Deployments meet the requirements.
  2. Check your Astro CLI version:
    If the version is earlier than 1.47.0, upgrade the Astro CLI to the latest version.
  3. From the root of your Astro project, turn on the Cosmos Boost pre-deploy step:
    With this setting, the Astro CLI prepares information about your dbt project at deploy time, so Airflow doesn’t need to compute it on every parse. To confirm the setting, run:
  4. Deploy your project:
    The pre-deploy step applies from this deploy onward.

Deploy with CI/CD

If you deploy with a CI/CD pipeline, turn on the Cosmos Boost pre-deploy step in the pipeline before the deploy step. For example, in GitHub Actions:

Turn on the magic loop optimization

Before a worker runs a task, Airflow parses the whole Dag file that defines the task’s Dag. If the file declares several DbtDag instances, for example in a loop over a list of dbt projects or schedules, the worker builds every one of them, which means one full dbt project parse for each, even though the task needs only one Dag. The magic loop optimization uses Airflow’s parsing context to build only the DbtDag that the task belongs to. The optimization applies only to DbtDag instances, not DbtTaskGroup instances, and only to the parse that a worker runs before a task. The Dag processor still builds every Dag in full, so scheduling isn’t affected.

Why the magic loop optimization is off by default

The magic loop optimization relies on each DbtDag in a Dag file being self-contained. Some Dag files share task definitions across several Dags, or add tasks that depend on another Dag in the same file. In those files, building only the Dag that the task belongs to isn’t enough: the code that reads another DbtDag fails, Airflow reports an import error for the whole file, and the task can’t start. The setting applies to every Dag file in the Deployment, not to individual files. Turn it on only after you confirm that the Cosmos Dag files in the Deployment don’t depend on other Dags in the same file.

Prerequisites

  • Cosmos Boost enabled for the Deployment.
  • Astro Runtime 3.0-1 or later, which runs Airflow 3. On earlier versions, the setting has no effect and workers build every Dag in the file.

Steps

  1. Check each Dag file that declares more than one DbtDag. Look for module-level code that accesses the tasks or properties of each DbtDag after Cosmos creates it, for example code that adds a task and sets a dependency with get_task(). In the following example, the loop adds a task to every DbtDag it creates. With the magic loop optimization on, get_task() fails for every DbtDag other than the one the worker is parsing for:
  2. Update the code so that it still registers every Dag, but skips the extra setup for a DbtDag that the magic loop optimization skipped. A skipped DbtDag has no tasks, and its dbt_graph attribute is None:
  3. Deploy the updated Dag code.
  4. In the Astro UI, set the following environment variable on the Deployment:

Turn off Cosmos Boost

To turn off the pre-deploy step, run the following commands from the root of your Astro project:
astro dbt cleanup removes the files that earlier deploys added. Turning off the setting doesn’t remove them. To turn off the magic loop optimization, delete the ASTRO_COSMOS_BOOST_PLUGIN_MAGIC_LOOP_ENABLED environment variable or set it to false. To turn off Cosmos Boost for a Deployment, contact your Astronomer account team.