BM
Bashar ModallalSOFTWARE · AI · SYSTEMS
Backend Engineering

Expert · Lesson 11 of 12

Software Architecture

Release a Breaking Change in Stages

An old till selects Khan, but a new tablet sends the parcel to Majaz. Follow one pickup order through a staged API and database migration.

A shopkeeper scans a notebook parcel between an older countertop terminal and a modern tablet.

An order for two notebooks changes pickup branches twice. On Sunday morning an old till moves order SH-481 from Majaz to Khan. A new tablet opens the same order and sends the clerk to Majaz. Both devices received successful responses. The database has two plausible answers: a branch code of KHAN and a branch ID of 17, which belongs to Majaz. Someone copied the old field into the new one, but nobody decided what should happen when yesterday's software wrote again.

The shop wants to replace its human-readable branch code with a stable identifier so a future rebranding will not change an order's identity. Assume MAJAZ maps to 17 and KHAN maps to 23 for this merchant. The old client sends and expects a string named pickupBranch. The new contract sends and expects an integer named pickupBranchId. Our job is to move the order safely while old phones, terminals, workers and server binaries have different upgrade dates. We will use the same order throughout so that a successful HTTP response never substitutes for checking where the parcel actually goes.

Establish what an existing client can observe

Start with a saved v1 exchange. Updating SH-481 with pickupBranch equal to KHAN returns a v1 order containing that exact code. Opening the order again shows the same branch. A missing branch in a partial update means leave the current choice alone. In this shop an explicit null for a pickup order is invalid. Capture those behaviors along with the existing error for an unknown code. A migration that quietly changes omission into “clear the branch” breaks the old client even if every field keeps its name.

Changing pickupBranch from a string to an integer would force a coordinated release across the fleet. Adding a new field to every response can also surprise a strict decoder. Here we choose a separate v2 route with its own representation, preserve the v1 response shape, and map both contracts to the same internal operation. The v2 route rejects a v1-only branch field rather than guessing its meaning. Authenticate the caller and resolve the branch within the order's merchant on both routes. A valid integer belonging to another shop must never select that shop's pickup desk. Google API guidance on backwards compatibility

Write down a business invariant. Whenever a pickup order has both fields populated, the ID must resolve to the same branch as its legacy code. A returned response must describe the committed choice for that order revision. Freeze the code-to-ID mapping during the transition, retain historical codes for closed branches, and prevent reuse of a code for a different location. Otherwise a perfectly repeatable migration can reproducibly send the parcel to the wrong desk. This is a case where keeping a small reference table stable buys more safety than adding clever conversion logic.

Expand storage before expanding traffic

Add a nullable pickup_branch_id alongside pickup_branch_code. Leave the code in place and initially treat it as authoritative. Existing rows can remain null in the new column. Test the old production binary against the expanded schema: its inserts, reads and updates must work. An old positional insert or a mapper that assumes a fixed result shape deserves a real execution test. A schema addition can look harmless in a review and still be incompatible with how a particular client talks to the database.

Use a short, independently deployable schema change. Rehearse lock acquisition and migration duration on a representative table. In PostgreSQL a foreign key can be added with NOT VALID so that new writes are checked while validation of older rows is scheduled separately. A nullable foreign key still allows missing IDs. It also cannot prove agreement between the old code and the new ID. Those are distinct properties with distinct checks. Record rows with an unmapped legacy code for investigation and leave their intended branch intact. PostgreSQL ALTER TABLE reference

Deploy a bridge version of the service next. It continues serving v1 and reading the legacy code, but every successful branch change now writes both representations in the same transaction. Both values come from one resolved branch record. For SH-481, a v1 edit to KHAN writes the pair KHAN and 23 and advances the order revision once. If branch resolution or either write fails, the whole edit rolls back. The till gets its familiar response, and the clerk sees one business event despite the two storage fields.

During this rollout some servers still write only the code. Keep the v2 route disabled and keep all readers on the legacy authority. Suppose the bridge writes MAJAZ and 17, then an old server accepts KHAN. Storage temporarily reads KHAN and 17. A reader that prefers the new ID whenever it is non-null would now return the wrong branch. Null counts cannot detect this failure. In this phase tolerate and measure disagreement in the untrusted copy. The invariant is a gate for changing authority, not a claim about every row while legacy writers still run. Martin Fowler on parallel change

Migrate the writes you cannot see on the checkout screen

