# Lab4 load balancer: leastconn across Compose DNS name "api" (all replicas). # Uploads hold a connection for seconds, so new requests go to the replica with the # fewest open connections (equal load still rotates, like round-robin). global maxconn 2048 log stdout format raw local0 # Fixed socket buffers: a burst of 10 MiB uploads must not grow kernel buffers # (charged to this container's 128m cgroup) until the OOM killer stops the LB. tune.rcvbuf.client 262144 tune.sndbuf.client 262144 tune.rcvbuf.server 262144 tune.sndbuf.server 262144 defaults mode http log global option httplog option dontlognull # A stopped replica's IP vanishes and connects to it hang instead of being refused; # on the local bridge a real connect takes milliseconds, so fail over after 1s. timeout connect 1s timeout client 30s # After the body is forwarded the API may still verify the image and write to S3 # (bounded at ≈35 s in the worst case: decode 15 s + S3 ≈15 s + DB 6 s). timeout server 45s timeout check 1500ms timeout http-request 10s retries 2 # Connect-level retries go to another replica (request not sent yet, safe for POST). option redispatch 1 # LB-generated errors use the API error envelope and carry the contract headers. unique-id-format %[uuid()] http-error status 502 content-type application/json lf-string '{"error":{"code":"unavailable","message":"Bad gateway","request_id":"%[unique-id]"}}' hdr X-Request-Id %[unique-id] hdr X-Instance-Id lb http-error status 503 content-type application/json lf-string '{"error":{"code":"unavailable","message":"No API replica available","request_id":"%[unique-id]"}}' hdr X-Request-Id %[unique-id] hdr X-Instance-Id lb http-error status 504 content-type application/json lf-string '{"error":{"code":"unavailable","message":"Gateway timeout","request_id":"%[unique-id]"}}' hdr X-Request-Id %[unique-id] hdr X-Instance-Id lb resolvers docker nameserver dns1 127.0.0.11:53 resolve_retries 3 timeout resolve 1s timeout retry 1s hold valid 5s hold obsolete 10s frontend messenger_fe bind *:8080 acl is_lb_live path /__lb/live http-request return status 200 content-type text/plain string ok if is_lb_live # Normal routing uses readiness. If NO replica is ready (shared dependency down: # PostgreSQL, Redis or S3), live replicas still answer so clients get the API's # own JSON 503 for affected endpoints and unaffected ones keep working. use_backend messenger_api_degraded if { nbsrv(messenger_api) lt 1 } default_backend messenger_api backend messenger_api balance leastconn option httpchk http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api http-check expect status 200 # server-template expands A records for Compose service DNS name "api". # inter=1s fall=2 → ≈2–3s to mark a backend down after failed checks; a failed # live connect marks it down at once (observe layer4). server-template api 8 api:8080 check inter 1s fall 2 rise 1 observe layer4 error-limit 1 on-error mark-down resolvers docker resolve-prefer ipv4 init-addr none backend messenger_api_degraded balance leastconn option httpchk http-check send meth GET uri /health/live ver HTTP/1.1 hdr Host api http-check expect status 200 server-template live 8 api:8080 check inter 1s fall 2 rise 1 observe layer4 error-limit 1 on-error mark-down resolvers docker resolve-prefer ipv4 init-addr none