AlgoViz

API versioning

medium 22 min

Programs you cannot update depend on your API, so some changes would break them.

See it run

One product in three versions, the adapters between them, and how a version ends.

Operations

One product, three versions

Product 123 as v1, v2 and v3 clients see it.

Step 1 of 4

v1

v1v2v3
namename
priceprice: 999
stocknone

Simple

A name and a price as a plain number.

Step 1: v1

GET /api/v1/products/123{"id": 123, "name": "iPhone", "price": 999}GET /api/v2/products/123{"id": 123, "name": "iPhone", "price": {"amount": 999, "currency": "USD"}, "stock": 50}GET /api/v3/products/123{"id": 123, "title": "iPhone", "pricing": {"amount": 999, "currency": "USD", "discount": null}, "inventory": {"stock": 50, "reserved": 5}}
Effort
Read
Client change
v2 and v3 break

Ask of each cell: would a client written for the column on the left still work?

In the real world

The same api versioning doing real jobs. Pick one and step through it.

The version is in a header, and the shared cache does not know it.

Step 1 of 3

v2 asks first

Our systemv1 clientv2 clientCDNshared cacheAPI server

stored

/products/123: v2

Stored

The v2 client asks for /products/123. The answer is stored under the address.

Step 1: v2 asks first

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Changing a type in place

    critical

    Every client that reads the old type fails at the same moment.

    Wrong
    "price": {"amount": 999, "currency": "USD"}   # was 999
    Right
    "price": 999,"price_details": {"amount": 999, "currency": "USD"}
  • Copying the code per version

    major

    Fixes are forgotten in one copy and the rules drift apart.

    Wrong
    def get_v1(id): ...   # 400 linesdef get_v2(id): ...   # 400 similar lines
    Right
    def get_v1(id): return v2_to_v1(get_v2(id))def get_v2(id): return v3_to_v2(core.get(id))
  • Deprecation middleware after the routes

    minor

    The route has already answered, so the headers are never sent.

    Wrong
    app.use("/v1", v1Routes)app.use("/v1", deprecationHeaders)
    Right
    app.use("/v1", deprecationHeaders)app.use("/v1", v1Routes)
  • Deprecation: true

    minor

    The standard header holds a time, not a yes.

    Wrong
    Deprecation: true
    Right
    Deprecation: @1796083200Sunset: Tue, 01 Jun 2027 00:00:00 GMT

Interview tips

  • How to explain it

    • Old clients keep their form.
    • Adding rarely breaks.
    • One core, thin adapters.
  • Memory trick

    • Remove, Rename, Retype, Require: four R that break.
    • Deprecation looks back, Sunset looks forward.
    • Wall sockets with an adapter.
  • Do from day one

    • v1 in the path.
    • Ignore unknown fields.
    • Log version and client.
  • Avoid

    • Latest as a silent default.
    • Header versions without Vary.
    • Removing by date alone.