Skip to content

core: batch expired cache and channel record cleanup - #27587

Merged
BeryJu merged 2 commits into
goauthentik:mainfrom
nalves599:nalves599/batch-expired-cache
Nov 5, 2026
Merged

BeryJu merged 2 commits into
goauthentik:mainfrom
nalves599:nalves599/batch-expired-cache

Conversation

@nalves599

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

Replaces individual deletes with batches of up to 1,000 rows when cleaning up expired PostgreSQL cache entries, messages, and group-channel records.

Each batch runs in a transaction and uses SELECT FOR UPDATE SKIP LOCKED to protect against concurrent cache refreshes and avoid waiting on rows locked by other transactions. Skipped rows are left for a later cleanup run.

Existing ExpiringModel.expire_action() hooks and logging levels are preserved. The helper returns the actual number of target-model records deleted.

Also replaces return [] with return in chunked_queryset(), since it is a generator whose return type is None.

Why is this change needed?

Deleting records individually requires many database round trips. With a high-latency PostgreSQL connection, clean_expired_models can exceed its 600-second time limit.

Batching reduces those round trips while keeping each transaction bounded.

How was this tested?

  • Ran 13 targeted tests through authentik’s test runner against local PostgreSQL, using the locked dependencies.
  • Covered multiple batches, unexpired records, concurrent refreshes, concurrent cleanup workers, locked rows, and rollback of a failed batch.
  • Verified query counts scale with batches and existing token-expiration behavior still works.
  • Ruff, Black, targeted type checks, and git diff --check passed.

A local benchmark with 2,000 expired cache entries and an injected 10 ms delay per SQL execution produced:

Implementation SQL executions Duration
Individual deletes 2,006 25.6 seconds
Batched deletes 5 0.07 seconds

These are simulated-latency results, not production measurements. The full make all suite was not run.

Linked issues

Refs #26989 for the related cleanup-timeout symptom. This PR does not address the worker connection-recovery problem described there.


Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)
  • I have read the AI usage policy.

@nalves599
nalves599 requested a review from a team as a code owner November 4, 2026 11:33
@netlify

netlify Bot commented Nov 4, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 8c7c22e
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6aeb180f6b4d3fabd5b4221a
😎 Deploy Preview https://deploy-preview-27587--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.

@nalves599
nalves599 force-pushed the nalves599/batch-expired-cache branch from 8c7c22e to 41fbf8b Compare November 4, 2026 11:37
@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.35484% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.96%. Comparing base (d11d12a) to head (26e5d40).
⚠️ Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
authentik/lib/tests/test_utils_db.py 99.07% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #27587      +/-   ##
==========================================
- Coverage   93.00%   92.96%   -0.04%     
==========================================
  Files        1226     1229       +3     
  Lines       85235    85689     +454     
  Branches     4894     4894              
==========================================
+ Hits        79270    79665     +395     
- Misses       5878     5937      +59     
  Partials       87       87              
Flag Coverage Δ
conformance 30.46% <2.58%> (-0.11%) ⬇️
e2e 35.18% <2.58%> (-0.15%) ⬇️
integration 27.63% <2.58%> (-0.42%) ⬇️
rust 51.12% <ø> (ø)
unit 94.52% <99.35%> (-0.02%) ⬇️
unit-migrate 94.53% <99.35%> (-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.

Signed-off-by: Jens Langhammer <[email protected]>
@BeryJu BeryJu added the backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 label Nov 5, 2026
@netlify

netlify Bot commented Nov 5, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit 26e5d40
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6aec71ca722b48eba8712f90
😎 Deploy Preview https://deploy-preview-27587--authentik-storybook.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.

@netlify

netlify Bot commented Nov 5, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit 26e5d40
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6aec71cac441dd3cb5d2b225
😎 Deploy Preview https://deploy-preview-27587--authentik-integrations.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.

@BeryJu
BeryJu merged commit 79ef208 into goauthentik:main Nov 5, 2026
163 checks passed
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

🍒 Cherry-pick to version-2026.8 created: #27634

BeryJu added a commit that referenced this pull request Nov 5, 2026
 to version-2026.8) (#27634)

core: batch expired cache and channel record cleanup (#27587)

* core: batch expired cache and channel record cleanup

* delete more efficiently



---------

Signed-off-by: Jens Langhammer <[email protected]>
Co-authored-by: Nuno Alves <[email protected]>
Co-authored-by: Jens Langhammer <[email protected]>
kensternberg-authentik added a commit that referenced this pull request Nov 5, 2026
* main: (313 commits)
  web/admin: fix ldap source parent group form (#26421)
  stages/prompt: preserve empty initial values (#25148)
  core: batch expired cache and channel record cleanup (#27587)
  web: bump mermaid from 11.16.0 to 12.3.0 in /website (#27598)
  ci: bump taiki-e/install-action from 2.89.0 to 2.89.1 in /.github/actions/setup (#27630)
  core: bump goauthentik/fips-python from `d4c46c4` to `69e7058` in /lifecycle/container (#27626)
  ci: bump AndreKurait/docker-cache from 0.7.0 to 0.7.1 in /.github/actions/setup (#27628)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 in /.github/actions/setup (#27629)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 (#27627)
  providers/oauth2: sign tokens with ML-DSA keys (#27172)
  sources/oauth: verify ML-DSA signed tokens (#27170)
  web: bump postcss from 8.5.18 to 8.5.32 in /website (#27602)
  web: bump postcss from 8.5.16 to 8.5.32 in /web (#27604)
  web/element: fix missing line break between elapsed time and displayed time. (#27624)
  web/elements: rename ak-codemirror to ak-code-editor (#27406)
  website/integrations: add Google Cloud Workforce Identity Federation guide (#27571)
  root: test that FIPS mode refuses non-approved algorithms (#27536)
  web: bump nanoid from 3.3.16 to 3.3.20 in /website (#27599)
  web: bump baseline-browser-mapping from 2.10.43 to 2.11.35 in /website (#27600)
  web: bump mermaid from 11.16.1 to 11.17.2 in /web (#27603)
  ...
kensternberg-authentik added a commit that referenced this pull request Nov 5, 2026
* main: (1729 commits)
  web/admin: fix ldap source parent group form (#26421)
  stages/prompt: preserve empty initial values (#25148)
  core: batch expired cache and channel record cleanup (#27587)
  web: bump mermaid from 11.16.0 to 12.3.0 in /website (#27598)
  ci: bump taiki-e/install-action from 2.89.0 to 2.89.1 in /.github/actions/setup (#27630)
  core: bump goauthentik/fips-python from `d4c46c4` to `69e7058` in /lifecycle/container (#27626)
  ci: bump AndreKurait/docker-cache from 0.7.0 to 0.7.1 in /.github/actions/setup (#27628)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 in /.github/actions/setup (#27629)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 (#27627)
  providers/oauth2: sign tokens with ML-DSA keys (#27172)
  sources/oauth: verify ML-DSA signed tokens (#27170)
  web: bump postcss from 8.5.18 to 8.5.32 in /website (#27602)
  web: bump postcss from 8.5.16 to 8.5.32 in /web (#27604)
  web/element: fix missing line break between elapsed time and displayed time. (#27624)
  web/elements: rename ak-codemirror to ak-code-editor (#27406)
  website/integrations: add Google Cloud Workforce Identity Federation guide (#27571)
  root: test that FIPS mode refuses non-approved algorithms (#27536)
  web: bump nanoid from 3.3.16 to 3.3.20 in /website (#27599)
  web: bump baseline-browser-mapping from 2.10.43 to 2.11.35 in /website (#27600)
  web: bump mermaid from 11.16.1 to 11.17.2 in /web (#27603)
  ...
kensternberg-authentik added a commit that referenced this pull request Nov 5, 2026
* main: (28 commits)
  events: bundle DB-IP Lite GeoIP databases instead of MaxMind GeoLite2 (#27102)
  web/admin: fix ldap source parent group form (#26421)
  stages/prompt: preserve empty initial values (#25148)
  core: batch expired cache and channel record cleanup (#27587)
  web: bump mermaid from 11.16.0 to 12.3.0 in /website (#27598)
  ci: bump taiki-e/install-action from 2.89.0 to 2.89.1 in /.github/actions/setup (#27630)
  core: bump goauthentik/fips-python from `d4c46c4` to `69e7058` in /lifecycle/container (#27626)
  ci: bump AndreKurait/docker-cache from 0.7.0 to 0.7.1 in /.github/actions/setup (#27628)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 in /.github/actions/setup (#27629)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 (#27627)
  providers/oauth2: sign tokens with ML-DSA keys (#27172)
  sources/oauth: verify ML-DSA signed tokens (#27170)
  web: bump postcss from 8.5.18 to 8.5.32 in /website (#27602)
  web: bump postcss from 8.5.16 to 8.5.32 in /web (#27604)
  web/element: fix missing line break between elapsed time and displayed time. (#27624)
  web/elements: rename ak-codemirror to ak-code-editor (#27406)
  website/integrations: add Google Cloud Workforce Identity Federation guide (#27571)
  root: test that FIPS mode refuses non-approved algorithms (#27536)
  web: bump nanoid from 3.3.16 to 3.3.20 in /website (#27599)
  web: bump baseline-browser-mapping from 2.10.43 to 2.11.35 in /website (#27600)
  ...
kensternberg-authentik added a commit that referenced this pull request Nov 5, 2026
…b-controller' into web/admin/breadcrumb-controller

* refs/remotes/origin/web/admin/breadcrumb-controller: (214 commits)
  The updated format for crumbs required an update to the unit tests.
  web/admin: Add a Breadcrumb context manager to the navigation suite
  Adding the Breadcrumbs visual component.
  Revised naming code to track certain features.
  web/elements: vendor sidebar and add icons (#27289)
  providers/oauth2: don't set state in responses if not supplied (#9735)
  web/elements: expansion visual hierarchy (#27562)
  providers/oauth2: accept ML-DSA keys for DPoP (#27174)
  website/docs: draft 2026.11 release notes (#26814)
  sources/ldap: store the directory date when resetting a password (#27578)
  events: bundle DB-IP Lite GeoIP databases instead of MaxMind GeoLite2 (#27102)
  web/admin: fix ldap source parent group form (#26421)
  stages/prompt: preserve empty initial values (#25148)
  core: batch expired cache and channel record cleanup (#27587)
  web: bump mermaid from 11.16.0 to 12.3.0 in /website (#27598)
  ci: bump taiki-e/install-action from 2.89.0 to 2.89.1 in /.github/actions/setup (#27630)
  core: bump goauthentik/fips-python from `d4c46c4` to `69e7058` in /lifecycle/container (#27626)
  ci: bump AndreKurait/docker-cache from 0.7.0 to 0.7.1 in /.github/actions/setup (#27628)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 in /.github/actions/setup (#27629)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 (#27627)
  ...
kensternberg-authentik added a commit that referenced this pull request Nov 5, 2026
…k-controller

* web/admin/breadcrumb-controller: (215 commits)
  Changes requested by reviewer (@GirlBossRush).
  The updated format for crumbs required an update to the unit tests.
  web/admin: Add a Breadcrumb context manager to the navigation suite
  Adding the Breadcrumbs visual component.
  Revised naming code to track certain features.
  web/elements: vendor sidebar and add icons (#27289)
  providers/oauth2: don't set state in responses if not supplied (#9735)
  web/elements: expansion visual hierarchy (#27562)
  providers/oauth2: accept ML-DSA keys for DPoP (#27174)
  website/docs: draft 2026.11 release notes (#26814)
  sources/ldap: store the directory date when resetting a password (#27578)
  events: bundle DB-IP Lite GeoIP databases instead of MaxMind GeoLite2 (#27102)
  web/admin: fix ldap source parent group form (#26421)
  stages/prompt: preserve empty initial values (#25148)
  core: batch expired cache and channel record cleanup (#27587)
  web: bump mermaid from 11.16.0 to 12.3.0 in /website (#27598)
  ci: bump taiki-e/install-action from 2.89.0 to 2.89.1 in /.github/actions/setup (#27630)
  core: bump goauthentik/fips-python from `d4c46c4` to `69e7058` in /lifecycle/container (#27626)
  ci: bump AndreKurait/docker-cache from 0.7.0 to 0.7.1 in /.github/actions/setup (#27628)
  ci: bump actions-rust-lang/setup-rust-toolchain from 2.0.0 to 2.0.1 in /.github/actions/setup (#27629)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend backport/version-2026.8 Add this label to PRs to backport changes to version-2026.8 goal:performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants