Performance
Traffic shaping
Available since
- HAProxy 2.7
- HAProxy Enterprise 2.6r1
- HAProxy ALOHA 14.5
Traffic shaping allows you to control the bandwidth of data flow into and out of your load balancers. These measures are important in avoiding networking congestion issues such as excessive latency.
Traffic shaping involves delaying HTTP requests and responses when bandwidth consumption exceeds specified limits. By contrast, the process of dropping traffic under extreme conditions is called traffic policing.
Traffic shaping is configured separately for uploads vs downloads.
- For uploads, the filter bwlim-indirective defines the filter, and thehttp-request set-bandwidth-limitdirective enables it.
- For downloads, the filter bwlim-outdirective defines the filter, and thehttp-response set-bandwidth-limitdirective enables it.
Ordering of filter lines
The position of a filter bwlim-in or filter bwlim-out line relative to other filters in the configuration affects how they influence traffic. For example, if a compression filter precedes a bwlim filter, the bwlim filter is applied to compressed traffic and is therefore less likely to delay any traffic. However, if the compression filter follows the bwlim filter, more traffic, being uncompressed, will be delayed.
Set a bandwidth limit for downloads Jump to heading
You can limit the network bandwidth used when sending data to a client. This limit is applied per stream and not at the connection level, meaning that for multiplexed protocols like HTTP/2, where a single connection can simultaneously transfer multiple requests and responses, each of those streams will have its own limit. The limit is expressed in bytes per second.
Use cases for this configuration include:
- Setting the maximum download speed for a high-definition video file to be 5 Mbps so that users who have faster connections cannot download faster than that, which would not improve the video quality but could consume bandwidth away from other users.
- Setting a more constricted download speed for bots (including search engine crawlers).
To set a download speed limit:
- 
In the frontendsection where you would like to enable the limit, add afilter bwlim-outdirective that setsdefault-limitanddefault-period. Thedefault-limitargument sets the number of bytes that can be transferred during the interval defined bydefault-period.Below, the value 62500, which represents bytes, equals 5 Mbps (0.625 megabytes per second = 5 megabits per second).haproxyfrontend myfrontendmode httpbind :80filter bwlim-out mylimit default-limit 625000 default-period 1shaproxyfrontend myfrontendmode httpbind :80filter bwlim-out mylimit default-limit 625000 default-period 1s
- 
Add the http-response set-bandwidth-limitdirective, which enables the filter.haproxyfrontend myfrontendmode httpbind :80filter bwlim-out mylimit default-limit 625000 default-period 1shttp-response set-bandwidth-limit mylimithaproxyfrontend myfrontendmode httpbind :80filter bwlim-out mylimit default-limit 625000 default-period 1shttp-response set-bandwidth-limit mylimitYou can add an ifstatement to the end of thehttp-response set-bandwidth-limitline to set the bandwidth limit value conditionally.The filter bwlim-outdirective defines the filter, but it doesn’t enable it. The filter isn’t enabled until it is specified using thehttp-response set-bandwidth-limitdirective.
Set a bandwidth limit for uploads Jump to heading
You can limit the network bandwidth used when receiving data from a client. This limit is applied per stream and not at the connection level, meaning that for multiplexed protocols like HTTP/2, where a single connection can simultaneously transfer multiple requests and responses, each of those streams will have its own limit. The limit is expressed in bytes per second.
Use cases for this configuration include limiting the bandwidth used when a client uploads a large file.
To set an upload speed limit:
- 
In the frontendsection where you would like to enable the limit, add afilter bwlim-indirective that setsdefault-limitanddefault-period. Thedefault-limitargument sets the number of bytes that can be transferred during the interval defined bydefault-period.The value 625000, which represents bytes, equals 5 Mbps (0.625 megabytes per second = 5 megabits per second).haproxyfrontend myfrontendmode httpbind :80filter bwlim-in mylimit default-limit 625000 default-period 1shaproxyfrontend myfrontendmode httpbind :80filter bwlim-in mylimit default-limit 625000 default-period 1s
- 
Add the http-request set-bandwidth-limitdirective, which enables the filter.haproxyfrontend myfrontendmode httpbind :80filter bwlim-in mylimit default-limit 625000 default-period 1shttp-request set-bandwidth-limit mylimithaproxyfrontend myfrontendmode httpbind :80filter bwlim-in mylimit default-limit 625000 default-period 1shttp-request set-bandwidth-limit mylimitYou can add an ifstatement to the end of thehttp-request set-bandwidth-limitline to set the bandwidth limit value conditionally.The filter bwlim-indirective defines the filter, but it doesn’t enable it. The filter isn’t enabled until you use it with thehttp-request set-bandwidth-limitdirective.
Set a bandwidth limit per backend Jump to heading
You can limit the network bandwidth used by a particular application by defining a limit for a given backend (group of servers). This can be for either download or upload speed. The limit is applied across all requests and responses for all clients accessing the application, giving the application a total bandwidth allotment. The limit is expressed in bytes per second.
Use cases for this configuration include prioritizing throughput for important applications, while limiting the maximum throughput of less important applications.
To set a path-based bandwidth limit:
- 
Define a stick table that will store the outbound bytes-per-second rate and another that will store the inbound bytes-per-second rate. The key for each record in the table will be an integer indicating the backend’s identifier, so set typetointeger. To have this work in an active-active or active-standby load balancer setup, it’s best to define the tables in apeerssection, as shown below:haproxypeers mypeerspeer hapee 127.0.0.1:10000table downloadrate type integer size 1m expire 3600s store bytes_out_rate(1s)table uploadrate type integer size 1m expire 3600s store bytes_in_rate(1s)haproxypeers mypeerspeer hapee 127.0.0.1:10000table downloadrate type integer size 1m expire 3600s store bytes_out_rate(1s)table uploadrate type integer size 1m expire 3600s store bytes_in_rate(1s)
- 
In the backendsection where you would like to enable the limit:- add a filter bwlim-outdirective to limit download speeds
- add a filter bwlim-indirective to limit upload speeds
 Set the following arguments for each filter: - limit <size>: defines the bytes-per-second maximum
- key <pattern>: adds or updates a record in the stick table using the backend’s identifier as the table key
- table <table>: references the stick table where the application’s current data transfer information is stored
 haproxybackend webserversserver web1 192.168.56.6:80 check maxconn 30server web2 192.168.56.7:80 check maxconn 30filter bwlim-out mydownloadlimit limit 625000 key be_id table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key be_id table mypeers/uploadratehaproxybackend webserversserver web1 192.168.56.6:80 check maxconn 30server web2 192.168.56.7:80 check maxconn 30filter bwlim-out mydownloadlimit limit 625000 key be_id table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key be_id table mypeers/uploadrate
- add a 
- 
Add the http-response set-bandwidth-limitandhttp-request set-bandwidth-limitdirectives to the backend, which enable the filters.haproxybackend webserversserver web1 192.168.56.6:80 check maxconn 30server web2 192.168.56.7:80 check maxconn 30filter bwlim-out mydownloadlimit limit 625000 key be_id table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key be_id table mypeers/uploadratehttp-response set-bandwidth-limit mydownloadlimithttp-request set-bandwidth-limit myuploadlimithaproxybackend webserversserver web1 192.168.56.6:80 check maxconn 30server web2 192.168.56.7:80 check maxconn 30filter bwlim-out mydownloadlimit limit 625000 key be_id table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key be_id table mypeers/uploadratehttp-response set-bandwidth-limit mydownloadlimithttp-request set-bandwidth-limit myuploadlimitThe filter bwlim-inandfilter bwlim-outdirectives define filters, but they don’t enable them. Enable thebwlim-infilter by using it with thehttp-request set-bandwidth-limitdirective. Likewise, enable thebwlim-outfilter by using it with thehttp-response set-bandwidth-limitdirective.When using the tableargument, you cannot override the initial limit set by thefilterdirective via thehttp-response set-bandwidth-limitandhttp-request set-bandwidth-limitdirectives.
Set a bandwidth limit per client IP Jump to heading
You can limit the network bandwidth used by a single client, based on their IP address. This can be for either download or upload speed. The limit is applied across all of the client’s streams, giving them a total bandwidth allotment. The limit is expressed in bytes per second.
Use cases for this configuration include ensuring that clients cannot consume an unfair portion of your bandwidth, applied across all of their requests.
To set a per-client bandwidth limit:
- 
Define a stick table that will store the outbound bytes-per-second rate and another that will store the inbound bytes-per-second rate. The key for each record in the table will be an IP address, so set typetoip. To have this work in an active-active or active-standby load balancer setup, it’s best to define the tables in apeerssection, as shown below:haproxypeers mypeerspeer hapee 127.0.0.1:10000table downloadrate type ip size 1m expire 3600s store bytes_out_rate(1s)table uploadrate type ip size 1m expire 3600s store bytes_in_rate(1s)haproxypeers mypeerspeer hapee 127.0.0.1:10000table downloadrate type ip size 1m expire 3600s store bytes_out_rate(1s)table uploadrate type ip size 1m expire 3600s store bytes_in_rate(1s)
- 
In the frontendsection where you would like to enable the limit:- add a filter bwlim-outdirective to limit download speeds
- add a filter bwlim-indirective to limit upload speeds
 Set the following arguments on each filter: - limit <size>: defines the bytes-per-second maximum
- key <pattern>: adds or updates a record in the stick table using the client’s source IP address as the table key
- table <table>: references the stick table where the client’s current data transfer information is stored
 haproxyfrontend fe_mainbind :80filter bwlim-out mydownloadlimit limit 625000 key src table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key src table mypeers/uploadratehaproxyfrontend fe_mainbind :80filter bwlim-out mydownloadlimit limit 625000 key src table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key src table mypeers/uploadrate
- add a 
- 
Add the http-response set-bandwidth-limitandhttp-request set-bandwidth-limitdirectives frontend, which enable the filters.haproxyfrontend fe_mainbind :80filter bwlim-out mydownloadlimit limit 625000 key src table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key src table mypeers/uploadratehttp-response set-bandwidth-limit mydownloadlimithttp-request set-bandwidth-limit myuploadlimithaproxyfrontend fe_mainbind :80filter bwlim-out mydownloadlimit limit 625000 key src table mypeers/downloadratefilter bwlim-in myuploadlimit limit 625000 key src table mypeers/uploadratehttp-response set-bandwidth-limit mydownloadlimithttp-request set-bandwidth-limit myuploadlimitThe filter bwlim-inandfilter bwlim-outdirectives define filters, but they don’t enable them. Enable thebwlim-infilter by using it with thehttp-request set-bandwidth-limitdirective. Likewise, enable thebwlim-outfilter by using it with thehttp-response set-bandwidth-limitdirective.When using the tableargument, you cannot override the initial limit set by the filter directive via thehttp-response set-bandwidth-limitandhttp-request set-bandwidth-limitdirectives.
See also Jump to heading
- For complete information on the filters used for bandwidth limitation, see Bandwidth limitation in the HAProxy Configuration Manual.
- For complete information on peers configurations, see the Peers reference in the HAProxy Configuration Manual.
Do you have any suggestions on how we can improve the content of this page?