Problem
/api/leaderboard recomputes rankings from scratch on every request by aggregating metrics for all opted-in users. With hundreds of users, this triggers many DB queries and slows the page.
Proposed solution
Cache the leaderboard response for 5 minutes using Redis (Upstash) or Next.js unstable_cache:
import { unstable_cache } from 'next/cache';
const getCachedLeaderboard = unstable_cache(
async () => computeLeaderboard(),
['leaderboard'],
{ revalidate: 300 }
);
Trade-offs
- Rankings may be up to 5 minutes stale — acceptable since leaderboard is not real-time
- Cache should be invalidated when a user opts out of the leaderboard
Acceptance criteria
Problem
/api/leaderboardrecomputes rankings from scratch on every request by aggregating metrics for all opted-in users. With hundreds of users, this triggers many DB queries and slows the page.Proposed solution
Cache the leaderboard response for 5 minutes using Redis (Upstash) or Next.js
unstable_cache:Trade-offs
Acceptance criteria