Skip to content

[PERF] Cache leaderboard API response for 5 minutes to reduce database load #1908

@Priyanshu-byte-coder

Description

@Priyanshu-byte-coder

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

  • Leaderboard response cached for 5 minutes
  • Cache invalidated on opt-out
  • Response time for leaderboard < 200ms on cache hit

Metadata

Metadata

Labels

gssoc26GSSoC 2026 contributiongssoc:assignedGSSoC: Issue assigned to a contributorlevel:intermediateGSSoC: Intermediate difficulty (35 pts)type:performanceGSSoC type bonus: performance (+15 pts)

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions