Skip to content

packages/django-postgres-cache, flows: count cache keys in the database - #25808

Merged
BeryJu merged 9 commits into
mainfrom
dominic/flow-cache-count
Oct 27, 2026
Merged

BeryJu merged 9 commits into
mainfrom
dominic/flow-cache-count

Conversation

@dominic-r

@dominic-r dominic-r commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Flow cache statistics currently download every matching cache key and then count the strings in Python. This asks PostgreSQL for the count directly, so the database returns one number regardless of how many keys match.

The cache backend gets a keys_count() method next to keys(). Both build their query through one private filter method, so a count always matches the length of the key list for the same pattern and version. Exact keys, prefix wildcards, complex patterns, key versions, and the inclusion of expired entries behave as they do for keys(). The flow serializer's cache_count, the flow cache_info endpoint, and the cached-flows monitoring gauge now use keys_count(). Cache clearing still lists keys, because it needs them to delete. Flow execution and cache expiry do not change.

The policy cache endpoint and gauge still count with len(cache.keys(...)). They can move to keys_count() in a follow-up.

This is a modest optimization, not a large endpoint speedup. The timings below measure one count operation against PostgreSQL, without HTTP or browser rendering. Both versions make one query; the gain is skipping the key transfer and the Python list. Counts matched at every size. Figures are medians of three runs after warmup up to 1,000 keys; 10,000 is one run per version.

Rows Before After Queries before → after
50 0.72 ms 0.58 ms 1 → 1
100 0.68 ms 0.65 ms 1 → 1
500 1.26 ms 1.24 ms 1 → 1
1,000 1.35 ms 1.13 ms 1 → 1
10,000 4.93 ms 2.16 ms 1 → 1

Testing: a new backend test checks keys_count() against keys() for exact, prefix, complex, and missing patterns, a second key version, and an expired entry. The flow API tests check cache_count and cache_info against cached plans for two flows, another key version, an expired entry, and an unrelated key. The API schema does not change.

@dominic-r
dominic-r requested a review from a team as a code owner September 5, 2026 16:31
@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 bec0e10
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6ab030101f0dd100081b22a0
😎 Deploy Preview https://deploy-preview-25808--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.40%. Comparing base (7ff197b) to head (a1b703a).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #25808   +/-   ##
=======================================
  Coverage   92.40%   92.40%           
=======================================
  Files        1191     1191           
  Lines       80332    80352   +20     
  Branches     4351     4351           
=======================================
+ Hits        74227    74250   +23     
+ Misses       6039     6036    -3     
  Partials       66       66           
Flag Coverage Δ
conformance 32.31% <0.00%> (-0.01%) ⬇️
e2e 36.99% <0.00%> (-0.01%) ⬇️
integration 29.20% <0.00%> (-0.01%) ⬇️
rust 44.42% <ø> (ø)
unit 94.05% <100.00%> (+<0.01%) ⬆️
unit-migrate 94.05% <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-5c586a23fecd6994d886404d84e1dc8d9b5dce1e
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-5c586a23fecd6994d886404d84e1dc8d9b5dce1e

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

@BeryJu BeryJu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

the only I dont like is adding custom methods to the cache backend since django.core.cache's cache object is not typed at all

@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Sep 17, 2026
@netlify

netlify Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit bec0e10
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6ab03010530b2d0008b46aa5
😎 Deploy Preview https://deploy-preview-25808--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 Sep 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

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

@dominic-r
dominic-r requested a review from BeryJu September 20, 2026 19:08
@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Playwright e2e

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

gh run download 40249045805 -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-a1b703a9f9a127bc904b7b1df549709528c893d5
# Distroless image: AUTHENTIK_TAG=gh-a1b703a9f9a127bc904b7b1df549709528c893d5-distroless
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-a1b703a9f9a127bc904b7b1df549709528c893d5
        # Distroless image: tag: gh-a1b703a9f9a127bc904b7b1df549709528c893d5-distroless

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


def get_cache_count(self, flow: Flow) -> int:
"""Get count of cached flows"""
return len(cache.keys(f"{cache_key(flow)}*"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather add a keys_count method on the cache backend.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats what it was and I what I voted against since the django.core.cache.cache object is un-typed I dont like adding custom methods to it, but I also don't like directly calling the ORM here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, keys already isn't standard

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok fair enough, TIL keys isn't standard, lets do the custom one then

…ckend

keys() is already a method of our backend rather than Django's cache
API, so counting belongs next to it instead of in a direct CacheEntry
query in the flow API. Both methods share one filter, so a count always
matches the length of the key list for the same pattern and version.
@dominic-r dominic-r changed the title flows: count cache keys in the database packages/django-postgres-cache, flows: count cache keys in the database Oct 15, 2026
@dominic-r
dominic-r requested a review from rissson October 15, 2026 16:17
@BeryJu
BeryJu merged commit aa020fd into main Oct 27, 2026
171 checks passed
@BeryJu
BeryJu deleted the dominic/flow-cache-count branch October 27, 2026 14:31
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Oct 27, 2026
kensternberg-authentik added a commit that referenced this pull request Nov 5, 2026
* main: (89 commits)
  providers/scim: scope-aware adoption (#26174)
  sources/ldap: respect accountExpires attributes (#26135)
  web/flows: never let continuous login block the final redirect (#26269)
  website: document password hash import restrictions (#24235)
  core: restrict imported password hashers (#24234)
  core, web: update translations (#27148)
  server: retry requests on connections closed by a recycled worker (#27275)
  packages/django-postgres-cache, flows: count cache keys in the database (#25808)
  events: simplify execution of event rules/notifications (#26873)
  core: bump github.com/pires/go-proxyproto from 0.15.0 to 0.16.0 (#27260)
  core: bump goauthentik/platform-ssh from 26.04-ak-0.72.3 to 26.04-ak-0.72.4 in /tests/e2e (#27263)
  core: bump goauthentik/selenium from 155.0-ak-0.72.3 to 155.0-ak-0.72.4 in /tests/e2e (#27261)
  core: bump astral-sh/uv from 0.13.5 to 0.13.6 in /lifecycle/container (#27264)
  core: bump jsonpatch from 1.35 to 1.36 (#27269)
  outposts: refresh channel groups on hello (#27136)
  core: bump certifi from 2026.10.19 to 2026.10.23 (#27266)
  core: bump pluggy from 1.6.0 to v1.7.0 (#27238)
  core: bump markupsafe from 3.0.3 to v3.0.4 (#27225)
  core: bump httptools from 0.8.0 to v0.9.0 (#27219)
  core: bump iniconfig from 2.3.0 to v2.3.1 (#27221)
  ...
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.

3 participants