sehaj/learn
System design/14 Reliability, security and operations

14.05 · concept · 10 min

Safe deploys

Most outages start with a change. Rolling, blue-green and canary deploys, waves with bake time and automatic rollback, and feature flags that separate shipping code from turning it on. What makes a release small, observable and reversible.

The problem: the deploy at 13:58

Snip's lunchtime incident (14.01 Observability: logs, metrics, traces) began with a deploy: a migration dropped an index, and redirect latency climbed. The deploy went to all 40 servers in two minutes, and it took the team half an hour to notice and roll back. Most incidents start like this, with a change. The goal isn't to stop changing; it's to make every change small, observed and quickly reversible.

Ways to replace running code

  • Rolling: replace servers a batch at a time, behind the load balancer. Old and new versions run side by side for a while, so every change must work with both (04.09 Schema changes and online migrations).
  • Blue-green: bring up a full new fleet (green) beside the old (blue), then switch traffic over in one step. Rolling back is switching back. It needs twice the capacity during the switch.
  • Canary: send a small share of traffic (1%, then 5%, then 25%) to the new version, compare its errors and latency with the old version's, and continue only if they match.

Waves, bake time and automatic rollback

Amazon's deployment pipelines, described by Clare Liguori, show the full shape:

  1. One box first. Deploy to a single server, or the smallest unit, serving at most about 10% of a region's requests, and watch its error rate specifically.
  2. Waves of increasing size. Then a first region, then groups of regions, never everything at once.
  3. Bake time. Wait between waves, so slow-burning problems (memory leaks, daily jobs) have time to show: at least an hour after the one-box stage and at least 12 hours after the first regional wave.
  4. Automatic rollback. The pipeline watches alarms, and if they fire, it rolls back on its own, without waiting for a person.

Snip's smaller version: one server, wait 15 minutes, 25% of servers, wait 30 minutes, the rest; the pipeline rolls back if the canary's error rate or p99 is worse than the rest of the fleet's.

Feature flags: deploy is not release

A feature flag wraps new behaviour in a runtime switch. The code ships turned off, then is turned on separately: for staff, then 1% of users, then everyone. Turning a misbehaving feature off takes seconds, with no deploy. It also makes big changes into many small ones, since unfinished work can be merged and deployed dark.

Flags have costs: two code paths to test, and stale flags that linger for years. Give each a planned removal date.

Global config is a deploy too

Configuration, feature flags, firewall rules and data files can break everything as fast as code, and are often pushed everywhere at once. Cloudflare's July 2019 outage was caused by a single firewall rule with a regular expression that backtracked catastrophically, pushed globally in one step; CPU on every core of every server hit 100% and traffic was down for 27 minutes. Among its fixes: roll out rule changes in stages, like other software.

Seen in production

Amazon

2020

Deployment pipelines

Amazon's pipelines deploy first to a one-box stage that typically serves at most 10% of a region's or zone's requests, then in waves of increasing size. A typical pipeline waits at least an hour after each one-box stage, at least 12 hours after the first regional wave, and two to four hours after each later wave, and the deployment system monitors alarms and rolls back automatically. Reaching every region typically takes four or five business days.

What we foundOne box, then waves of increasing size with bake time between them, and automatic rollback on alarms.

Automating safe, hands-off deployments (Clare Liguori, Amazon Builders' Library)builder.aws.com ↗

Cloudflare

2019

WAF rule outage

A single firewall rule containing a poorly written regular expression caused excessive backtracking and exhausted CPUs on every core, taking Cloudflare's service down for 27 minutes. The standard procedure allowed a non-emergency rule change to go globally into production without a staged rollout. Cloudflare's changes included staged rollouts of rules in the same way as its other software, performance profiling of rules in tests, and moving to a regex engine with run-time guarantees.

What we foundA config-style change pushed globally at once took everything down; the fix was staged rollouts for rules too.

Details of the Cloudflare outage on July 2, 2019blog.cloudflare.com ↗
Predict first

A new redirect code path is deployed to 5% of Snip's servers as a canary. Its error rate is 0.3%, against 0.1% on the rest of the fleet. What should the pipeline do?

Commit to an answer before you read on.

Recap

Interview questions

MidfreqMust-knowConcept

How would you deploy changes to a high-traffic service safely?

Report a mistakeEdit this page