Skip to content

Performance: Optimize historical performance fetching & implement backend caching to avoid rate-limiting #154

@yashvi-3106

Description

@yashvi-3106

Problem Description

Currently, when a user accesses a student's analytics profile, the server triggers the /api/student/:username endpoint, calling the fetchStudentHistory function in scripts/fetch-student-info.js.

To generate a 365-day graph, this function fetches daily JSON snapshots directly from the raw GitHub contents of the data repository (leetcode-ranking-data) via fetch() in chunks of 100 requests.

This leads to several critical issues:

  1. Excessive Network Requests: Up to 365 HTTP requests are dispatched to raw.githubusercontent.com for a single page view.
  2. GitHub API Rate Limiting: The server will quickly get rate-limited by GitHub when multiple profiles are viewed.
  3. High Latency: Loading the profile page takes several seconds due to network roundtrips.
  4. Memory Leak Risk: The in-memory studentCache in server.js is unbounded, meaning its size grows indefinitely as different usernames are queried.

Proposed Solution

  1. Server-side Cache Persistence: Store the compiled student histories in a local file-based cache or a lightweight database (e.g., SQLite) on the Express backend server.
  2. Incremental Sync: Instead of scanning 365 historical files on every request, fetch the full history once, and then only poll/fetch incremental new daily files during synchronization, appending them to the cached record.
  3. Bounded Cache: Implement a size limit or LRU (Least Recently Used) cache strategy for studentCache inside server.js to automatically evict stale entries and prevent memory growth.
  4. Alternative - Pre-Compiled History Files: Compile the unified history of each student into a single JSON file (e.g., history/username.json) in the data repository during the scheduled synchronization cron job. The backend can then fetch one file instead of hundreds.

Affected Files

  • server.js
  • scripts/fetch-student-info.js
  • scripts/sync-leaderboard.js

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions