# HAProxy 3.2 LTS — the single public entry point of the lab 3 stand (compose service `lb`). # # Backend discovery: Docker DNS name `api` returns one A record per RUNNING replica of the # compose service `api` (deploy.replicas / --scale). server-template keeps 8 slots and # re-resolves every second, so scale up/down, a restarted container with a new IP and a # full stack restart are picked up without restarting the LB. A stopped container drops # out of DNS and its slot goes to MAINT. # # Readiness: an ACTIVE check `GET /health/ready` (PostgreSQL + Valkey reachable, not draining) # every 1 s; 2 failures -> DOWN, 2 successes -> UP. Passive errors do not decide anything on # their own, and there are NO backup/fallback servers without checks: a replica that answers # /health/live but not /health/ready never gets business traffic. # Worst case detection of a failed replica: # inter 1 s + fall 2 x (check timeout 1 s) ~= 3 s (frozen process); # refused connection / 503 ready: inter 1 s + fastinter 0.5 s ~= 1.5 s. # Requests that could not even be SENT (connection refused/reset before the request) # are retried on another replica (retry-on conn-failure + redispatch) - safe for POST, # because the API has not seen them. Nothing that may have reached the API is retried by # the LB: an uncertain POST is retried by the CLIENT with the same Idempotency-Key. # No sticky sessions: sessions live in Valkey, every replica serves every user. global log stdout format raw local0 info maxconn 4096 # Runtime API for `show servers state` / `show stat` (scripts and tests, no network port). stats socket /var/lib/haproxy/admin.sock mode 600 level admin hard-stop-after 15s resolvers docker nameserver dns1 127.0.0.11:53 accepted_payload_size 8192 resolve_retries 3 timeout resolve 1s timeout retry 1s hold valid 1s hold other 1s hold refused 1s hold nx 1s hold timeout 1s hold obsolete 1s defaults mode http log global option httplog option dontlognull option http-keep-alive timeout connect 1s timeout client 30s timeout server 10s # > DB_TIMEOUT_SECONDS (3 s): the API answers 503 itself first timeout http-request 10s timeout http-keep-alive 10s timeout queue 3s timeout check 1s retries 2 retry-on conn-failure option redispatch 1 # Errors generated by the LB itself, in the contract error format with the same request id. # They carry no X-Instance-Id: no API replica produced them. 502/504 = the outcome of the # request is unknown (a replica died mid-request / did not answer in time): retry with the # same Idempotency-Key. 503 = no ready replica at all. http-error status 502 content-type application/json lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"API-реплика оборвала ответ; повторите с тем же Idempotency-Key\",\"request_id\":\"%[var(txn.request_id)]\"}}" hdr X-Request-Id "%[var(txn.request_id)]" http-error status 503 content-type application/json lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"Нет готовых API-реплик\",\"request_id\":\"%[var(txn.request_id)]\"}}" hdr X-Request-Id "%[var(txn.request_id)]" http-error status 504 content-type application/json lf-string "{\"error\":{\"code\":\"unavailable\",\"message\":\"API-реплика не ответила вовремя; повторите с тем же Idempotency-Key\",\"request_id\":\"%[var(txn.request_id)]\"}}" hdr X-Request-Id "%[var(txn.request_id)]" frontend public bind :8080 # One request id end to end: keep a valid client X-Request-Id, otherwise create one; the # API echoes it, and errors generated by the LB itself carry the same value. http-request set-header X-Request-Id %[uuid()] unless { req.hdr(x-request-id) -m reg ^[A-Za-z0-9._:-]{1,128}$ } http-request set-var(txn.request_id) req.hdr(x-request-id) log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq rid=%[var(txn.request_id)] \"%HM %HPO\"" default_backend api backend api balance roundrobin option httpchk http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api http-check expect status 200 default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 2 check resolvers docker init-addr none resolve-prefer ipv4 maxconn 200 server-template api 8 api:8080 # Local-only status page (127.0.0.1:8404 on the host): which replica is UP/DOWN right now. frontend stats bind :8404 stats enable stats uri / stats refresh 2s stats show-legends