# Measured login DoS budget and shared rate limit

Date: 2026-11-27
Stand: local Docker Desktop, compose project hla-lab7, two API replicas
         (0.75 CPU / 768 MiB each), nginx HTTPS ingress, Redis 7.4 over mTLS.

Commands:
  powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\benchmark-argon2.ps1
  powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\measure-login-dos.ps1 -SourceBudget

Argon2id calibration (0.75 CPU / 768 MiB, 5 warmups, 21 samples, unique salt):
  32768 KiB, t=2, p=1:
    encode p50 100.182 ms, p95 134.817 ms
    verify p50 115.483 ms, p95 165.503 ms
  Previous run of the same script: encode p95 150.3 ms, verify p95 142.7 ms.

Observed login budget (black-box through nginx):
  Per username: 5 attempts / 60 s, shared by both replicas.
    - 5 concurrent failed logins returned 401 and were served by two
      distinct X-Instance-Id values.
    - The 6th attempt returned 429 rate_limited with Retry-After 58 s.
    - 5 concurrent Argon2 verifications: p50 1547.045 ms, p95 1662.582 ms.
      This is queueing on the 0.75-CPU limit, not a single-request latency.
    - The 429 response took 19.682 ms, i.e. it did not run Argon2.
  Per source: 100 attempts / 60 s, shared by both replicas.
    - After the 60 s window was allowed to expire, 100 failed logins with
      distinct usernames returned 401; both replicas were observed.
    - The 101st attempt returned 429 rate_limited with Retry-After 60 s.
    - Sequential 401 path: p50 287.473 ms, p95 490.411 ms (includes
      TLS, Redis, PostgreSQL and Argon2id).
    - The 429 response took 18.654 ms, i.e. it did not run Argon2.
  Known and unknown usernames:
    - Both returned 401 with code "unauthorized" and the same message.
    - Known-user request 510.424 ms; unknown-user request 309.891 ms in
      that run. The difference is not a stable user-existence oracle; both
      paths run one Argon2id verification (the unknown path uses the dummy
      hash).

Budget calculation using the p95 verify time:
  - Per username: 5 * 165.503 ms = 0.83 s of Argon2 CPU per 60 s window.
  - Per source: 100 * 165.503 ms = 16.55 s of Argon2 CPU per 60 s window,
    i.e. about 0.276 CPU averaged over the window on the 0.75-CPU API limit.
  - The limiter is a rate cap, not a concurrency cap: a simultaneous burst
    can still queue on the CPU limit, as the 5-way burst above shows.
  - A Redis outage fails closed (503), so the expensive path is not exposed.

Shared-state and key handling:
  - The fixed-window counters live in Redis under
    messenger:login:user:<sha256> and messenger:login:source:<sha256>.
  - The Lua script increments both counters and sets the TTL atomically.
  - Both API replicas use the same Redis keys, so the budget is global for
    the deployment, not per process.
  - Raw usernames and source addresses are not used as Redis key material.

Known limitation:
  - The source key currently uses request.getRemoteAddr(), which is the
    nginx/LB address. It is therefore a global source cap shared by all
    clients behind the LB, not a per-client cap. The per-username limit is
    the primary per-account control. A trusted-proxy client-address model
    remains the next step.

No passwords, session tokens or password hashes are included.
