HAProxy community

How to setup/make Haproxy send read-write request based on REST method

How to setup/make Haproxy send read-write request coming on same port 5432 from application, to backend servers of postgres-patroni cluster based on REST method.

I have three node postgres-patroni cluster running as docker service/containers behind Haproxy. I wanna sent read/get request coming from my-application on port 5432 to relicas of my cluster and sent write/(Delete|Post|Update) request coming from my-application also on port 5432 to master server.

I have tried to achieve it with below config. But didn’t succeed as of now.

Any idea How to make Haproxy achieve above scenario.

Here is my Haproxy.cfg file

global
        maxconn 100

defaults
        log     global
        mode    tcp
        retries 2
        timeout client 30m
        timeout connect 4s
        timeout server 30m
        timeout check 5s
        # never fail on address resolution
        default-server init-addr none

resolvers docker_resolver
        nameserver dns 127.0.0.11:53
listen stats
    mode http
    bind *:7000
    stats enable
    stats uri /

frontend master_postgresql
        bind *:5432
        acl is_post method "POST|UPDATE|DELETE"
        use_backend backend_master if is_post
        default_backend backend_master

frontend replicas_postgresql
        bind *:5432
        acl is_get  method GET
        use_backend backend_replicas  if is_get
        default_backend backend_replicas

frontend patroni_api
        bind *:8008
        default_backend backend_api

backend backend_master
        balance source
        hash-type consistent
        option httpchk OPTIONS /master
        server postgres1 postgres1:5432 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4
        server postgres2 postgres2:5432 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4
        server postgres3 postgres3:5432 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4

backend backend_replicas
        option httpchk OPTIONS /replica
        server postgres1 postgres1:5432 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4
        server postgres2 postgres2:5432 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4
        server postgres3 postgres3:5432 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4

backend backend_api
        option httpchk OPTIONS /master
        server postgres1 postgres1:8008 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4
        server postgres2 postgres2:8008 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4
        server postgres3 postgres3:8008 maxconn 100 check port 8008 resolvers docker_resolver resolve-prefer ipv4


The documentation mentions mentions matching multiple methods directly:

https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7.3.6-method

So try this:

    acl is_write method POST UPDATE DELETE
    use_backend backend_master if is_write