HTTP Cache
The performance of websites and applications can be significantly improved by reusing previously fetched resources. HTTP caches reduce latency and network traffic and thus lessen the time needed to display a representation of a resource. By making use of HTTP caching, Websites and applications become more responsive as the APIs powering them become faster.
Traefik Hub API Gateway HTTP Cache Middleware allows you to add caching to your routers and improve the performance of your infrastructure. The HTTP cache middleware follows the RFC 7234.
Configuration Example
Below is an advanced configuration that enables the memory cache store, sets its size limit to 3Gi and also assigns a maximum TTL of 600 seconds to cached entries.
- Middleware HTTP Cache
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-http-cache
namespace: apps
spec:
plugin:
httpCache:
maxTtl: 600
store:
memory:
limit: "3Gi"
Configuration Options
Field | Description | Default | Required |
---|---|---|---|
maxTtl | Maximum amount of seconds after which cached HTTP responses are expire and invalidated. The time after which an HTTP response is no longer cached will be the lowest value between maxTtl and the specified expiry time in the HTTP response headers.For example, if a response has an Expires header set to expire after 10 minutes but the middleware is configured with a maxTtl of 5 minutes, the response will only be cached for 5 minutes. | 300 | Yes |
disableCacheStatusHeader | Prevents the middleware from adding a X-Cache-Status header to responses.More information here | false | No |
maxStale | Allows the middleware to serve stale responses when allowed by the request's cache-control directive max-stale . It controls the duration in seconds a stale cached entry is kept in cache and served. | 0 | No |
excludedResponseCodes | Response status codes or range of response status codes for which the cache is disabled. | "" | No |
store.memory.limit | Maximum amount of memory that the cache can use for storing HTTP responses. More information here | "1Gi" | No |
disableCacheStatusHeader
By default, the middleware adds a cache status header (X-Cache-Status
) to responses, to indicate whether the request did HIT
, MISS
or BYPASS
the cache.
HIT
: The response was fetched from the cache, and the request did not reach the upstream applicationMISS
: The response was fetched from the upstream application and not from the cacheBYPASS
: The request or response did not allow HTTP caching
The option disableCacheStatusHeader
prevents the middleware from adding this header.
store.memory.limit
Its value is measured in bytes, and can be expressed as a plain integer or as a fixed-point integer using one of
these suffixes: E
, P
, T
, G
, M
, K
. You can also use the power-of-two
equivalents: Ei
, Pi
, Ti
, Gi
, Mi
, Ki
.
Measure | Unit | Exponent | Bytes | Unit | Exponent | Bytes |
---|---|---|---|---|---|---|
kilo- | K | 10^3 | 1000 | Ki | 1024 | 1024 |
mega- | M | 10^6 | 100 000 | Mi | 1024^2 | 1 048 576 |
giga- | G | 10^9 | 100 000 000 | Gi | 1024^3 | 1 073 741 824 |
tera- | T | 10^12 | 100 000 000 000 | Ti | 1024^4 | 1 099 511 627 776 |
peta- | P | 10^15 | 100 000 000 000 | Pi | 1024^5 | 1 125 899 906 842 624 |
exa- | E | 10^18 | 100 000 000 000 000 | Ei | 1024^6 | 1 152 921 504 606 846 976 |
The value given to store.memory.limit
should remain within the bounds of each gateway's specified memory limit.
For now, Traefik Hub API Gateway only provides an in-memory store option.
The memory storage stores the cache content in the gateway's memory. It is efficient and requires very little configuration. However, with this storage each gateway replica maintains its own cache and does not share it with other instances.
The memory limit is set per middleware and is not global to the gateway. If you have multiple middlewares with different memory limits, they will add-up, but if you share the same middleware across multiple routers they will use the same limit.