Use an external Redis-compatible cache in Azure API Management

In addition to utilizing the built-in cache, Azure API Management allows for caching responses in an external Redis-compatible cache, e.g. Azure Cache for Redis.

Using an external cache allows to overcome a few limitations of the built-in cache:

  • Avoid having your cache periodically cleared during API Management updates
  • Have more control over your cache configuration
  • Cache more data than your API Management tier allows to
  • Use caching with the Consumption tier of API Management
  • Enable caching in the API Management self-hosted gateways

For more detailed information about caching, see API Management caching policies and Custom caching in Azure API Management.

Bring your own cache to APIM

What you'll learn:

  • Add an external cache in API Management

Prerequisites

To complete this tutorial, you need to:

Create Azure Cache for Redis

This section explains how to create an Azure Cache for Redis in Azure. If you already have an Azure Cache for Redis, within or outside of Azure, you can skip to the next section.

  1. To create a cache, sign in to the Azure portal and select Create a resource.

    Select Create a resource

  2. On the New page, select Databases and then select Azure Cache for Redis.

    Select Azure Cache for Redis

  3. On the New Redis Cache page, configure the settings for your new cache.

    Setting Suggested value Description
    DNS name Enter a globally unique name. The cache name must be a string between 1 and 63 characters that contains only numbers, letters, or hyphens. The name must start and end with a number or letter, and can't contain consecutive hyphens. Your cache instance's host name will be <DNS name>.redis.cache.windows.net.
    Subscription Drop down and select your subscription. The subscription under which to create this new Azure Cache for Redis instance.
    Resource group Drop down and select a resource group, or select Create new and enter a new resource group name. Name for the resource group in which to create your cache and other resources. By putting all your app resources in one resource group, you can easily manage or delete them together.
    Location Drop down and select a location. Select a region near other services that will use your cache.
    Pricing tier Drop down and select a Pricing tier. The pricing tier determines the size, performance, and features that are available for the cache. For more information, see Azure Cache for Redis Overview.
  4. Select Create.

    Create Azure Cache for Redis

    Note

    You can expect the provisioning of the Cache to take around 15 to 20 minutes.

    You can monitor progress on the Azure Cache for Redis Overview page. When Status shows as Running, the cache is ready to use.

    Azure Cache for Redis created

Deploy Redis cache to Kubernetes

For caching, self-hosted gateways rely exclusively on external caches. For caching to be effective self-hosted gateways and the cache they rely on must be located close to each other to minimize lookup and store latencies. Deploying a Redis cache into the same Kubernetes cluster or in a separate cluster nearby are the best options. Follow this link to learn how to deploy Redis cache to a Kubernetes cluster.

Add an external cache

Follow the steps below to add an external Azure Cache for Redis in Azure API Management.

Bring your own cache to APIM

Note

The Use from setting specifies an Azure region or a self-hosted gateway location that will use the configured cache. The caches configured as Default will be overridden by caches with a specific matching region or location value.

For example, if API Management is hosted in the East US, Southeast Asia and West Europe regions and there are two caches configured, one for Default and one for Southeast Asia, API Management in Southeast Asia will use its own cache, while the other two regions will use the Default cache entry.

Add an Azure Cache for Redis from the same subscription

  1. Browse to your API Management instance in the Azure portal.
  2. Select the External cache tab from the menu on the left.
  3. Click the + Add button.
  4. Select your cache in the Cache instance dropdown field.
  5. Select Default or specify the desired region in the Use from dropdown field.
  6. Click Save.

Add an Azure Cache for Redis hosted outside of the current Azure subscription or Azure in general

  1. Browse to your API Management instance in the Azure portal.
  2. Select the External cache tab from the menu on the left.
  3. Click the + Add button.
  4. Select Custom in the Cache instance dropdown field.
  5. Select Default or specify the desired region in the Use from dropdown field.
  6. Provide your Azure Cache for Redis connection string in the Connection string field.
  7. Click Save.

Add a Redis cache to a self-hosted gateway

  1. Browse to your API Management instance in the Azure portal.
  2. Select the External cache tab from the menu on the left.
  3. Click the + Add button.
  4. Select Custom in the Cache instance dropdown field.
  5. Specify the desired self-hosted gateway location or Default in the Use from dropdown field.
  6. Provide your Redis cache connection string in the Connection string field.
  7. Click Save.

Use the external cache

Once the external cache is configured in Azure API Management, it can be used through caching policies. See Add caching to improve performance in Azure API Management for detailed steps.

Next steps