LearnThatStack Ace your next interview
API Design · question
Question 64 of 92

How do you handle backward compatibility in APIs?

expert Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Versioning an API: changing the contract you already published

Only a breaking change needs a new version, and most changes are additive. The deprecation lifecycle matters far more than where the version number goes.

For this question · chapters 1 and 2 of 3

1/3 The line holds until a field is renamed
api contractThe line holds until a renameadditive ships, a rename needs v2Clientu = get("/v1/users/42")u["name"]"Ada"your api/v2/v1 shape4 fieldsid: intfull_name: stremail: str?role: str = username: strtest passcontract test
api contractThe line holds until a renameadditive ships, a rename needs v2Clientu = get("/v1/users/42")u["name"]"Ada"your api/v2/v1 shape4 fieldsid: intfull_name: stremail: str?role: str = username: strcontract testtest pass

The line holds until a field is renamed

  1. A published response shape is code that other people already run in production, not documentation. You cannot redeploy that code for them. Each strand in the line stands for one field your API returns. The contract test checks those fields, and the test passes.
  2. Adding an optional field such as email to the /v1 shape costs nothing. A tolerant reader ignores keys it does not recognise, so clients on /v1 skip the new email key. The contract test runs again and still passes. Additive changes like this one are most of the changes any API ever makes.
  3. A new required input is still an additive change if the server can answer without it. Here the server fills in a default role for any client that does not send a role. The default keeps old clients working correctly. So the published contract still holds, now with a fourth additive change.
  4. A rename is two changes: a removal and an addition. The removal is the part that breaks clients. The name field becomes full_name, so a client that reads u["name"] gets a KeyError. The contract test (the check against what that client expects) fails. So the rename has broken that client.
  5. A new version exists to move a breaking change out of /v1. Here the rename moves to /v2, and /v1 goes back to the shape it published. So the old client works again, and the contract test passes. A new version is only for breaking changes, so a change that breaks nothing should never get one.
Chapter 1 · step 1 of 5

A published response shape is code that other people already run in production, not documentation. You cannot redeploy that code for them. Each strand in the line stands for one field your API returns. The contract test checks those fields, and the test passes.

1 of 5 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime