Backward compatibility and deprecating an API safely

Difficulty: Advanced

Question

You need to rename a field and remove an old endpoint that thousands of clients use. How do you do it without breaking them?

Answer

The core idea is that you don't own your clients' release schedules. Once an API is public, a change is like renovating a bridge while traffic is still crossing: you build the new lane, move traffic across gradually, and close the old one only after nobody drives on it. The technique is often called expand and contract, or parallel change.

Start with the compatibility rules. Robustness principle for consumers: be liberal in what you accept, so clients must ignore unknown fields and tolerate new enum values. For producers: only make additive changes within a version. Safe: adding optional request fields, adding response fields, adding endpoints, relaxing validation. Unsafe: removing or renaming fields, changing types, changing semantics of an existing field (for example amount switching from rupees to paise), tightening validation, changing pagination defaults, making optional things required, and changing error codes.

Renaming a field safely, step by step. Expand: start returning both the old and new names, e.g. customer_name and full_name with identical values, and accept both on input. Migrate: publish the change in docs and the changelog, notify integrators by email and dashboard banners, update SDKs to use the new name. Observe: instrument usage. Log which API keys still use the old field or endpoint, so that you can chase the specific clients. Contract: after the announced window, remove the old name in the next major version, or, if your policy allows, at the sunset date.

Deprecating an endpoint. Announce with a concrete timeline that matches the audience: for internal services perhaps weeks, for public APIs typically six to twelve months, for mobile clients as long as old app versions remain in the wild. Signal in-band with the Deprecation header (RFC 9745) and the Sunset header (RFC 8594) giving the retirement date, plus a Link header with rel="deprecation" or "successor-version" pointing at migration docs. Mark the operation deprecated: true in OpenAPI so generated SDKs show warnings. Send emails to owners of keys observed calling it.

Retirement mechanics: before the sunset, run brownouts, brief scheduled periods where the old endpoint returns 410 Gone or 404, to flush out forgotten callers and gauge impact. At sunset, return 410 Gone with a problem+json body pointing to the replacement; 410 is more informative than 404 because it says the removal is intentional. Keep a kill switch and be ready to grant extensions to large partners, since a business relationship often outweighs a date.

Engineering safeguards: contract tests using consumer-driven contracts (Pact) so consumers' expectations are verified in your CI; automated OpenAPI diff checks that fail a PR on breaking changes; feature flags and gradual rollouts; canary traffic; and a strict rule that behaviour changes go behind explicit opt-in, like an API version header, never silently. Stripe's model of pinning each account to a dated version and transforming responses is the gold standard for avoiding surprise.

Data model changes: if the database column is renamed, do it with the same expand and contract discipline in schema migrations, so old and new code versions can run simultaneously during deploys.

To sum up in an interview: keep changes additive, run old and new in parallel, communicate loudly in-band and out-of-band, measure real usage, brownout, then remove with 410.

Code examples

Expand phase: both fields returned

GET /v1/customers/cus_1 HTTP/1.1

HTTP/1.1 200 OK
Deprecation: @1790000000
Link: <https://docs.example.com/changelog#full-name>; rel="deprecation"
Content-Type: application/json

{
  "id": "cus_1",
  "customer_name": "Asha Rao",
  "full_name": "Asha Rao"
}

Both names coexist during the migration window; the header nudges clients toward the new one.

After sunset: 410 Gone

GET /v1/legacy/orders HTTP/1.1

HTTP/1.1 410 Gone
Content-Type: application/problem+json
Link: </v2/orders>; rel="successor-version"

{ "type": "https://api.example.com/problems/endpoint-retired", "title": "This endpoint was retired on 2027-03-31", "status": 410, "detail": "Use GET /v2/orders instead." }

An explicit, actionable retirement message with a pointer to the successor.

Key points

Common follow-up questions

Interview tips

Concepts covered

backward compatibility, deprecation, Sunset header, expand and contract

More API Design questions