|
| 1 | +--- |
| 2 | +title: "Blue/Green Deployments" |
| 3 | +description: "How to perform Blue/Green Deployments in Materialize." |
| 4 | +menu: |
| 5 | + main: |
| 6 | + parent: manage |
| 7 | + name: "Blue/Green Deployments" |
| 8 | + weight: 11 |
| 9 | +--- |
| 10 | + |
| 11 | +Materialize offers some helpful tools to manage Blue/Green deployments. We |
| 12 | +recommend using the Blue/Green pattern any time you need to deploy changes to |
| 13 | +the definition of objects in Materialize in production environments. |
| 14 | + |
| 15 | +## Structuring your environment |
| 16 | + |
| 17 | +{{< note >}} |
| 18 | +We recommend using [dbt](../dbt/) to manage deployments, but you can recreate this |
| 19 | +workflow using your own custom scripts. |
| 20 | +{{</ note >}} |
| 21 | + |
| 22 | +1. Use schemas and clusters to isolate your changes. Schemas should be the |
| 23 | +primary container for database objects (views, indexes, and materialized |
| 24 | +views), while clusters are the compute resources that will perform view |
| 25 | +maintenance and serve queries. |
| 26 | + |
| 27 | +1. Configure `profiles.yml` to set up the different targets. Use consistent |
| 28 | +naming across the clusters and schemas that will be deployed together. In this |
| 29 | +example, we'll switch between two targets: `prod` and `prod_deploy`. |
| 30 | + |
| 31 | + ```yaml |
| 32 | + default: |
| 33 | + outputs: |
| 34 | + |
| 35 | + prod: |
| 36 | + type: materialize |
| 37 | + threads: 1 |
| 38 | + host: <host> |
| 39 | + port: 6875 |
| 40 | + |
| 41 | + pass: <password> |
| 42 | + database: materialize |
| 43 | + schema: prod |
| 44 | + cluster: prod |
| 45 | + sslmode: require |
| 46 | + prod_deploy: |
| 47 | + type: materialize |
| 48 | + threads: 1 |
| 49 | + host: <host> |
| 50 | + port: 6875 |
| 51 | + |
| 52 | + pass: <password> |
| 53 | + database: materialize |
| 54 | + schema: prod_deploy |
| 55 | + cluster: prod_deploy |
| 56 | + sslmode: require |
| 57 | + |
| 58 | + target: prod_deploy |
| 59 | + ``` |
| 60 | +
|
| 61 | +1. Leave your sources in a separate schema (e.g. `public`) and don't touch them. |
| 62 | +Instead, define them as `sources` in a `schema.yml` file. Since the same source |
| 63 | +can be shared across your production views and clusters, you won’t need to |
| 64 | +recreate them. |
| 65 | + |
| 66 | +1. Create cluster `prod` and schema `prod` in Materialize and deploy your |
| 67 | +production objects here. |
| 68 | + |
| 69 | +## Deploying changes to production |
| 70 | + |
| 71 | +1. When introducing changes, deploy the updated views to a separate cluster |
| 72 | +`prod_deploy` in schema `prod_deploy`. We recommend using a CI/CD Workflow and |
| 73 | +validating all changes on a staging environment before building them in |
| 74 | +production. |
| 75 | + |
| 76 | + ```bash |
| 77 | + dbt run --exclude config.materialized:source --target prod_deploy |
| 78 | + ``` |
| 79 | + |
| 80 | +1. For multi-cluster deployments, co-locate clusters with dependent views or |
| 81 | +indexes in the same schema. For instance: |
| 82 | + |
| 83 | + - Schema: prod |
| 84 | + - Cluster: prod_compute |
| 85 | + - Cluster: prod_serve |
| 86 | + - Views: static_view |
| 87 | + - Materialized View: maintained_view |
| 88 | + - Indexes: index_on_mainained_view_idx |
| 89 | + |
| 90 | +## Cutting over |
| 91 | + |
| 92 | +{{< note >}} |
| 93 | +We're working on allowing programmatic hydration checks {{% gh 22166 %}}. |
| 94 | +{{</ note >}} |
| 95 | + |
| 96 | +1. Wait for all views on `prod_deploy` to hydrate. You can look at the lag in |
| 97 | +the Workflow graph in the [Materialize Console](https://console.materialize.com) |
| 98 | +to get a rough sense of when rehydration is complete. The view will appear |
| 99 | +as “caught up”, and you can compare both the `prod` and `prod_deploy` versions |
| 100 | +by viewing the Workflow graph from a common source or other upstream |
| 101 | +Materialization. |
| 102 | + |
| 103 | +1. Perform your end-to-end application tests on `prod_deploy` objects to ensure |
| 104 | +it is safe to cut over. |
| 105 | + |
| 106 | +1. Use the `SWAP` operation to atomically rename your objects in a way that is |
| 107 | +transparent to clients. |
| 108 | + |
| 109 | + ```sql |
| 110 | + BEGIN; |
| 111 | + ALTER SCHEMA prod SWAP WITH prod_deploy; |
| 112 | + ALTER CLUSTER prod SWAP WITH prod_deploy; |
| 113 | + COMMIT; |
| 114 | + ``` |
| 115 | + |
| 116 | +1. Now that changes are running in `prod` and the legacy version is in |
| 117 | +`prod_deploy`, you can drop the prod_deploy compute objects and schema. |
| 118 | + |
| 119 | + ```sql |
| 120 | + DROP CLUSTER prod_deploy CASCADE; |
| 121 | + DROP SCHEMA prod_deploy CASCADE; |
| 122 | + ``` |
| 123 | + |
| 124 | +## Additional customizations |
| 125 | + |
| 126 | +### Fine-grained `ALTER ... RENAME` |
| 127 | + |
| 128 | +Schemas and clusters are the most common units for swapping out. But you can |
| 129 | +also use `ALTER...RENAME` operations on: |
| 130 | + |
| 131 | +- Schemas |
| 132 | +- Coming soon: [Databases](https://github.com/MaterializeInc/materialize/issues/3680) |
| 133 | +- Tables |
| 134 | +- Views |
| 135 | +- Materialized Views |
| 136 | +- Indexes |
| 137 | + |
| 138 | + ```sql |
| 139 | + BEGIN; |
| 140 | + -- Swap schemas |
| 141 | + ALTER SCHEMA prod RENAME TO temp; |
| 142 | + ALTER SCHEMA prod_deploy RENAME TO prod; |
| 143 | + ALTER SCHEMA temp RENAME TO prod_deploy; |
| 144 | + -- Swap multiple clusters |
| 145 | + ALTER CLUSTER prod_serve RENAME TO temp_compute; |
| 146 | + ALTER CLUSTER prod_deploy_serve RENAME TO prod_serve; |
| 147 | + ALTER CLUSTER temp_serve RENAME TO prod_deploy_serve; |
| 148 | +
|
| 149 | + ALTER CLUSTER prod_compute RENAME TO temp_compute; |
| 150 | + ALTER CLUSTER prod_deploy_compute RENAME TO prod_compute; |
| 151 | + ALTER CLUSTER temp_compute RENAME TO prod_deploy_compute; |
| 152 | + COMMIT; |
| 153 | + ``` |
| 154 | + |
| 155 | +These operations can be interspersed with `ALTER SWAP` operations as well. |
| 156 | + |
| 157 | +### Canary deployments |
| 158 | + |
| 159 | +Deploy a canary instance of your application that looks in schema `prod_deploy` |
| 160 | +and connects to cluster `prod_deploy`. Slowly move traffic to the canary until |
| 161 | +you are confident in the new deployment. See an example for [traffic shifting |
| 162 | +in AWS Lambda](https://aws.amazon.com/blogs/compute/implementing-canary-deployments-of-aws-lambda-functions-with-alias-traffic-shifting/). |
0 commit comments