Network performance

Caching

Available since

  • HAProxy 2.1
  • HAProxy Enterprise 2.1r1
  • HAProxy ALOHA 12.0

Caching lets you offload work from your application servers by returning cached resources directly from the load balancer. It can be applied to any reusable content that is safe to be shared among multiple clients, such as:

  • CSS, Javascript and icon files
  • 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 Jump to heading

  1. Add a cache section to your configuration.

    haproxy
    cache mycache
    total-max-size 4
    max-object-size 10000
    max-age 240
    haproxy
    cache mycache
    total-max-size 4
    max-object-size 10000
    max-age 240
    Directive Description
    total-max-size Sets the total memory the cache can consume in megabytes. The maximum value is 4095.
    max-object-size Sets the max size in bytes for a single object; Larger objects will not be stored. It cannot be larger than half of total-max-size.
    max-age Sets the seconds for an object to stay the cache; It can be overridden with a Cache-Control header.
  2. Add an http-request cache-use and an http-response cache-store directive to a frontend or listen section to enable caching in that section. The filter line is required only when the section includes other filter lines, but otherwise can be omitted, since it is implicitly defined.

    haproxy
    frontend fe_main
    bind :80
    filter cache mycache
    http-request cache-use mycache
    http-response cache-store mycache
    default_backend be_main
    haproxy
    frontend fe_main
    bind :80
    filter cache mycache
    http-request cache-use mycache
    http-response cache-store mycache
    default_backend be_main
    Directive Description
    http-request cache-use Fetches the requested resource from the cache. Place an if or unless statement afterwards to include or exclude a resource from being cached.
    http-response cache-store Saves the resource to the cache once it comes from the server.

Cache based on a condition Jump to heading

You can select which resources to cache by using an if statement on the http-request cache-use line. You need an if statement only on the http-request cache-use line and not the http-response cache-store line too. The load balancer will not store items in the cache if there is no chance of them being used.

Below, we add the ACL path_beg /api/ to cache responses when the requested URL path begins with /api/:

haproxy
cache mycache
total-max-size 4095
max-object-size 10000
max-age 30
backend servers
http-request cache-use mycache if { path_beg /api/ }
http-response cache-store mycache
server s1 192.168.1.25:80 check
server s2 192.168.1.26:80 check
haproxy
cache mycache
total-max-size 4095
max-object-size 10000
max-age 30
backend servers
http-request cache-use mycache if { path_beg /api/ }
http-response cache-store mycache
server s1 192.168.1.25:80 check
server s2 192.168.1.26:80 check

Cache restrictions Jump to heading

Objects are cached only if all of the following are true:

  • The size of the resource does not exceed max-object-size
  • 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

See also Jump to heading

Do you have any suggestions on how we can improve the content of this page?