Caching lets you offload work from your application servers by returning cached resources directly from HAProxy Enterprise. It can be applied to any reusable content that is safe to be shared among multiple clients, such as:
API responses (e.g. JSON) that do not contain client-specific data
Small, static HTML pages
The cache runs in memory and does not store state on disk. This makes it very fast, but not durable past a reload of the load balancer. We recommend storing objects for a short duration, such as less than a minute to reduce the chance of serving stale content.
Enable the cache
cachesection to your configuration.
cache mycache total-max-size 4 max-object-size 10000 max-age 240
Sets the total memory the cache can consume in megabytes. The maximum value is 4095.
Sets the max size in bytes for a single object; Larger objects will not be stored. It cannot be larger than half of
Sets the seconds for an object to stay the cache; It can be overriden with a Cache-Control header.
http-request cache-useand an
http-response cache-storedirective to a
listensection to enable caching in that section. The
filterline is required only when the section includes other
filterlines, but otherwise can be omitted, since it is implicitly defined.
frontend fe_main bind :80 filter cache mycache http-request cache-use mycache http-response cache-store mycache default_backend be_main
Fetches the requested resource from the cache. Place an if or unless statement afterwards to include or exclude a resource from being cached.
Saves the resource to the cache once it comes from the server.
Objects are cached only if all of the following are true:
The size of the resource does not exceed
The response from the server is 200 OK
The response does not have a Vary header
The response does not have a Cache-Control: no-cache header