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

[RBAC] memoize user.singleton_permissions() in Django (redis) cache #224

Open
AlanCoding opened this issue Mar 15, 2024 · 0 comments
Open
Labels
app:rbac performance Reduction of queries, query performance, etc.

Comments

@AlanCoding
Copy link
Member

As documented in rbac.md docs, enabling the global roles for users, and enabling global roles for teams, both incur 1 additional query per request. This can be knocked down significantly by simply using the local redis cache, avoiding round-tripping to a database serving an entire cluster. Ping @john-westcott-iv

In AWX we have a simple decorator that stores the return value in the cache.

https://github.com/ansible/awx/blob/8ff7260bc66f4de9aa177cb81ba83da3a2f53ec8/awx/main/utils/common.py#L171

Note that this uses from django.core.cache import cache which is the standard Django cache framework.

https://docs.djangoproject.com/en/5.0/topics/cache/

The use case of getting singleton permissions is a perfect use case for this because:

  • it will be called repeatedly for the same user - ideally, I imagine we will require the user primary key as the input argument
  • it will be used repeatedly by multiple uWSGI workers on the same node, meaning more workers will get more benefit from this
  • the output is trivially JSON serializable, which is just a set/list (I don't care) of strings
  • cache invalidation is rare, and only happens when membership in a system-wide role changes, and I would suggest it is sufficient to use a 5 to 60 second delay as a lazy solution

Note that Django does not offer this @memoize decorator. That's fine, since it's in AWX it is ours to adopt. We should move it into DAB utils, and have AWX import from there. The method is written in a very widely compatible way.

@AlanCoding AlanCoding added the performance Reduction of queries, query performance, etc. label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app:rbac performance Reduction of queries, query performance, etc.
Projects
None yet
Development

No branches or pull requests

1 participant