Prompt
I'm building a search feature for a large app and need predictable latency
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
To get predictable latency in a large-app search feature, optimize for bounded work per query and controlled variability. The main goal is not just “fast on average,” but “consistently fast.”
Practical strategies
1) Use an index designed for the query pattern
- Prefer an inverted index or search engine (Elasticsearch/OpenSearch/Solr/Lucene) for text search.
- If you need prefix/autocomplete, use:
- edge n-grams
- trie/FST-based structures
- dedicated autocomplete indexes
- Avoid full scans or ad hoc filtering over large datasets.
2) Keep query shapes simple and bounded
Predictability improves when every query does similar amounts of work.
- Limit the number of filters/facets per request
- Cap result window size (
limit,offset) - Avoid deep pagination; use
search_after/ cursor-based paging - Restrict expensive wildcard queries (
*foo*) and regex - Prefer exact/term filters before fuzzy matching
3) Precompute expensive work
- Denormalize data needed for search results
- Precompute sortable fields, ranking features, popularity, permissions flags
- Build materialized views for common query combinations
- Cache computed aggregates/facets
4) Use caching strategically
- Cache:
- common queries
- autocomplete responses
- filters/facets
- document summaries
- Use short TTLs if freshness matters
- Consider two-level caching:
- in-memory cache for hot queries
- distributed cache for shared results
- Be careful: caches help median latency more than worst-case unless you also control misses
5) Bound backend variability
- Set query timeouts
- Use circuit breakers and fallback responses
- Limit shard fan-out / partition fan-out
- Keep shard sizes balanced
- Avoid too many shards, which increases coordination overhead
6) Make ranking predictable
Ranking can become the hidden latency sink.
- Use simpler scoring models for the first pass
- Re-rank only the top N results
- Avoid per-document expensive script scoring
- If using ML ranking, apply it only to a small candidate set
7) Separate “fast path” and “slow path”
A common pattern:
- Fast path: basic keyword + filters + cached facets
- Slow path: richer ranking, personalization, semantic search, ML re-rank If the slow path times out, return fast-path results instead of blocking.
8) Control data access patterns
- Keep the search index local to the query service or close in network terms
- Avoid joining many remote services during request handling
- Fetch only necessary fields from storage
- Batch secondary lookups if needed
9) Measure tail latency, not just average
For predictable latency, track:
- p50, p95, p99, p99.9
- per-stage timing: parsing, candidate retrieval, ranking, fetch, serialization
- cache hit rate
- shard latency distribution
- queueing time and GC pauses
10) Load shed intentionally
If the system is overloaded, latency becomes unpredictable.
- Use backpressure
- Reject or degrade expensive queries first
- Enforce per-user or per-tenant quotas
- Rate-limit autocomplete and broad queries
Architectural pattern that works well
A robust pattern for predictable search is:
- Query gateway
- validates query shape
- enforces limits/timeouts
- Search index
- retrieves candidate docs quickly
- Cached/filterable metadata
- cheap filtering/faceting
- Lightweight ranker
- top-N re-ranking only
- Optional enrichment
- asynchronous or best-effort
Rules of thumb
- Keep query latency mostly independent of corpus size.
- Keep work roughly proportional to
topK, not total matches. - Avoid operations that depend on “all matching documents.”
- Prefer predictable algorithms over slightly faster but spiky ones.
- Design for graceful degradation.
If you want the simplest reliable approach
For a large app with predictable latency:
- Use a dedicated search engine
- Keep the query DSL limited
- Precompute common fields
- Use cursor pagination
- Cache hot queries/facets
- Enforce timeouts and fallback behavior
- Monitor p95/p99 aggressively
If you want, I can also give you:
- a reference architecture,
- a latency budget template, or
- recommendations based on your stack (Postgres, Elasticsearch, vector search, etc.).