Skip to content

Optimize API Calls and Implement User Identification for Quiz Service #141

@code-soubhik

Description

@code-soubhik

Currently, we are calling the Gemini API each time a user wants to take a quiz, even if different users are attempting the same quiz. This increases API costs unnecessarily. The following improvements are proposed:

  1. Schema Update:

    • Introduce a schema to store quiz data (quiz_id and quiz_data) and track user attempts (user_id and quiz_id).
    • Implement a UserQuiz mapping to track which quizzes a user has already attempted, so we can serve existing quizzes from the database if available.
  2. User Identification:

    • Implement a method to identify logged-in users through authentication tokens (JWT or session tokens).
    • For non-logged-in users, create a temporary session ID to track quiz attempts, ensuring that quizzes can be fetched based on session instead of user login state.
  3. Optimize Quiz Retrieval:

    • Before calling the Gemini API, check if the quiz data already exists in the database for the user or session. If it does, serve it from the database, avoiding unnecessary API calls.
    • If the quiz is not available, make the API call to fetch it, then store the data in the database for future use.
  4. API Response Format:

    • Ensure the Gemini API returns data in a structured JSON format (instead of raw data) for easier processing and display.
    • Implement an optimistic response pattern on the frontend, where quiz data is displayed (or a loading indicator) while the API fetches the actual content in the background.

Challenges:

  • User Identification: Properly identifying and managing sessions for non-logged-in users.
  • Database Management: Efficiently storing and retrieving quiz data, especially with large numbers of users and quizzes.
  • API Cost: Minimizing unnecessary Gemini API calls to optimize cost and performance.

By implementing these changes, we can reduce API calls, lower costs, and improve the user experience.

Please assign me so I can take it over and raise a PR.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions