Skip to content

perf(triangulation): keep API responsive during overlap work - #673

Merged
MateoLostanlen merged 1 commit into
mainfrom
codex/extract-geospatial-inference
Nov 7, 2026
Merged

MateoLostanlen merged 1 commit into
mainfrom
codex/extract-geospatial-inference

Conversation

@frgfm

@frgfm frgfm commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Triangulation blocks the backend's event loop, so unrelated requests wait for the calculation. Run the same calculation in one bounded worker thread per backend process. Keep the current algorithm, results, dependencies, and deployment.

This replaces the original service extraction with a smaller latency fix: 5 files, +9 net production lines, +45 net lines including tests. Both alert attachment and alert refresh use the same worker limit.

flowchart LR
    C["Requests"] --> A
    subgraph B["Existing backend process"]
        A["API event loop"] -->|"await"| T["One geometry worker thread"]
        T -->|"same result"| A
    end
    A --> D["Database and storage"]
Loading

Queue and database-session limits

The limit applies to running geometry jobs, not waiting callers. Callers retain their database sessions and active transactions while waiting for a geometry slot. Transactions stay caller-owned so related writes still commit or roll back together. Bursts can increase connection occupancy; sustained overload and database-pool wait times have not been measured.

Measured results (2026-10-15)

Historical measurements against baseline 5d965dd on 2026-10-15. Same optimized algorithm and package versions, one backend worker, five fresh runs per option:

Option Peak stack RSS (MiB) Health p95 under load (ms) 80-view calculation request (ms)
Baseline 5d965dd 181.6 1,437.3 379.1
Bounded thread — this PR 186.9 26.6 380.4
Optimized HTTP split — prototype 243.6 17.1 390.7

RSS is resident process memory. The memory column includes backend and compute processes. The p95 is the response time within which 95% of health requests finish. Values are medians of each run's peak memory, p95 health time, and median calculation time.

The thread cuts unrelated-request tail latency by 98.1%, with 2.9% more RSS. Calculation time stays roughly unchanged. The split adds 34.1% RSS at one backend worker, so this PR adds no service, HTTP call, token, network, or dependency.

At four backend workers, a separate two-run experiment saved 12.8% RSS and 8.6% proportionally shared memory with the split. That is a different deployment from the repository's one-worker configuration. Native libraries remain in the backend in this PR; process and database isolation are deferred.

Raw samples, pinned environment, benchmark code, and reproduction instructions.

Conditions: native Linux ARM64, CPython 3.11.17, one CPU quota shared by the whole stack. Each mixed-load condition sends the same 12 geometry requests and 96 scheduled health requests. All geometry and timezone output hashes match across all 21 conditions.

Limits: these are local synthetic HTTP diagnostics with real backend imports. Database, S3, notification delivery, and background jobs are excluded. The split is an optimistic loopback prototype. These are not VPS ingestion measurements or Docker image-size savings.

Test plan

  • Full backend suite on the rebased branch against migrated PostgreSQL and S3 test services: 782 passed, 1 skipped.
  • Regression test verifies worker-thread execution, a responsive event loop, serialized geometry jobs, and unchanged results.
  • Backend Docker build; make quality; make precommit.

@codecov

codecov Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.18%. Comparing base (f2c1604) to head (056300c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #673   +/-   ##
=======================================
  Coverage   95.17%   95.18%           
=======================================
  Files          60       60           
  Lines        3402     3407    +5     
=======================================
+ Hits         3238     3243    +5     
  Misses        164      164           
Flag Coverage Δ
backend 95.33% <100.00%> (+<0.01%) ⬆️
client 91.83% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frgfm frgfm self-assigned this Aug 4, 2026
@frgfm
frgfm force-pushed the codex/extract-geospatial-inference branch from 6d997b7 to c7fd372 Compare October 15, 2026 13:19
@frgfm frgfm changed the title feat(inference): extract geospatial workloads from backend perf(triangulation): keep API responsive during overlap work Oct 15, 2026
@frgfm
frgfm requested a review from MateoLostanlen November 5, 2026 15:19
@frgfm frgfm added the type: improvement New feature or request label Nov 5, 2026
@frgfm
frgfm marked this pull request as ready for review November 5, 2026 15:19

@MateoLostanlen MateoLostanlen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @frgfm, thanks for the PR!

Small, focused change with clear numbers, nice work. Moving the overlap work to a single bounded thread is the right trade-off here.

  • The branch is 14 commits behind main, and main touched detections.py since. It merges cleanly, but please update the branch so CI runs on the merged state.
  • (nit) The DB session stays open while a request waits for the geometry thread. Fine with current compute times, just something to watch if detections burst.

Happy to merge once the branch is updated and CI is green.

@frgfm
frgfm force-pushed the codex/extract-geospatial-inference branch from c7fd372 to 056300c Compare November 6, 2026 17:30
@frgfm

frgfm commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

@MateoLostanlen all addressed ✅

@MateoLostanlen

Copy link
Copy Markdown
Member

Hi @frgfm, thanks for the PR!

Thanks for updating the branch and documenting the DB session trade-off. Everything looks good and CI is green.

Merging now.

@MateoLostanlen
MateoLostanlen merged commit b5ee45d into main Nov 7, 2026
24 checks passed
@MateoLostanlen
MateoLostanlen deleted the codex/extract-geospatial-inference branch November 7, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants