OFFSET pagination debugging lab. Page 1 of the audit log loads in a few milliseconds. The last pages take seconds, and the nightly export dies partway through.
Practice debugging a offset pagination 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
Every night the finance team's export client downloads the whole audit log through the public API, 100 rows at a time. It used to finish in a minute. The log now holds 500,000 events and keeps growing, and last night's export died partway through with timeouts. The runs that do finish sometimes contain the same event twice.
2. Observed Symptoms & Metrics
- Affected Endpoints / Components:
GET /audit-events?page={n}&size=100 on the audit service, POST /audit-events (other services write events here), Postgres audit_events table (500,000 rows, growing at about 50 rows/s).
- Behavior: The export client starts at
GET /audit-events?size=100 and follows each response's next link until it is null. Early pages come back in a few milliseconds. Each page after that is a little slower than the one before, and the last pages take hundreds of milliseconds each. Some events arrive late, so their created_at is up to two days in the past.
- Error Rate: Late pages run into the export client's 3 s timeout when the database is busy. Finished exports occasionally contain duplicate rows.
- System Metrics: Postgres CPU is high while an export runs. App CPU is low. Postgres reports no slow lock waits, and
\d audit_events shows an index on (created_at, id).
3. Application Logs
2026/10/12 01:00:00 audit service listening on :8080
# pg_stat_statements
SELECT id, created_at, actor, action, target FROM audit_events ORDER BY created_at, id LIMIT $1 OFFSET $2
calls=5000 mean_ms=210.4 max_ms=472.9
# export client
page 340 ok (9ms)
page 4800 ok (412ms)
page 4801 FAILED: timeout after 3s
export finished: 499,700 rows, 412 duplicates
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 during one full export run, while new events (some of them late) are written at 50 rows/s:
- Error Rate: Fewer than 1% of requests fail (
http_errors < 1%).
- Performance: P99 latency stays below 50 ms on every page, including the last ones (
p99_latency_ms < 50).
- Data Integrity: The export reaches the last page, and it contains every row that existed when it started exactly once. All 500,000 original events are still in the database (
data_integrity).
Environment note: the export client only follows the next link each response returns. You may change how pages are addressed, as long as the response still has items (at most size rows, in created_at, id order) and next (null on the last page).
Toolbox: curl 'localhost:8080/audit-events?page=4900&size=100' | head -c 400, time curl -s -o /dev/null 'localhost:8080/audit-events?page=4900&size=100', psql "$DATABASE_URL" (try EXPLAIN ANALYZE). Save a file in the editor to rebuild and restart the app. Click Verify when you think it's fixed.
Terms in this lab: p99, req/s, OFFSET / LIMIT, EXPLAIN ANALYZE, CPU.
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.
This site needs JavaScript to run labs.