HAProxy Configuration: Connection Limits
When configuring HAProxy, understanding the connection limits is crucial for optimal performance. This guide will explain the maxconn settings at both the global and server levels.
Global Settings
The global section of your HAProxy configuration defines parameters that apply to the entire HAProxy instance. Here’s an example:
# Global settings
global
log 127.0.0.1 syslog emerg
maxconn 4000 # Maximum total connections allowed
quiet
user haproxy
group haproxy
daemon
In this example, maxconn 4000 sets the maximum number of concurrent connections that HAProxy can handle across all frontends and backends. If this limit is reached, additional connections will be rejected until existing connections are closed.
Default Settings
The defaults section specifies common settings for all proxies unless overridden:
# Default settings for all proxies
defaults
mode http
log global
option abortonclose
option dontlognull
timeout connect 10s
timeout client 5m
timeout server 5m
Listen Section
The listen section combines frontend and backend configurations. Here’s how you might set it up:
listen http_proxy localhost:81
balance roundrobin
option httpchk GET /empty.html
server server1 myip:80 maxconn 15 check inter 10s
server server2 myip:80 maxconn 15 check inter 10s
In this configuration, each server is limited to maxconn 15, meaning that at most 15 connections will be sent to each server at any given time. This is particularly useful when the backend service (like PHP-FPM) has a limited number of child processes, ensuring that HAProxy manages the load effectively.
Key Questions Addressed
What happens if global connections exceed 4000?
- Connections beyond this limit will be rejected. HAProxy does not queue them; instead, it simply denies new connections until the number drops below the limit.
How are global and server connection limits related?
- The global limit caps the total number of connections across all servers, while each server can have its own limit. The server limits must always be considered within the global limit.
How to calculate global connections?
- The global connection limit should encompass the sum of all server-level limits, plus any additional connections that might be needed for pooling or other overheads.
Understanding these settings helps in configuring HAProxy for optimal performance and resource management. Adjust these values based on your specific application needs and server capabilities.