I have haproxy listening on several ports and pointed at several backend servers.

Ideally, I would like it so that repeated communications to the same port get pointed at the same backend.

"balance source" isn't workable because often requests come from the same source.

Is this doable?

I'm also open to non-haproxy solutions. The protocol being used isn't important but is definitely not http. Just assume its ssh and you shouldn't go wrong.

EDIT: To clarify, imagine I have 10 'backend servers' and 5 'clients'. Each backend server can only ever cope with one connection at a time. I have more servers than required incase some become unavailable. I will manually point each client to the haproxy port(s). I want to ensure each client always gets to a backend server (which is simple) but preferably to the same one each time as long as it is available.


Here's how I solved my problem.

frontend name_of_frontend
    bind *:20000-20010
    default_backend     servers

backend servers
    balance roundrobin
    stick-table type integer size 1k expire 3h
    stick on dst_port
    server name ip-address check maxconn 1
    server name ip-address check maxconn 1

Willy warned in a previous answer's comment that it "will not guarantee that each client will go to a different port".

In my specific case, I have complete control over my 'clients'. The only reason I am pointing them at different ports on the proxy server is so that I can identify them as different clients. This is most definitely not a 'public' system.

  • This is precisely the solution I came here for. Thanks for sharing. – mkoistinen Nov 6 '17 at 0:44

Haproxy does not implement ip:port persistence because in a proxy it doesn't make any sense. Doing this makes a lot of sense in a packet-based load balancer (eg: LVS) because it allows stateless load balancing with round-robin like distribution and persistence. But in a proxy, the connections are necessarily maintained on both ends from the beginning to the end. So I really don't know what you're trying to precisely achieve.

If what you need is just a destination port -> server relation, that looks quite strange, because you need to listen on far more ports than you have servers in order to get a smooth balancing. I'm really not sure you'd want to do that.

  • Yes & I fully appreciate I'm nowhere near your standard use case. I'm just hoping I can 'bodge' a solution to a problem I have with haproxy seen as its already so close. With regards your number of ports > number of servers expectation, no that isn't true and I'll update my question. – Pricey Mar 21 '11 at 10:46
  • hmmm reading about stick store-request/stick-table etc. etc. make me think this 'might' be possible... – Pricey Mar 21 '11 at 12:49
  • Then dest port hashing will not guarantee that each client will go to a different port. What you'd better do then is to manually assign the servers to the ports. It would be a lot easier for your case. – Willy Tarreau Mar 23 '11 at 6:30
  • Just reread your latest comment. Yep that's probably what I'll end up doing. The only annoying bit is that I have (e.g.) 10 servers used to begin with and 5 'backups'. I don't have enough to give each server 1 backup, nor can I really put a backup on more than one server incase it is accessed twice. – Pricey Mar 29 '11 at 13:13

I'm not an expert on haproxy, but I know it does have other load balancing options besides source (at a minimum it has round-robin). I'd advise checking the HAProxy documentation to see what the capabilities and limitations are of each load balancing mode.

You can also achieve what you want by using the pf firewall (in one of its many incarnations -- pfSense is a good one if you need a GUI). Set up load balancing on the firewall (round-robin or similar) with the sticky-address option set and you should have a workable setup.

  • 1
    Seems like the OP needs source "IP-Port" persistence, rather than just IP persistence. With HTTP, HAProxy can just insert a cookie with the serverid, and then use that to ensure the client sticks to one server. I'm not sure if it can do IP:Port, and AFAIK, LVS doesn't do this either. – James Little Mar 17 '11 at 18:11
  • @James If that's the case I'm pretty sure haproxy can't do IP:Port without the ability to tweak the protocol data -- I THINK pf can, but I've never tested it (The state table entries that are used to preserve the mapping include port numbers though, so it should work as expected) – voretaq7 Mar 17 '11 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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