Inventory all writers before starting the final reconciliation. A rolling deployment can serve requests from old and new replicas during replacement. A green health check on the newest replica says nothing about a queue worker that loaded the old order model yesterday. Check web replicas, long-running workers, imports, partner jobs and administrative utilities. Drain or upgrade each old writer, including in-flight work. Keep accepting legacy message payloads through a translator in the bridge. An old message can be processed safely by new code; an old process writing directly is a different problem. Kubernetes Deployment behavior

Once the bridge owns all writes, backfill in bounded batches. Read an order's primary key, revision and current code, resolve its branch, then conditionally update the ID only if the observed revision and code still match and the ID differs from the intended value. In PostgreSQL, IS DISTINCT FROM lets that last test include nulls. A zero-row result is a reason to reread or skip an already-correct row. A checkpoint makes scanning resumable, while a final full reconciliation catches discrepancies behind the checkpoint. Set a work budget per batch and watch write latency before increasing it.

For a concrete race, the batch reads SH-481 at revision 41 with KHAN and prepares ID 23. A clerk changes it to MAJAZ through the bridge, which commits revision 42 and ID 17. The delayed batch's revision predicate now fails. A fresh read confirms MAJAZ and 17; the batch must leave them alone. An unconditional update would have put 23 back, and a test that merely counted non-null IDs would have declared victory. Check mappings across every merchant and account for unresolvable rows before allowing new reads.

Switch readers and clients independently

Shadow the ID-based read on a sample of ordinary requests while serving the code-based result. Compare business identities, not the raw string and integer. Count mismatches, missing mappings and failed joins separately. Inspect old orders as well as this week's traffic. Once reconciliation is complete and every writer maintains both fields, turn on ID-based reads for a small group. Keep the bridge's dual writes active. If a comparison fails, switch reads back to code, retain the evidence and repair the discrepancy under a revision guard.

Interaction Expected result
Old till edits SH-481 through v1 Code and ID change together, response contains the code
New tablet reads through v2 Integer ID identifies the till's chosen branch
Tablet moves the order through v2 Both fields change, old till reads the equivalent code
Unmodified old server receives v2 traffic Routing must prevent this combination before v2 is enabled

Enable v2 only on a fleet that understands it, or route it exclusively to compatible instances. Do not let clients silently retry a rejected v2 edit against v1 with a different interpretation. Exercise the matrix above under real routing and include a delayed retry after a lost response. Use the established order revision and request identity for conflict detection and duplicate handling. Keep emitted events readable by their current consumers. A passing test of two HTTP routes offers little comfort if the dispatch worker sends the order to yesterday's branch.

Decide how far rollback can go

Before admitting the first v2 tablet, the original binary can still serve the legacy contract against expanded storage, provided the code remains current and v2 stays off. After v2 clients depend on the new route, the rollback target must itself serve v2 and maintain both fields. Retain a tested bridge build with those capabilities. Deploying an older image that lacks v2 would restore yesterday's binary while breaking today's fleet. Practise a read rollback and a binary rollback separately, with a real write between them and a read from each client afterwards.

Retire the storage column and retire the public v1 contract on separate decisions. A v1 adapter can derive KHAN from ID 23 while supported tills still need the old wire format. Remove the column only after every reader, writer and rollback target runs without it and old jobs can still be decoded. Drop the external route only after the documented support window and consumer migration have actually finished. A quiet weekend with no v1 calls is poor evidence about a terminal that syncs only at month end.

اقرأ الدرس بالعربية

Exercise: approve the next release?

The backfill dashboard shows 100 percent of pickup orders with an ID. One comparison still finds KHAN paired with 17. A nightly importer runs the old binary, and two supported tills use v1. Should the team enable ID-based reads, and must it replace both tills first?

Answer. Hold the read switch. Upgrade or isolate the importer, reconcile mismatched pairs against the authoritative code with revision checks, and repeat the mixed-client tests. The tills can continue using v1 through the bridge. Approve a limited read switch when the importer cannot recreate the mismatch, independent checks agree on branch identity, and a rollback rehearsal preserves a fresh edit from either client. Those observations establish that the next release can move the parcel to the correct branch even when the devices at the counter disagree about the field's name.

YOUR TAKE

What did you think?

Choose one reaction for this article.

Was this useful?

A cookie helps limit repeat responses. Your feedback is private.

Share this noteLinkedIn ↗X ↗Email ↗

Keep exploring.

Software Architecture9 min read

Find a Tail Latency Regression

Checkout barely moves at the median, but its p99 triples. Trace a missing 610 milliseconds, shorten a connection hold and decide whether to roll out the fix.

Read article ↗