Skip to content

Cache leaderboard summaries (30s TTL)#208

Merged
msaroufim merged 1 commit intomainfrom
cache-leaderboard-summaries
Feb 26, 2026
Merged

Cache leaderboard summaries (30s TTL)#208
msaroufim merged 1 commit intomainfrom
cache-leaderboard-summaries

Conversation

@msaroufim
Copy link
Member

Summary

  • Adds a 30-second in-memory TTL cache to /api/leaderboard-summaries
  • Follows the same cache pattern already used in events.py
  • Keyed by query version (v1/v2) so both paths are cached independently

Why

The homepage summary query takes ~1.3s because it scans all 508K submissions and sorts 189K rows across all 30 leaderboards to compute rankings. With the cache, only the first request in each 30s window hits the database — subsequent requests return instantly.

EXPLAIN ANALYZE on prod showed:

  • Seq Scan on submission: 508K rows, 68ms
  • Sort for RANK() window function: 189K rows, 20MB memory, 1.2s

Test plan

  • Verify homepage loads fast on repeated refreshes
  • Verify data updates within 30s of new submissions
  • Verify ?v1_query still works independently

The homepage query scans all 508K submissions and sorts 189K rows
for ranking, taking ~1.3s. Add a 30-second in-memory cache following
the same pattern used in events.py to make repeated homepage loads
near-instant.
@msaroufim msaroufim merged commit beebbf1 into main Feb 26, 2026
1 check passed
msaroufim added a commit that referenced this pull request Feb 26, 2026
msaroufim added a commit that referenced this pull request Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant