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)
Access your Kong Manager instance and your default workspace.
Go to API Gateway and click Plugins.
Click New Plugin.
Scroll down to the Traffic Control section and find the Proxy Caching plugin.
Select to apply the plugin as Global. This means that proxy caching applies to all requests.
Scroll down and complete only the following fields with the parameters listed.
- config.cache_ttl:
- config.content_type:
application/json; charset=utf-8
- 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.
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
name=proxy-cache \
config.strategy=memory \
config.cache_ttl=30 \
config.content_type="application/json; charset=utf-8"
In the
plugins
section of yourkong.yaml
file, add theproxy-cache
plugin with a timeout of 30 seconds for Content-Typeapplication/json; charset=utf-8
.plugins:
config:
content_type:
- "application/json; charset=utf-8"
cache_ttl: 30
strategy: memory
Your file should now look like this:
Sync the configuration:
deck sync
Let’s check that proxy caching works. You’ll need the Kong Admin API for this step.
cURL
HTTPie
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
:
HTTP/1.1 200 OK
X-Cache-Key: d2ca5751210dbb6fefda397ac6d103b1
X-Cache-Status: Miss
...
X-Kong-Proxy-Latency: 25
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:
HTTP/1.1 200 OK
...
X-Cache-Key: d2ca5751210dbb6fefda397ac6d103b1
X-Cache-Status: Hit
...
X-Kong-Proxy-Latency: 0
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 .