Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Cache user for optimized authentication #207

Open
rajdip-b opened this issue May 11, 2024 · 0 comments
Open

API: Cache user for optimized authentication #207

rajdip-b opened this issue May 11, 2024 · 0 comments
Labels
difficulty: 2 foss hack Clustering all the curated issues for Foss Hack 2024 help wanted Extra attention is needed priority: medium scope: api Everything related to the API type: enhancement New feature or request

Comments

@rajdip-b
Copy link
Member

Description

keyshade uses stateless cookie-based authentication to let users in. In the long run, this will impose a strain on our database since we will have over 100qps. To optimize this, we would like to cache our user data during authentication.

Solution

In auth.guard.ts, if the authentication is successful, we fetch the user details and set it in the current session. We would also like to cache this data. We will be using the key structure user-<userId>.

  • Import Redis in auth.guard.ts:
    @Inject(REDIS_CLIENT) readonly redisClient: {
        publisher: RedisClientType
    },
  • Try to fetch the user from redis by the user-<userId>. If it's null, fetch from the database by email.
  • JSON.stringify() the data retrieved, and set it in cache as userId -> userObject(stringified)
  • In user.serviec.ts, whenever a user is updated, fetch the cached user, and update the value of the cache.

Additional context

Relevant files can be found in auth and user module.

@rajdip-b rajdip-b added type: enhancement New feature or request help wanted Extra attention is needed scope: api Everything related to the API priority: medium labels May 11, 2024
@rajdip-b rajdip-b added difficulty: 2 foss hack Clustering all the curated issues for Foss Hack 2024 labels Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: 2 foss hack Clustering all the curated issues for Foss Hack 2024 help wanted Extra attention is needed priority: medium scope: api Everything related to the API type: enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

1 participant