N+1 queries: Order history crawls for loyal customers
N+1 queries debugging lab. New customers load their order history instantly. Loyal customers wait seconds, and no single query is slow.
Practice debugging a n+1 queries 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
Support is escalating complaints that the "My orders" page spins for seconds, but only for long-time customers. New accounts load instantly. Nothing has been deployed this week, and the database team says every query on the orders tables is fast and indexed.
2. Observed Symptoms & Metrics
- Affected Endpoints / Components:
GET /customers/{id}/orders on the orders service, Postgres orders and order_items tables.
- Behavior: Response time grows with how many orders a customer has placed. Customers with about 300 orders wait over a second on an idle system. At 20 req/s, latency keeps climbing as requests queue up.
- Error Rate: Low, but p99 latency is far above the 300 ms SLO.
- System Metrics: All 10 connections in the app's Postgres pool are busy, and requests wait to get one. Postgres CPU is modest, and
pg_stat_statements shows no slow queries. App CPU is low.
3. Application Logs
2026/10/12 11:48:02 orders service listening on :8080
# curl
$ time curl -s localhost:8080/customers/1/orders > /dev/null
real 0m1.240s
$ time curl -s localhost:8080/customers/9000/orders > /dev/null
real 0m0.006s # a brand-new account with no orders yet
# k6
http_req_duration..............: avg=1.31s p(95)=2.62s p(99)=3.05s
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:
- Error Rate: Fewer than 1% of requests fail (
http_errors < 1%).
- Performance: P99 latency stays below 300 ms (
p99_latency_ms < 300).
- Data Integrity: All 15,000 orders are still in the database, and the response stays the same: every order with all of its items (
data_integrity).
Environment note: as in production, Postgres runs in another availability zone, about 2 ms per round trip. The app reaches it through localhost:6432, and terminal tools can connect directly on localhost:5432.
Toolbox: curl localhost:8080/customers/1/orders | head -c 500, psql "$DATABASE_URL". 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.