Разделить стенд и настройки нагрузочных экспериментов, ограничить число запросов на реплику в HAProxy и передавать X-Request-Id от балансировщика в API, сократить комментарии

This commit is contained in:
2026-11-04 12:23:14 +03:00
parent 7fbd781ca3
commit ce47c138e4
7 changed files with 94 additions and 111 deletions
+20 -29
View File
@@ -1,45 +1,39 @@
# 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.
# Docker DNS, with active readiness checks and no client stickiness.
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}"
# The container has at most one CPU; by default HAProxy starts a thread
# per host CPU and fights the quota.
nbthread 1
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.
# Above the API's 5 s request deadline: the API answers a slow request
# itself, this only cuts off a hung replica.
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).
# Below the 500 ms latency objective: excess load gets a fast 503.
timeout queue 300ms
# Re-sent only when nothing reached the API: a timeout or an empty
# response may follow a committed write.
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.
# Errors produced by the balancer itself use the contract's envelope.
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.
# `api` resolves to every running replica, re-resolved every second; an
# address that disappears leaves at once.
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 3
@@ -52,10 +46,9 @@ resolvers docker
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.
# Replaces any client value; the API keeps it, so one id spans both logs.
http-request set-header X-Request-Id %[unique-id]
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 }
@@ -66,13 +59,11 @@ backend replicas
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.
# Out within 1.5 s (inter + fastinter), less than the API's 2 s drain delay.
# maxconn 20: the replica's 10 pooled database connections plus requests
# that need no database; the rest waits in the queue. 0 means no limit.
default-server maxconn "${API_MAXCONN-20}" inter 1s fastinter 500ms downinter 1s fall 2 rise 2 check resolvers docker init-addr none resolve-prefer ipv4
# Up to five replicas, doubled while docker rollout replaces them.
server-template api 10 api:8080
listen stats