Files
mtusi2026lab3/infra/haproxy/haproxy.cfg
T

84 lines
3.7 KiB
INI

# The single entry point: round-robin over the API replicas found through
# Docker DNS, with active readiness checks and no client stickiness. Replicas
# are interchangeable: sessions live in Valkey, everything else in PostgreSQL.
global
log stdout format raw local0 info
maxconn 20000
# One thread per CPU of the container limit; by default HAProxy would
# start one per CPU of the host and fight the CPU quota.
nbthread "${LB_THREADS}"
defaults
mode http
log global
option dontlognull
# Inside the Docker network a replica accepts or refuses at once; a
# second means it is gone.
timeout connect 1s
timeout client 30s
# Above the API's own 5 s request deadline: a slow request ends in the
# API's 503 with its cause logged, and this timeout only cuts off a
# replica that hangs entirely.
timeout server 7s
timeout http-request 10s
timeout http-keep-alive 10s
timeout check 2s
# A request is re-sent, to another replica, only when the connection
# failed and so nothing reached the API. A timeout or an empty response
# may follow a committed write, and POST /users or /chats must never be
# repeated behind the client's back (contract, idempotency section).
retries 2
option redispatch 1
retry-on conn-failure
# Errors produced by the balancer itself use the contract's envelope. They
# carry X-Request-Id but no X-Instance-Id: no replica answered.
http-error status 502 content-type application/json lf-string '{"error":{"code":"unavailable","message":"the API replica failed to answer","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
http-error status 503 content-type application/json lf-string '{"error":{"code":"unavailable","message":"no API replica is available","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
http-error status 504 content-type application/json lf-string '{"error":{"code":"unavailable","message":"the API replica did not answer in time","request_id":"%[unique-id]"}}' hdr X-Request-Id "%[unique-id]"
# Docker's embedded DNS: the service name `api` resolves to every running
# replica. Re-resolved every second, so scaled, restarted or replaced
# replicas join and stopped ones leave without a reload.
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 10s
hold obsolete 0s
accepted_payload_size 8192
frontend entry
bind :8080
option httplog
# 32 hex characters, like the request ids of the API.
unique-id-format %[uuid(4),regsub(-,,g)]
# The access log names the replica that answered and its request id, which
# is the key to the replica's own log line.
capture response header X-Instance-Id len 32
capture response header X-Request-Id len 40
http-after-response set-log-level silent if { status lt 500 } { env(LB_QUIET) -m str 1 }
default_backend replicas
backend replicas
balance roundrobin
option httpchk
http-check send meth GET uri /health/ready
http-check expect status 200
# Detection: a replica that fails readiness is out after the next check
# (inter 1 s) plus one fast re-check (fastinter 0.5 s), so within 1.5 s.
# A stopping API keeps serving for APP_DRAIN_DELAY (2 s) after readiness
# turns 503, longer than this, and only then closes its listener. Back in
# rotation after two good checks (rise 2), about 2 s.
default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 2 check resolvers docker init-addr none resolve-prefer ipv4
# Ten slots: up to five replicas, doubled while docker rollout replaces them.
server-template api 10 api:8080
listen stats
bind :8404
no log
stats enable
stats uri /
stats refresh 2s