Improve Performance with Proxy Caching

    If you are following the getting started workflow, make sure you have completed Protect your Services before continuing.

    Kong Gateway delivers fast performance through caching. The Proxy Caching plugin provides this fast performance using a reverse proxy cache implementation. It caches response entities based on the request method, configurable response code, content type, and can cache per Consumer or per API.

    Cache entities are stored for a configurable period of time. When the timeout is reached, the gateway forwards the request to the Upstream, caches the result and responds from cache until the timeout. The plugin can store cached data in memory, or for improved performance, in Redis.

    Use proxy caching so that Upstream services are not bogged down with repeated requests. With proxy caching, Kong Gateway can respond with cached results for better performance.

    Using Kong Manager

    Using the Admin API

    Using decK (YAML)

    1. Access your Kong Manager instance and your default workspace.

    2. Go to API Gateway and click Plugins.

    3. Click New Plugin.

    4. Scroll down to the Traffic Control section and find the Proxy Caching plugin.

    5. Select to apply the plugin as Global. This means that proxy caching applies to all requests.

    6. Scroll down and complete only the following fields with the parameters listed.

      1. config.cache_ttl:
      2. config.content_type: application/json; charset=utf-8
      3. config.strategy: memory

      Besides the above fields, there may be others populated with default values. For this example, leave the rest of the fields as they are.

    7. Click Create.

    Call the Admin API on port 8001 and configure plugins to enable in-memory caching globally, with a timeout of 30 seconds for Content-Type application/json.

    cURL

    HTTPie

    1. name=proxy-cache \
    2. config.strategy=memory \
    3. config.cache_ttl=30 \
    4. config.content_type="application/json; charset=utf-8"
    1. In the plugins section of your kong.yaml file, add the proxy-cache plugin with a timeout of 30 seconds for Content-Type application/json; charset=utf-8.

      1. plugins:
      2. config:
      3. content_type:
      4. - "application/json; charset=utf-8"
      5. cache_ttl: 30
      6. strategy: memory

      Your file should now look like this:

    2. Sync the configuration:

      1. deck sync

    Let’s check that proxy caching works. You’ll need the Kong Admin API for this step.

    cURL

    HTTPie

    1. curl -i -X GET http://<admin-hostname>:8000/mock/request

    In particular, pay close attention to the values of X-Cache-Status, X-Kong-Proxy-Latency, and X-Kong-Upstream-Latency:

    1. HTTP/1.1 200 OK
    2. X-Cache-Key: d2ca5751210dbb6fefda397ac6d103b1
    3. X-Cache-Status: Miss
    4. ...
    5. X-Kong-Proxy-Latency: 25
    6. X-Kong-Upstream-Latency: 37

    Next, access the /mock route one more time.

    This time, notice the differences in the values of X-Cache-Status, X-Kong-Proxy-Latency, and X-Kong-Upstream-Latency. Cache status is a hit, which means Kong Gateway is responding to the request directly from cache instead of proxying the request to the Upstream service.

    Further, notice the minimal latency in the response, which allows Kong Gateway to deliver the best performance:

    1. HTTP/1.1 200 OK
    2. ...
    3. X-Cache-Key: d2ca5751210dbb6fefda397ac6d103b1
    4. X-Cache-Status: Hit
    5. ...
    6. X-Kong-Proxy-Latency: 0
    7. X-Kong-Upstream-Latency: 1

    To test more rapidly, the cache can be deleted by calling the Admin API:

    cURL

    HTTPie

    In this section, you:

    • Set up the Proxy Caching plugin, then accessed the /mock route multiple times to see caching in effect.
    • Witnessed the performance differences in latency with and without caching.

    Next, you’ll learn about .