DEV Community

Rohan Nalawade
Rohan Nalawade

Posted on

Blue Green deployment strategy.

Blue-Green deployment is a release management strategy that minimizes downtime and reduces risk by running two identical production environments.
It moves away from the traditional "in-place" upgrade model, where you overwrite the live application, to a model based on traffic routing.
Here is the technical breakdown of how it works, the workflow, and the database constraints you need to know.

The Architecture
The core concept relies on maintaining two separate environments:

  • Blue (Live): The currently active environment hosting the old version (v1). It receives 100% of user traffic.
  • Green (Idle): A clone of the Blue environment (same infrastructure, OS, configs). It is idle or accessible only via a private network. Sitting in front of these environments is a Load Balancer (or Router/DNS). This component dictates which environment is "Live" by directing traffic to the appropriate backend target group.

The Deployment Workflow

  • Preparation: You have traffic flowing to Blue (v1).
  • Deploy: You deploy the new version (v2) to the Green environment. Since Green is disconnected from public traffic, this has no impact on users.
  • Verification: The QA team or automated test suites run smoke tests against the Green environment using a private URL or internal port.
  • The Switch (Cutover): Once verified, you update the Load Balancer rules to route traffic from Blue to Green.
  • Monitoring: You monitor metrics (latency, error rates) on Green.
  • Rollback (If needed): If critical errors appear, you immediately revert the Load Balancer routing back to Blue (which is still running v1).
  • Cleanup: If Green is stable, Blue is eventually decommissioned or recycled to become the "Green" environment for the next release.

The Database Challenge
In stateful applications, Blue and Green generally share the same database. You cannot replicate the database easily because data written to Blue during the deployment would be lost when switching to Green.

Because both environments access the same DB simultaneously during the switch, database schema changes must be backward compatible.

The "N-1" Compatibility Rule
If you need to rename a column or change a schema:

  • Expand: Add the new column but keep the old one. Deploy this schema change first.
  • Deploy Code: Deploy the application code (v2) that writes to the new column but can still read the old one if necessary.
  • Contract: Only after v1 is completely offline do you remove the old column in a separate cleanup migration.

Pros and Cons
The Advantages:

  • Zero Downtime: The switch happens at the load balancer level, often within milliseconds.
  • Instant Rollback: Reverting to the previous version is just a routing change, not a redeployment.
  • Environment Isolation: You are testing on the actual infrastructure configuration that will serve production traffic.

The Trade-offs:

  • Cost: You effectively double your infrastructure footprint (compute resources) during the deployment phase.
  • Complexity: Requires sophisticated load balancing and CI/CD pipelines.
  • Data Handling: Requires strict discipline regarding database migrations and state management.

Summary
Blue-Green deployment is the gold standard for mission-critical systems where downtime is not an option. By decoupling the deployment of artifacts from the release of traffic, you gain control, speed, and a significantly lower Mean Time To Recovery (MTTR) in case of failure.

Top comments (0)