Repository navigation
Add guardrails against the aggregate endpoint's memory usage - #100
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SETNXvia the existing Flask-Caching cache) so only one worker builds a given(hash_digest, release, table_code)aggregate download at a time; concurrent requests/retries for the same download get a409instead of piling on redundant work.aggregate_decennial(): count the census blocks a geography joins to before running the expensive block-level query, and reject with413aboveMAX_SYNC_AGGREGATION_BLOCKS = 150,000.Why
On 2026-08-26 a request for
/1.0/aggregate/523ee393241af8bb996db584a704d913/dec_pl94_compare_2020_2010/P3(an uploaded "2024 Census Counties" geography spanning 336,949 + 421,507 census blocks across both decennial vintages) was retried 3 times by the client in ~40 seconds while the backend was still building it.aggregate_decennial_comparison()holds two full block-level dataframes (with geometry) in memory at once with no streaming, so the concurrent duplicate attempts drove the box into heavy swapping, and gunicorn killed and respawned 3 workers over the next several minutes.MAX_SYNC_AGGREGATION_BLOCKSis chosen from the real distribution of existing uploads (p50 4,632 / p90 11,574 / p99 163,592 blocks) — it passes everything except this hash and one other similarly oversized upload that's never actually been exercised through this endpoint.Test plan
dokku postgres:connect.CACHE_TYPE=redisis set in production so the lock isn't a silent no-op, and thatcachelib'sRedisCache.add()uses an atomicSETNX.