Редактиране

Споделяне чрез


Use an external Redis-compatible cache in Azure API Management

APPLIES TO: All API Management tiers

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

Using an external cache allows you 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
  • Use caching with the Consumption tier of API Management
  • Enable caching in the API Management self-hosted gateway

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

Note

Currently, this feature isn't available in workspaces.

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, or another Redis-compatible cache within or outside of Azure, you can skip to the next section.

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

    Sceenshot that shows the Create a resource option highlighted on the left navigation pane in the Azure portal.

  2. On the Get Started pane, enter Azure Cache for Redis in the search bar. In the search results, find Azure Cache for Redis, and then select Create.

    Screenshot that shows Azure Marketplace with Azure Cache for Redis in the search box, and the Create button is highlighted.

  3. On the New Redis Cache pane, on the Basics tab, configure the following settings for your cache:

    Setting Action Description
    Subscription Select your Azure subscription. The subscription to use to create the new instance of Azure Cache for Redis.
    Resource group Select a resource group, or select Create new and enter a new resource group name. A 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.
    DNS name Enter a unique name. The cache name must be a string of 1 to 63 characters that contains only numbers, letters, and hyphens. The name must start and end with a number or letter, and it can't contain consecutive hyphens. Your cache instance's host name is \<DNS name>.redis.cache.windows.net.
    Location Select a location. An Azure region that is near other services that use your cache.
    Cache SKU Select a SKU. The SKU determines the size, performance, and feature parameters that are available for the cache. For more information, see Azure Cache for Redis overview.
    Cache size Select a cache size. For more information, see Azure Cache for Redis overview.
  4. Select the Networking tab or select Next: Networking.

  5. On the Networking tab, select a connectivity method to use for the cache.

  6. Select the Advanced tab or select Next: Advanced.

  7. On the Advanced pane, verify or select an authentication method based on the following information:

    Screenshot showing the Advanced pane and the available options to select.

    • By default, for a new Basic, Standard, or Premium cache, Microsoft Entra Authentication is enabled and Access Keys Authentication is disabled.
    • For Basic or Standard caches, you can choose the selection for a non-TLS port.
    • For Standard and Premium caches, you can choose to enable availability zones. You can't disable availability zones after the cache is created.
    • For a Premium cache, configure the settings for non-TLS port, clustering, managed identity, and data persistence.

    Important

    For optimal security, we recommend that you use Microsoft Entra ID with managed identities to authorize requests against your cache if possible. Authorization by using Microsoft Entra ID and managed identities provides superior security and ease of use over shared access key authorization. For more information about using managed identities with your cache, see Use Microsoft Entra ID for cache authentication.

  8. (Optional) Select the Tags tab or select Next: Tags.

  9. (Optional) On the Tags tab, enter a tag name and value if you want to categorize your cache resource.

  10. Select the Review + create button.

    On the Review + create tab, Azure automatically validates your configuration.

  11. After the green Validation passed message appears, select Create.

A new cache deployment occurs over several minutes. You can monitor the progress of the deployment on the Azure Cache for Redis Overview pane. When Status displays Running, the cache is ready to use.

Deploy Redis cache to Kubernetes

For a self-hosted gateway, caching requires an external cache. For caching to be effective, a self-hosted gateway and the cache it relies 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. Learn how to deploy Redis cache to a Kubernetes cluster.

Add an external cache

Follow the steps below to add an external Redis-compatible cache in Azure API Management. You can limit the cache to a specific gateway in your API Management instance.

Screenshot that shows how to add an external Azure Cache for Redis in Azure API Management.

Use from setting

The Use from setting in the configuration specifies the location of your API Management instance that will use the cache. Select one of the following:

  • The Azure region where the API Management instance is hosted (or one of the configured locations, if you have a multi-region deployment)

  • A self-hosted gateway location

  • Default, to configure the cache as the default for all gateway locations in the API Management instance

    A cache used for Default will be overridden by a cache used for a specific matching region or location.

    For example, consider an API Management instance that's hosted in the East US, Southeast Asia, and West Europe regions. There are two caches configured, one for Default and one for Southeast Asia. In this example, API Management in Southeast Asia will use its own cache, while the other two regions will use the Default cache entry.

Note

You can configure the same external cache for more than one API Management instance. The API Management instances can be in the same or different regions. When sharing the cache for more than one instance, you must select Default in the Use from setting.

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. Select 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. Select Save.

Add a Redis-compatible cache 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. Select 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 (or Redis-compatible cache) connection string in the Connection string field.
  7. Select 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. Select 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. Select Save.

Use the external cache

After adding a Redis-compatible cache, configure caching policies to enable response caching, or caching of values by key, in the external cache.

For a detailed example, see Add caching to improve performance in Azure API Management.

Next steps