coalmee
coalmee

Reputation: 1556

Asp.Net Web Api Versioning with Load Balancer (Layer 4)

I'm currently in charge of developing a rather complex REST api with .Net and Web Api.

The interface should later be consumed by various public and private (in house) Clients. I read a lot about how you should version your api, but opinions seem to be very different.

The application runs behind a load balancer in a multi server environment.

Now my problem is, the load balancer is hard restricted and only supports layer 4 load balancing, so I'm not able to inspect the URL / headers etc. of the incoming requests to route them to the correct version of the application.

We don't want to have versioned api controllers in our code base since we have a lot external dependecies which should be updated frequently, so we could break some functionality.

Currently it seems to be the only solution to use subdomains for versioning, e.g.

ver1.api.domain.com

Is there anything against this approach, do you have other solutions?

Upvotes: 1

Views: 1024

Answers (1)

Toan Nguyen
Toan Nguyen

Reputation: 11591

The issue with that versioning approach is that there will be duplicate entries for all resources including un-modified ones.

In my opinion, a better approach is to include the version of a resource in the Uri. Let's have a look at a concrete example. Supposing there is a CarsController like the one below:

public class CarsController : ApiController
{
    [Route("cars/{id}")]
    public async Task<IHttpActionResult> Get(int id)
    {

        DoSomething();

        return Ok(result);
    }


}

After the first release, we introduce the second version of the Get method, we can do something like

[Route("cars/{id}/v2")]

public async Task<IHttpActionResult> GetCarsVersion2(int id)
{
    DoSomethingElse();

    return Ok(result);

}

So the existing clients still refer to the old Uri /Cars/{id}, whereas new clients can use the new Uri /Cars/{id}/v2.

In addition, if there aren't many differences between the two versions, the original implementation can be refactored to satisfy new requirements. Which, in turns, reduces code duplication.

Upvotes: 1

Related Questions