Repository navigation
fix(backend): serve app search from the shared public-app cache, not a per-request full scan - #11851
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
GET /v2/apps/search— the Apps tab search box — times out in prod. Load-balancer logs forapi.omi.me, 2026-08-18T12Z → 2026-08-19T05Z on imagec5b0b5d, broken down by query shape:q=<term>&limit=100installed_apps=true&limit=100my_apps=true&limit=10032 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/memories504 spikes — this is its own defect, not shared instance starvation.Root cause
search_apps_dbstreamedapproved == True AND private == Falsefrom 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=truedoes only when the user has more than 30 enabled apps (Firestore'sinlimit forces the fallback) — which is precisely its 43-request tail; theid in [...]fast path sits at p50 0.15s.my_apps=truenever 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 underget_public_approved_apps_data. Search was the one reader that bypassed it.Fix
search_apps_dbreads that shared key for both public-set branches, via a newget_public_approved_apps_cached_db()that uses the same key, TTL andApp.reduce_dictreduction as the list path, so search can never serve a staler or differently-shaped view than the marketplace.category/capabilitymove to Python filters (they were server-side filters on the read being replaced;my_appsis False on this branch, so they apply unconditionally).Unchanged and still querying Firestore:
my_apps=true, theid 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 andinvalidate_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.routermounted on a FastAPI app, realsearch_apps_db, only the Firestore client and the two Redis cache helpers stubbed:chat_promptabsent,descriptionpresent)?q=with zero Firestore streams — the read that cost 13s+ in prodcategoryandcapabilityfilters all served from the cache, matching and non-matching values both assertedmy_apps=truestill streams Firestoreinstalled_appswith ≤30 enabled ids still streams Firestoreinstalled_appswith >30 enabled ids takes the public set from the cache and streams once for the user's own appsControl run against the pre-fix source (source files stashed, test file kept, cache helpers patched with
raising=Falseso 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/calendarall 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: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.shover the 36 app/marketplace/persona test files: all pass, exit 0. Fullbackend/test.sh(857 files): the only failures aretest_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 cleanorigin/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 leavesdatabase/apps.pyimports, 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