Sub topic 12
Failure modes and fallback behavior
This chapter assumes you know the earlier material: the search index is a copy of data held in a primary database, kept current by a sync pipeline. It also assumes you are comfortable with timeouts and retries between services.
Why search fails differently from the database
The search service is a derived system. If it goes down, your data is still safe in the source of truth. The question is what users see while it is down. Decide this before launch, because the default behavior is usually an error page.
Common failure modes
| Failure | Symptom | Typical cause |
|---|---|---|
| Cluster unreachable | Connection errors | Node crash, network partition, bad deploy |
| Slow queries | Latency spikes, timeouts | Expensive aggregations, deep pagination, overloaded shards |
| Rejected requests | HTTP 429 or queue-full errors | Too much traffic for the thread pool |
| Stale index | Missing or outdated results | Sync pipeline lag or a stopped consumer |
| Partial results | Some shards fail to answer | One shard has no healthy copy |
Partial results are the least obvious failure. Many engines return HTTP 200 with a flag saying some shards failed, so a client that ignores the flag silently shows incomplete results.
Decide how search degrades
Choose a degradation level for each feature, based on how much the product depends on search.
- Fail visibly. Show a clear error. This suits admin tools, where a wrong answer is worse than none.
- Fall back to the database. Run a simpler query, such as a
LIKEmatch or a database full-text index. Results are worse and slower, but the feature works. - Serve cached or static results. Show popular queries, recent items, or a cached response for repeated searches.
- Hide the feature. For optional extras such as autocomplete, remove the dropdown instead of showing an error.
A database fallback can overload the database. Search traffic is often much higher than the database was sized for. Limit fallback queries, restrict them to a few columns, or return only the first page.
A timeout and fallback in code
Always set a timeout shorter than your page's latency budget. A search call with no timeout can tie up every request thread. The example below is self-contained. The search call always fails, to demonstrate the fallback path.
import time
class SearchUnavailable(Exception):
pass
def search_service(query, timeout):
raise SearchUnavailable("connection refused")
def database_search(query):
return [{"id": 3, "name": "Red jacket"}]
class Breaker:
def __init__(self, max_failures=3, cooldown=30):
self.max_failures = max_failures
self.cooldown = cooldown
self.failures = 0
self.opened_at = 0.0
def allow(self):
if self.failures < self.max_failures:
return True
return time.time() - self.opened_at > self.cooldown
def record(self, ok):
if ok:
self.failures = 0
else:
self.failures += 1
self.opened_at = time.time()
breaker = Breaker()
def search(query):
if breaker.allow():
try:
result = search_service(query, timeout=0.3)
breaker.record(True)
return {"results": result, "degraded": False}
except SearchUnavailable:
breaker.record(False)
return {"results": database_search(query), "degraded": True}
for _ in range(5):
print(search("red jacket"))After three failures the breaker stops calling the search service for 30 seconds. This keeps a struggling cluster from receiving more load, and users no longer wait for a timeout on every request. The degraded flag lets the UI tell users that results may be incomplete.
Protect the sync path too
Failures also happen on the write side. If the index is down, sync events should wait in the queue and be retried, not dropped. Send events that keep failing to a dead-letter queue so one bad document does not block the rest. After a long outage, replay from the queue or reindex from the database. Then compare document counts between the two to confirm nothing was lost.
What to monitor
- Query latency at the 95th and 99th percentiles, and the timeout rate.
- Rejected requests and the number of shards that failed per response.
- Sync lag: the age of the oldest unprocessed event.
- How often the fallback runs.
Exercise
Your search cluster returns HTTP 200 on every request, but users report missing products. Name two possible causes and one check for each.
Show answer
One cause is partial results, where some shards failed. Check the shard-failure count in the response body. Another cause is sync lag or a stalled consumer. Check the age of the oldest unprocessed event, and compare document counts between the database and the index.