Skip to content

fix(backend): serve app search from the shared public-app cache, not a per-request full scan - #11851

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/apps-search-cached-public-set
Aug 19, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/apps-search-cached-public-set

Conversation

@kodjima33

@kodjima33 kodjima33 commented Aug 19, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

GET /v2/apps/search — the Apps tab search box — times out in prod. Load-balancer logs for api.omi.me, 2026-08-18T12Z → 2026-08-19T05Z on image c5b0b5d, broken down by query shape:

query shape n p50 p90 >5s
q=<term>&limit=100 29 13.44s 30.05s 24
installed_apps=true&limit=100 802 0.15s 0.40s 43
my_apps=true&limit=100 12 0.13s 0.36s 0

32 requests terminated as HTTP 504 at the 30s edge in that window (Dart/3.11 mobile and the web app). The 504s are spread evenly across the 17 hours and do not co-occur with the /v3/memories 504 spikes — this is its own defect, not shared instance starvation.

Root cause

search_apps_db streamed approved == True AND private == False from Firestore on every request. Measured against prod Firestore right now: 3,247 documents, ~25MB of JSON.

The split above is the proof: the only slow shapes are the ones that issue that read. ?q= always does. installed_apps=true does only when the user has more than 30 enabled apps (Firestore's in limit forces the fallback) — which is precisely its 43-request tail; the id in [...] fast path sits at p50 0.15s. my_apps=true never does and never times out.

utils.apps.get_approved_available_apps — the marketplace list the user came from — already serves those same documents from a 10-minute Redis cache under get_public_approved_apps_data. Search was the one reader that bypassed it.

Fix

search_apps_db reads that shared key for both public-set branches, via a new get_public_approved_apps_cached_db() that uses the same key, TTL and App.reduce_dict reduction as the list path, so search can never serve a staler or differently-shaped view than the marketplace. category / capability move to Python filters (they were server-side filters on the read being replaced; my_apps is False on this branch, so they apply unconditionally).

Unchanged and still querying Firestore: my_apps=true, the id in [...] installed-apps fast path, and the uid-scoped second read that adds a user's own private/unapproved enabled apps. Those records are not in the public cache.

The cache key becomes one shared constant (database.apps.PUBLIC_APPROVED_APPS_CACHE_KEY) used by the reader, the writer and invalidate_apps_cache, so no reader can drift off the invalidation path.

What I tested

backend/tests/unit/test_apps_search_reads_cached_public_set.py (new, 6 tests) drives the real route over HTTP — routers.apps.router mounted on a FastAPI app, real search_apps_db, only the Firestore client and the two Redis cache helpers stubbed:

  • cold read streams the collection exactly once and populates the shared key with reduced records (chat_prompt absent, description present)
  • warm cache serves ?q= with zero Firestore streams — the read that cost 13s+ in prod
  • default browse plus category and capability filters all served from the cache, matching and non-matching values both asserted
  • my_apps=true still streams Firestore
  • installed_apps with ≤30 enabled ids still streams Firestore
  • installed_apps with >30 enabled ids takes the public set from the cache and streams once for the user's own apps

Control run against the pre-fix source (source files stashed, test file kept, cache helpers patched with raising=False so it executes rather than erroring at collection): 4 failed, 2 passed — the four cache assertions fail with a Firestore stream per request, and the two "still reads Firestore" tests pass, confirming those branches are genuinely untouched.

Real prod data. Pulled the live 3,247-document set and drove the same route against it: warm ?q=todoist / grok / calendar all answer in 0.02s with 0 Firestore streams; the cold read populates a 10.8MB reduced payload (down from 25MB raw). And the mechanism behind the tail, timing the real Firestore query against prod:

sequential full-set stream:  5.69s, 2.31s, 2.64s
4 concurrent streams:        per-request p50 9.27s  (wall 9.31s)
8 concurrent streams:        per-request p50 8.33s  (wall 8.45s)

Concurrent readers of the same 25MB streaming read degrade each other roughly linearly — which is how a query that costs ~2.5s alone reaches a 13.4s median and a 30s p90 under real traffic. (Absolute numbers are inflated by WAN from a laptop; the shape is the point, and in-region the payload transfer is the same 25MB.)

Suites. backend/test.sh over the 36 app/marketplace/persona test files: all pass, exit 0. Full backend/test.sh (857 files): the only failures are test_action_item_vector_best_effort, test_desktop_proactivity, test_modulate_stt, test_streaming_deepgram_backoff, test_sync_cloud_tasks — the identical pre-existing set documented on the previous run against clean origin/main (four STT/import failures plus the fast-unit CPU-time guard). One further file, test_app_visibility_missing_doc_guard.py, did break: it hand-stubs the exact leaves database/apps.py imports, so the two new imports had to be added to its stub inventory. That is included and it passes.

Not addressed

Cold-cache reads still stream the full set once per 10 minutes per Redis key (shared across all instances and users). Bounding the catalog read itself — a served index or a real search backend for 3k+ apps — is the durable fix and is out of scope here.

Line-Count-Exception: backend/utils/apps.py | 1652 -> 1653 | one import line, so the cache key the reader, writer and invalidation all use is a single shared constant instead of five literals

Failure-Class: FC-bounded-read-exceeds-request-budget

🤖 automated by hourly watchdog — tested and merged

Review in cubic

…a per-request full scan

GET /v2/apps/search streamed `approved==True AND private==False` from Firestore on
every request. In prod that is 3,247 documents / ~25MB per request.

Prod evidence (api.omi.me load-balancer logs, 2026-08-18T12Z..2026-08-19T05Z, image
c5b0b5d), broken down by query shape:

  q=<term>&limit=100        n=29   p50 13.44s  p90 30.05s   24/29 over 5s
  installed_apps=true       n=802  p50  0.15s  p90  0.40s   43 over 5s
  my_apps=true              n=12   p50  0.13s  p90  0.36s    0 over 5s

Only the branches that stream the whole public set are slow. `?q=` always does;
`installed_apps=true` does only when the user has more than 30 enabled apps
(Firestore's `in` limit), which is exactly its 43-request tail. 32 of these
terminated as 504 at the 30s edge — the Apps tab search box returning nothing.

The marketplace list path (utils.apps.get_approved_available_apps) already serves
those same documents from a 10-minute Redis cache under
`get_public_approved_apps_data`. Search was the one reader that bypassed it.

search_apps_db now reads that shared key for both public-set branches and applies
category/capability in Python (they were server-side filters on the read being
replaced). my_apps and the `id in [...]` installed-apps fast path are unchanged and
still query Firestore, because private/unapproved records are not in the public
cache. The cache key moves to one shared constant so a reader cannot drift off the
invalidation path.

Failure-Class: FC-bounded-read-exceeds-request-budget
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