Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haproxy close connections to backup hosts when primary comes back

Question

Can I get haproxy to close all connections to backup hosts when a primary host becomes available after being down?

Context

I am using HAproxy to do failover for pubsub.

The haproxy backend config looks something like this:

listen pubsub 0.0.0.0:1234
    mode tcp

    server primary primary.x.com:1234 weight 1 inter 500 rise 10 fall 5 check
    server backup backup.x.com:1234 weight 1 inter 500 rise 10 fall 5 check backup

I make long lived connections for both the publish and the subscribe sides.

the problem is in the scenario that:

  • primary goes down
  • A connects to backup and publishes
  • B connects to backup and subscribes
  • primary is fixed and becomes alive again
  • C connects to primary and publishes but B does not receive it
  • D connects to primary and subscribes but does not get A's messages

The best solution to this problem that I can see is to have haproxy forcibly close all connections to backup hosts when a primary becomes available again.

like image 806
Nick Avatar asked Jun 09 '13 03:06

Nick


People also ask

What is keep-alive in HAProxy?

Keep-alive and server maxconnIt can be used to protect servers against traffic spikes by routing requests to other servers or by queueing requests within HAProxy. The server's maxconn parameter was created at a time when HAProxy did not support keep-alive nor manage a connection pool (back in 2006, around HAProxy 1.2.

What is Maxconn in HAProxy?

The maxcon attributes represent a load factor multiplier that is used to compute the maximum number of connections that HAProxy allows for backends.

How many connections can HAProxy handle?

Here, HAProxy will accept up to 60,000 TCP connections concurrently. Having this limit prevents denial-of-service scenarios that could happen if HAProxy had to maintain more connections than the server's memory resources allowed.

How does HAProxy load balancing Work?

An HAProxy load balancer lets you specify different load balancing modes, depending on the type of application you're serving. Its round-robin mode will send requests to your servers in a rotation and works well when requests take a small and predictable amount of time to handle, such as HTTP requests.


1 Answers

I know this is an older question, and you've probably found a solution by now, but if you haven't then I think the answer you are looking for is the on-marked-up server option. This is a new server option as of version 1.5.

According to the documentation, you can provide an action to perform when a server is marked as "up". Currently, the only action is:

shutdown-backup-sessions: Shutdown sessions on all backup servers...

server primary ... on-marked-up shutdown-backup-sessions
like image 114
Anthony Fammartino Avatar answered Sep 22 '22 12:09

Anthony Fammartino