Shipping safely: deploys, flags and migrations
Most incidents follow a change. Canaries sized from the signal, flags, schema migrations that keep old and new code working, and rollbacks that are tested.
This chapter lets you change a running Dabba many times a day without the changes being the main cause of outages. It covers the pipeline that builds and tests a change, the rollout that exposes it gradually, the flags that separate shipping code from turning it on, the database migrations that let old and new code run side by side, and the rollback that has to work when everything else did not.
Here is the kind of incident this chapter is about. On a Tuesday at 19:40, a deploy goes out with a change to delivery-fee calculation. It passed its tests. Within minutes, orders in Pune are being charged a delivery fee of zero, because the new code reads the city’s fee table by a key that Pune’s configuration spells differently. The deploy went to every machine at once, so every Pune order is affected. Rolling back takes twenty minutes, because the previous build has to be found and redeployed, and by then roughly 2,500 Pune orders have gone out without a fee.
No dependency failed and no region went down. Across the industry, a large share of incidents follow a change to code or configuration, and that is good news in one sense: unlike hardware failure, the timing and size of changes are under our control.
What the pipeline must prove, and what it cannot
Continuous integration runs on every change: compile, unit tests, integration tests against real dependencies in containers, static checks, and a scan for secrets and known vulnerable libraries. Its output is a single artefact, a container image with an immutable version, and that same artefact moves through staging to production. Rebuilding for production from the same commit is not the same thing; a dependency may have changed in between.
It is worth being honest about what tests can catch. They catch logic errors in the cases someone thought to write down. The Pune fee bug passed because no test used Pune’s configuration, and in general production differs from any test environment in data, configuration, traffic shape and scale. Tests reduce the number of bad changes that reach production. The rest of this chapter limits how much damage the remaining ones do, and how quickly they are undone.
Dabba has one constraint that web services do not: mobile apps. A release of the customer or rider app cannot be rolled back. Once a phone has version 5.2, it keeps it until the user updates, which for some users takes months. So the server must keep working with every app version still in meaningful use, API changes must be additive, and the server holds a minimum supported version below which the app is asked to update. New app features ship turned off and are enabled from the server, which brings us to flags.
Let a small slice of traffic disagree first
A canary deploy sends a small share of traffic to the new version while the rest stays on the old one, compares the two, and only then continues. Dabba’s order service rolls out in steps: 1% of traffic, then 10%, then 50%, then all of it, pausing at each step and comparing the canary’s metrics against the old version over the same minutes. If the canary is worse beyond a threshold, the rollout stops and traffic returns to the old version automatically, without waiting for a person to notice a graph.
Had the fee change gone out this way, the damage would have been bounded by the canary’s share. But only if the canary was measuring the right thing.
The order service’s canary runs at 1% of traffic for 10 minutes in the dinner hour (about 42 orders a second). Normally 0.5% of order placements fail. The new version has a bug that doubles that to 1%. Will the canary catch it?
Show the worked answer
Almost certainly not. One percent of 42 orders a second is 0.42 a second, about 250 orders in 10 minutes. At 0.5% we expect one or two failures; at 1%, two or three. Counts that small vary by chance as much as they differ, so no sensible threshold separates them. The canary passes and the bug ships.
At 10% for 15 minutes the canary sees about 3,800 orders: an expected 19 failures against 38, which is a clear difference. So the steps are set from the signal, not from habit. The 1% step catches what is visible in high-volume signals (crashes, HTTP 5xx across all endpoints, latency), where even 1% of traffic is thousands of requests a minute. Business metrics such as order failure rate, payment success and average delivery fee need the 10% step and enough minutes to accumulate data. A delivery-fee check would have caught the Pune bug: the canary’s average fee for Pune was zero.
The comparison should be against the old version running at the same time, not against yesterday. Dinner traffic differs from lunch, and a canary judged against last night’s numbers will be blamed for the weather. And a canary needs the two versions to be safely interchangeable: a customer’s requests may hit old and new versions within one session, so both must read and write data in compatible ways. That requirement is what makes database changes the hard part of shipping.
With two regions, a region is a natural larger step. Dabba deploys to one city first (a small one, through city-aware routing), then to one region, then to the other, which also means a bad change cannot take out both regions in the same minute.
The arithmetic above also says something about when to deploy. At the dinner peak a canary gathers evidence fastest, because traffic is highest, but a bad minute then costs the most orders. At 15:00 traffic is perhaps a third of peak, so each canary step must last about three times as long to see the same number of orders, and a bad minute costs a third as much. Dabba chooses the afternoon for anything touching checkout, payments or assignment, and accepts slower rollouts as the price. What it does not do is freeze deploys for weeks before a festival: a freeze lets changes pile up, and the first deploy after it is large, unfamiliar and released by people who have not deployed in a month. Small, frequent, well-observed changes are the safer habit, and each deploy is marked on the dashboards so that anyone looking at a graph can see which change preceded a bend in it.
Flags separate deploying code from releasing a feature
Suppose Dabba is building a new rider-assignment algorithm. It takes weeks, it touches the busiest path in the system, and the team wants to compare it with the old algorithm on real orders. Merging it to the main branch only when finished means a large, risky deploy. A feature flag lets the code ship in small pieces, turned off. When ready, the team turns it on for one city, compares delivery times and rider idle time, then widens it. If something goes wrong, turning it off takes seconds and needs no deploy.
That last property is why flags are also the main tool for incidents. The degraded modes from the failure chapter (generic ranking instead of personalised, slower location updates) are flags that operations staff can flip. These operational flags live forever and should be tested regularly, unlike release flags, which should be removed once the feature is fully on.
Flags have costs that grow quietly. Each flag doubles the number of possible combinations of behaviour, and nobody tests all of them. A flag left on for two years after its feature launched is dead code with a switch attached, and the day someone flips it by mistake, a two-year-old code path runs in production. Dabba gives each release flag an owner and an expiry date when it is created, and a weekly report lists flags past their date. Flag evaluation must also not become a dependency: services cache the flag configuration locally and keep using the last known values if the flag service is unreachable, and every flag has a safe default.
Flags and canaries answer different questions. A canary asks whether the new build is healthy: does it crash, is it slower. A flag asks whether the new behaviour is good: do deliveries get faster. Most changes need the first; changes to product behaviour need both.
Change the schema while old and new code both run
During any rollout, and for the whole of a canary, two versions of the code serve traffic against one database. So every schema change has to be compatible with the code before it and the code after it. Consider a real Dabba change: orders need a zone_id (the delivery zone within the city), replacing a free-text area column that different services parse differently.
The tempting version is one migration and one deploy: add zone_id, fill it, drop area, and release code that uses zone_id. During the rollout, old instances still read area, which no longer exists, and fail. Rolling back does not help, because the column is gone. The safe version is called expand and contract, and it takes several deploys.
First, expand: add zone_id as a nullable column. Old code ignores it. Second, deploy code that writes both area and zone_id on every new order but still reads area. Third, backfill zone_id for existing rows. Fourth, deploy code that reads zone_id, still writing both, and compare the two for a while to catch parsing mistakes. Fifth, once nothing reads area, stop writing it, and in a later release, drop it. At every step before the last, rolling the code back one version is safe, because the previous version’s columns are all still there and still being written.
The backfill deserves its own arithmetic. At a million orders a day, a year of orders is about 365 million rows. A single UPDATE across all of them would hold locks and generate an enormous transaction, competing with checkout for hours. Instead the backfill runs in batches of a few thousand rows by primary-key range, with a short pause between batches, and slows down automatically when replication lag or database latency rises. At 5,000 rows every 100 ms, that is 50,000 rows a second, so the year takes about 2 hours, run overnight. It is boring, it is slow, and it does not cause an incident.
Walk-through: a millisecond migration that stopped checkout for six minutes
Even the “expand” step can hurt. Dabba runs PostgreSQL. Adding a nullable column is a metadata change that completes in milliseconds, so the team runs ALTER TABLE orders ADD COLUMN zone_id bigint at 19:45. Here is what happens.
The ALTER TABLE needs an exclusive lock on orders, briefly. At 19:45 an analyst’s report has been reading orders for six minutes and holds a shared lock. The ALTER waits for it. Every new query on orders, including every checkout insert and every tracking read, now queues behind the waiting ALTER, because PostgreSQL grants locks in order. For six minutes, until the report finishes, nothing touches the orders table. The migration itself then takes milliseconds. From the outside, Dabba was down for six minutes because of a change that was correct and fast.
The fixes are small and worth making standard. Migrations set a short lock timeout (SET lock_timeout = '2s'), so a blocked migration gives up quickly and retries later instead of holding the queue. Long analytical queries run on a replica, not the primary. Indexes are built with CREATE INDEX CONCURRENTLY, which does not block writes. And schema changes run outside peak hours, through the same pipeline as code, reviewed by someone who knows which operations rewrite a table and which take an exclusive lock. The cost is process: migrations are slower to land and sometimes need several attempts. That is a fair price for not stopping checkout.
Rollback is a path you build and test
In the fee incident, rollback took twenty minutes because it was improvised. A rollback should be the same operation as a deploy, pointed at the previous artefact, and it should be exercised often enough that people trust it. With canaries, most rollbacks are automatic and touch only the canary’s share.
| Kind of change | How it is undone | Time to undo | What can stop it |
|---|---|---|---|
| Server code | redeploy the previous artefact | minutes | the new code wrote data the old code cannot read |
| Behaviour behind a flag | turn the flag off | seconds | the flag service unreachable with no local cache |
| Configuration | revert the config version | seconds to minutes | config not versioned, changed by hand |
| Schema (expand steps) | usually nothing to undo; old code still works | none | a destructive step taken too early |
| Mobile app | cannot be undone; fix forward and disable by flag | days for a new release | the feature was not behind a flag |
The first row hides the most common trap. If version 2 writes a new status value, 'AT_PICKUP_COUNTER', and version 1 does not know it, rolling back to version 1 leaves orders in a state the running code cannot handle. The rule that prevents this is the same one as for schemas: first deploy code that can read the new value, then, separately, deploy code that writes it. A change that is only safe to roll forward should be known to be so before it ships, and flagged off until the team is ready to commit to it.
Configuration deserves the same care as code. The Pune bug was really a configuration mismatch, and many large public outages have come from a configuration change pushed everywhere at once. Dabba stores configuration in version control, validates it in CI, and rolls it out through the same staged process, one city first.
When the process is heavier than the risk
All of this costs time. A team of six shipping an internal admin page used by twenty support agents does not need a staged canary with automated statistical analysis; it needs tests, a quick rollback and someone watching after the deploy. The process should be proportional to the blast radius. Checkout, payments, rider assignment and anything that touches the orders table get the full treatment. A marketing banner gets a flag and a quick look.
Expand and contract is the exception: it is never optional for a table that serving code reads, because the failure it prevents does not depend on how important the feature is. Revisit the rest when incidents tell you to. If several incidents in a quarter came from changes that a canary would have caught, the canary is worth its cost. If none did, the time might be better spent elsewhere.
That completes the toolkit. The next chapter is a worked design that uses all of it at once: we take one Dabba feature, assigning a rider to an order, and design it end to end as a design review would, from requirements and numbers to failure modes and rollout.
The taxonomy and the traps — release vs ops vs permission flags, and how not to drown in them.
Two pages, and the vocabulary every safe-deploy conversation builds on.