All Products
Search
Document Center

API Gateway Caches

Last Updated: May 23, 2019

API Gateway locally caches backend responses of API requests. When subsequent API requests arrive, API Gateway serves the matching backend responses from the cache to the API callers without sending these requests to backend services. This greatly reduces the workload on backend services.

Usage instructions

  • API Gateway caches only GET responses.
  • API Gateway does not cache backend responses to the requests that use the default second-level domain names bound to API groups. The default second-level domain name of each API group can be accessed for up to 1,000 times each day and is used for testing purposes only.
  • You can configure the following parameters in the caching plug-ins to match and serve cached responses based on different criteria:
    • varyByApp: Cached responses are matched and served based on the applications of API callers.
    • varyByParameters: Cached responses are matched and served based on the values of specified parameters. You can extract these parameters from the APIs to which the plug-ins are bound.
    • varyByHeaders: Cached responses are matched and served based on request headers such as Accept and Accept-Language.
  • Each user is allotted a cache space of 5 MB for each region. When the cache space is full, no new responses can be cached. The cached data is automatically deleted upon expiration.
  • API Gateway caches backend responses for the amount of time retrieved from the Cache-Control header in the responses. If a backend response does not contain the Cache-Control header, API Gateway uses the time specified in the Duration field of the plug-in.
  • The maximum cache duration for a response is 48 hours (172,800 seconds). Cache durations longer than 48 hours are considered as 48 hours.
  • API Gateway ignores the Cache-Control headers of all client requests by default. You can configure API Gateway to process the Cache-Control headers by specifying mode in clientCacheControl. The mode field can be set to any of the following values:
    • off: API Gateway ignores the Cache-Control headers of all client requests.
    • all: API Gateway processes the Cache-Control headers of all client requests.
    • app: API Gateway processes only the Cache-Control headers of client requests whose application IDs (AppId) are included in the configured apps list.
  • API Gateway caches only the Content-Typeheaders of backend responses by default. You can useCacheableHeaders to configure API Gateway to cache other response headers.

Plug-in configurations

You can configure caching plug-ins in the JSON or YAML format as these two formats use the same schema. You can use the yaml to json tool to convert the configuration format of a caching plug-in. The following table describes a plug-in configuration template in the YAML format.

  1. ---
  2. varyByApp: false # Controls whether to match and serve cached responses based on the application IDs of API callers. Default value: false.
  3. varyByParameters: # Controls whether to match and serve cached responses based on the values of specified parameters.
  4. - userId # The name of a backend parameter. If the backend parameter is mapped to a parameter with a different name, set userId to the mapped parameter name.
  5. varyByHeaders: # Controls whether to match and serve cached responses based on different request headers.
  6. - Accept #Cached responses are matched and served based on the Accept header.
  7. clientCacheControl: # API Gateway determines how to process the Cache-Control headers of client requests based on the clientCacheControl settings.
  8. mode: "app" # Valid values: off, all, and app. Default value: off. off indicates that API Gateway ignores the Cache-Control headers of all client requests. all indicates that API Gateway processes the Cache-Control headers of all client requests. app indicates that API Gateway processes only the Cache-Control headers of client requests whose application IDs are included in the configured apps list.
  9. apps: # A list of application IDs. If mode is set to app, API Gateway processes only the Cache-Control headers of client requests whose application IDs are in this list.
  10. - 1992323 #A sample application ID. It is not an AppKey.
  11. - 1239922 #A sample application ID. It is not an AppKey.
  12. cacheableHeaders: # The cacheable response headers. API Gateway caches only the Content-Type and Content-Length headers of backend responses by default.
  13. - X-Customer-Token # The name of a cacheable response header.
  14. duration: 3600 # The default response cache duration. Unit: seconds.