Service reliability
Retries and redispatches
The retry and redispatch options attempt to reconnect after a failed connection or resend after a failed HTTP request, which can protect against the following faults:
- network connectivity issues (including SSL handshake errors and unexpected server failures)
- HTTP errors
- servers taking too long to respond
First, consider the behavior of health checks, which monitor each backend server’s connectivity and remove problematic servers from the load balancing rotation. After removing a server from load balancing, the flow of traffic is stopped to that server, which is preferable when the connectivity problem is likely to be long-lasting.
Nevertheless, not all connection problems are long lasting and retrying the connection immediately could successfully overcome short-term connection problems. Connection retries are enabled by default, but you can change their behavior.
Set the number of retries Jump to heading
By default, the load balancer will retry a failed connection 3 times. To change the number of times the load balancer will try to reconnect, set the retries
directive to a different value.
-
In a
defaults
orbackend
section, add aretries
directive and set its value to the number of times to retry a failed connection attempt.Below, we attempt to connect to the server 4 times before giving up and returning a
Service Unavailable
error to the client:haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4 -
Optional: Add the
timeout connect
directive to set the delay between retries.Below, the server performs the next retry after 3 seconds, optimistically giving the server time to overcome from short-term connectivity issues.
haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4timeout connect 3shaproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4timeout connect 3s
Change the value with set-retries Jump to heading
This section applies to:
- HAProxy 3.1 and newer
- HAProxy Enterprise 3.1 and newer
- HAProxy ALOHA 17.0 and newer
Use tcp-request content set-retries
if using mode tcp
or http-request set-retries
if using mode http
to change the value of the retries
directive dynamically. Assign an interger between 0 and 100. For example, suppose that all servers except for one have become unreachable. In that case, you might make more connection attempts to the server that you have left.
Below, we use nbsrv
to check the number of servers that are currently up in the webservers
backend. If it’s only one, we use http-request set-retries
to change the retries value to 10.
haproxy
backend webserversmode httpbalance roundrobinretry-on all-retryable-errors# Default retries valueretries 3# Dynamically change retries value if only one server is uphttp-request set-retries 10 if { nbsrv(webservers) 1 }# list of serversserver web1 172.16.0.10:80 check maxconn 30server web2 172.16.0.11:80 check maxconn 30server web3 172.16.0.12:80 check maxconn 30
haproxy
backend webserversmode httpbalance roundrobinretry-on all-retryable-errors# Default retries valueretries 3# Dynamically change retries value if only one server is uphttp-request set-retries 10 if { nbsrv(webservers) 1 }# list of serversserver web1 172.16.0.10:80 check maxconn 30server web2 172.16.0.11:80 check maxconn 30server web3 172.16.0.12:80 check maxconn 30
Redispatch to a different server Jump to heading
Instead of trying to reconnect to the same server, you can configure option redispatch
to try with a different server in the backend
.
-
Add the
option redispatch
directive to retry with a different server:haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4option redispatchhaproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4option redispatch -
Optional: Add a number of times to retry with the same server before redispatching to a different server. Below, we dispatch the request to another server after 2 failed retries:
haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4option redispatch 2haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4option redispatch 2
Set when to retry Jump to heading
Available since
- HAProxy 2.0
- HAProxy Enterprise 2.0r1
- HAProxy ALOHA 11.5
Beyond retrying after a failed connection, you can also enable other conditions that should trigger a retry. Use the retry-on
directive to specify the conditions.
-
Add the
retry-on
directive to define types of HTTP response codes that should trigger a retry. Below, we retry when the request fails due to failure503 Service Unavailable
or504 Gateway Timeout
:haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4retry-on 503 504haproxybackend webserversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkretries 4retry-on 503 504Options for the codes are:
- 404
- 408
- 425
- 500
- 501
- 502
- 503
- 504
If you set
retry-on
, the default behavior of retrying failed TCP connections is not enabled unless you setretry-on
toall-retryable-errors
or a specific connection failure condition, such asconn-failure
. -
Optional: Add to the
retry-on
directive specific types of TCP connection problems that should also trigger a retry, in addition to HTTP response codes. Options include:- conn-failure
- empty-response
- junk-response
- response-timeout
- 0rtt-rejected
- all-retryable-errors
Below, we retry the connection if the connection could not be established or was closed while sending the request, in addition to when receiving a 503 response:
haproxybackend webserversretry-on conn-failure empty-response 503haproxybackend webserversretry-on conn-failure empty-response 503
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?