Proposal preview: "Universal versioning"
Universal versioning
This is a proposal for expanding the discussion of versioning to include a description of "universal versioning" and encourage its adoption.
:warning: This is the first time I've transcribed this concept, so hopefully it makes some semblance of sense. I plan to revise this proposal based on comments received for clarity and completeness, and eventually close this issue and create a new one with what I would consider to be a more "final draft" type of proposal.
Changes in API versions can be problematic for existing applications. In particular, small businesses who use external contractors to develop applications that interact with web services can be left without a critical piece of functionality after elimination of a deprecated API, even if the service provider continues to offer the underlying functionality under a different name. To avoid unnecessary problems in this space, service authors are encourages to follow a universal versioning practice, which minimizes or eliminates the need to change API versions over time.
Universal versioning involves defining the behavior of new API calls for all preceding versions of a service, even in cases where early versions of a service did not support the call.
Basic characteristics
APIs using universal versioning use a single (fixed) API version for an extended period of time.
The primary impact to consumers of a universal API is existing applications which strictly utilize API behaviors which were documented at the time the application was created will continue to work for an indefinite period of time.
Expanding an API
Expanding an API after the initial release of a service involves two primary steps.
- Define the behavior of the new API in the new version¹ of the service.
- Define the behavior of the new API when invoked in any prior version of the service. This typically involves documenting a specific failure (e.g. 501 as described below), as the distinguishing characteristic(s)² of the new API are not understood by the old version of the service.
By providing both of these definitions, the definition of the new API is constructed in a manner that all versions of the service implement the API in a supported form.
¹ In this context, the "new version" does not actually result in an increase of the major or minor version number used in the URI. ² Should we define distinguishing characteristics in the context of an API? If so, we could define it such that distinguishing characteristics should lie within the reserved API space (next section) to make it easier to define the behavior of new APIs.
Reserved API space
Supporting expansions to an API under a universal versioning approach requires the first version of a universal API to reserve portions of the API space for future expansion. The API space includes several specific items:
- URIs (path and query parameters)
- Request bodies
- HTTP method and headers
Reserved URI for future resources
For any resource resource-name which is not defined by the public API, all HTTP requests to a path starting with the following must return a 501. In addition, a 501 must be returned if the trailing slash is omitted but the resource name is otherwise unchanged.
https://api.contoso.com/v1.0/resource-name/
Unexpected query parameters
The behavior of an HTTP API in the presence of an unrecognized but reserved query parameter, HTTP header, or property of the request body is implementation defined. If the API rejects the call due to the inclusion of an unrecognized reserved element, the return code should be 501.
Deprecation
One of the primary concerns regarding universal versioning is the intentional lack of support for deprecating and eventually removing a supported API call. In general, this design principle increases consumer confidence when developing against an API. However, the identification of certain fundamental performance or security concerns baked into an API may require developers limit or prevent the use of the call at any time.
Deprecation via rate limiting
In cases where performance or security concerns require eliminating support for a prior API, the existing behavior used for reporting failures due to rate limiting may be used to report the API is no longer in service.
Suggested upgrades
The return value used for rate limiting in a universal API should include a provision for notifying callers in cases where an alternate API may be available. When rate limits are reduced (potentially to 0) as a result of a performance or security concern, this provision should be utilized to inform the caller of the change.
If I can summarize what I'm reading, to see if I understand it:
- When new changes are introduced, they should be back-ported to earlier versions.
- Clients on the earlier versions ("/v1.2/") now have access to the same API's that are in "/v6.9".
This approach works for additive APIs - that is, API's that done make breaking changes to behavior. Services such as Azure and O365 spend huge amounts of effots to make sure API's don't require breaking changes, but sometimes there is no choice. I don't see how your proposal can work for changes that are themselves breaking.
Services frequently use their logging infrastructure to determine call rates on older API versions. It's not unusual to get an email/phone call saying "You app is the only user of API-X on Version Y which is 4 years old. We would really like to deprecate this API. Can you please upgrade? ". That way API can be deprecated, freeing the API Service to make any needed changes.
Cheers, Chris
When new changes are introduced, they should be back-ported to earlier versions.
A better way to interpret this is the documentation accommodates for prior versions. For example, you could say that a call has been available since version 1.2, and implementations prior to 1.2 will return a 501 when you call the method. The client is then responsible for ensuring the client behaves properly if a 501 is ever received from this call.
Clients on the earlier versions ("/v1.2/") now have access to the same API's that are in "/v6.9".
In most forms where I've considered this, all URIs used the same version (e.g. /v1.0). In another form, the server could simply maintain redirects such that /vY is handled by /vLATEST for any Y <= LATEST.
Services such as Azure and O365 spend huge amounts of effort to make sure API's don't require breaking changes, but sometimes there is no choice.
In my experience, the overwhelming majority of breaking changes occurred simply because no one stopped to ask if there was a non-breaking change which is just as straightforward. I'd be interested in hearing about scenarios where a breaking change was needed for some reason.
A common example is error codes. The adding of a new error code to an API is often a breaking change to clients. Yet more detailed error codes are often something clients really want added between API versions.
This means front ends are stuck mapping between old/new error codes according to API version, which is just rough and hard to maintain.
@cleemullins There are multiple ways to support new error codes (and more specifically, the need to add new error codes in the future) without needing to make breaking changes. Two possibilities include:
- Make sure the HTTP status code (number), and optionally a reference error code if the response body includes one, does not change over time for a given input. Provide an additional field in the response body that includes more details about the error in human-readable form, and indicate that the value of this field may change over time.
- Allow sub-specialization of error codes. For example, if error code InvalidArg exists and you get an enhancement request for indicating the user omitted a required argument, you could define MissingRequiredArg as a specialization of InvalidArg. To derive this on the client without breaking support for old clients, you could either provide an error codes API which defines derived errors, or return the result in a form with enough information to derive this (e.g.
InvalidArg:MissingRequiredArg).
What you describe is pretty much exactly what the docs call out doing. :)
With that said, over time, it dos accumulate. It's is baggage and legacy and slows down development, makes testing harder, complicates documentation, and so on.
There isn't an ideal answer that I've seen yet.
@sharwell You guidance is pretty much what we tell our teams: never break anything if possible. Old versions of the API still work until there is no more traffic on them. Being explicit about the version number allows for cases when things really do semantically change too far to any longer be supported for us to run the APIs side-by-side without creating an entirely new endpoint.
For a little deeper dive, we distinguish between things we know can break clients and things that must break clients. For example, we've found that simply adding a new, optional field to a JSON payload, which clients can and should ignore, commonly results in live site incidents because (for example) clients used a type-strict serializer and throw when they can't map the field. For these we tell services to increment the version number and still maintain the old one.
Somewhat harder are changes that will break applications who are not expecting it. For example, if we change the auth schema for an API obviously any old clients are going to be dead in the water.
Most subtle and difficult are semantic changes. We introduced a new kind of append blob that did not updating or deleting existing blocks as all older types did. This had the potential to break all generic storage explorers. In that case we had to work with all the explorer applications to make sure they were updated.