Skip to content

fix(www): queue API requests during cron bursts instead of returning 502 - #1309

Merged
peterthomassen merged 1 commit into
mainfrom
20261110_www_queue_api_bursts
Dec 3, 2026
Merged

peterthomassen merged 1 commit into
mainfrom
20261110_www_queue_api_bursts

Conversation

@peterthomassen

Copy link
Copy Markdown
Member

nginx allowed only as many upstream connections as the api has worker processes, and open-source nginx cannot queue requests beyond max_conns. Whenever all workers were busy, further requests therefore got an immediate 502 "no live upstreams". This happens in the seconds after 5-minute marks, when cron-driven clients (dynDNS updates, among others) fire at once.

Let additional connections wait in uwsgi's listen backlog for a free worker, so that bursts are delayed briefly instead of failing; this does not add worker processes, memory, or database connections. As each waiting request holds a client and an upstream connection, raise nginx's worker_connections (already insufficient at times) and the workers' file descriptor limit to match.

@nils-wisiol

Copy link
Copy Markdown
Contributor

Thanks, queueing works as described when I test it.

Client aborts can overflow the backlog. When a client gives up while queued (499), nginx frees the max_conns slot right away. The connection stays in uwsgi's accept queue anyway, and a worker still runs the request. nginx then admits new connections into an already-full backlog, which leads to SYN drops, retries with backoff, and possibly 504s, most likely during exactly these bursts. I'd give listen headroom instead of sizing it exactly:

# well above max_conns - processes: aborted connections stay in the backlog
# after nginx has released their max_conns slot
listen = 4096

(4096 is the container's somaxconn.)

Minor:

  • Queue wait vs. timeouts: a full queue means waiting about 11× the average service time, which counts against uwsgi_read_timeout (60s). Could you check p95 latency of /api/v1/dyndns/update around the 5-minute marks, and size max_conns so that (max_conns − 128) / 128 × p95 stays under typical client timeouts? Setting uwsgi_read_timeout explicitly in the API locations would also make the limit visible.
  • Prometheus scrapes: they share the socket, so they queue behind bursts. A separate socket for the http router (socket = 127.0.0.1:3032, http-to = 127.0.0.1:3032) should let the next free worker take them, though I haven't tested it. Otherwise, raise the scrape timeout for the api job.
  • max_conns is per nginx worker: add zone desecapi 64k; to the upstream so the limit stays global if worker_processes ever goes up. Also update the processes = 128 comment in uwsgi.ini to point at max_conns/listen.
  • Kernels below 5.4: somaxconn is 128 there and uwsgi won't start. Adding sysctls: [net.core.somaxconn=4096] to api in docker-compose.yml makes this explicit.

@peterthomassen
peterthomassen force-pushed the 20261110_www_queue_api_bursts branch from 76d029e to 74893cd Compare December 2, 2026 19:48
@peterthomassen

Copy link
Copy Markdown
Member Author

Thanks, addressed the main finding, and the 3rd minor one.

The 4th minor one is n/a. The first two are risks I'd accept (processing time is usually not above 1s, and bursts happen mainly at modulo-5' points in time, not all the time).

nginx allowed only as many upstream connections as the api has worker
processes, and open-source nginx cannot queue requests beyond
max_conns. Whenever all workers were busy, further requests therefore
got an immediate 502 "no live upstreams". This happens in the seconds
after 5-minute marks, when cron-driven clients (dynDNS updates, among
others) fire at once.

Let additional connections wait in uwsgi's listen backlog for a free
worker, so that bursts are delayed briefly instead of failing; this
does not add worker processes, memory, or database connections. As
each waiting request holds a client and an upstream connection, raise
nginx's worker_connections (already insufficient at times) and the
workers' file descriptor limit to match.
@peterthomassen
peterthomassen force-pushed the 20261110_www_queue_api_bursts branch from 74893cd to e79d655 Compare December 3, 2026 09:31
@peterthomassen
peterthomassen merged commit e79d655 into main Dec 3, 2026
17 of 19 checks passed
@peterthomassen
peterthomassen deleted the 20261110_www_queue_api_bursts branch December 3, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants