Skip to content

Share public cache entries across ignored tracking queries - #350

Draft
abhi1693 wants to merge 4 commits into
masterfrom
perf/public-cache-query-identity
Draft

abhi1693 wants to merge 4 commits into
masterfrom
perf/public-cache-query-identity

Conversation

@abhi1693

@abhi1693 abhi1693 commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

What changes and why?

Ignored tracking parameters currently create distinct server-cache entries for identical public responses. Owned cached public endpoints now opt into query normalization based on FastAPI's native dependency metadata. A request such as /v1/feed?limit=1&utm_source=second can reuse the response for the same declared filters with another tracking value.

Aliases, dependency fields, repeated-value ordering, path parameters and policy/schema identities remain significant. Declared parameter values are not coerced, so validation failures cannot reuse valid responses. Query models and complex alias paths conservatively retain all parameters because unknown keys can carry validation semantics. Custom cache policies remain exact by default.

Before/after PostgreSQL measurements

A local x86-64 PostgreSQL 18 / session-mode PgBouncer / Redis comparison ran three
samples per policy with alternating order. The baseline uses the original exact
query identity (normalization metadata omitted); the candidate uses native declared
query metadata. Warm-up is excluded. A separate observer role captured all workload
PostgreSQL statements, including driver setup, read-only transactions and pooler
DISCARD ALL, rather than relying only on SQLAlchemy application SQL events.
The warmed pool has one existing backend, so backend startups were zero in both
modes. The fixture reads the same approved source detail with unchanged inputs.

Metric Before After Measurement basis
Cache outcomes for 100 tracking variants 100 MISS 100 HIT Exact-query baseline versus normalization, three samples each.
API data SELECTs / all recorded PostgreSQL commands 100 / 500 0 / 0 Same 100 requests, including transaction/configuration/pooler commands.
Rows returned / shared buffer hits 100 / 300 0 / 0 Same tracking fixture; no reads, temporary spill or WAL in the candidate.
Read transactions rolled back / pooler resets 100 / 100 0 / 0 Read-only session completion, not failed requests.
PG planning + execution elapsed 24.243 ms 0 ms Median of three tracking samples; elapsed time is not CPU.
PostgreSQL-container CPU 130.147 ms 18.659 ms Median cgroup delta; includes logging, background work and probes. Candidate has no workload PG statements; the nonzero counter is not attributed to SQL.
Real Redis outage: commands / rows / buffer hits 500 / 100 / 300 500 / 100 / 300 100 requests per mode, three samples; owned Redis paused, all requests bypass cache.
Real Redis outage: PostgreSQL-container CPU range 122.921–130.143 ms 127.980–133.433 ms Three samples; overlapping noisy ranges with identical statement/row/buffer work.
Epoch churn: commands / rows / buffer hits 100 / 20 / 60 100 / 20 / 60 20 requests per mode, three samples, public epoch invalidated before every read.
Epoch churn: PostgreSQL-container CPU range 39.485–55.934 ms 40.285–41.330 ms Three samples; no PG work added by normalization.
WAL / idle transactions / remaining locks 0 / 0 / 0 0 / 0 / 0 All measured read scenarios.

There is no new SQL path or schema change. The native policy regression exercises
both exact and normalized identities with available cache, unavailable cache and
public-epoch churn. The avoided work depends on actual irrelevant-query traffic;
these fixtures do not establish production container savings. Native ARM64 and a
representative traffic/pooler canary remain required before promotion. Equal work
under cache failure does not mean the database can absorb an arbitrary cache outage.

Validation

  • 175 cache/API-policy/contract unit cases pass, including native dependency aliases, repeated-value order, strict and nullable query-model extra=forbid, malformed declared values, mounted routes and policy revisions.
  • 17 disposable-service cache integration cases pass, including exact-query/normalized controls with available cache, unavailable cache and epoch churn. The measured old exact-query policy produces 100 misses and 100 API reads for 100 variants; normalization produces 100 hits and zero API reads. Actual server command counts, including setup/transactions/pooler resets, fall 500 to zero. Existing source rejection, article maintenance, rollback and cache publication fences pass.
  • The current targeted cache/contract unit rerun passes 80 cases.
  • Python lint, formatting, types and workspace versions pass across 400 source modules; whitespace and commit hooks pass.
  • Hosted CI and native ARM64/production request-mix measurements remain pending. The controlled key-reuse result does not estimate how many real requests use irrelevant parameters or promise a total CPU percentage.

Rollout notes

No cache TTL, schema, resource-limit or production deployment changes. Existing declared-query identities remain compatible; the policy opt-in drops only ignored query keys. Future policies whose response depends on arbitrary Request.query_params must retain exact identities. The query-identity guide documents conservative fallbacks and reproduction.

Ready for review

  • Regression tests and operational limitations are recorded.
  • Current-head CI and CodeQL pass before merge.
  • Native ARM64 and representative request-mix CPU/cache measurements pass before leaving draft.

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Backend unit coverage

PASS · minimum combined coverage 98.00%.

Tests: 11965 passed, 0 failed, 0 skipped. Source files: 400.

Workspace Lines Branches Combined
All backend code 98.68% 96.03% 98.09%
apps/admin-api 96.26% 88.28% 94.93%
apps/aggregator 98.04% 94.90% 97.30%
apps/api 98.91% 97.47% 98.66%
apps/article-enrichment-worker 80.00% 50.00% 71.43%
apps/cli 99.39% 90.63% 98.13%
apps/images-worker 80.00% 50.00% 71.43%
apps/mcp 99.72% 98.17% 99.44%
apps/notifications 100.00% 100.00% 100.00%
apps/partner-api 98.70% 95.51% 98.22%
apps/search-indexer 85.48% 85.71% 85.53%
apps/source-discovery-worker 80.00% 50.00% 71.43%
apps/user-api 99.20% 97.95% 98.99%
packages/core 99.20% 97.12% 98.69%
packages/http 99.18% 97.99% 98.93%

Unit coverage includes every backend workspace. PostgreSQL/Redis integration tests run separately.

Commit: 23fffeb · HTML, JSON, XML reports and job logs · attempt 1

@abhi1693 abhi1693 mentioned this pull request Oct 31, 2026
1 of 2 tasks
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant