perf: fix N+1 queries in load_posts_keyed, add DB pool timeout - #374
Merged
Conversation
- Batch-load community titles and roles in 2 queries instead of 2 per community, eliminating 2N round-trips for threads spanning many communities. Guard lookups so a stale community_id (e.g. a community deleted by fork rollback) falls back to the post category / guest role instead of raising KeyError. - Add aiopg connection pool acquisition timeout (10s) to prevent health-check starvation when all connections are busy. - Raise _child_ids cache TTL from 30s to 120s to reduce repeated lookups on deep comment trees.
ety001
force-pushed
the
fix/discussion-n-plus-one-timeout
branch
from
July 23, 2026 17:38
3d1d4eb to
5af78d7
Compare
kuny0707
approved these changes
Jul 29, 2026
This was referenced Jul 31, 2026
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
Three targeted performance fixes to address recurring hivemind outage (every 1-2 days, EB health goes Yellow with ~46% 4xx).
Root cause
get_discussionpipeline exhausts the 25-connection aiopg pool under burst traffic, starving health checks → ELB marks instance unhealthy → EB replaces it.Changes
Eliminate N+1 community queries (
objects.py:load_posts_keyed)Add connection pool acquisition timeout (
db.py)aiopg.sa.create_engine(timeout=10)Increase
_child_idscache TTL (thread.py)Verification
make fmt✓python3 -m py_compile✓ (all 3 files)get_discussionas 50% of failure traffic