Missing LIMIT in query: Feed times out only for people with lots of friends
Missing LIMIT in query debugging lab. The feed loads in 50 ms for most users, but users with thousands of friends get a 504. The tests passed because test users only had 3 friends.
Practice debugging a missing limit in query in a live, broken production system in your browser, with PostgreSQL. Find the root cause in the logs, terminal and code, fix it, and a load test checks that the fix holds under real traffic.
1. The Incident Context
Most users load their feed in about 50 ms. A handful of users with thousands of friends get a 504, and support is getting tickets. The automated tests never caught it because every test user has only 3 friends.
2. Observed Symptoms & Metrics
- Affected Endpoints / Components:
GET /feed, the posts and friendships tables.
- Behavior:
/feed?userId=1 (a user following 5000 people) takes seconds. /feed?userId=3 returns in milliseconds.
- Error Rate: The slow requests eventually time out and read as 504. Otherwise no errors.
- System Metrics: Database CPU spikes in step with the slow feeds. The table holds one million posts.
3. Application Logs
16:10:02 feed service listening on :8080
16:10:40 GET /feed?userId=1 took 1,940ms
16:10:41 GET /feed?userId=3 took 6ms
4. Your Objective
Step into the live environment while synthetic traffic is running, diagnose the root cause, and patch the codebase.
To clear this stack, your fix must satisfy these automated verification checks under a 60-second load test:
- Latency: The feed for the 5000-friend user stays below 300 ms p99.
- Error Rate: Fewer than 1% of requests fail.
- Data Integrity: The 5000 friendships are still present.
Toolbox: curl 'localhost:8080/feed?userId=1', psql "$DATABASE_URL", the app logs.
The code fetches the friend list, then loads every single post from every friend and keeps the newest 20. The database already has an index it can use — the code just never asks it to stop early.
Save a file in the editor to rebuild and restart the app. Click Verify when you think it's fixed.
Your account and data
You can browse stacks without an account. To run labs you sign in with your email or Google account. BustedStack uses your email address to
sign you in, and your name and profile picture to show your account and pre-fill certificates. We never access your Gmail, contacts or Drive,
sell your data or run ad trackers.
Privacy Policy · Terms of Service · Browse stacks ·
hello@bustedstack.com
This site needs JavaScript to run labs.