Skip to content

core: prefetch token owners and memberships - #25803

Merged
dominic-r merged 4 commits into
mainfrom
dominic/token-list-prefetch
Oct 16, 2026
Merged

dominic-r merged 4 commits into
mainfrom
dominic/token-list-prefetch

Conversation

@dominic-r

Copy link
Copy Markdown
Member

A token list includes details about each token's owner, including their groups and roles. If 1,000 tokens belong to the same person, the endpoint repeatedly asks the database for the same person and their memberships.

This one-line change adds prefetch_related("user__groups", "user__roles") to the token view's queryset. Django fetches the page's owners, groups, and roles in batches. Tokens with the same owner share that loaded user object for the request.

The endpoint still returns the same tokens and owner details. The serializers, permission checks, filtering, and pagination are unchanged. Ordinary users still see their own tokens; users with the appropriate permission can see others' tokens. Expired tokens remain included. The loaded objects are local to the request, so changing a membership is reflected in the next request.

Before and after

Local fixture: 10,000 users, each with a group and role, plus 10,000 tokens sharing one owner and another 10,000 tokens with distinct owners. These are medians of three warmed, paired Django API-client requests against dev PostgreSQL. Each before/after pair uses the same fixture and produces byte-for-byte identical responses. Timing includes rendering, excludes network/browser time, and is specific to this fixture. The page-size limit was temporarily raised to 1,000.

Owners Page size Before After Queries before → after
One shared owner 100 522 ms 236 ms 1,006 → 410
One shared owner 1,000 6.66 s 4.79 s 10,006 → 4,010
Distinct owners 100 853 ms 693 ms 1,006 → 509
Distinct owners 1,000 12.93 s 8.14 s 10,006 → 5,009

This removes repeated owner and membership reads. Other per-token serialization queries remain, so the endpoint's query count still grows with page size. Benchmark data and the page-size setting were rolled back afterward.

Validation

  • One added test checks that an expired token still includes its owner's groups and roles, and that removing those memberships is reflected in the next request. It passes before and after the change and does not assert a fixed query budget. Existing tests cover ordinary-user visibility and access with view_token permission; the benchmark compares complete responses for shared and distinct owners.
  • Token and user API suites: 60 tests and 4 subtests pass, including existing token creation, ownership-change rejection, and key-access tests.
  • Black, Ruff, and the pending-migration check pass. make gen succeeds with no schema or generated-client changes.

Made with GPT 6 Astra in Codex, which investigated the bottleneck, wrote the change and tests, ran the benchmarks and checks, and drafted this description under maintainer direction.

@dominic-r
dominic-r requested a review from a team as a code owner September 5, 2026 15:45
@dominic-r dominic-r self-assigned this Sep 5, 2026
@netlify

netlify Bot commented Sep 5, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 841c4f2
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6a9c39040afa84000854833b
😎 Deploy Preview https://deploy-preview-25803--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.29%. Comparing base (1425c71) to head (4a88c86).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #25803   +/-   ##
=======================================
  Coverage   92.29%   92.29%           
=======================================
  Files        1185     1185           
  Lines       79557    79577   +20     
  Branches     4351     4351           
=======================================
+ Hits        73424    73445   +21     
+ Misses       6067     6066    -1     
  Partials       66       66           
Flag Coverage Δ
conformance 32.42% <13.04%> (-0.01%) ⬇️
e2e 37.12% <13.04%> (-0.01%) ⬇️
integration 29.28% <13.04%> (-0.01%) ⬇️
rust 44.42% <ø> (ø)
unit 93.95% <100.00%> (+<0.01%) ⬆️
unit-migrate 93.95% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

authentik PR Installation instructions

Instructions for docker-compose

Add the following block to your .env file:

AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-841c4f240a4bbf4ad033830fec7a06ce63fa25d2
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s

Afterwards, run the upgrade commands from the latest release notes.

Instructions for Kubernetes

Add the following block to your values.yml file:

authentik:
    outposts:
        container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
    image:
        repository: ghcr.io/goauthentik/dev-server
        tag: gh-841c4f240a4bbf4ad033830fec7a06ce63fa25d2

Afterwards, run the upgrade commands from the latest release notes.

Comment thread authentik/core/api/tokens.py Resolved
Nothing reads an owner's groups, roles, or computed user fields from
the token API, and fetching them cost several queries per token. Use
PartialUserSerializer and join the owner, so serializing a page of
tokens takes one query.

user_obj on tokens, and on token_obj in SCIM sources and SSF
providers, now has the PartialUser shape instead of the full User.
@dominic-r
dominic-r requested a review from a team as a code owner October 16, 2026 13:49
Comment thread authentik/core/tests/test_token_api.py Resolved
@dominic-r
dominic-r requested a review from BeryJu October 16, 2026 13:52
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Oct 16, 2026
@dominic-r
dominic-r enabled auto-merge (squash) October 16, 2026 14:00
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Playwright e2e

Download this run's HTML report and traces, then open the report:

gh run download 38911247180 -D playwright-artifacts
npx playwright show-report playwright-artifacts/playwright-report

Installation instructions

Instructions for docker-compose

Add the following block to your .env file:

AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
AUTHENTIK_TAG=gh-4a88c8645a378ed09836bd8536d9b21ba7156874
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s

Afterwards, run the upgrade commands from the latest release notes.

Instructions for Kubernetes

Add the following block to your values.yml file:

authentik:
    outposts:
        container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
global:
    image:
        repository: ghcr.io/goauthentik/dev-server
        tag: gh-4a88c8645a378ed09836bd8536d9b21ba7156874

Afterwards, run the upgrade commands from the latest release notes.

@dominic-r
dominic-r merged commit 5703c1d into main Oct 16, 2026
146 checks passed
@dominic-r
dominic-r deleted the dominic/token-list-prefetch branch October 16, 2026 14:14
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Oct 16, 2026
kensternberg-authentik added a commit that referenced this pull request Oct 19, 2026
* main: (101 commits)
  ci: exclude node_modules from Go lint (#26961)
  website/integrations: ch-ui: document the email_verified requirement (#26978)
  website/docs: add expression policy for case-insensitive unique usernames (#26974)
  core: prefetch token owners and memberships (#25803)
  website/integrations: add detailed explanation for setting GH Org limit (#8604)
  website/integrations: add opensearch (#26938)
  endpoints: fix MultipleObjectsReturned (#26971)
  core, web: update translations (#26963)
  website/docs: single-logout: add information around proxy providers for single logout (#26563)
  common/saml: move exceptions and logout request parser to common (#26533)
  stages/authenticator_webauthn: accept ML-DSA credentials (#26381)
  crypto: add ML-DSA certificate generation (#26380)
  website/integrations: add ch-ui (#26935)
  website/integrations: add kibana (#26936)
  core: bump pydantic from 2.13.5 to 2.14.0 (#26969)
  core: bump rust-toolchain from 1.99.0 to 1.99.1 (#26967)
  core: bump aws-cdk-lib from 2.272.0 to 2.273.0 (#26968)
  core: bump github.com/prometheus/client_golang from 1.24.1 to 1.25.0 (#26965)
  core: bump github.com/getsentry/sentry-go from 0.49.0 to 0.50.0 (#26966)
  web/admin: version banner (#26948)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants