dbt: Blue-green deployments in dbt are, and how using temporary schemas with atomic swaps


🔵🟢 What is Blue-Green Deployment in dbt?

In the context of dbt, a blue-green deployment is a strategy to deploy new data models with zero downtime by maintaining two versions of your models:

  • Blue: current production models (in use).
  • Green: new version being prepared (not yet live).

You build and test the new models (green) in isolation, and only switch traffic to them once they’re ready.


🧪 Why Temporary Schemas?

To avoid interrupting consumers (e.g., dashboards, data apps), you don’t want to build or rebuild tables in-place. Instead, you:

  1. Use a temporary schema (e.g., my_project__temp) for the build.
  2. Test the models in this temp schema.
  3. Atomically swap the temp schema with the production schema (e.g., rename schemas).

✅ Result: Consumers instantly switch to the new version without downtime.


📦 Concrete Example (BigQuery)

Imagine you have a dbt project with production schema:
analytics_prod

You want to deploy a new version using blue-green. Here’s the process:


1. Configure the target schema dynamically

In dbt_project.yml:

models:
  my_project:
    +schema: "{{ target.schema }}__temp"  # Temporary schema: analytics_prod__temp

In profiles.yml:

target: blue_green_temp

outputs:
  blue_green_temp:
    type: bigquery
    dataset: analytics_prod__temp
    ...

2. Build into the temp schema

dbt run --target blue_green_temp

All tables are now created in analytics_prod__temp.


3. Run tests

dbt test --target blue_green_temp

Validate data quality and integrity in isolation.


4. Atomic Swap

In BigQuery (example with SQL or bq CLI):

# Delete backup if exists
bq rm -r -f analytics_prod__backup

# Rename current prod to backup
bq update --set_label='env:backup' analytics_prod analytics_prod__backup

# Rename temp to prod
bq update --set_label='env:prod' analytics_prod__temp analytics_prod

In Snowflake or Redshift, similar schema rename operations exist. In Databricks, you may need to simulate it with view replacement.


✅ Benefits

  • Zero downtime: consumers always read from a stable schema.
  • Safe rollback: revert to analytics_prod__backup if something goes wrong.
  • Full testing: test your models before exposing them.

Leave a Reply

Your email address will not be published. Required fields are marked *