In this post I will discuss how to use Azure API Management to build a scalable and cost effective API gateway.

API Gateway

First of all, what is an API gateway?

Conceptually an API gateway is a layer between incoming client requests and your underlying API services. This is a pattern you see a lot when dealing with microservices. Instead of allowing clients to hit your microservices directly, all requests go through your API gateway before being routed to the appropriate microservice on the backend. Routing requests through an API gateway offers a lot benefits since you now have a central location for consolidating concerns like authentication, request/response transformation and validation.

The benefits I mention above are some of the more generic benefits that come with API gateways as a pattern by default. In the following section I want to focus on some additional benefits that you may see in terms of scalability and cost effectiveness.

There are many ways to configure an API gateway, but in this article I am using Azure’s API management product in combination with Azure functions.

Serverless Functions

Serverless Azure functions have some very appealing properties that make them both very scalable and cost effective. The first part is that Azure will only charge you for the computing cost (e.g CPU utilization) per function call, with no extra charge from statically allocated hosting hardware. Additionally if you are using API management to configure your API gateway you may also want to enable autoscaling of the functions to spin up more instances as traffic increases. In some ways this is the best of both worlds since you get automatic scaling, but only pay for it when you need it to accommodate spikes in traffic.

Demo

The code listing below shows a simple C# Azure function used for the purposes of this article.

After deploying the function to Azure I imported it from my Azure API management instance running in the consumption pricing tier.

For more information on how to configure the API management instance I recommend checking out this great documentation provided by Microsoft.

[FunctionName("Demo")] public static async Task Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string name = ComputeName(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; return (ActionResult)new OkObjectResult($"Hello, {name}") }

To measure how the function scales out during increased load I ran a load test hitting the API gateway with an increasing load. By going to the live metrics pane of the Azure function I was able to watch real time as the server instance count increased with the load. For my specific test case it started from 0 and ran up to 29 server instances as my load test peaked. See screenshot below: