Skip to main content

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.

apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-http-cache
namespace: apps
spec:
plugin:
httpCache:
maxTtl: 600
store:
memory:
limit: "3Gi"

Configuration Options

FieldDescriptionDefaultRequired
maxTtlMaximum 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.
300Yes
disableCacheStatusHeaderPrevents the middleware from adding a X-Cache-Status header to responses.
Moreinforamtion here
falseNo
maxStaleAllows 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.0No
excludedResponseCodesResponse status codes or range of response status codes for which the cache is disabled.""No
store.memory.limitMaximum 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 application
  • MISS: The response was fetched from the upstream application and not from the cache
  • BYPASS: 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.

MeasureUnitExponentBytesUnitExponentBytes
kilo-K10^31000Ki10241024
mega-M10^6100 000Mi1024^21 048 576
giga-G10^9100 000 000Gi1024^31 073 741 824
tera-T10^12100 000 000 000Ti1024^41 099 511 627 776
peta-P10^15100 000 000 000Pi1024^51 125 899 906 842 624
exa-E10^18100 000 000 000 000Ei1024^61 152 921 504 606 846 976
Do not exceed the proxy memory limits

The value given to store.memory.limit should remain within the bounds of each gateway's specified memory limit.

Storage options

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.