Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm running an HAProxy load balancing server to balance load to multiple Apache servers. I need to reload HAProxy at any given time in order to change the load balancing algorithm.

This all works fine, except for the fact that I have to reload the server without losing a single packet (at the moment a reload is giving me 99.76% success on average, with 1000 requests per second for 5 seconds). I have done many hours of research about this, and have found the following command for "gracefully reloading" the HAProxy server:

haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)

However, this has little or no effect versus the plain old service haproxy reload, it's still dropping 0.24% on average.

Is there any way of reloading the HAProxy config file without a single dropped packet from any user?

share|improve this question
If you need that much reliability a better solution would be to run more than one instance of HAproxy where you can take one out of service to reload, put it back in and repeat for the other(s). –  yoonix Mar 7 '14 at 21:19

3 Answers 3

up vote 11 down vote accepted

According to https://github.com/aws/opsworks-cookbooks/pull/40 and consequently http://www.mail-archive.com/haproxy@formilux.org/msg06885.html you can:

iptables -I INPUT -p tcp --dport $PORT --syn -j DROP
sleep 1
service haproxy restart
iptables -D INPUT -p tcp --dport $PORT --syn -j DROP

This has the effect of dropping the SYN before a restart, so that clients will resend this SYN until it reaches the new process.

share|improve this answer
both of these commands gave me this: iptables v1.4.14: invalid port/service --syn' specified` –  Dmitri DB Oct 28 '14 at 21:26
@DmitriDB you're supposed to replace $PORT with the actual port haproxy is listening on. If haproxy is listening on multiple ports, write replace --dport $PORT with --dports $PORTS_SEPARATED_BY_COMMAS, e.g., --dports 80,443. –  pepoluan Dec 18 '14 at 11:17

Yelp shared a more sophisticated approach based on meticulous testing.

True Zero Downtime HAProxy Reloads

tl;dr use Linux tc (traffic control) and iptables to temporarily queue SYN packets while HAProxy is reloading and has two pids attached to the same port (SO_REUSEPORT).

Cheers to Yelp for sharing such amazing insights.

share|improve this answer
Excellent link! But perhaps you'd like to summarize it in here in case the link expires. That's the only reason for no upvote. –  Matt May 20 at 22:54

If you're on a kernel that supports SO_REUSEPORT, then this problem shouldn't happen.

The process that haproxy takes when it restarts is:

1) Try setting SO_REUSEPORT when opening the port (https://github.com/haproxy/haproxy/blob/3cd0ae963e958d5d5fb838e120f1b0e9361a92f8/src/proto_tcp.c#L792-L798)

2) Try opening the port (will succeed with SO_REUSEPORT)

3) If it didn't succeed, signal the old process to close its port, wait 10ms and try it all again. (https://github.com/haproxy/haproxy/blob/3cd0ae963e958d5d5fb838e120f1b0e9361a92f8/src/haproxy.c#L1554-L1577)

It was first supported in the Linux 3.9 kernel but some distros have backported it. For example, EL6 kernels from 2.6.32-417.el6 support it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.