08. Shipping / Lesson 2 of 2
CI/CD & Deployments
Ship small changes often, and make a bad one cheap to undo.
- 10 min
- Intermediate
- 10 steps
- Users seeing the change100% (bad)
- Downtime5 min (bad)
- Time to undo30 min (bad)
- Request
- Health check
- Failed request
Step 1 of 10: Release day is scary
Read every step as text
Step 1: Release day is scary
A developer logs in to the server, stops the app, copies new files, runs a few commands from a checklist, and starts it again. Say it takes 5 minutes of downtime, so it happens rarely and at night.
Big, rare releases hold many changes at once, so a bug could be any of them. And undoing it by hand, with the old files in someone's head, can take 30 minutes. Everyone dreads it.
Rare releases make each release riskier, which makes people release even less. Break that circle with small, frequent, automatic ones.
Step 2: Continuous integration: merge often, test always
With continuous integration (CI), everyone merges small changes into the shared branch many times a day. Every push starts a robot (the CI runner) that builds the code and runs the tests.
A red result blocks the merge, so the main branch always works. Small changes are easy to review, and a failure points at a few lines, not at a month of work.
CI answers "does my change work together with everyone else's?" within minutes.
Step 3: The build pipeline: the same steps every time
A pipeline is a list of stages that run in order: check style, run unit tests, build one artifact (for example a container image), run integration tests, and store the artifact with a version number.
The key rule: build once, deploy the same artifact everywhere. What you tested in staging is then byte for byte what runs in production, not something rebuilt and slightly different.
Each stage is a gate. The fastest, cheapest checks go first, so a mistake fails in seconds, not minutes.
Step 4: Continuous delivery: always ready to ship
Continuous delivery extends the pipeline: every green build is deployed automatically to a staging copy of production, and can go to production with one click, at any hour.
Continuous deployment goes one step further: no click, every green build goes live by itself. Teams with strong tests and safe release methods (next steps) often choose this.
CD makes releasing boring. A boring release is a safe release.
Step 5: Rolling deploy: one server at a time
With 4 servers behind a load balancer, update them one by one: take it out of the balancer, update it, check that it is healthy, put it back. At no time are all servers down, so downtime is 0.
At 2 minutes a server the whole roll takes
4 × 2 = 8 minutes. Halfway, 50% of servers run the new version. The old and new versions serve users at the same time, so they must work with the same database.Change the database in two steps: first add the new column (both versions work), later remove the old one. Never rename it in one go.
Step 6: Blue-green: switch the whole thing at once
Keep two full copies of production. Blue runs the current version and takes the traffic. You deploy the new version to green, test it with no users on it, then switch the load balancer to green in one step.
Undoing is the same switch back, in seconds, because blue is still there. The price is double the servers while both exist, and every user moves at the same moment.
Great when you want a fast, simple undo. Cost and the all-at-once switch are the trade-off.
Step 7: Canary: try it on a few users first
A canary release sends the new version to a small share of traffic first. Here 1 of 20 servers runs v2, so 5% of requests reach it. You watch its errors and latency against the old servers.
If it looks healthy you grow it: 5%, 25%, 50%, 100%. If not, only 5% of users ever saw the problem, and undoing means sending that share back to v1.
Canary limits the blast radius: a bug hurts 5% of users for minutes, not 100% of users for an hour.
Step 8: Feature flags: deploy now, release later
A feature flag is an
ifaround new code, switched by a setting you can change without deploying. The new checkout is deployed to every server, but off. Then you turn it on for staff, then 1% of users, then everyone.This separates deploying (putting code on servers) from releasing (letting users see it). If something is wrong, you flip the flag off in seconds, with no new deploy.
Flags pile up. Remove each one after the feature is fully released, or the code fills with old branches.
Step 9: Rollback: the way back must exist
The canary's error rate jumps from 0.2% to 8%. An automatic check sends its traffic back to v1, so no one reaches the bad version any more. Only the canary's 5% share saw errors.
A fast rollback needs three things: the previous artifact is still stored, the database change works with both versions, and you can switch without a long process. Practise it, so the first rollback is not the real one.
Rollback speed by method: flag flip, canary or blue-green switch in seconds; rolling back a rolling deploy takes another full roll, here 8 minutes.
Step 10: Shipping in one picture
CI builds and tests every change; the pipeline makes one artifact; delivery puts it in staging and is ready for production at a click, or with no click at all.
Release it with a rolling, blue-green or canary method, hide risky features behind flags, and keep a quick rollback. Small, frequent, reversible: a bad change then reaches 5% of users for a few minutes.