oliver-zehentleitner / maity

hub: versioned avatar URLs cacheable for good (max-age long, immutable)

Sign in to report

tsk-vvyqmk9vwg8e Done An agent

From the prod baseline (nte-ne4r1148qchp on tsk-a5pxwgkgr73e, eyq5 seq 94693): about 28 /avatars/{id} calls per board/card page, 300–500 ms each while they queue, take the load from ~1.3 s to ~3.2 s. On a reload each still costs a round trip (304). Every avatar_url the hub hands out carries ?v=<etag>, so a request with the matching v can be answered Cache-Control: public|private, max-age=31536000, immutable (decide public vs private by who may read the avatar today; never widen who can read it). A request without v or with a stale v keeps today's headers.
Contract: the caching sentence for avatars in §1/§16.11 in the same PR. This is under Oliver's caching decision (req-s246xb6k1mpp option A); state the sentence in the PR body.
Boundary: avatar responses only; no change to who can read an avatar, to API routes or to the dashboard assets of #1518.
Order: after #1518 merges (both touch the cache headers in middleware). Measure before/after with q8q9 (balu) on board and a card page.

Notes

  1. Maity Coder Claude 004 · Nov 8, 2026, 18:42 UTC

    Plan (built after #1518 merges)

    • GET /avatars/{member_id}: when the request's v equals the current tag (the same value avatar_url carries, url_of), the answer gets Cache-Control: private, max-age=31536000, immutable. Without v, or with a stale v, it keeps today's avatars.HEADERS.
    • private: this route answers only after avatars.may_read for the caller, so no shared cache may hold it. The public route (/avatars/public/{digest}) keeps its own header, unchanged.
    • A changed picture gets a new tag, so a new avatar_url: the long cache never shows an old picture to a reader who asks for the new one.
    • Tests: the matching v gives the long header; no v, a stale v and a v for another size each give today's header; a caller who may not read the avatar still gets 404 and no cache header.
    • The contract sentence (§6.10 or §16.11, wherever avatar caching is described) goes in the same PR and is quoted in the PR body, under the owner's caching decision.
    • Measure before and after with maity-ops-team (balu, board and a card page).

    Boundary: avatar answers only; who may read an avatar is unchanged; no API route other than this one; the dashboard assets of #1518 untouched.

  2. An agent · Nov 9, 2026, 07:55 UTC

    Step 1: why the board's avatars queue (maity-coder-claude-001#hrjz, 2026-11-09, main 17fce84f73b8558d55ee8f43bb02c7f96a1a1e1e; no code)

    How the web fetches them. M.avatar (web/js/avatar.js:60) builds a plain <img src="{avatar_url}">, with no fetch, blob or auth header (the cookie goes along) and no concurrency limit in our code. On error it swaps in the identicon. The URL carries no ?s=, so a 24 px avatar loads the 256 px picture. There is no loading="lazy". Over HTTP/2 the browser sends all ~35 at once, so nothing queues on the client.

    Why they queue: the hub serves them one after another. Prod runs one hub process (config.workers: 1, deploy/helm/maity-hub/values.yaml:70). Each avatar GET costs about 10 ms of Python CPU, and under the GIL one process runs that work serially. 35 × ~11 ms ≈ 380 ms, which matches the gap q8q9 saw (52 ms in the hub, ~380 ms in the browser).

    Reproduced locally, on PostgreSQL with a real uvicorn (one worker) and the client in its own process. One signed-in human requests their uploaded avatar.

    • One request: 9–15 ms.
    • 35 at once: 360–460 ms wall (median of 5 bursts), with the last answer at about the wall time.
    • Server CPU: 11–18 ms per request.
    • Without the session touch in auth (the three UPDATEs in auth/caller.py:_cookie), the burst is 406–420 ms. That is the same within the noise, so the row locks on the session are not the cause.

    Where the ~10 ms goes (cProfile, 200 GETs; the profiler about doubles the times):

    • 13 SQL statements per avatar GET.
    • Cookie auth (_cookie): ~40 % of the CPU. It reads the session and the human, then writes three UPDATEs: the session, the human and every membership.
    • Second-factor gate (second_factor.gate): ~20 %. It reads the instance's sign-in settings twice.
    • Route matching (middleware._unrouted plus FastAPI's own match): ~20 %. Every route is matched about 70 times per request.
    • The avatar itself (may_read, stored): ~20 %.

    Levers, biggest first:

    1. This card: `immutable` on a matching `?v=`. A warm load sends 0 avatar requests instead of 35 revalidations. A cold load still sends all 35.
    2. Web (second PR): render the identicon at once and swap in the picture when it loads. The board no longer waits for the queue. Add ?s=32/64 for the sizes shown: fewer bytes, but the same CPU. Add loading="lazy" for avatars off screen.
    3. Hub CPU per request (each needs its own card): the auth touch at most once per N seconds (changes how fresh last_seen_at is, §2.6.5, so a contract question); the instance settings read once per request; the route match done once.
    4. Ops: `workers: 2` or more. This multiplies the CPU budget; the pool math is in values.yaml. It is a deploy decision for Oliver/ops.

    The probes are kept outside the repo, in /home/agents/projects/oz-agent-b/hrjz-notes/test_probe_avatar_*.py and burst_client.py.

  3. An agent · Nov 9, 2026, 08:03 UTC

    Step 1, addendum: with several workers (maity-coder-claude-001#hrjz, main 17fce84f73b8558d55ee8f43bb02c7f96a1a1e1e; corrects nte-bjbtv87x1t17)

    q8q9 (96672): prod runs MAITY_WORKERS=2 in each of 2 pods. So "one process" in my first note was wrong. Repeated locally with the real python -m maity_hub serve, MAITY_WORKERS=2 and 4, on PostgreSQL. 35 parallel avatar GETs with one cookie, 5 bursts. The client was in its own process and opened its 35 connections first, as a proxy holds them. Shared machine, 8 cores, load ~5, so the numbers are noisy.

    | workers | client p50 / max | hub ms= p50 / p90 | connections per worker, per burst |
    |---|---|---|---|
    | 1 | 417 / 589 ms | 136 / 510 | 35 |
    | 2 | 400 / 518 ms | 33 / 498 | [33,2] [22,13] [33,2] [26,9] [20,15] |
    | 4 | 237 / 290 ms | 33 / 326 | [21,7,7] [17,16,2] [24,7,3,1] [22,9,3,1] [11,10,8,6] |

    What it shows:

    1. The connections are spread unevenly over the workers. uvicorn's workers share one listening socket. The kernel hands each new connection to whichever worker accepts first, often the same one: 33 of 35 on one of two workers, 21–24 of 35 on one of four. HTTP keep-alive then pins each connection to its worker, so the busy worker serves most of the burst while the others idle. Behind Traefik this is the same: it holds keep-alive connections to the pods, and inside a pod the workers share the socket.
    2. Much of the wait happens before the hub's clock starts. ms= starts at the middleware's first line (middleware.py:138). Its median is 33 ms, while the client's median is 237–400 ms. Reproduced: the gap between the two medians. Inferred, not measured: on the busy worker, the threads doing the requests' CPU work hold the GIL, so its event loop reads the next requests late. Those requests sit in the socket buffer, and the hub never counts that time. This explains "52 ms in the hub, ~380 ms in the browser". The ms= tail (p90 326–498) shows only the part of the wait that is already inside the middleware.
    3. More workers alone help little while one of them takes most connections.

    To check on prod (ops, read-only): the hub's periodic load line (loop_late_max_ms, threads_waiting_max, request_stats.py) per worker during a board load, and how many requests each pod and worker serves.

    Levers, besides the two PRs on this card: even out the spread, for example with SO_REUSEPORT per worker (the kernel balances new connections by hash) or one worker per pod with more pods; and less CPU per request (zwhq's tsk-6srccd2r1h62). Both are decisions for lotse, ops and Oliver; I build neither. The web PR (identicon at once, ?s=, lazy) and immutable stay right either way: they take the avatars off the board's critical path and out of warm loads.

    The probe files are in /home/agents/projects/oz-agent-b/hrjz-notes/ (test_probe_avatar_workers.py, burst_client.py).